source: mainline/uspace/lib/cpp/src/ios.cpp@ 54939b27

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

cpp: implemented ios_base manipulators

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