source: mainline/uspace/lib/cpp/src/ios.cpp@ 8624d1f

Last change on this file since 8624d1f was b57ba05, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Update headers in C++ files

  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <ios>
8
9namespace std
10{
11 int ios_base::index_{};
12 bool ios_base::sync_{true};
13 long ios_base::ierror_{0};
14 void* ios_base::perror_{nullptr};
15
16 ios_base::ios_base()
17 : iarray_{}, parray_{}, iarray_size_{}, parray_size_{},
18 flags_{}, precision_{}, width_{}, locale_{/* TODO: use locale()? */},
19 callbacks_{}
20 { /* DUMMY BODY */ }
21
22 ios_base::~ios_base()
23 {
24 for (auto& callback: callbacks_)
25 callback.first(erase_event, *this, callback.second);
26 }
27
28 auto ios_base::flags() const -> fmtflags
29 {
30 return flags_;
31 }
32
33 auto ios_base::flags(fmtflags fmtfl) -> fmtflags
34 {
35 auto old = flags_;
36 flags_ = fmtfl;
37
38 return old;
39 }
40
41 auto ios_base::setf(fmtflags fmtfl) -> fmtflags
42 {
43 auto old = flags_;
44 flags_ |= fmtfl;
45
46 return old;
47 }
48
49 auto ios_base::setf(fmtflags fmtfl, fmtflags mask) -> fmtflags
50 {
51 auto old = flags_;
52 flags_ &= ~mask;
53 flags_ |= fmtfl & mask;
54
55 return old;
56 }
57
58 void ios_base::unsetf(fmtflags fmtfl)
59 {
60 flags_ &= ~fmtfl;
61 }
62
63 streamsize ios_base::precision() const
64 {
65 return precision_;
66 }
67
68 streamsize ios_base::precision(streamsize prec)
69 {
70 auto old = precision_;
71 precision_ = prec;
72
73 return old;
74 }
75
76 streamsize ios_base::width() const
77 {
78 return width_;
79 }
80
81 streamsize ios_base::width(streamsize wide)
82 {
83 auto old = width_;
84 width_ = wide;
85
86 return old;
87 }
88
89 locale ios_base::imbue(const locale& loc)
90 {
91 auto old = locale_;
92 locale_ = loc;
93
94 for (auto& callback: callbacks_)
95 callback.first(imbue_event, *this, callback.second);
96
97 return old;
98 }
99
100 locale ios_base::getloc() const
101 {
102 return locale_;
103 }
104
105 long& ios_base::iword(int index)
106 {
107 if (!iarray_)
108 {
109 iarray_ = new long[initial_size_];
110 iarray_size_ = initial_size_;
111 }
112
113 auto idx = static_cast<size_t>(index);
114 if (idx > iarray_size_)
115 { // TODO: Enclose in try block and set failbit if needed
116 // and return ierror_.
117 size_t new_size = max(iarray_size_ * 2, idx + 1);
118 auto tmp = new long[new_size];
119
120 for (size_t i = 0; i < iarray_size_; ++i)
121 tmp[i] = iarray_[i];
122 for (size_t i = iarray_size_; i < new_size; ++i)
123 tmp[i] = 0;
124
125 swap(tmp, iarray_);
126 delete[] tmp;
127 iarray_size_ = new_size;
128
129 return iarray_[idx];
130 }
131 else
132 return iarray_[idx];
133
134 return ierror_;
135 }
136
137 void*& ios_base::pword(int index)
138 {
139 if (!parray_)
140 {
141 parray_ = new void*[initial_size_];
142 parray_size_ = initial_size_;
143 }
144
145 auto idx = static_cast<size_t>(index);
146 if (idx > parray_size_)
147 { // TODO: Enclose in try block and set failbit if needed
148 // and return perror_.
149 size_t new_size = max(parray_size_ * 2, idx + 1);
150 auto tmp = new void*[new_size];
151
152 for (size_t i = 0; i < parray_size_; ++i)
153 tmp[i] = parray_[i];
154 for (size_t i = parray_size_; i < new_size; ++i)
155 tmp[i] = nullptr;
156
157 swap(tmp, parray_);
158 delete[] tmp;
159 parray_size_ = new_size;
160
161 return parray_[idx];
162 }
163 else
164 return parray_[idx];
165
166 return perror_;
167 }
168
169 void ios_base::register_callback(event_callback fn, int index)
170 {
171 callbacks_.emplace_back(fn, index);
172 }
173
174 ios_base& boolalpha(ios_base& str)
175 {
176 str.setf(ios_base::boolalpha);
177 return str;
178 }
179
180 ios_base& noboolalpha(ios_base& str)
181 {
182 str.unsetf(ios_base::boolalpha);
183 return str;
184 }
185
186 ios_base& showbase(ios_base& str)
187 {
188 str.setf(ios_base::showbase);
189 return str;
190 }
191
192 ios_base& noshowbase(ios_base& str)
193 {
194 str.unsetf(ios_base::showbase);
195 return str;
196 }
197
198 ios_base& showpoint(ios_base& str)
199 {
200 str.setf(ios_base::showpoint);
201 return str;
202 }
203
204 ios_base& noshowpoint(ios_base& str)
205 {
206 str.unsetf(ios_base::showpoint);
207 return str;
208 }
209
210 ios_base& showpos(ios_base& str)
211 {
212 str.setf(ios_base::showpos);
213 return str;
214 }
215
216 ios_base& noshowpos(ios_base& str)
217 {
218 str.unsetf(ios_base::showpos);
219 return str;
220 }
221
222 ios_base& skipws(ios_base& str)
223 {
224 str.setf(ios_base::skipws);
225 return str;
226 }
227
228 ios_base& noskipws(ios_base& str)
229 {
230 str.unsetf(ios_base::skipws);
231 return str;
232 }
233
234 ios_base& uppercase(ios_base& str)
235 {
236 str.setf(ios_base::uppercase);
237 return str;
238 }
239
240 ios_base& nouppercase(ios_base& str)
241 {
242 str.unsetf(ios_base::uppercase);
243 return str;
244 }
245
246 ios_base& unitbuf(ios_base& str)
247 {
248 str.setf(ios_base::unitbuf);
249 return str;
250 }
251
252 ios_base& nounitbuf(ios_base& str)
253 {
254 str.unsetf(ios_base::unitbuf);
255 return str;
256 }
257
258 ios_base& internal(ios_base& str)
259 {
260 str.setf(ios_base::internal, ios_base::adjustfield);
261 return str;
262 }
263
264 ios_base& left(ios_base& str)
265 {
266 str.setf(ios_base::left, ios_base::adjustfield);
267 return str;
268 }
269
270 ios_base& right(ios_base& str)
271 {
272 str.setf(ios_base::right, ios_base::adjustfield);
273 return str;
274 }
275
276 ios_base& dec(ios_base& str)
277 {
278 str.setf(ios_base::dec, ios_base::basefield);
279 return str;
280 }
281
282 ios_base& hex(ios_base& str)
283 {
284 str.setf(ios_base::hex, ios_base::basefield);
285 return str;
286 }
287
288 ios_base& oct(ios_base& str)
289 {
290 str.setf(ios_base::oct, ios_base::basefield);
291 return str;
292 }
293
294 ios_base& fixed(ios_base& str)
295 {
296 str.setf(ios_base::fixed, ios_base::floatfield);
297 return str;
298 }
299
300 ios_base& scientific(ios_base& str)
301 {
302 str.setf(ios_base::scientific, ios_base::floatfield);
303 return str;
304 }
305
306 ios_base& hexfloat(ios_base& str)
307 {
308 str.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield);
309 return str;
310 }
311
312 ios_base& defaultfloat(ios_base& str)
313 {
314 str.unsetf(ios_base::floatfield);
315 return str;
316 }
317}
Note: See TracBrowser for help on using the repository browser.