source: mainline/uspace/lib/cpp/src/ios.cpp@ 5608106c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5608106c was d13b67a, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: fixed minor bugs that prevented ios from compilation

  • Property mode set to 100644
File size: 7.9 KB
Line 
1/*
2 * Copyright (c) 2017 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <ios>
30
31namespace std
32{
33 int ios_base::index_{};
34 bool ios_base::sync_{true};
35 long ios_base::ierror_{0};
36 void* ios_base::perror_{nullptr};
37
38 ios_base::ios_base()
39 : iarray_{}, parray_{}, iarray_size_{}, parray_size_{},
40 flags_{}, precision_{}, width_{}, locale_{/* TODO: use locale()? */},
41 callbacks_{}
42 { /* DUMMY BODY */ }
43
44 ios_base::~ios_base()
45 {
46 for (auto& callback: callbacks_)
47 callback.first(erase_event, *this, callback.second);
48 }
49
50 auto ios_base::flags() const -> fmtflags
51 {
52 return flags_;
53 }
54
55 auto ios_base::flags(fmtflags fmtfl) -> fmtflags
56 {
57 auto old = flags_;
58 flags_ = fmtfl;
59
60 return old;
61 }
62
63 auto ios_base::setf(fmtflags fmtfl) -> fmtflags
64 {
65 auto old = flags_;
66 flags_ |= fmtfl;
67
68 return old;
69 }
70
71 auto ios_base::setf(fmtflags fmtfl, fmtflags mask) -> fmtflags
72 {
73 auto old = flags_;
74 flags_ &= ~mask;
75 flags_ |= fmtfl & mask;
76
77 return old;
78 }
79
80 void ios_base::unsetf(fmtflags fmtfl)
81 {
82 flags_ &= ~fmtfl;
83 }
84
85 streamsize ios_base::precision() const
86 {
87 return precision_;
88 }
89
90 streamsize ios_base::precision(streamsize prec)
91 {
92 auto old = precision_;
93 precision_ = prec;
94
95 return old;
96 }
97
98 streamsize ios_base::width() const
99 {
100 return width_;
101 }
102
103 streamsize ios_base::width(streamsize wide)
104 {
105 auto old = width_;
106 width_ = wide;
107
108 return old;
109 }
110
111 locale ios_base::imbue(const locale& loc)
112 {
113 auto old = locale_;
114 locale_ = loc;
115
116 for (auto& callback: callbacks_)
117 callback.first(imbue_event, *this, callback.second);
118
119 return old;
120 }
121
122 locale ios_base::getloc() const
123 {
124 return locale_;
125 }
126
127 long& ios_base::iword(int index)
128 {
129 if (!iarray_)
130 {
131 iarray_ = new long[initial_size_];
132 iarray_size_ = initial_size_;
133 }
134
135 auto idx = static_cast<size_t>(index);
136 if (idx > iarray_size_)
137 { // TODO: Enclose in try block and set failbit if needed
138 // and return ierror_.
139 size_t new_size = max(iarray_size_ * 2, idx + 1);
140 auto tmp = new long[new_size];
141
142 for (size_t i = 0; i < iarray_size_; ++i)
143 tmp[i] = iarray_[i];
144 for (size_t i = iarray_size_; i < new_size; ++i)
145 tmp[i] = 0;
146
147 swap(tmp, iarray_);
148 delete[] tmp;
149 iarray_size_ = new_size;
150
151 return iarray_[idx];
152 }
153 else
154 return iarray_[idx];
155
156 return ierror_;
157 }
158
159 void*& ios_base::pword(int index)
160 {
161 if (!parray_)
162 {
163 parray_ = new void*[initial_size_];
164 parray_size_ = initial_size_;
165 }
166
167 auto idx = static_cast<size_t>(index);
168 if (idx > parray_size_)
169 { // TODO: Enclose in try block and set failbit if needed
170 // and return perror_.
171 size_t new_size = max(parray_size_ * 2, idx + 1);
172 auto tmp = new void*[new_size];
173
174 for (size_t i = 0; i < parray_size_; ++i)
175 tmp[i] = parray_[i];
176 for (size_t i = parray_size_; i < new_size; ++i)
177 tmp[i] = nullptr;
178
179 swap(tmp, parray_);
180 delete[] tmp;
181 parray_size_ = new_size;
182
183 return parray_[idx];
184 }
185 else
186 return parray_[idx];
187
188 return perror_;
189 }
190
191 void ios_base::register_callback(event_callback fn, int index)
192 {
193 callbacks_.emplace_back(fn, index);
194 }
195
196 ios_base& boolalpha(ios_base& str)
197 {
198 str.setf(ios_base::boolalpha);
199 return str;
200 }
201
202 ios_base& noboolalpha(ios_base& str)
203 {
204 str.unsetf(ios_base::boolalpha);
205 return str;
206 }
207
208 ios_base& showbase(ios_base& str)
209 {
210 str.setf(ios_base::showbase);
211 return str;
212 }
213
214 ios_base& noshowbase(ios_base& str)
215 {
216 str.unsetf(ios_base::showbase);
217 return str;
218 }
219
220 ios_base& showpoint(ios_base& str)
221 {
222 str.setf(ios_base::showpoint);
223 return str;
224 }
225
226 ios_base& noshowpoint(ios_base& str)
227 {
228 str.unsetf(ios_base::showpoint);
229 return str;
230 }
231
232 ios_base& showpos(ios_base& str)
233 {
234 str.setf(ios_base::showpos);
235 return str;
236 }
237
238 ios_base& noshowpos(ios_base& str)
239 {
240 str.unsetf(ios_base::showpos);
241 return str;
242 }
243
244 ios_base& skipws(ios_base& str)
245 {
246 str.setf(ios_base::skipws);
247 return str;
248 }
249
250 ios_base& noskipws(ios_base& str)
251 {
252 str.unsetf(ios_base::skipws);
253 return str;
254 }
255
256 ios_base& uppercase(ios_base& str)
257 {
258 str.setf(ios_base::uppercase);
259 return str;
260 }
261
262 ios_base& nouppercase(ios_base& str)
263 {
264 str.unsetf(ios_base::uppercase);
265 return str;
266 }
267
268 ios_base& unitbuf(ios_base& str)
269 {
270 str.setf(ios_base::unitbuf);
271 return str;
272 }
273
274 ios_base& nounitbuf(ios_base& str)
275 {
276 str.unsetf(ios_base::unitbuf);
277 return str;
278 }
279
280 ios_base& internal(ios_base& str)
281 {
282 str.setf(ios_base::internal, ios_base::adjustfield);
283 return str;
284 }
285
286 ios_base& left(ios_base& str)
287 {
288 str.setf(ios_base::left, ios_base::adjustfield);
289 return str;
290 }
291
292 ios_base& right(ios_base& str)
293 {
294 str.setf(ios_base::right, ios_base::adjustfield);
295 return str;
296 }
297
298 ios_base& dec(ios_base& str)
299 {
300 str.setf(ios_base::dec, ios_base::basefield);
301 return str;
302 }
303
304 ios_base& hex(ios_base& str)
305 {
306 str.setf(ios_base::hex, ios_base::basefield);
307 return str;
308 }
309
310 ios_base& oct(ios_base& str)
311 {
312 str.setf(ios_base::oct, ios_base::basefield);
313 return str;
314 }
315
316 ios_base& fixed(ios_base& str)
317 {
318 str.setf(ios_base::fixed, ios_base::floatfield);
319 return str;
320 }
321
322 ios_base& scientific(ios_base& str)
323 {
324 str.setf(ios_base::scientific, ios_base::floatfield);
325 return str;
326 }
327
328 ios_base& hexfloat(ios_base& str)
329 {
330 str.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield);
331 return str;
332 }
333
334 ios_base& defaultfloat(ios_base& str)
335 {
336 str.unsetf(ios_base::floatfield);
337 return str;
338 }
339}
Note: See TracBrowser for help on using the repository browser.