source: libcfa/src/iostream.cfa@ 15a0f6f

Last change on this file since 15a0f6f was 15a0f6f, checked in by Peter A. Buhr <pabuhr@…>, 5 days ago

fix problem with printing 0 for largest int_128 values

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