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