source: libcfa/src/iostream.cfa@ 3e93c00

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 3e93c00 was 4f37255, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

formatting

  • Property mode set to 100644
File size: 28.1 KB
RevLine 
[86bd7c1f]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//
[b117e0c]7// iostream.cfa --
[86bd7c1f]8//
[90c3b1c]9// Author : Peter A. Buhr
[86bd7c1f]10// Created On : Wed May 27 17:56:53 2015
[e24f13a]11// Last Modified By : Peter A. Buhr
[4f37255]12// Last Modified On : Sat Jul 13 08:07:59 2019
13// Update Count : 821
[86bd7c1f]14//
[51b73452]15
[58b6d1b]16#include "iostream.hfa"
[d3b7937]17
[51b73452]18extern "C" {
19#include <stdio.h>
[53a6c2a]20#include <stdbool.h> // true/false
[200fcb3]21//#include <string.h> // strlen, strcmp
[44574f2]22extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
[3c573e9]23extern int strcmp (const char *__s1, const char *__s2) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
24extern char *strcpy (char *__restrict __dest, const char *__restrict __src) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2)));
25extern void *memcpy (void *__restrict __dest, const void *__restrict __src, size_t __n) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2)));
[52f85e0]26#include <float.h> // DBL_DIG, LDBL_DIG
[b2ac656]27#include <math.h> // isfinite
[d3b7937]28#include <complex.h> // creal, cimag
[3c573e9]29} // extern "C"
[51b73452]30
[61c7239]31
[65240bb]32//*********************************** ostream ***********************************
[61c7239]33
34
[3ce0d440]35forall( dtype ostype | ostream( ostype ) ) {
[17a1b21]36 ostype & ?|?( ostype & os, zero_t ) {
37 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
38 fmt( os, "%d", 0n );
39 return os;
40 } // ?|?
41 void ?|?( ostype & os, zero_t z ) {
[65240bb]42 (ostype &)(os | z); ends( os );
[17a1b21]43 } // ?|?
44
45 ostype & ?|?( ostype & os, one_t ) {
46 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
47 fmt( os, "%d", 1n );
48 return os;
49 } // ?|?
50 void ?|?( ostype & os, one_t o ) {
[65240bb]51 (ostype &)(os | o); ends( os );
[17a1b21]52 } // ?|?
53
[93c2e0a]54 ostype & ?|?( ostype & os, bool b ) {
[3ce0d440]55 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
56 fmt( os, "%s", b ? "true" : "false" );
57 return os;
58 } // ?|?
[200fcb3]59 void ?|?( ostype & os, bool b ) {
[65240bb]60 (ostype &)(os | b); ends( os );
[200fcb3]61 } // ?|?
[51b73452]62
[200fcb3]63 ostype & ?|?( ostype & os, char c ) {
64 fmt( os, "%c", c );
65 if ( c == '\n' ) setNL( os, true );
66 return sepOff( os );
67 } // ?|?
68 void ?|?( ostype & os, char c ) {
[65240bb]69 (ostype &)(os | c); ends( os );
[3ce0d440]70 } // ?|?
71
[200fcb3]72 ostype & ?|?( ostype & os, signed char sc ) {
[3ce0d440]73 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
[200fcb3]74 fmt( os, "%hhd", sc );
[3ce0d440]75 return os;
76 } // ?|?
[200fcb3]77 void ?|?( ostype & os, signed char sc ) {
[65240bb]78 (ostype &)(os | sc); ends( os );
[200fcb3]79 } // ?|?
[3ce0d440]80
[200fcb3]81 ostype & ?|?( ostype & os, unsigned char usc ) {
[3ce0d440]82 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
[200fcb3]83 fmt( os, "%hhu", usc );
[3ce0d440]84 return os;
85 } // ?|?
[200fcb3]86 void ?|?( ostype & os, unsigned char usc ) {
[65240bb]87 (ostype &)(os | usc); ends( os );
[200fcb3]88 } // ?|?
[3ce0d440]89
90 ostype & ?|?( ostype & os, short int si ) {
91 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
92 fmt( os, "%hd", si );
93 return os;
94 } // ?|?
[200fcb3]95 void & ?|?( ostype & os, short int si ) {
[65240bb]96 (ostype &)(os | si); ends( os );
[200fcb3]97 } // ?|?
[3ce0d440]98
99 ostype & ?|?( ostype & os, unsigned short int usi ) {
100 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
101 fmt( os, "%hu", usi );
102 return os;
103 } // ?|?
[200fcb3]104 void & ?|?( ostype & os, unsigned short int usi ) {
[65240bb]105 (ostype &)(os | usi); ends( os );
[200fcb3]106 } // ?|?
[3ce0d440]107
108 ostype & ?|?( ostype & os, int i ) {
109 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
110 fmt( os, "%d", i );
111 return os;
112 } // ?|?
[200fcb3]113 void & ?|?( ostype & os, int i ) {
[65240bb]114 (ostype &)(os | i); ends( os );
[200fcb3]115 } // ?|?
[3ce0d440]116
117 ostype & ?|?( ostype & os, unsigned int ui ) {
118 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
119 fmt( os, "%u", ui );
120 return os;
121 } // ?|?
[200fcb3]122 void & ?|?( ostype & os, unsigned int ui ) {
[65240bb]123 (ostype &)(os | ui); ends( os );
[200fcb3]124 } // ?|?
[3ce0d440]125
126 ostype & ?|?( ostype & os, long int li ) {
127 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
128 fmt( os, "%ld", li );
129 return os;
130 } // ?|?
[200fcb3]131 void & ?|?( ostype & os, long int li ) {
[65240bb]132 (ostype &)(os | li); ends( os );
[200fcb3]133 } // ?|?
[3ce0d440]134
135 ostype & ?|?( ostype & os, unsigned long int uli ) {
136 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
137 fmt( os, "%lu", uli );
138 return os;
139 } // ?|?
[200fcb3]140 void & ?|?( ostype & os, unsigned long int uli ) {
[65240bb]141 (ostype &)(os | uli); ends( os );
[200fcb3]142 } // ?|?
[3ce0d440]143
144 ostype & ?|?( ostype & os, long long int lli ) {
145 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
146 fmt( os, "%lld", lli );
147 return os;
148 } // ?|?
[200fcb3]149 void & ?|?( ostype & os, long long int lli ) {
[65240bb]150 (ostype &)(os | lli); ends( os );
[200fcb3]151 } // ?|?
[3ce0d440]152
153 ostype & ?|?( ostype & os, unsigned long long int ulli ) {
154 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
155 fmt( os, "%llu", ulli );
156 return os;
157 } // ?|?
[200fcb3]158 void & ?|?( ostype & os, unsigned long long int ulli ) {
[65240bb]159 (ostype &)(os | ulli); ends( os );
[200fcb3]160 } // ?|?
[3ce0d440]161
[e63326b]162 #define PrintWithDP( os, format, val, ... ) \
163 { \
164 enum { size = 48 }; \
165 char buf[size]; \
166 int len = snprintf( buf, size, format, ##__VA_ARGS__, val ); \
167 fmt( os, "%s", buf ); \
168 if ( isfinite( val ) ) { /* if number, always print decimal point */ \
169 for ( int i = 0;; i += 1 ) { \
170 if ( i == len ) { fmt( os, "." ); break; } \
171 if ( buf[i] == '.' ) break; \
172 } /* for */ \
173 } /* if */ \
174 }
[b2ac656]175
[3ce0d440]176 ostype & ?|?( ostype & os, float f ) {
177 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
[e63326b]178 PrintWithDP( os, "%g", f );
[3ce0d440]179 return os;
180 } // ?|?
[200fcb3]181 void & ?|?( ostype & os, float f ) {
[65240bb]182 (ostype &)(os | f); ends( os );
[200fcb3]183 } // ?|?
[3ce0d440]184
185 ostype & ?|?( ostype & os, double d ) {
186 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
[e63326b]187 PrintWithDP( os, "%.*lg", d, DBL_DIG );
[3ce0d440]188 return os;
189 } // ?|?
[200fcb3]190 void & ?|?( ostype & os, double d ) {
[65240bb]191 (ostype &)(os | d); ends( os );
[200fcb3]192 } // ?|?
[3ce0d440]193
194 ostype & ?|?( ostype & os, long double ld ) {
195 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
[e63326b]196 PrintWithDP( os, "%.*Lg", ld, LDBL_DIG );
[3ce0d440]197 return os;
198 } // ?|?
[200fcb3]199 void & ?|?( ostype & os, long double ld ) {
[65240bb]200 (ostype &)(os | ld); ends( os );
[200fcb3]201 } // ?|?
[3ce0d440]202
203 ostype & ?|?( ostype & os, float _Complex fc ) {
204 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
[b2ac656]205// os | crealf( fc ) | nonl;
[3c573e9]206 PrintWithDP( os, "%g", crealf( fc ) );
207 PrintWithDP( os, "%+g", cimagf( fc ) );
[3c5dee4]208 fmt( os, "i" );
[3ce0d440]209 return os;
210 } // ?|?
[200fcb3]211 void & ?|?( ostype & os, float _Complex fc ) {
[65240bb]212 (ostype &)(os | fc); ends( os );
[200fcb3]213 } // ?|?
[3ce0d440]214
215 ostype & ?|?( ostype & os, double _Complex dc ) {
216 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
[b2ac656]217// os | creal( dc ) | nonl;
[3c573e9]218 PrintWithDP( os, "%.*lg", creal( dc ), DBL_DIG );
219 PrintWithDP( os, "%+.*lg", cimag( dc ), DBL_DIG );
[3c5dee4]220 fmt( os, "i" );
[3ce0d440]221 return os;
222 } // ?|?
[200fcb3]223 void & ?|?( ostype & os, double _Complex dc ) {
[65240bb]224 (ostype &)(os | dc); ends( os );
[200fcb3]225 } // ?|?
[3ce0d440]226
227 ostype & ?|?( ostype & os, long double _Complex ldc ) {
228 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
[b2ac656]229// os | creall( ldc ) || nonl;
[3c573e9]230 PrintWithDP( os, "%.*Lg", creall( ldc ), LDBL_DIG );
231 PrintWithDP( os, "%+.*Lg", cimagl( ldc ), LDBL_DIG );
[3c5dee4]232 fmt( os, "i" );
[3ce0d440]233 return os;
234 } // ?|?
[200fcb3]235 void & ?|?( ostype & os, long double _Complex ldc ) {
[65240bb]236 (ostype &)(os | ldc); ends( os );
[200fcb3]237 } // ?|?
[3ce0d440]238
239 ostype & ?|?( ostype & os, const char * str ) {
240 enum { Open = 1, Close, OpenClose };
241 static const unsigned char mask[256] @= {
242 // opening delimiters, no space after
243 ['('] : Open, ['['] : Open, ['{'] : Open,
[ad72c8b]244 ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
245 [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
[3ce0d440]246 // closing delimiters, no space before
247 [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
[ad72c8b]248 ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
[3ce0d440]249 [')'] : Close, [']'] : Close, ['}'] : Close,
250 // opening-closing delimiters, no space before or after
251 ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose,
252 [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
253 }; // mask
254
255 if ( str[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator
256
257 // first character IS NOT spacing or closing punctuation => add left separator
258 unsigned char ch = str[0]; // must make unsigned
259 if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
260 fmt( os, "%s", sepGetCur( os ) );
261 } // if
262
263 // if string starts line, must reset to determine open state because separator is off
264 sepReset( os ); // reset separator
265
266 // last character IS spacing or opening punctuation => turn off separator for next item
267 size_t len = strlen( str );
268 ch = str[len - 1]; // must make unsigned
269 if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
270 sepOn( os );
271 } else {
272 sepOff( os );
273 } // if
274 if ( ch == '\n' ) setNL( os, true ); // check *AFTER* sepPrt call above as it resets NL flag
275 return write( os, str, len );
276 } // ?|?
[200fcb3]277 void ?|?( ostype & os, const char * str ) {
[65240bb]278 (ostype &)(os | str); ends( os );
[200fcb3]279 } // ?|?
[3ce0d440]280
281// ostype & ?|?( ostype & os, const char16_t * str ) {
282// if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
283// fmt( os, "%ls", str );
284// return os;
285// } // ?|?
[d3b7937]286
[3ce0d440]287// #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
288// ostype & ?|?( ostype & os, const char32_t * str ) {
289// if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
290// fmt( os, "%ls", str );
291// return os;
292// } // ?|?
293// #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
[d3b7937]294
[3ce0d440]295// ostype & ?|?( ostype & os, const wchar_t * str ) {
296// if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
297// fmt( os, "%ls", str );
298// return os;
299// } // ?|?
300
301 ostype & ?|?( ostype & os, const void * p ) {
302 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
303 fmt( os, "%p", p );
304 return os;
305 } // ?|?
[200fcb3]306 void ?|?( ostype & os, const void * p ) {
[65240bb]307 (ostype &)(os | p); ends( os );
[200fcb3]308 } // ?|?
[3ce0d440]309
310 // manipulators
311 ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
[9d362a0]312 (ostype &)(manip( os ));
[200fcb3]313 return os;
314 } // ?|?
315 void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
[9d362a0]316 (ostype &)(manip( os ));
[65240bb]317 if ( getPrt( os ) ) ends( os ); // something printed ?
[5ea5b28]318 setPrt( os, false ); // turn off
[3ce0d440]319 } // ?|?
320
321 ostype & sep( ostype & os ) {
[9d362a0]322 return (ostype &)(os | sepGet( os ));
[3ce0d440]323 } // sep
324
325 ostype & sepTuple( ostype & os ) {
[200fcb3]326 return os | sepGetTuple( os );
[3ce0d440]327 } // sepTuple
328
[200fcb3]329 ostype & nl( ostype & os ) {
[9d362a0]330 (ostype &)(os | '\n');
[5ea5b28]331 setPrt( os, false ); // turn off
[3ce0d440]332 setNL( os, true );
333 flush( os );
[200fcb3]334 return sepOff( os ); // prepare for next line
335 } // nl
336
337 ostype & nonl( ostype & os ) {
[5ea5b28]338 setPrt( os, false ); // turn off
[3ce0d440]339 return os;
[200fcb3]340 } // nonl
[3ce0d440]341
342 ostype & sepOn( ostype & os ) {
[200fcb3]343 sepOn( os ); // call void returning
[3ce0d440]344 return os;
345 } // sepOn
346
347 ostype & sepOff( ostype & os ) {
[200fcb3]348 sepOff( os ); // call void returning
[3ce0d440]349 return os;
350 } // sepOff
[6de9f4a]351
[3ce0d440]352 ostype & sepEnable( ostype & os ) {
[200fcb3]353 sepEnable( os ); // call void returning
[3ce0d440]354 return os;
355 } // sepEnable
[b6dc097]356
[3ce0d440]357 ostype & sepDisable( ostype & os ) {
[200fcb3]358 sepDisable( os ); // call void returning
[3ce0d440]359 return os;
360 } // sepDisable
[cf16f94]361
[200fcb3]362 ostype & nlOn( ostype & os ) {
363 nlOn( os ); // call void returning
364 return os;
365 } // nlOn
366
367 ostype & nlOff( ostype & os ) {
368 nlOff( os ); // call void returning
369 return os;
370 } // nlOff
371} // distribution
[cf16f94]372
[c443d1d]373// tuples
[200fcb3]374forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
375 ostype & ?|?( ostype & os, T arg, Params rest ) {
[9d362a0]376 (ostype &)(os | arg); // print first argument
[200fcb3]377 sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator
[9d362a0]378 (ostype &)(os | rest); // print remaining arguments
[200fcb3]379 sepSetCur( os, sepGet( os ) ); // switch to regular separator
380 return os;
381 } // ?|?
382 void ?|?( ostype & os, T arg, Params rest ) {
[65240bb]383 // (ostype &)(?|?( os, arg, rest )); ends( os );
[9d362a0]384 (ostype &)(os | arg); // print first argument
[200fcb3]385 sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator
[9d362a0]386 (ostype &)(os | rest); // print remaining arguments
[200fcb3]387 sepSetCur( os, sepGet( os ) ); // switch to regular separator
[65240bb]388 ends( os );
[200fcb3]389 } // ?|?
390} // distribution
[c443d1d]391
[61c7239]392// writes the range [begin, end) to the given stream
393forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) {
394 void write( iterator_type begin, iterator_type end, ostype & os ) {
395 void print( elt_type i ) { os | i; }
396 for_each( begin, end, print );
397 } // ?|?
398
399 void write_reverse( iterator_type begin, iterator_type end, ostype & os ) {
400 void print( elt_type i ) { os | i; }
401 for_each_reverse( begin, end, print );
402 } // ?|?
403} // distribution
404
[65240bb]405//*********************************** manipulators ***********************************
[3c573e9]406
[65240bb]407//*********************************** integral ***********************************
[3c573e9]408
409static const char * shortbin[] = { "0", "1", "10", "11", "100", "101", "110", "111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };
410static const char * longbin[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };
411
412// Default prefix for non-decimal prints is 0b, 0, 0x.
413#define IntegralFMTImpl( T, CODE, IFMTNP, IFMTP ) \
414forall( dtype ostype | ostream( ostype ) ) { \
415 ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
416 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); \
417\
418 if ( f.base == 'b' || f.base == 'B' ) { /* bespoke binary format */ \
419 int bits; \
420 if ( f.val == (T){0} ) bits = 1; /* force at least one bit to print */ \
421 else bits = sizeof(long long int) * 8 - __builtin_clzll( f.val ); /* position of most significant bit */ \
422 bits = bits > sizeof(f.val) * 8 ? sizeof(f.val) * 8 : bits; \
423 int spaces = f.wd - bits; /* can be negative */ \
424 if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \
425 /* printf( "%d %d\n", bits, spaces ); */ \
426 if ( ! f.flags.left ) { /* right justified ? */ \
427 /* Note, base prefix then zero padding or spacing then prefix. */ \
428 if ( f.flags.pad0 || f.flags.pc ) { \
429 if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
430 if ( f.flags.pc ) spaces = f.pc - bits; \
431 if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
432 } else { \
433 if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \
434 if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
435 } /* if */ \
436 } else if ( ! f.flags.nobsdp ) { \
437 fmt( os, "0%c", f.base ); \
438 } /* if */ \
439 int shift = (bits - 1) / 4 * 4; /* floor( bits - 1, 4 ) */ \
440 typeof( f.val ) temp = f.val; \
441 fmt( os, "%s", shortbin[(temp >> shift) & 0xf] ); \
442 for () { \
443 shift -= 4; \
444 if ( shift < 0 ) break; \
445 temp = f.val; \
446 fmt( os, "%s", longbin[(temp >> shift) & 0xf] ); \
447 } /* for */ \
448 if ( f.flags.left && spaces > 0 ) fmt( os, "%*s", spaces, " " ); \
449 return os; \
450 } /* if */ \
451\
452 char fmtstr[sizeof(IFMTP)]; /* sizeof includes '\0' */ \
453 if ( ! f.flags.pc ) memcpy( &fmtstr, IFMTNP, sizeof(IFMTNP) ); \
454 else memcpy( &fmtstr, IFMTP, sizeof(IFMTP) ); \
455 int star = 4; /* position before first '*' */ \
456\
457 /* Insert flags into spaces before '*', from right to left. */ \
458 if ( ! f.flags.nobsdp ) { fmtstr[star] = '#'; star -= 1; } \
459 if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \
460 if ( f.flags.sign && f.base == CODE ) { fmtstr[star] = '+'; star -= 1; } \
461 if ( f.flags.pad0 && ! f.flags.pc ) { fmtstr[star] = '0'; star -= 1; } \
462 fmtstr[star] = '%'; \
463\
464 if ( ! f.flags.pc ) { /* no precision */ \
465 /* printf( "%s\n", &fmtstr[star] ); */ \
466 fmtstr[sizeof(IFMTNP)-2] = f.base; /* sizeof includes '\0' */ \
467 fmt( os, &fmtstr[star], f.wd, f.val ); \
468 } else { /* precision */ \
469 fmtstr[sizeof(IFMTP)-2] = f.base; /* sizeof includes '\0' */ \
470 /* printf( "%s\n", &fmtstr[star] ); */ \
471 fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); \
472 } /* if */ \
473 return os; \
474 } /* ?|? */ \
[65240bb]475 void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \
[3c573e9]476} // distribution
477
478IntegralFMTImpl( signed char, 'd', "% *hh ", "% *.*hh " )
479IntegralFMTImpl( unsigned char, 'u', "% *hh ", "% *.*hh " )
480IntegralFMTImpl( signed short int, 'd', "% *h ", "% *.*h " )
481IntegralFMTImpl( unsigned short int, 'u', "% *h ", "% *.*h " )
482IntegralFMTImpl( signed int, 'd', "% * ", "% *.* " )
483IntegralFMTImpl( unsigned int, 'u', "% * ", "% *.* " )
484IntegralFMTImpl( signed long int, 'd', "% *l ", "% *.*l " )
485IntegralFMTImpl( unsigned long int, 'u', "% *l ", "% *.*l " )
486IntegralFMTImpl( signed long long int, 'd', "% *ll ", "% *.*ll " )
487IntegralFMTImpl( unsigned long long int, 'u', "% *ll ", "% *.*ll " )
488
[65240bb]489//*********************************** floating point ***********************************
[3c573e9]490
491#define PrintWithDP2( os, format, val, ... ) \
492 { \
493 enum { size = 48 }; \
494 char buf[size]; \
495 int bufbeg = 0, i, len = snprintf( buf, size, format, ##__VA_ARGS__, val ); \
496 if ( isfinite( val ) && (f.base != 'g' || f.pc != 0) ) { /* if number, print decimal point */ \
497 for ( i = 0; i < len && buf[i] != '.' && buf[i] != 'e' && buf[i] != 'E'; i += 1 ); /* decimal point or scientific ? */ \
498 if ( i == len && ! f.flags.nobsdp ) { \
499 if ( ! f.flags.left ) { \
500 buf[i] = '.'; buf[i + 1] = '\0'; \
[4f37255]501 if ( buf[0] == ' ' ) bufbeg = 1; /* decimal point within width */ \
[3c573e9]502 } else { \
503 for ( i = 0; i < len && buf[i] != ' '; i += 1 ); /* trailing blank ? */ \
504 buf[i] = '.'; \
505 if ( i == len ) buf[i + 1] = '\0'; \
506 } /* if */ \
507 } /* if */ \
508 } /* if */ \
509 fmt( os, "%s", &buf[bufbeg] ); \
510 }
511
512#define FloatingPointFMTImpl( T, DFMTNP, DFMTP ) \
513forall( dtype ostype | ostream( ostype ) ) { \
514 ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
515 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); \
516 char fmtstr[sizeof(DFMTP)]; /* sizeof includes '\0' */ \
517 if ( ! f.flags.pc ) memcpy( &fmtstr, DFMTNP, sizeof(DFMTNP) ); \
518 else memcpy( &fmtstr, DFMTP, sizeof(DFMTP) ); \
519 int star = 4; /* position before first '*' */ \
520\
521 /* Insert flags into spaces before '*', from right to left. */ \
522 if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \
523 if ( f.flags.sign ) { fmtstr[star] = '+'; star -= 1; } \
524 if ( f.flags.pad0 ) { fmtstr[star] = '0'; star -= 1; } \
525 fmtstr[star] = '%'; \
526\
527 if ( ! f.flags.pc ) { /* no precision */ \
528 fmtstr[sizeof(DFMTNP)-2] = f.base; /* sizeof includes '\0' */ \
529 /* printf( "%g %d %s\n", f.val, f.wd, &fmtstr[star]); */ \
530 PrintWithDP2( os, &fmtstr[star], f.val, f.wd ) \
531 } else { /* precision */ \
532 fmtstr[sizeof(DFMTP)-2] = f.base; /* sizeof includes '\0' */ \
533 /* printf( "%g %d %d %s\n", f.val, f.wd, f.pc, &fmtstr[star] ); */ \
534 PrintWithDP2( os, &fmtstr[star], f.val, f.wd, f.pc ) \
535 } /* if */ \
536 return os; \
537 } /* ?|? */ \
[65240bb]538 void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \
[3c573e9]539} // distribution
540
541FloatingPointFMTImpl( double, "% * ", "% *.* " )
542FloatingPointFMTImpl( long double, "% *L ", "% *.*L " )
543
[65240bb]544//*********************************** character ***********************************
[3c573e9]545
546forall( dtype ostype | ostream( ostype ) ) {
547 ostype & ?|?( ostype & os, _Ostream_Manip(char) f ) {
[4f37255]548 if ( f.base != 'c' ) { // bespoke binary/octal/hex format
[3c573e9]549 _Ostream_Manip(unsigned char) fmtuc @= { f.val, f.wd, f.pc, f.base, {'\0'} };
550 fmtuc.flags.pc = f.flags.pc;
551 fmtuc.flags.nobsdp = f.flags.nobsdp;
552// os | fmtuc | nonl;
553 (ostype &)(os | fmtuc);
554 return os;
555 } // if
556
557 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
558
559 #define CFMTNP "% * "
[4f37255]560 char fmtstr[sizeof(CFMTNP)]; // sizeof includes '\0'
[3c573e9]561 memcpy( &fmtstr, CFMTNP, sizeof(CFMTNP) );
[4f37255]562 int star = 1; // position before first '*'
[3c573e9]563
564 // Insert flags into spaces before '*', from right to left.
565 if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; }
566 fmtstr[star] = '%';
567
[4f37255]568 fmtstr[sizeof(CFMTNP)-2] = f.base; // sizeof includes '\0'
[3c573e9]569 // printf( "%d %s\n", f.wd, &fmtstr[star] );
570 fmt( os, &fmtstr[star], f.wd, f.val );
571 return os;
572 } // ?|?
[65240bb]573 void ?|?( ostype & os, _Ostream_Manip(char) f ) { (ostype &)(os | f); ends( os ); }
[3c573e9]574} // distribution
575
[65240bb]576//*********************************** C string ***********************************
[3c573e9]577
578forall( dtype ostype | ostream( ostype ) ) {
579 ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f ) {
580 if ( ! f.val ) return os; // null pointer ?
581
582 if ( f.base != 's' ) { // bespoke binary/octal/hex format
583 _Ostream_Manip(unsigned char) fmtuc @= { 0, f.wd, f.pc, f.base, {'\0'} };
584 fmtuc.flags.pc = f.flags.pc;
585 fmtuc.flags.nobsdp = f.flags.nobsdp;
586 for ( unsigned int i = 0; f.val[i] != '\0'; i += 1 ) {
587 fmtuc.val = f.val[i];
588// os | fmtuc | nonl;
589 (ostype &)(os | fmtuc);
590 } // for
591 return os;
592 } // if
593
594 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
595
596 #define SFMTNP "% * "
597 #define SFMTP "% *.* "
598 char fmtstr[sizeof(SFMTP)]; // sizeof includes '\0'
599 if ( ! f.flags.pc ) memcpy( &fmtstr, SFMTNP, sizeof(SFMTNP) );
600 else memcpy( &fmtstr, SFMTP, sizeof(SFMTP) );
601 int star = 1; // position before first '*'
602
603 // Insert flags into spaces before '*', from right to left.
604 if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; }
605 fmtstr[star] = '%';
606
607 if ( ! f.flags.pc ) { // no precision
608 // printf( "%d %s\n", f.wd, &fmtstr[star] );
609 fmtstr[sizeof(SFMTNP)-2] = f.base; // sizeof includes '\0'
610 fmt( os, &fmtstr[star], f.wd, f.val );
611 } else { // precision
612 fmtstr[sizeof(SFMTP)-2] = f.base; // sizeof includes '\0'
613 // printf( "%d %d %s\n", f.wd, f.pc, &fmtstr[star] );
614 fmt( os, &fmtstr[star], f.wd, f.pc, f.val );
615 } // if
616 return os;
617 } // ?|?
[65240bb]618 void ?|?( ostype & os, _Ostream_Manip(const char *) f ) { (ostype &)(os | f); ends( os ); }
[3c573e9]619} // distribution
620
621
[65240bb]622//*********************************** istream ***********************************
[3c573e9]623
[e56cfdb0]624
[3ce0d440]625forall( dtype istype | istream( istype ) ) {
[93c2e0a]626 istype & ?|?( istype & is, bool & b ) {
[3ce0d440]627 char val[6];
628 fmt( is, "%5s", val );
629 if ( strcmp( val, "true" ) == 0 ) b = true;
630 else if ( strcmp( val, "false" ) == 0 ) b = false;
631 else {
[93c2e0a]632 fprintf( stderr, "invalid Boolean constant\n" );
[4f37255]633 abort(); // cannot use abort stream
[3ce0d440]634 } // if
635 return is;
636 } // ?|?
637
638 istype & ?|?( istype & is, char & c ) {
[0efb269]639 char temp;
640 for () {
[4f37255]641 fmt( is, "%c", &temp ); // must pass pointer through varg to fmt
[0efb269]642 // do not overwrite parameter with newline unless appropriate
643 if ( temp != '\n' || getANL( is ) ) { c = temp; break; }
644 if ( eof( is ) ) break;
645 } // for
[3ce0d440]646 return is;
647 } // ?|?
648
649 istype & ?|?( istype & is, signed char & sc ) {
[3c573e9]650 fmt( is, "%hhi", &sc );
[3ce0d440]651 return is;
652 } // ?|?
653
654 istype & ?|?( istype & is, unsigned char & usc ) {
[3c573e9]655 fmt( is, "%hhi", &usc );
[3ce0d440]656 return is;
657 } // ?|?
658
659 istype & ?|?( istype & is, short int & si ) {
[3c573e9]660 fmt( is, "%hi", &si );
[3ce0d440]661 return is;
662 } // ?|?
663
664 istype & ?|?( istype & is, unsigned short int & usi ) {
[3c573e9]665 fmt( is, "%hi", &usi );
[3ce0d440]666 return is;
667 } // ?|?
668
669 istype & ?|?( istype & is, int & i ) {
[3c573e9]670 fmt( is, "%i", &i );
[3ce0d440]671 return is;
672 } // ?|?
673
674 istype & ?|?( istype & is, unsigned int & ui ) {
[3c573e9]675 fmt( is, "%i", &ui );
[3ce0d440]676 return is;
677 } // ?|?
678
679 istype & ?|?( istype & is, long int & li ) {
[3c573e9]680 fmt( is, "%li", &li );
[3ce0d440]681 return is;
682 } // ?|?
683
684 istype & ?|?( istype & is, unsigned long int & ulli ) {
[3c573e9]685 fmt( is, "%li", &ulli );
[3ce0d440]686 return is;
687 } // ?|?
688
689 istype & ?|?( istype & is, long long int & lli ) {
[3c573e9]690 fmt( is, "%lli", &lli );
[3ce0d440]691 return is;
692 } // ?|?
693
694 istype & ?|?( istype & is, unsigned long long int & ulli ) {
[3c573e9]695 fmt( is, "%lli", &ulli );
[3ce0d440]696 return is;
697 } // ?|?
698
699
700 istype & ?|?( istype & is, float & f ) {
701 fmt( is, "%f", &f );
702 return is;
703 } // ?|?
704
705 istype & ?|?( istype & is, double & d ) {
706 fmt( is, "%lf", &d );
707 return is;
708 } // ?|?
709
710 istype & ?|?( istype & is, long double & ld ) {
711 fmt( is, "%Lf", &ld );
712 return is;
713 } // ?|?
714
715
716 istype & ?|?( istype & is, float _Complex & fc ) {
717 float re, im;
[3c573e9]718 fmt( is, "%f%fi", &re, &im );
[3ce0d440]719 fc = re + im * _Complex_I;
720 return is;
721 } // ?|?
722
723 istype & ?|?( istype & is, double _Complex & dc ) {
724 double re, im;
725 fmt( is, "%lf%lfi", &re, &im );
726 dc = re + im * _Complex_I;
727 return is;
728 } // ?|?
729
730 istype & ?|?( istype & is, long double _Complex & ldc ) {
731 long double re, im;
732 fmt( is, "%Lf%Lfi", &re, &im );
733 ldc = re + im * _Complex_I;
734 return is;
735 } // ?|?
736
[dc5072f]737 // istype & ?|?( istype & is, const char * fmt ) {
738 // fmt( is, fmt, "" );
739 // return is;
740 // } // ?|?
[04396aa]741
742 istype & ?|?( istype & is, char * s ) {
743 fmt( is, "%s", s );
744 return is;
745 } // ?|?
746
[3ce0d440]747 // manipulators
748 istype & ?|?( istype & is, istype & (* manip)( istype & ) ) {
749 return manip( is );
750 } // ?|?
751
[200fcb3]752 istype & nl( istype & is ) {
[3c5dee4]753 fmt( is, "%*[^\n]" ); // ignore characters to newline
[3ce0d440]754 return is;
[200fcb3]755 } // nl
[0efb269]756
757 istype & nlOn( istype & is ) {
758 nlOn( is ); // call void returning
759 return is;
760 } // nlOn
761
762 istype & nlOff( istype & is ) {
763 nlOff( is ); // call void returning
764 return is;
765 } // nlOff
[3ce0d440]766} // distribution
[44574f2]767
[65240bb]768//*********************************** manipulators ***********************************
[3c573e9]769
770forall( dtype istype | istream( istype ) )
[61c7239]771istype & ?|?( istype & is, _Istream_Cstr f ) {
772 // skip xxx
773 if ( ! f.s ) {
[dc5072f]774 // printf( "skip %s %d\n", f.scanset, f.wd );
[4f37255]775 if ( f.wd == -1 ) fmt( is, f.scanset, "" ); // no input arguments
[dc5072f]776 else for ( f.wd ) fmt( is, "%*c" );
[61c7239]777 return is;
778 } // if
779 size_t len = 0;
780 if ( f.scanset ) len = strlen( f.scanset );
781 char fmtstr[len + 16];
782 int start = 1;
783 fmtstr[0] = '%';
784 if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; }
785 if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); }
786 // cstr %s, %*s, %ws, %*ws
787 if ( ! f.scanset ) {
788 fmtstr[start] = 's'; fmtstr[start + 1] = '\0';
789 // printf( "cstr %s\n", fmtstr );
790 fmt( is, fmtstr, f.s );
791 return is;
792 } // if
793 // incl %[xxx], %*[xxx], %w[xxx], %*w[xxx]
794 // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx]
795 fmtstr[start] = '['; start += 1;
796 if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; }
797 strcpy( &fmtstr[start], f.scanset ); // copy includes '\0'
798 len += start;
799 fmtstr[len] = ']'; fmtstr[len + 1] = '\0';
800 // printf( "incl/excl %s\n", fmtstr );
801 fmt( is, fmtstr, f.s );
[3c573e9]802 return is;
[61c7239]803} // ?|?
[3c573e9]804
[86a8be5]805forall( dtype istype | istream( istype ) )
806istype & ?|?( istype & is, _Istream_Char f ) {
807 fmt( is, "%*c" ); // argument variable unused
808 return is;
809} // ?|?
810
[3c573e9]811#define InputFMTImpl( T, CODE ) \
812forall( dtype istype | istream( istype ) ) \
813istype & ?|?( istype & is, _Istream_Manip(T) f ) { \
814 enum { size = 16 }; \
815 char fmtstr[size]; \
[86a8be5]816 if ( f.wd == -1 ) { \
[3c573e9]817 snprintf( fmtstr, size, "%%%s%s", f.ignore ? "*" : "", CODE ); \
818 } else { \
819 snprintf( fmtstr, size, "%%%s%d%s", f.ignore ? "*" : "", f.wd, CODE ); \
820 } /* if */ \
821 /* printf( "%d %s %p\n", f.wd, fmtstr, &f.val ); */ \
822 fmt( is, fmtstr, &f.val ); \
823 return is; \
[61c7239]824} // ?|?
[3c573e9]825
826InputFMTImpl( signed char, "hhi" )
827InputFMTImpl( unsigned char, "hhi" )
828InputFMTImpl( signed short int, "hi" )
829InputFMTImpl( unsigned short int, "hi" )
830InputFMTImpl( signed int, "i" )
831InputFMTImpl( unsigned int, "i" )
832InputFMTImpl( signed long int, "li" )
833InputFMTImpl( unsigned long int, "li" )
834InputFMTImpl( signed long long int, "lli" )
835InputFMTImpl( unsigned long long int, "lli" )
836
837InputFMTImpl( float, "f" )
838InputFMTImpl( double, "lf" )
839InputFMTImpl( long double, "Lf" )
840
[61c7239]841forall( dtype istype | istream( istype ) )
842istype & ?|?( istype & is, _Istream_Manip(float _Complex) fc ) {
843 float re, im;
844 _Istream_Manip(float) fmtuc @= { re, fc.wd, fc.ignore };
845 is | fmtuc;
846 &fmtuc.val = &im;
847 is | fmtuc;
848 if ( ! fc.ignore ) fc.val = re + im * _Complex_I; // re/im are uninitialized for ignore
849 return is;
850} // ?|?
851
852forall( dtype istype | istream( istype ) )
853istype & ?|?( istype & is, _Istream_Manip(double _Complex) dc ) {
854 double re, im;
855 _Istream_Manip(double) fmtuc @= { re, dc.wd, dc.ignore };
856 is | fmtuc;
857 &fmtuc.val = &im;
858 is | fmtuc;
859 if ( ! dc.ignore ) dc.val = re + im * _Complex_I; // re/im are uninitialized for ignore
860 return is;
861} // ?|?
862
863forall( dtype istype | istream( istype ) )
864istype & ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) {
865 long double re, im;
866 _Istream_Manip(long double) fmtuc @= { re, ldc.wd, ldc.ignore };
867 is | fmtuc;
868 &fmtuc.val = &im;
869 is | fmtuc;
870 if ( ! ldc.ignore ) ldc.val = re + im * _Complex_I; // re/im are uninitialized for ignore
871 return is;
872} // ?|?
[3c573e9]873
[86bd7c1f]874// Local Variables: //
875// tab-width: 4 //
[084520f]876// compile-command: "cfa iostream.cfa" //
[86bd7c1f]877// End: //
Note: See TracBrowser for help on using the repository browser.