source: libcfa/src/iostream.cfa@ 59f74a2

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 59f74a2 was ac9ba12, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

add comments

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