source: libcfa/src/iostream.cfa@ cb0bcf1

Last change on this file since cb0bcf1 was 0f107e4, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

add reading fixed strings, seperate skip input manipulator, getline checks for delimiter

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