1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // iostream.cfa -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Wed May 27 17:56:53 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Aug 25 18:05:49 2022 |
---|
13 | // Update Count : 1354 |
---|
14 | // |
---|
15 | |
---|
16 | #include "iostream.hfa" |
---|
17 | |
---|
18 | #include <stdio.h> |
---|
19 | #include <stdbool.h> // true/false |
---|
20 | #include <stdint.h> // UINT64_MAX |
---|
21 | #include <float.h> // DBL_DIG, LDBL_DIG |
---|
22 | #include <complex.h> // creal, cimag |
---|
23 | //#include <string.h> // strlen, strcmp, memcpy |
---|
24 | extern "C" { |
---|
25 | extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1))); |
---|
26 | extern int strcmp (const char *__s1, const char *__s2) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2))); |
---|
27 | extern char *strcpy (char *__restrict __dest, const char *__restrict __src) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); |
---|
28 | extern void *memcpy (void *__restrict __dest, const void *__restrict __src, size_t __n) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); |
---|
29 | } // extern "C" |
---|
30 | |
---|
31 | #include "math.hfa" // isfinite, floor, ceiling_div |
---|
32 | #include "bitmanip.hfa" // high1 |
---|
33 | |
---|
34 | #pragma GCC visibility push(default) |
---|
35 | |
---|
36 | // *********************************** ostream *********************************** |
---|
37 | |
---|
38 | |
---|
39 | forall( ostype & | basic_ostream( ostype ) ) { |
---|
40 | ostype & ?|?( ostype & os, bool b ) { |
---|
41 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
42 | fmt( os, "%s", b ? "true" : "false" ); |
---|
43 | return os; |
---|
44 | } // ?|? |
---|
45 | void ?|?( ostype & os, bool b ) { |
---|
46 | (ostype &)(os | b); ends( os ); |
---|
47 | } // ?|? |
---|
48 | |
---|
49 | ostype & ?|?( ostype & os, char c ) { |
---|
50 | fmt( os, "%c", c ); |
---|
51 | if ( c == '\n' ) setNL$( os, true ); |
---|
52 | return sepOff( os ); |
---|
53 | } // ?|? |
---|
54 | void ?|?( ostype & os, char c ) { |
---|
55 | (ostype &)(os | c); ends( os ); |
---|
56 | } // ?|? |
---|
57 | |
---|
58 | ostype & ?|?( ostype & os, signed char sc ) { |
---|
59 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
60 | fmt( os, "%'hhd", sc ); |
---|
61 | return os; |
---|
62 | } // ?|? |
---|
63 | void ?|?( ostype & os, signed char sc ) { |
---|
64 | (ostype &)(os | sc); ends( os ); |
---|
65 | } // ?|? |
---|
66 | |
---|
67 | ostype & ?|?( ostype & os, unsigned char usc ) { |
---|
68 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
69 | fmt( os, "%'hhu", usc ); |
---|
70 | return os; |
---|
71 | } // ?|? |
---|
72 | void ?|?( ostype & os, unsigned char usc ) { |
---|
73 | (ostype &)(os | usc); ends( os ); |
---|
74 | } // ?|? |
---|
75 | |
---|
76 | ostype & ?|?( ostype & os, short int si ) { |
---|
77 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
78 | fmt( os, "%'hd", si ); |
---|
79 | return os; |
---|
80 | } // ?|? |
---|
81 | void & ?|?( ostype & os, short int si ) { |
---|
82 | (ostype &)(os | si); ends( os ); |
---|
83 | } // ?|? |
---|
84 | |
---|
85 | ostype & ?|?( ostype & os, unsigned short int usi ) { |
---|
86 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
87 | fmt( os, "%'hu", usi ); |
---|
88 | return os; |
---|
89 | } // ?|? |
---|
90 | void & ?|?( ostype & os, unsigned short int usi ) { |
---|
91 | (ostype &)(os | usi); ends( os ); |
---|
92 | } // ?|? |
---|
93 | |
---|
94 | ostype & ?|?( ostype & os, int i ) { |
---|
95 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
96 | fmt( os, "%'d", i ); |
---|
97 | return os; |
---|
98 | } // ?|? |
---|
99 | void & ?|?( ostype & os, int i ) { |
---|
100 | (ostype &)(os | i); ends( os ); |
---|
101 | } // ?|? |
---|
102 | |
---|
103 | ostype & ?|?( ostype & os, unsigned int ui ) { |
---|
104 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
105 | fmt( os, "%'u", ui ); |
---|
106 | return os; |
---|
107 | } // ?|? |
---|
108 | void & ?|?( ostype & os, unsigned int ui ) { |
---|
109 | (ostype &)(os | ui); ends( os ); |
---|
110 | } // ?|? |
---|
111 | |
---|
112 | ostype & ?|?( ostype & os, long int li ) { |
---|
113 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
114 | fmt( os, "%'ld", li ); |
---|
115 | return os; |
---|
116 | } // ?|? |
---|
117 | void & ?|?( ostype & os, long int li ) { |
---|
118 | (ostype &)(os | li); ends( os ); |
---|
119 | } // ?|? |
---|
120 | |
---|
121 | ostype & ?|?( ostype & os, unsigned long int uli ) { |
---|
122 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
123 | fmt( os, "%'lu", uli ); |
---|
124 | return os; |
---|
125 | } // ?|? |
---|
126 | void & ?|?( ostype & os, unsigned long int uli ) { |
---|
127 | (ostype &)(os | uli); ends( os ); |
---|
128 | } // ?|? |
---|
129 | |
---|
130 | ostype & ?|?( ostype & os, long long int lli ) { |
---|
131 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
132 | fmt( os, "%'lld", lli ); |
---|
133 | return os; |
---|
134 | } // ?|? |
---|
135 | void & ?|?( ostype & os, long long int lli ) { |
---|
136 | (ostype &)(os | lli); ends( os ); |
---|
137 | } // ?|? |
---|
138 | |
---|
139 | ostype & ?|?( ostype & os, unsigned long long int ulli ) { |
---|
140 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
141 | fmt( os, "%'llu", ulli ); |
---|
142 | return os; |
---|
143 | } // ?|? |
---|
144 | void & ?|?( ostype & os, unsigned long long int ulli ) { |
---|
145 | (ostype &)(os | ulli); ends( os ); |
---|
146 | } // ?|? |
---|
147 | |
---|
148 | #if defined( __SIZEOF_INT128__ ) |
---|
149 | // UINT64_MAX 18_446_744_073_709_551_615_ULL |
---|
150 | #define P10_UINT64 10_000_000_000_000_000_000_ULL // 19 zeroes |
---|
151 | |
---|
152 | static inline void base10_128( ostype & os, unsigned int128 val ) { |
---|
153 | #if defined(__GNUC__) && __GNUC_PREREQ(7,0) // gcc version >= 7 |
---|
154 | if ( val > P10_UINT64 ) { |
---|
155 | #else |
---|
156 | if ( (uint64_t)(val >> 64) != 0 || (uint64_t)val > P10_UINT64 ) { // patch gcc 5 & 6 -O3 bug |
---|
157 | #endif // __GNUC_PREREQ(7,0) |
---|
158 | base10_128( os, val / P10_UINT64 ); // recursive |
---|
159 | fmt( os, "%.19lu", (uint64_t)(val % P10_UINT64) ); |
---|
160 | } else { |
---|
161 | fmt( os, "%lu", (uint64_t)val ); |
---|
162 | } // if |
---|
163 | } // base10_128 |
---|
164 | |
---|
165 | static inline void base10_128( ostype & os, int128 val ) { |
---|
166 | if ( val < 0 ) { |
---|
167 | fmt( os, "-" ); // leading negative sign |
---|
168 | val = -val; |
---|
169 | } // if |
---|
170 | base10_128( os, (unsigned int128)val ); // print zero/positive value |
---|
171 | } // base10_128 |
---|
172 | |
---|
173 | ostype & ?|?( ostype & os, int128 llli ) { |
---|
174 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
175 | base10_128( os, llli ); |
---|
176 | return os; |
---|
177 | } // ?|? |
---|
178 | void & ?|?( ostype & os, int128 llli ) { |
---|
179 | (ostype &)(os | llli); ends( os ); |
---|
180 | } // ?|? |
---|
181 | |
---|
182 | ostype & ?|?( ostype & os, unsigned int128 ullli ) { |
---|
183 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
184 | base10_128( os, ullli ); |
---|
185 | return os; |
---|
186 | } // ?|? |
---|
187 | void & ?|?( ostype & os, unsigned int128 ullli ) { |
---|
188 | (ostype &)(os | ullli); ends( os ); |
---|
189 | } // ?|? |
---|
190 | #endif // __SIZEOF_INT128__ |
---|
191 | |
---|
192 | #define PrintWithDP( os, format, val, ... ) \ |
---|
193 | { \ |
---|
194 | enum { size = 48 }; \ |
---|
195 | char buf[size]; \ |
---|
196 | int len = snprintf( buf, size, format, ##__VA_ARGS__, val ); \ |
---|
197 | fmt( os, "%s", buf ); \ |
---|
198 | if ( isfinite( val ) ) { /* if number, print decimal point when no fraction or exponent */ \ |
---|
199 | for ( i; 0 ~ @ ) { \ |
---|
200 | if ( i == len ) { fmt( os, "." ); break; } \ |
---|
201 | if ( buf[i] == '.' || buf[i] == 'e' || buf[i] == 'E' ) break; /* decimal point or scientific ? */ \ |
---|
202 | } /* for */ \ |
---|
203 | } /* if */ \ |
---|
204 | } |
---|
205 | |
---|
206 | ostype & ?|?( ostype & os, float f ) { |
---|
207 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
208 | PrintWithDP( os, "%'g", f ); |
---|
209 | return os; |
---|
210 | } // ?|? |
---|
211 | void & ?|?( ostype & os, float f ) { |
---|
212 | (ostype &)(os | f); ends( os ); |
---|
213 | } // ?|? |
---|
214 | |
---|
215 | ostype & ?|?( ostype & os, double d ) { |
---|
216 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
217 | PrintWithDP( os, "%'.*lg", d, DBL_DIG ); |
---|
218 | return os; |
---|
219 | } // ?|? |
---|
220 | void & ?|?( ostype & os, double d ) { |
---|
221 | (ostype &)(os | d); ends( os ); |
---|
222 | } // ?|? |
---|
223 | |
---|
224 | ostype & ?|?( ostype & os, long double ld ) { |
---|
225 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
226 | PrintWithDP( os, "%'.*Lg", ld, LDBL_DIG ); |
---|
227 | return os; |
---|
228 | } // ?|? |
---|
229 | void & ?|?( ostype & os, long double ld ) { |
---|
230 | (ostype &)(os | ld); ends( os ); |
---|
231 | } // ?|? |
---|
232 | |
---|
233 | ostype & ?|?( ostype & os, float _Complex fc ) { |
---|
234 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
235 | // os | crealf( fc ) | nonl; |
---|
236 | PrintWithDP( os, "%'g", crealf( fc ) ); |
---|
237 | PrintWithDP( os, "%'+g", cimagf( fc ) ); |
---|
238 | fmt( os, "i" ); |
---|
239 | return os; |
---|
240 | } // ?|? |
---|
241 | void & ?|?( ostype & os, float _Complex fc ) { |
---|
242 | (ostype &)(os | fc); ends( os ); |
---|
243 | } // ?|? |
---|
244 | |
---|
245 | ostype & ?|?( ostype & os, double _Complex dc ) { |
---|
246 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
247 | // os | creal( dc ) | nonl; |
---|
248 | PrintWithDP( os, "%'.*lg", creal( dc ), DBL_DIG ); |
---|
249 | PrintWithDP( os, "%'+.*lg", cimag( dc ), DBL_DIG ); |
---|
250 | fmt( os, "i" ); |
---|
251 | return os; |
---|
252 | } // ?|? |
---|
253 | void & ?|?( ostype & os, double _Complex dc ) { |
---|
254 | (ostype &)(os | dc); ends( os ); |
---|
255 | } // ?|? |
---|
256 | |
---|
257 | ostype & ?|?( ostype & os, long double _Complex ldc ) { |
---|
258 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
259 | // os | creall( ldc ) || nonl; |
---|
260 | PrintWithDP( os, "%'.*Lg", creall( ldc ), LDBL_DIG ); |
---|
261 | PrintWithDP( os, "%'+.*Lg", cimagl( ldc ), LDBL_DIG ); |
---|
262 | fmt( os, "i" ); |
---|
263 | return os; |
---|
264 | } // ?|? |
---|
265 | void & ?|?( ostype & os, long double _Complex ldc ) { |
---|
266 | (ostype &)(os | ldc); ends( os ); |
---|
267 | } // ?|? |
---|
268 | |
---|
269 | ostype & ?|?( ostype & os, const char s[] ) { |
---|
270 | enum { Open = 1, Close, OpenClose }; |
---|
271 | static const unsigned char mask[256] @= { |
---|
272 | // opening delimiters, no space after |
---|
273 | ['('] : Open, ['['] : Open, ['{'] : Open, |
---|
274 | ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open, |
---|
275 | [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open, |
---|
276 | // closing delimiters, no space before |
---|
277 | [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close, |
---|
278 | ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close, |
---|
279 | [')'] : Close, [']'] : Close, ['}'] : Close, |
---|
280 | // opening-closing delimiters, no space before or after |
---|
281 | ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose, |
---|
282 | [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace |
---|
283 | }; // mask |
---|
284 | |
---|
285 | if ( s == 0p ) { fmt( os, "%s", "0p" ); return os; } // null pointer |
---|
286 | if ( s[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator |
---|
287 | |
---|
288 | // first character IS NOT spacing or closing punctuation => add left separator |
---|
289 | unsigned char ch = s[0]; // must make unsigned |
---|
290 | if ( sepPrt$( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) { |
---|
291 | fmt( os, "%s", sepGetCur$( os ) ); |
---|
292 | } // if |
---|
293 | |
---|
294 | // if string starts line, must reset to determine open state because separator is off |
---|
295 | sepReset$( os ); // reset separator |
---|
296 | |
---|
297 | // last character IS spacing or opening punctuation => turn off separator for next item |
---|
298 | int len = strlen( s ); |
---|
299 | ch = s[len - 1]; // must make unsigned |
---|
300 | fmt( os, "%s", s ); // fmt resets seperator, but reset it again |
---|
301 | if ( sepPrt$( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) { |
---|
302 | sepOn( os ); |
---|
303 | } else { |
---|
304 | sepOff( os ); |
---|
305 | } // if |
---|
306 | if ( ch == '\n' ) setNL$( os, true ); // check *AFTER* sepPrt$ call above as it resets NL flag |
---|
307 | return os; |
---|
308 | // return write( os, s, len ); |
---|
309 | } // ?|? |
---|
310 | void ?|?( ostype & os, const char s[] ) { |
---|
311 | (ostype &)(os | s); ends( os ); |
---|
312 | } // ?|? |
---|
313 | |
---|
314 | // ostype & ?|?( ostype & os, const char16_t * s ) { |
---|
315 | // if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
316 | // fmt( os, "%ls", s ); |
---|
317 | // return os; |
---|
318 | // } // ?|? |
---|
319 | |
---|
320 | // #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous |
---|
321 | // ostype & ?|?( ostype & os, const char32_t * s ) { |
---|
322 | // if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
323 | // fmt( os, "%ls", s ); |
---|
324 | // return os; |
---|
325 | // } // ?|? |
---|
326 | // #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) |
---|
327 | |
---|
328 | // ostype & ?|?( ostype & os, const wchar_t * s ) { |
---|
329 | // if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
330 | // fmt( os, "%ls", s ); |
---|
331 | // return os; |
---|
332 | // } // ?|? |
---|
333 | |
---|
334 | ostype & ?|?( ostype & os, const void * p ) { |
---|
335 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
336 | fmt( os, "%p", p ); |
---|
337 | return os; |
---|
338 | } // ?|? |
---|
339 | void ?|?( ostype & os, const void * p ) { |
---|
340 | (ostype &)(os | p); ends( os ); |
---|
341 | } // ?|? |
---|
342 | |
---|
343 | // manipulators |
---|
344 | ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) { |
---|
345 | return manip( os ); |
---|
346 | } // ?|? |
---|
347 | void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) { |
---|
348 | manip( os ); |
---|
349 | if ( getPrt$( os ) ) ends( os ); // something printed ? |
---|
350 | setPrt$( os, false ); // turn off |
---|
351 | } // ?|? |
---|
352 | |
---|
353 | ostype & sep( ostype & os ) { |
---|
354 | return (ostype &)(os | sepGet( os )); |
---|
355 | } // sep |
---|
356 | |
---|
357 | ostype & sepTuple( ostype & os ) { |
---|
358 | return os | sepGetTuple( os ); |
---|
359 | } // sepTuple |
---|
360 | |
---|
361 | ostype & nl( ostype & os ) { |
---|
362 | (ostype &)(os | '\n'); |
---|
363 | setPrt$( os, false ); // turn off |
---|
364 | setNL$( os, true ); |
---|
365 | return sepOff( os ); // prepare for next line |
---|
366 | } // nl |
---|
367 | |
---|
368 | ostype & nonl( ostype & os ) { |
---|
369 | setPrt$( os, false ); // turn off |
---|
370 | return os; |
---|
371 | } // nonl |
---|
372 | |
---|
373 | ostype & sepOn( ostype & os ) { |
---|
374 | sepOn( os ); // call void returning |
---|
375 | return os; |
---|
376 | } // sepOn |
---|
377 | |
---|
378 | ostype & sepOff( ostype & os ) { |
---|
379 | sepOff( os ); // call void returning |
---|
380 | return os; |
---|
381 | } // sepOff |
---|
382 | |
---|
383 | ostype & sepEnable( ostype & os ) { |
---|
384 | sepEnable( os ); // call void returning |
---|
385 | return os; |
---|
386 | } // sepEnable |
---|
387 | |
---|
388 | ostype & sepDisable( ostype & os ) { |
---|
389 | sepDisable( os ); // call void returning |
---|
390 | return os; |
---|
391 | } // sepDisable |
---|
392 | |
---|
393 | ostype & nlOn( ostype & os ) { |
---|
394 | nlOn( os ); // call void returning |
---|
395 | return os; |
---|
396 | } // nlOn |
---|
397 | |
---|
398 | ostype & nlOff( ostype & os ) { |
---|
399 | nlOff( os ); // call void returning |
---|
400 | return os; |
---|
401 | } // nlOff |
---|
402 | } // distribution |
---|
403 | |
---|
404 | // tuples |
---|
405 | forall( ostype &, T, Params... | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) { |
---|
406 | ostype & ?|?( ostype & os, T arg, Params rest ) { |
---|
407 | (ostype &)(os | arg); // print first argument |
---|
408 | sepSetCur$( os, sepGetTuple( os ) ); // switch to tuple separator |
---|
409 | (ostype &)(os | rest); // print remaining arguments |
---|
410 | sepSetCur$( os, sepGet( os ) ); // switch to regular separator |
---|
411 | return os; |
---|
412 | } // ?|? |
---|
413 | void ?|?( ostype & os, T arg, Params rest ) { |
---|
414 | // (ostype &)(?|?( os, arg, rest )); ends( os ); |
---|
415 | (ostype &)(os | arg); // print first argument |
---|
416 | sepSetCur$( os, sepGetTuple( os ) ); // switch to tuple separator |
---|
417 | (ostype &)(os | rest); // print remaining arguments |
---|
418 | sepSetCur$( os, sepGet( os ) ); // switch to regular separator |
---|
419 | ends( os ); |
---|
420 | } // ?|? |
---|
421 | } // distribution |
---|
422 | |
---|
423 | // writes the range [begin, end) to the given stream |
---|
424 | forall( ostype &, elt_type | writeable( elt_type, ostype ), iterator_type | iterator( iterator_type, elt_type ) ) { |
---|
425 | void write( iterator_type begin, iterator_type end, ostype & os ) { |
---|
426 | void print( elt_type i ) { os | i; } |
---|
427 | for_each( begin, end, print ); |
---|
428 | } // ?|? |
---|
429 | |
---|
430 | void write_reverse( iterator_type begin, iterator_type end, ostype & os ) { |
---|
431 | void print( elt_type i ) { os | i; } |
---|
432 | for_each_reverse( begin, end, print ); |
---|
433 | } // ?|? |
---|
434 | } // distribution |
---|
435 | |
---|
436 | // *********************************** manipulators *********************************** |
---|
437 | |
---|
438 | // *********************************** integral *********************************** |
---|
439 | |
---|
440 | static const char * shortbin[] = { "0", "1", "10", "11", "100", "101", "110", "111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; |
---|
441 | static const char * longbin[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; |
---|
442 | |
---|
443 | // Default prefix for non-decimal prints is 0b, 0, 0x. |
---|
444 | #define IntegralFMTImpl( T, IFMTNP, IFMTP ) \ |
---|
445 | forall( ostype & | basic_ostream( ostype ) ) { \ |
---|
446 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \ |
---|
447 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); \ |
---|
448 | \ |
---|
449 | if ( f.base == 'b' || f.base == 'B' ) { /* bespoke binary format */ \ |
---|
450 | int bits = high1( f.val ); /* position of most significant bit */ \ |
---|
451 | if ( bits == 0 ) bits = 1; /* 0 value => force one bit to print */ \ |
---|
452 | int spaces; \ |
---|
453 | if ( ! f.flags.left ) { /* right justified ? */ \ |
---|
454 | /* Note, base prefix then zero padding or spacing then prefix. */ \ |
---|
455 | if ( f.flags.pc ) { \ |
---|
456 | spaces = f.wd - f.pc; \ |
---|
457 | if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \ |
---|
458 | if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \ |
---|
459 | if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \ |
---|
460 | spaces = f.pc - bits; \ |
---|
461 | if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \ |
---|
462 | } else { \ |
---|
463 | spaces = f.wd - bits; \ |
---|
464 | if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \ |
---|
465 | if ( f.flags.pad0 ) { \ |
---|
466 | if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \ |
---|
467 | if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \ |
---|
468 | } else { \ |
---|
469 | if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \ |
---|
470 | if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \ |
---|
471 | } /* if */ \ |
---|
472 | } /* if */ \ |
---|
473 | } else { \ |
---|
474 | if ( ! f.flags.nobsdp ) fmt( os, "0%c", f.base ); \ |
---|
475 | if ( f.flags.pc ) { \ |
---|
476 | spaces = f.pc - bits; \ |
---|
477 | if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \ |
---|
478 | spaces = f.wd - f.pc; \ |
---|
479 | } else { /* pad0 flag ignored with left flag */ \ |
---|
480 | spaces = f.wd - bits; \ |
---|
481 | } /* if */ \ |
---|
482 | if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \ |
---|
483 | } /* if */ \ |
---|
484 | int shift = floor( bits - 1, 4 ); \ |
---|
485 | typeof( f.val ) temp = f.val; \ |
---|
486 | fmt( os, "%s", shortbin[(temp >> shift) & 0xf] ); \ |
---|
487 | for () { \ |
---|
488 | shift -= 4; \ |
---|
489 | if ( shift < 0 ) break; \ |
---|
490 | temp = f.val; \ |
---|
491 | fmt( os, "%s", longbin[(temp >> shift) & 0xf] ); \ |
---|
492 | } /* for */ \ |
---|
493 | if ( f.flags.left && spaces > 0 ) fmt( os, "%*s", spaces, " " ); \ |
---|
494 | return os; \ |
---|
495 | } /* if */ \ |
---|
496 | \ |
---|
497 | char fmtstr[sizeof(IFMTP)]; /* sizeof includes '\0' */ \ |
---|
498 | if ( ! f.flags.pc ) memcpy( &fmtstr, IFMTNP, sizeof(IFMTNP) ); \ |
---|
499 | else memcpy( &fmtstr, IFMTP, sizeof(IFMTP) ); \ |
---|
500 | int star = 5; /* position before first '*' */ \ |
---|
501 | \ |
---|
502 | /* Insert flags into spaces before '*', from right to left. */ \ |
---|
503 | if ( ! f.flags.nobsdp ) { fmtstr[star] = '#'; star -= 1; } \ |
---|
504 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \ |
---|
505 | if ( f.flags.sign ) { fmtstr[star] = '+'; star -= 1; } \ |
---|
506 | if ( f.flags.pad0 && ! f.flags.pc ) { fmtstr[star] = '0'; star -= 1; } \ |
---|
507 | fmtstr[star] = '\''; star -= 1; /* locale */ \ |
---|
508 | fmtstr[star] = '%'; \ |
---|
509 | \ |
---|
510 | if ( ! f.flags.pc ) { /* no precision */ \ |
---|
511 | fmtstr[sizeof(IFMTNP)-2] = f.base; /* sizeof includes '\0' */ \ |
---|
512 | /* printf( "%s %c\n", &fmtstr[star], f.base ); */ \ |
---|
513 | fmt( os, &fmtstr[star], f.wd, f.val ); \ |
---|
514 | } else { /* precision */ \ |
---|
515 | fmtstr[sizeof(IFMTP)-2] = f.base; /* sizeof includes '\0' */ \ |
---|
516 | /* printf( "%s %c\n", &fmtstr[star], f.base ); */ \ |
---|
517 | fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); \ |
---|
518 | } /* if */ \ |
---|
519 | return os; \ |
---|
520 | } /* ?|? */ \ |
---|
521 | void ?|?( ostype & os, _Ostream_Manip(T) f ) { \ |
---|
522 | (ostype &)(os | f); ends( os ); \ |
---|
523 | } /* ?|? */ \ |
---|
524 | } // distribution |
---|
525 | |
---|
526 | IntegralFMTImpl( signed char, " *hh ", " *.*hh " ) |
---|
527 | IntegralFMTImpl( unsigned char, " *hh ", " *.*hh " ) |
---|
528 | IntegralFMTImpl( signed short int, " *h ", " *.*h " ) |
---|
529 | IntegralFMTImpl( unsigned short int, " *h ", " *.*h " ) |
---|
530 | IntegralFMTImpl( signed int, " * ", " *.* " ) |
---|
531 | IntegralFMTImpl( unsigned int, " * ", " *.* " ) |
---|
532 | IntegralFMTImpl( signed long int, " *l ", " *.*l " ) |
---|
533 | IntegralFMTImpl( unsigned long int, " *l ", " *.*l " ) |
---|
534 | IntegralFMTImpl( signed long long int, " *ll ", " *.*ll " ) |
---|
535 | IntegralFMTImpl( unsigned long long int, " *ll ", " *.*ll " ) |
---|
536 | |
---|
537 | |
---|
538 | #if defined( __SIZEOF_INT128__ ) |
---|
539 | // Default prefix for non-decimal prints is 0b, 0, 0x. |
---|
540 | forall( ostype & | basic_ostream( ostype ) ) |
---|
541 | static inline void base_128( ostype & os, unsigned int128 val, unsigned int128 power, _Ostream_Manip(uint64_t) & f, unsigned int maxdig, unsigned int bits, unsigned int cnt = 0 ) { |
---|
542 | int wd = 1; // f.wd is never 0 because 0 implies left-pad |
---|
543 | if ( val > power ) { // subdivide value into printable 64-bit values |
---|
544 | base_128( os, val / power, power, f, maxdig, bits, cnt + 1 ); // recursive |
---|
545 | f.val = val % power; |
---|
546 | if ( cnt == 1 && f.flags.left ) { wd = f.wd; f.wd = maxdig; } // copy f.wd and reset for printing middle chunk |
---|
547 | // printf( "R val:%#lx(%lu) wd:%u pc:%u base:%c neg:%d pc:%d left:%d nobsdp:%d sign:%d pad0:%d\n", |
---|
548 | // f.val, f.val, f.wd, f.pc, f.base, f.flags.neg, f.flags.pc, f.flags.left, f.flags.nobsdp, f.flags.sign, f.flags.pad0 ); |
---|
549 | (ostype &)(os | f); |
---|
550 | if ( cnt == 1 ) { |
---|
551 | if ( f.flags.left ) { wd -= maxdig; f.wd = wd < 0 ? 1 : wd; } // update and restore f.wd for printing end chunk |
---|
552 | sepOff( os ); // no seperator between chunks |
---|
553 | } // if |
---|
554 | } else { // print start chunk |
---|
555 | f.val = val; |
---|
556 | // f.pc is unsigned => use wd |
---|
557 | if ( f.flags.pc && f.pc > maxdig * cnt ) { wd = f.pc - maxdig * cnt; f.pc = wd < 0 ? 0 : wd; } |
---|
558 | else { f.flags.pc = false; f.pc = 0; } |
---|
559 | |
---|
560 | if ( ! f.flags.left ) { // right justify |
---|
561 | wd = f.wd - maxdig * cnt; |
---|
562 | f.wd = wd < 0 ? 1 : wd; |
---|
563 | wd = maxdig; |
---|
564 | } else { // left justify |
---|
565 | if ( cnt != 0 ) { // value >= 2^64 ? |
---|
566 | unsigned int dig, bs = 0; |
---|
567 | // compute size of prefix digits and base |
---|
568 | if ( f.base == 'd' || f.base == 'u' ) { // no base prefix |
---|
569 | dig = ceil( log10( f.val ) ); // use floating-point |
---|
570 | if ( f.base == 'd' && (f.flags.neg || f.flags.sign) ) bs = 1; // sign ? |
---|
571 | } else { |
---|
572 | dig = ceiling_div( high1( f.val ), bits ); |
---|
573 | if ( ! f.flags.nobsdp ) { // base prefix ? |
---|
574 | if ( f.base == 'o' ) { |
---|
575 | // 0 prefix for octal is not added for precision with leading zero |
---|
576 | if ( f.pc <= dig ) bs = 1; // 1 character prefix |
---|
577 | } else bs = 2; // 2 character prefix |
---|
578 | } // if |
---|
579 | } // if |
---|
580 | wd = f.wd - (f.pc > dig ? f.pc : dig) - bs; // precision > leading digits ? |
---|
581 | if ( wd < 0 ) wd = 1; |
---|
582 | f.wd = 1; |
---|
583 | } // if |
---|
584 | // all manipulators handled implicitly for value < 2^64 |
---|
585 | } // if |
---|
586 | // prior checks ensure wd not negative |
---|
587 | |
---|
588 | if ( f.flags.neg ) f.val = -f.val; |
---|
589 | // printf( "L val:%#lx(%lu) wd:%u pc:%u base:%c neg:%d pc:%d left:%d nobsdp:%d sign:%d pad0:%d\n", |
---|
590 | // f.val, f.val, f.wd, f.pc, f.base, f.flags.neg, f.flags.pc, f.flags.left, f.flags.nobsdp, f.flags.sign, f.flags.pad0 ); |
---|
591 | (ostype &)(os | f); |
---|
592 | |
---|
593 | // remaining middle and end chunks are padded with 0s on the left |
---|
594 | if ( ! f.flags.left ) { f.flags.pad0 = true; f.flags.pc = false; } // left pad with 0s |
---|
595 | else { f.pc = maxdig; f.flags.pc = true; } // left pad with precision |
---|
596 | |
---|
597 | if ( cnt != 0 ) sepOff( os ); // no seperator between chunks |
---|
598 | f.wd = wd; // reset f.wd for next chunk |
---|
599 | f.flags.sign = false; // no leading +/- sign |
---|
600 | f.flags.nobsdp = true; // no leading base prefix |
---|
601 | } // if |
---|
602 | } // base_128 |
---|
603 | |
---|
604 | #define IntegralFMTImpl128( T ) \ |
---|
605 | forall( ostype & | basic_ostream( ostype ) ) { \ |
---|
606 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \ |
---|
607 | _Ostream_Manip(uint64_t) fmt; \ |
---|
608 | fmt.[wd, pc, base, all] = f.[wd, pc, base, all]; \ |
---|
609 | if ( f.base == 'b' | f.base == 'B' ) { \ |
---|
610 | base_128( os, f.val, (unsigned int128)1 << 64, fmt, 64, 1 ); \ |
---|
611 | } else if ( f.base == 'o' ) { \ |
---|
612 | base_128( os, f.val, (unsigned int128)1 << 63, fmt, 21, 3 ); \ |
---|
613 | } else if ( f.base == 'd' || f.base == 'u' ) { \ |
---|
614 | if ( f.base == 'd' && f.val < 0 ) { f.val = -f.val; fmt.flags.neg = true; } \ |
---|
615 | base_128( os, f.val, (unsigned int128)10_000_000_000_000_000_000UL, fmt, 19, 0 ); \ |
---|
616 | } else { \ |
---|
617 | base_128( os, f.val, (unsigned int128)1 << 64, fmt, 16, 4 ); \ |
---|
618 | } /* if */ \ |
---|
619 | return os; \ |
---|
620 | } /* ?|? */ \ |
---|
621 | void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \ |
---|
622 | } // distribution |
---|
623 | |
---|
624 | IntegralFMTImpl128( int128 ) |
---|
625 | IntegralFMTImpl128( unsigned int128 ) |
---|
626 | #endif // __SIZEOF_INT128__ |
---|
627 | |
---|
628 | // *********************************** floating point *********************************** |
---|
629 | |
---|
630 | static const char *suffixes[] = { |
---|
631 | "y", "z", "a", "f", "p", "n", "u", "m", "", |
---|
632 | "K", "M", "G", "T", "P", "E", "Z", "Y" |
---|
633 | }; |
---|
634 | #define SUFFIXES_START (-24) /* Smallest power for which there is a suffix defined. */ |
---|
635 | #define SUFFIXES_END (SUFFIXES_START + (int)((sizeof(suffixes) / sizeof(char *) - 1) * 3)) |
---|
636 | |
---|
637 | #define PrintWithDP2( os, format, ... ) \ |
---|
638 | { \ |
---|
639 | if ( ! f.flags.eng ) { \ |
---|
640 | len = snprintf( buf, size, format, ##__VA_ARGS__ ); \ |
---|
641 | if ( isfinite( f.val ) && ( f.pc != 0 || ! f.flags.nobsdp ) ) { /* if number, print decimal point when no fraction or exponent */ \ |
---|
642 | for ( i = 0; i < len && buf[i] != '.' && buf[i] != 'e' && buf[i] != 'E'; i += 1 ); /* decimal point or scientific ? */ \ |
---|
643 | if ( i == len ) { \ |
---|
644 | if ( ! f.flags.left ) { \ |
---|
645 | buf[i] = '.'; buf[i + 1] = '\0'; \ |
---|
646 | if ( buf[0] == ' ' ) bufbeg = 1; /* decimal point within width */ \ |
---|
647 | } else { \ |
---|
648 | for ( i = 0; i < len && buf[i] != ' '; i += 1 ); /* trailing blank ? */ \ |
---|
649 | buf[i] = '.'; \ |
---|
650 | if ( i == len ) buf[i + 1] = '\0'; \ |
---|
651 | } /* if */ \ |
---|
652 | } /* if */ \ |
---|
653 | } /* if */ \ |
---|
654 | } else { \ |
---|
655 | int exp10, len2; \ |
---|
656 | eng( f.val, f.pc, exp10 ); /* changes arguments */ \ |
---|
657 | /* printf( "%g %d %d %d %s\n", f.val, f.wd, f.pc, exp10, format ); */ \ |
---|
658 | if ( ! f.flags.left && f.wd > 1 ) { \ |
---|
659 | /* Exponent size: 'e', optional minus sign, number of digits: log10(0) => undefined */ \ |
---|
660 | f.wd -= 1 + (exp10 < 0 ? 1 : 0) + lrint( floor( exp10 == 0 ? 0 : log10( abs( exp10 ) ) ) ) + 1; \ |
---|
661 | if ( f.wd < 1 ) f.wd = 1; \ |
---|
662 | } /* if */ \ |
---|
663 | len = snprintf( buf, size, format, ##__VA_ARGS__ ); \ |
---|
664 | if ( f.flags.left ) { \ |
---|
665 | for ( len -= 1; len > 0 && buf[len] == ' '; len -= 1 ); \ |
---|
666 | len += 1; \ |
---|
667 | } /* if */ \ |
---|
668 | if ( ! f.flags.nobsdp || (exp10 < SUFFIXES_START) || (exp10 > SUFFIXES_END) ) { \ |
---|
669 | len2 = snprintf( &buf[len], size - len, "e%d", exp10 ); \ |
---|
670 | } else { \ |
---|
671 | len2 = snprintf( &buf[len], size - len, "%s", suffixes[(exp10 - SUFFIXES_START) / 3] ); \ |
---|
672 | } /* if */ \ |
---|
673 | if ( f.flags.left && len + len2 < f.wd ) buf[len + len2] = ' '; \ |
---|
674 | } /* if */ \ |
---|
675 | fmt( os, "%s", &buf[bufbeg] ); \ |
---|
676 | } |
---|
677 | |
---|
678 | #define FloatingPointFMTImpl( T, DFMTNP, DFMTP ) \ |
---|
679 | forall( ostype & | basic_ostream( ostype ) ) { \ |
---|
680 | static void eng( T &value, int & pc, int & exp10 ) { \ |
---|
681 | exp10 = lrint( floor( log10( abs( value ) ) ) ); /* round to desired precision */ \ |
---|
682 | if ( exp10 < 0 ) exp10 -= 2; \ |
---|
683 | exp10 = floor( exp10, 3 ); \ |
---|
684 | value *= pow( 10.0, -exp10 ); \ |
---|
685 | if ( pc <= 3 ) pc = 3; \ |
---|
686 | } /* eng */ \ |
---|
687 | \ |
---|
688 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \ |
---|
689 | enum { size = 48 }; \ |
---|
690 | char buf[size]; \ |
---|
691 | int bufbeg = 0, i, len; \ |
---|
692 | \ |
---|
693 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); \ |
---|
694 | char fmtstr[sizeof(DFMTP) + 8]; /* sizeof includes '\0' */ \ |
---|
695 | if ( ! f.flags.pc ) memcpy( &fmtstr, DFMTNP, sizeof(DFMTNP) ); \ |
---|
696 | else memcpy( &fmtstr, DFMTP, sizeof(DFMTP) ); \ |
---|
697 | int star = 5; /* position before first '*' */ \ |
---|
698 | \ |
---|
699 | /* Insert flags into spaces before '*', from right to left. */ \ |
---|
700 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \ |
---|
701 | if ( f.flags.sign ) { fmtstr[star] = '+'; star -= 1; } \ |
---|
702 | if ( f.flags.pad0 ) { fmtstr[star] = '0'; star -= 1; } \ |
---|
703 | fmtstr[star] = '\''; star -= 1; /* locale */ \ |
---|
704 | fmtstr[star] = '%'; \ |
---|
705 | \ |
---|
706 | if ( ! f.flags.pc ) { /* no precision */ \ |
---|
707 | fmtstr[sizeof(DFMTNP)-2] = f.base; /* sizeof includes '\0' */ \ |
---|
708 | /* printf( "%g %d %s\n", f.val, f.wd, &fmtstr[star] ); */ \ |
---|
709 | PrintWithDP2( os, &fmtstr[star], f.wd, f.val ) \ |
---|
710 | } else { /* precision */ \ |
---|
711 | fmtstr[sizeof(DFMTP)-2] = f.base; /* sizeof includes '\0' */ \ |
---|
712 | /* printf( "%g %d %d %s\n", f.val, f.wd, f.pc, &fmtstr[star] ); */ \ |
---|
713 | PrintWithDP2( os, &fmtstr[star], f.wd, f.pc, f.val ) \ |
---|
714 | } /* if */ \ |
---|
715 | return os; \ |
---|
716 | } /* ?|? */ \ |
---|
717 | \ |
---|
718 | void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \ |
---|
719 | } // distribution |
---|
720 | |
---|
721 | FloatingPointFMTImpl( double, " * ", " *.* " ) |
---|
722 | FloatingPointFMTImpl( long double, " *L ", " *.*L " ) |
---|
723 | |
---|
724 | // *********************************** character *********************************** |
---|
725 | |
---|
726 | forall( ostype & | basic_ostream( ostype ) ) { |
---|
727 | ostype & ?|?( ostype & os, _Ostream_Manip(char) f ) { |
---|
728 | if ( f.base != 'c' ) { // bespoke binary/octal/hex format |
---|
729 | _Ostream_Manip(unsigned char) fmtuc @= { f.val, f.wd, f.pc, f.base, {'\0'} }; |
---|
730 | fmtuc.flags.pc = f.flags.pc; |
---|
731 | fmtuc.flags.nobsdp = f.flags.nobsdp; |
---|
732 | // os | fmtuc | nonl; |
---|
733 | (ostype &)(os | fmtuc); |
---|
734 | return os; |
---|
735 | } // if |
---|
736 | |
---|
737 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
738 | |
---|
739 | #define CFMTNP "% * " |
---|
740 | char fmtstr[sizeof(CFMTNP)]; // sizeof includes '\0' |
---|
741 | memcpy( &fmtstr, CFMTNP, sizeof(CFMTNP) ); |
---|
742 | int star = 1; // position before first '*' |
---|
743 | |
---|
744 | // Insert flags into spaces before '*', from right to left. |
---|
745 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } |
---|
746 | fmtstr[star] = '%'; |
---|
747 | |
---|
748 | fmtstr[sizeof(CFMTNP)-2] = f.base; // sizeof includes '\0' |
---|
749 | // printf( "%d %s\n", f.wd, &fmtstr[star] ); |
---|
750 | fmt( os, &fmtstr[star], f.wd, f.val ); |
---|
751 | return os; |
---|
752 | } // ?|? |
---|
753 | |
---|
754 | void ?|?( ostype & os, _Ostream_Manip(char) f ) { (ostype &)(os | f); ends( os ); } |
---|
755 | } // distribution |
---|
756 | |
---|
757 | // *********************************** C string *********************************** |
---|
758 | |
---|
759 | forall( ostype & | basic_ostream( ostype ) ) { |
---|
760 | ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f ) { |
---|
761 | if ( ! f.val ) return os; // null pointer ? |
---|
762 | |
---|
763 | if ( f.base != 's' ) { // bespoke binary/octal/hex format |
---|
764 | _Ostream_Manip(unsigned char) fmtuc @= { 0, f.wd, f.pc, f.base, {'\0'} }; |
---|
765 | fmtuc.flags.pc = f.flags.pc; |
---|
766 | fmtuc.flags.nobsdp = f.flags.nobsdp; |
---|
767 | for ( unsigned int i = 0; f.val[i] != '\0'; i += 1 ) { |
---|
768 | fmtuc.val = f.val[i]; |
---|
769 | // os | fmtuc | nonl; |
---|
770 | (ostype &)(os | fmtuc); |
---|
771 | } // for |
---|
772 | return os; |
---|
773 | } // if |
---|
774 | |
---|
775 | if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); |
---|
776 | |
---|
777 | #define SFMTNP "% * " |
---|
778 | #define SFMTP "% *.* " |
---|
779 | char fmtstr[sizeof(SFMTP)]; // sizeof includes '\0' |
---|
780 | if ( ! f.flags.pc ) memcpy( &fmtstr, SFMTNP, sizeof(SFMTNP) ); |
---|
781 | else memcpy( &fmtstr, SFMTP, sizeof(SFMTP) ); |
---|
782 | int star = 1; // position before first '*' |
---|
783 | |
---|
784 | // Insert flags into spaces before '*', from right to left. |
---|
785 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } |
---|
786 | fmtstr[star] = '%'; |
---|
787 | |
---|
788 | if ( ! f.flags.pc ) { // no precision |
---|
789 | // printf( "%d %s\n", f.wd, &fmtstr[star] ); |
---|
790 | fmtstr[sizeof(SFMTNP)-2] = f.base; // sizeof includes '\0' |
---|
791 | fmt( os, &fmtstr[star], f.wd, f.val ); |
---|
792 | } else { // precision |
---|
793 | fmtstr[sizeof(SFMTP)-2] = f.base; // sizeof includes '\0' |
---|
794 | // printf( "%d %d %s\n", f.wd, f.pc, &fmtstr[star] ); |
---|
795 | fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); |
---|
796 | } // if |
---|
797 | return os; |
---|
798 | } // ?|? |
---|
799 | |
---|
800 | void ?|?( ostype & os, _Ostream_Manip(const char *) f ) { (ostype &)(os | f); ends( os ); } |
---|
801 | } // distribution |
---|
802 | |
---|
803 | |
---|
804 | // *********************************** istream *********************************** |
---|
805 | |
---|
806 | |
---|
807 | forall( istype & | basic_istream( istype ) ) { |
---|
808 | istype & ?|?( istype & is, bool & b ) { |
---|
809 | char val[6]; |
---|
810 | fmt( is, "%5s", val ); |
---|
811 | if ( strcmp( val, "true" ) == 0 ) b = true; |
---|
812 | else if ( strcmp( val, "false" ) == 0 ) b = false; |
---|
813 | else { |
---|
814 | fprintf( stderr, "invalid Boolean constant\n" ); |
---|
815 | abort(); // cannot use abort stream |
---|
816 | } // if |
---|
817 | return is; |
---|
818 | } // ?|? |
---|
819 | void ?|?( istype & is, bool & b ) { |
---|
820 | (istype &)(is | b); ends( is ); |
---|
821 | } // ?|? |
---|
822 | |
---|
823 | istype & ?|?( istype & is, char & c ) { |
---|
824 | char temp; |
---|
825 | for () { |
---|
826 | fmt( is, "%c", &temp ); // must pass pointer through varg to fmt |
---|
827 | // do not overwrite parameter with newline unless appropriate |
---|
828 | if ( temp != '\n' || getANL$( is ) ) { c = temp; break; } |
---|
829 | if ( eof( is ) ) break; |
---|
830 | } // for |
---|
831 | return is; |
---|
832 | } // ?|? |
---|
833 | void ?|?( istype & is, char & c ) { |
---|
834 | (istype &)(is | c); ends( is ); |
---|
835 | } // ?|? |
---|
836 | |
---|
837 | istype & ?|?( istype & is, signed char & sc ) { |
---|
838 | fmt( is, "%hhi", &sc ); |
---|
839 | return is; |
---|
840 | } // ?|? |
---|
841 | void ?|?( istype & is, signed char & sc ) { |
---|
842 | (istype &)(is | sc); ends( is ); |
---|
843 | } // ?|? |
---|
844 | |
---|
845 | istype & ?|?( istype & is, unsigned char & usc ) { |
---|
846 | fmt( is, "%hhi", &usc ); |
---|
847 | return is; |
---|
848 | } // ?|? |
---|
849 | void ?|?( istype & is, unsigned char & usc ) { |
---|
850 | (istype &)(is | usc); ends( is ); |
---|
851 | } // ?|? |
---|
852 | |
---|
853 | istype & ?|?( istype & is, short int & si ) { |
---|
854 | fmt( is, "%hi", &si ); |
---|
855 | return is; |
---|
856 | } // ?|? |
---|
857 | void ?|?( istype & is, short int & si ) { |
---|
858 | (istype &)(is | si); ends( is ); |
---|
859 | } // ?|? |
---|
860 | |
---|
861 | istype & ?|?( istype & is, unsigned short int & usi ) { |
---|
862 | fmt( is, "%hi", &usi ); |
---|
863 | return is; |
---|
864 | } // ?|? |
---|
865 | void ?|?( istype & is, unsigned short int & usi ) { |
---|
866 | (istype &)(is | usi); ends( is ); |
---|
867 | } // ?|? |
---|
868 | |
---|
869 | istype & ?|?( istype & is, int & i ) { |
---|
870 | fmt( is, "%i", &i ); |
---|
871 | return is; |
---|
872 | } // ?|? |
---|
873 | void ?|?( istype & is, int & i ) { |
---|
874 | (istype &)(is | i); ends( is ); |
---|
875 | } // ?|? |
---|
876 | |
---|
877 | istype & ?|?( istype & is, unsigned int & ui ) { |
---|
878 | fmt( is, "%i", &ui ); |
---|
879 | return is; |
---|
880 | } // ?|? |
---|
881 | void ?|?( istype & is, unsigned int & ui ) { |
---|
882 | (istype &)(is | ui); ends( is ); |
---|
883 | } // ?|? |
---|
884 | |
---|
885 | istype & ?|?( istype & is, long int & li ) { |
---|
886 | fmt( is, "%li", &li ); |
---|
887 | return is; |
---|
888 | } // ?|? |
---|
889 | void ?|?( istype & is, long int & li ) { |
---|
890 | (istype &)(is | li); ends( is ); |
---|
891 | } // ?|? |
---|
892 | |
---|
893 | istype & ?|?( istype & is, unsigned long int & ulli ) { |
---|
894 | fmt( is, "%li", &ulli ); |
---|
895 | return is; |
---|
896 | } // ?|? |
---|
897 | void ?|?( istype & is, unsigned long int & ulli ) { |
---|
898 | (istype &)(is | ulli); ends( is ); |
---|
899 | } // ?|? |
---|
900 | |
---|
901 | istype & ?|?( istype & is, long long int & lli ) { |
---|
902 | fmt( is, "%lli", &lli ); |
---|
903 | return is; |
---|
904 | } // ?|? |
---|
905 | void ?|?( istype & is, long long int & lli ) { |
---|
906 | (istype &)(is | lli); ends( is ); |
---|
907 | } // ?|? |
---|
908 | |
---|
909 | istype & ?|?( istype & is, unsigned long long int & ulli ) { |
---|
910 | fmt( is, "%lli", &ulli ); |
---|
911 | return is; |
---|
912 | } // ?|? |
---|
913 | void & ?|?( istype & is, unsigned long long int & ulli ) { |
---|
914 | (istype &)(is | ulli); ends( is ); |
---|
915 | } // ?|? |
---|
916 | |
---|
917 | #if defined( __SIZEOF_INT128__ ) |
---|
918 | istype & ?|?( istype & is, int128 & llli ) { |
---|
919 | return (istype &)(is | (unsigned int128 &)llli); |
---|
920 | } // ?|? |
---|
921 | void ?|?( istype & is, int128 & llli ) { |
---|
922 | (istype &)(is | llli); ends( is ); |
---|
923 | } // ?|? |
---|
924 | |
---|
925 | istype & ?|?( istype & is, unsigned int128 & ullli ) { |
---|
926 | char s[40]; |
---|
927 | bool sign = false; |
---|
928 | |
---|
929 | if ( fmt( is, " %[-]", s ) == 1 ) sign = true; // skip whitespace, negative sign ? |
---|
930 | // If the input is too large, the value returned is undefined. If there is no input, no value is returned |
---|
931 | if ( fmt( is, "%39[0-9]%*[0-9]", s ) == 1 ) { // take first 39 characters, ignore remaining |
---|
932 | ullli = 0; |
---|
933 | for ( unsigned int i = 0; s[i] != '\0'; i += 1 ) { |
---|
934 | ullli = ullli * 10 + s[i] - '0'; |
---|
935 | } // for |
---|
936 | if ( sign ) ullli = -ullli; |
---|
937 | } else if ( sign ) ungetc( is, '-' ); // return minus when no digits |
---|
938 | return is; |
---|
939 | } // ?|? |
---|
940 | void ?|?( istype & is, unsigned int128 & ullli ) { |
---|
941 | (istype &)(is | ullli); ends( is ); |
---|
942 | } // ?|? |
---|
943 | #endif // __SIZEOF_INT128__ |
---|
944 | |
---|
945 | istype & ?|?( istype & is, float & f ) { |
---|
946 | fmt( is, "%f", &f ); |
---|
947 | return is; |
---|
948 | } // ?|? |
---|
949 | void ?|?( istype & is, float & f ) { |
---|
950 | (istype &)(is | f); ends( is ); |
---|
951 | } // ?|? |
---|
952 | |
---|
953 | istype & ?|?( istype & is, double & d ) { |
---|
954 | fmt( is, "%lf", &d ); |
---|
955 | return is; |
---|
956 | } // ?|? |
---|
957 | void ?|?( istype & is, double & d ) { |
---|
958 | (istype &)(is | d); ends( is ); |
---|
959 | } // ?|? |
---|
960 | |
---|
961 | istype & ?|?( istype & is, long double & ld ) { |
---|
962 | fmt( is, "%Lf", &ld ); |
---|
963 | return is; |
---|
964 | } // ?|? |
---|
965 | void ?|?( istype & is, long double & ld ) { |
---|
966 | (istype &)(is | ld); ends( is ); |
---|
967 | } // ?|? |
---|
968 | |
---|
969 | istype & ?|?( istype & is, float _Complex & fc ) { |
---|
970 | float re, im; |
---|
971 | fmt( is, "%f%fi", &re, &im ); |
---|
972 | fc = re + im * _Complex_I; |
---|
973 | return is; |
---|
974 | } // ?|? |
---|
975 | void ?|?( istype & is, float _Complex & fc ) { |
---|
976 | (istype &)(is | fc); ends( is ); |
---|
977 | } // ?|? |
---|
978 | |
---|
979 | istype & ?|?( istype & is, double _Complex & dc ) { |
---|
980 | double re, im; |
---|
981 | fmt( is, "%lf%lfi", &re, &im ); |
---|
982 | dc = re + im * _Complex_I; |
---|
983 | return is; |
---|
984 | } // ?|? |
---|
985 | void ?|?( istype & is, double _Complex & dc ) { |
---|
986 | (istype &)(is | dc); ends( is ); |
---|
987 | } // ?|? |
---|
988 | |
---|
989 | istype & ?|?( istype & is, long double _Complex & ldc ) { |
---|
990 | long double re, im; |
---|
991 | fmt( is, "%Lf%Lfi", &re, &im ); |
---|
992 | ldc = re + im * _Complex_I; |
---|
993 | return is; |
---|
994 | } // ?|? |
---|
995 | void ?|?( istype & is, long double _Complex & ldc ) { |
---|
996 | (istype &)(is | ldc); ends( is ); |
---|
997 | } // ?|? |
---|
998 | |
---|
999 | // istype & ?|?( istype & is, const char fmt[] ) { |
---|
1000 | // fmt( is, fmt, "" ); |
---|
1001 | // return is; |
---|
1002 | // } // ?|? |
---|
1003 | |
---|
1004 | istype & ?|?( istype & is, char s[] ) { |
---|
1005 | fmt( is, "%s", s ); |
---|
1006 | return is; |
---|
1007 | } // ?|? |
---|
1008 | void ?|?( istype & is, char s[] ) { |
---|
1009 | (istype &)(is | s); ends( is ); |
---|
1010 | } // ?|? |
---|
1011 | |
---|
1012 | // manipulators |
---|
1013 | istype & ?|?( istype & is, istype & (* manip)( istype & ) ) { |
---|
1014 | return manip( is ); |
---|
1015 | } // ?|? |
---|
1016 | void ?|?( istype & is, istype & (* manip)( istype & ) ) { |
---|
1017 | manip( is ); ends( is ); |
---|
1018 | } // ?|? |
---|
1019 | |
---|
1020 | istype & nl( istype & is ) { |
---|
1021 | fmt( is, "%*[^\n]" ); // ignore characters to newline |
---|
1022 | return is; |
---|
1023 | } // nl |
---|
1024 | |
---|
1025 | istype & nlOn( istype & is ) { |
---|
1026 | nlOn( is ); // call void returning |
---|
1027 | return is; |
---|
1028 | } // nlOn |
---|
1029 | |
---|
1030 | istype & nlOff( istype & is ) { |
---|
1031 | nlOff( is ); // call void returning |
---|
1032 | return is; |
---|
1033 | } // nlOff |
---|
1034 | } // distribution |
---|
1035 | |
---|
1036 | // *********************************** manipulators *********************************** |
---|
1037 | |
---|
1038 | forall( istype & | basic_istream( istype ) ) { |
---|
1039 | istype & ?|?( istype & is, _Istream_Cstr f ) { |
---|
1040 | // skip xxx |
---|
1041 | if ( ! f.s ) { |
---|
1042 | // printf( "skip %s %d\n", f.scanset, f.wd ); |
---|
1043 | if ( f.wd == -1 ) fmt( is, f.scanset, "" ); // no input arguments |
---|
1044 | else for ( f.wd ) fmt( is, "%*c" ); |
---|
1045 | return is; |
---|
1046 | } // if |
---|
1047 | size_t len = 0; |
---|
1048 | if ( f.scanset ) len = strlen( f.scanset ); |
---|
1049 | char fmtstr[len + 16]; |
---|
1050 | int start = 1; |
---|
1051 | fmtstr[0] = '%'; |
---|
1052 | if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; } |
---|
1053 | if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); } |
---|
1054 | // cstr %s, %*s, %ws, %*ws |
---|
1055 | if ( ! f.scanset ) { |
---|
1056 | fmtstr[start] = 's'; fmtstr[start + 1] = '\0'; |
---|
1057 | // printf( "cstr %s\n", fmtstr ); |
---|
1058 | fmt( is, fmtstr, f.s ); |
---|
1059 | return is; |
---|
1060 | } // if |
---|
1061 | // incl %[xxx], %*[xxx], %w[xxx], %*w[xxx] |
---|
1062 | // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx] |
---|
1063 | fmtstr[start] = '['; start += 1; |
---|
1064 | if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; } |
---|
1065 | strcpy( &fmtstr[start], f.scanset ); // copy includes '\0' |
---|
1066 | len += start; |
---|
1067 | fmtstr[len] = ']'; fmtstr[len + 1] = '\0'; |
---|
1068 | // printf( "incl/excl %s\n", fmtstr ); |
---|
1069 | fmt( is, fmtstr, f.s ); |
---|
1070 | return is; |
---|
1071 | } // ?|? |
---|
1072 | void ?|?( istype & is, _Istream_Cstr f ) { |
---|
1073 | (istype &)(is | f); ends( is ); |
---|
1074 | } // ?|? |
---|
1075 | |
---|
1076 | istype & ?|?( istype & is, _Istream_Char f ) { |
---|
1077 | fmt( is, "%*c" ); // argument variable unused |
---|
1078 | return is; |
---|
1079 | } // ?|? |
---|
1080 | void ?|?( istype & is, _Istream_Char f ) { |
---|
1081 | (istype &)(is | f); ends( is ); |
---|
1082 | } // ?|? |
---|
1083 | } // distribution |
---|
1084 | |
---|
1085 | #define InputFMTImpl( T, CODE ) \ |
---|
1086 | forall( istype & | basic_istream( istype ) ) { \ |
---|
1087 | istype & ?|?( istype & is, _Istream_Manip(T) f ) { \ |
---|
1088 | enum { size = 16 }; \ |
---|
1089 | char fmtstr[size]; \ |
---|
1090 | if ( f.wd == -1 ) { \ |
---|
1091 | snprintf( fmtstr, size, "%%%s%s", f.ignore ? "*" : "", CODE ); \ |
---|
1092 | } else { \ |
---|
1093 | snprintf( fmtstr, size, "%%%s%d%s", f.ignore ? "*" : "", f.wd, CODE ); \ |
---|
1094 | } /* if */ \ |
---|
1095 | /* printf( "%d %s %p\n", f.wd, fmtstr, &f.val ); */ \ |
---|
1096 | fmt( is, fmtstr, &f.val ); \ |
---|
1097 | return is; \ |
---|
1098 | } /* ?|? */ \ |
---|
1099 | void ?|?( istype & is, _Istream_Manip(T) f ) { \ |
---|
1100 | (istype &)(is | f); ends( is ); \ |
---|
1101 | } /* ?|? */ \ |
---|
1102 | } // distribution |
---|
1103 | |
---|
1104 | InputFMTImpl( signed char, "hhi" ) |
---|
1105 | InputFMTImpl( unsigned char, "hhi" ) |
---|
1106 | InputFMTImpl( signed short int, "hi" ) |
---|
1107 | InputFMTImpl( unsigned short int, "hi" ) |
---|
1108 | InputFMTImpl( signed int, "i" ) |
---|
1109 | InputFMTImpl( unsigned int, "i" ) |
---|
1110 | InputFMTImpl( signed long int, "li" ) |
---|
1111 | InputFMTImpl( unsigned long int, "li" ) |
---|
1112 | InputFMTImpl( signed long long int, "lli" ) |
---|
1113 | InputFMTImpl( unsigned long long int, "lli" ) |
---|
1114 | |
---|
1115 | InputFMTImpl( float, "f" ) |
---|
1116 | InputFMTImpl( double, "lf" ) |
---|
1117 | InputFMTImpl( long double, "Lf" ) |
---|
1118 | |
---|
1119 | forall( istype & | basic_istream( istype ) ) { |
---|
1120 | istype & ?|?( istype & is, _Istream_Manip(float _Complex) fc ) { |
---|
1121 | float re, im; |
---|
1122 | _Istream_Manip(float) fmtuc @= { re, fc.wd, fc.ignore }; |
---|
1123 | is | fmtuc; |
---|
1124 | &fmtuc.val = &im; |
---|
1125 | is | fmtuc; |
---|
1126 | if ( ! fc.ignore ) fc.val = re + im * _Complex_I; // re/im are uninitialized for ignore |
---|
1127 | return is; |
---|
1128 | } // ?|? |
---|
1129 | void ?|?( istype & is, _Istream_Manip(float _Complex) fc ) { |
---|
1130 | (istype &)(is | fc); ends( is ); |
---|
1131 | } // ?|? |
---|
1132 | |
---|
1133 | istype & ?|?( istype & is, _Istream_Manip(double _Complex) dc ) { |
---|
1134 | double re, im; |
---|
1135 | _Istream_Manip(double) fmtuc @= { re, dc.wd, dc.ignore }; |
---|
1136 | is | fmtuc; |
---|
1137 | &fmtuc.val = &im; |
---|
1138 | is | fmtuc; |
---|
1139 | if ( ! dc.ignore ) dc.val = re + im * _Complex_I; // re/im are uninitialized for ignore |
---|
1140 | return is; |
---|
1141 | } // ?|? |
---|
1142 | void ?|?( istype & is, _Istream_Manip(double _Complex) dc ) { |
---|
1143 | (istype &)(is | dc); ends( is ); |
---|
1144 | } // ?|? |
---|
1145 | |
---|
1146 | istype & ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) { |
---|
1147 | long double re, im; |
---|
1148 | _Istream_Manip(long double) fmtuc @= { re, ldc.wd, ldc.ignore }; |
---|
1149 | is | fmtuc; |
---|
1150 | &fmtuc.val = &im; |
---|
1151 | is | fmtuc; |
---|
1152 | if ( ! ldc.ignore ) ldc.val = re + im * _Complex_I; // re/im are uninitialized for ignore |
---|
1153 | return is; |
---|
1154 | } // ?|? |
---|
1155 | void ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) { |
---|
1156 | (istype &)(is | ldc); ends( is ); |
---|
1157 | } // ?|? |
---|
1158 | } // distribution |
---|
1159 | |
---|
1160 | // Local Variables: // |
---|
1161 | // tab-width: 4 // |
---|
1162 | // compile-command: "cfa iostream.cfa" // |
---|
1163 | // End: // |
---|