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