source: libcfa/src/iostream.cfa@ cc18e17

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

add concurrency lock to IO stream and provide user interface to lock stream

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