// // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // iostream.cfa -- // // Author : Peter A. Buhr // Created On : Wed May 27 17:56:53 2015 // Last Modified By : Peter A. Buhr // Last Modified On : Sun Oct 13 10:53:09 2024 // Update Count : 2041 // #include "iostream.hfa" #include #include // true/false #include // UINT64_MAX #include // DBL_DIG, LDBL_DIG #include // creal, cimag #include // isspace //#include extern "C" { extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1))); extern int strcmp (const char *__s1, const char *__s2) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2))); extern char *strcpy (char *__restrict __dest, const char *__restrict __src) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); extern void *memcpy (void *__restrict __dest, const void *__restrict __src, size_t __n) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); extern char *strchr(const char *str, int ch); } // extern "C" #include "math.hfa" // isfinite, floor, ceiling_div #include "bitmanip.hfa" // high1 #pragma GCC visibility push(default) // *********************************** ostream *********************************** forall( ostype & | basic_ostream( ostype ) ) { ostype & ?|?( ostype & os, bool b ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); fmt( os, "%s", b ? "true" : "false" ); return os; } // ?|? OSTYPE_VOID_IMPL( os, bool ) ostype & ?|?( ostype & os, char c ) { fmt( os, "%c", c ); if ( c == '\n' ) setNL$( os, true ); return nosep( os ); } // ?|? OSTYPE_VOID_IMPL( os, char ) ostype & ?|?( ostype & os, signed char sc ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); fmt( os, "%'hhd", sc ); return os; } // ?|? OSTYPE_VOID_IMPL( os, signed char ) ostype & ?|?( ostype & os, unsigned char usc ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); fmt( os, "%'hhu", usc ); return os; } // ?|? OSTYPE_VOID_IMPL( os, unsigned char ) ostype & ?|?( ostype & os, short int si ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); fmt( os, "%'hd", si ); return os; } // ?|? OSTYPE_VOID_IMPL( os, short int ) ostype & ?|?( ostype & os, unsigned short int usi ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); fmt( os, "%'hu", usi ); return os; } // ?|? OSTYPE_VOID_IMPL( os, unsigned short int ) ostype & ?|?( ostype & os, int i ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); fmt( os, "%'d", i ); return os; } // ?|? OSTYPE_VOID_IMPL( os, int ) ostype & ?|?( ostype & os, unsigned int ui ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); fmt( os, "%'u", ui ); return os; } // ?|? OSTYPE_VOID_IMPL( os, unsigned int ) ostype & ?|?( ostype & os, long int li ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); fmt( os, "%'ld", li ); return os; } // ?|? OSTYPE_VOID_IMPL( os, long int ) ostype & ?|?( ostype & os, unsigned long int uli ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); fmt( os, "%'lu", uli ); return os; } // ?|? OSTYPE_VOID_IMPL( os, unsigned long int ) ostype & ?|?( ostype & os, long long int lli ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); fmt( os, "%'lld", lli ); return os; } // ?|? OSTYPE_VOID_IMPL( os, long long int ) ostype & ?|?( ostype & os, unsigned long long int ulli ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); fmt( os, "%'llu", ulli ); return os; } // ?|? OSTYPE_VOID_IMPL( os, unsigned long long int ) #if defined( __SIZEOF_INT128__ ) // UINT64_MAX 18_446_744_073_709_551_615_ULL #define P10_UINT64 10_000_000_000_000_000_000_ULL // 19 zeroes static inline void base10_128( ostype & os, unsigned int128 val ) { #if defined(__GNUC__) && __GNUC_PREREQ(7,0) // gcc version >= 7 if ( val > P10_UINT64 ) { #else if ( (uint64_t)(val >> 64) != 0 || (uint64_t)val > P10_UINT64 ) { // patch gcc 5 & 6 -O3 bug #endif // __GNUC_PREREQ(7,0) base10_128( os, val / P10_UINT64 ); // recursive fmt( os, "%.19lu", (uint64_t)(val % P10_UINT64) ); } else { fmt( os, "%lu", (uint64_t)val ); } // if } // base10_128 static inline void base10_128( ostype & os, int128 val ) { if ( val < 0 ) { fmt( os, "-" ); // leading negative sign val = -val; } // if base10_128( os, (unsigned int128)val ); // print zero/positive value } // base10_128 ostype & ?|?( ostype & os, int128 llli ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); base10_128( os, llli ); return os; } // ?|? OSTYPE_VOID_IMPL( os, int128 ) ostype & ?|?( ostype & os, unsigned int128 ullli ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); base10_128( os, ullli ); return os; } // ?|? OSTYPE_VOID_IMPL( os, unsigned int128 ) #endif // __SIZEOF_INT128__ #define PRINT_WITH_DP( os, format, val, ... ) \ { \ enum { size = 48 }; \ char buf[size]; \ int len = snprintf( buf, size, format, ##__VA_ARGS__, val ); \ fmt( os, "%s", buf ); \ if ( isfinite( val ) ) { /* if number, print decimal point when no fraction or exponent */ \ for ( i; 0 ~ @ ) { \ if ( i == len ) { fmt( os, "." ); break; } \ if ( buf[i] == '.' || buf[i] == 'e' || buf[i] == 'E' || \ buf[i] == 'p' || buf[i] == 'P' ) break; /* decimal point or scientific ? */ \ } /* for */ \ } /* if */ \ } ostype & ?|?( ostype & os, float f ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); PRINT_WITH_DP( os, "%'g", f ); return os; } // ?|? OSTYPE_VOID_IMPL( os, float ) ostype & ?|?( ostype & os, double d ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); PRINT_WITH_DP( os, "%'.*lg", d, DBL_DIG ); return os; } // ?|? OSTYPE_VOID_IMPL( os, double ) ostype & ?|?( ostype & os, long double ld ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); PRINT_WITH_DP( os, "%'.*Lg", ld, LDBL_DIG ); return os; } // ?|? OSTYPE_VOID_IMPL( os, long double ) ostype & ?|?( ostype & os, float _Complex fc ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); // os | crealf( fc ) | nonl; PRINT_WITH_DP( os, "%'g", crealf( fc ) ); PRINT_WITH_DP( os, "%'+g", cimagf( fc ) ); fmt( os, "i" ); return os; } // ?|? OSTYPE_VOID_IMPL( os, float _Complex ) ostype & ?|?( ostype & os, double _Complex dc ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); // os | creal( dc ) | nonl; PRINT_WITH_DP( os, "%'.*lg", creal( dc ), DBL_DIG ); PRINT_WITH_DP( os, "%'+.*lg", cimag( dc ), DBL_DIG ); fmt( os, "i" ); return os; } // ?|? OSTYPE_VOID_IMPL( os, double _Complex ) ostype & ?|?( ostype & os, long double _Complex ldc ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); // os | creall( ldc ) || nonl; PRINT_WITH_DP( os, "%'.*Lg", creall( ldc ), LDBL_DIG ); PRINT_WITH_DP( os, "%'+.*Lg", cimagl( ldc ), LDBL_DIG ); fmt( os, "i" ); return os; } // ?|? OSTYPE_VOID_IMPL( os, long double _Complex ) ostype & ?|?( ostype & os, const char s[] ) { enum { Open = 1, Close, OpenClose }; static const unsigned char mask[256] @= { // 256 covers all Latin-1 characters // opening delimiters, no space after ['('] = Open, ['['] = Open, ['{'] = Open, ['='] = Open, ['$'] = Open, [(unsigned char)'£'] = Open, [(unsigned char)'¥'] = Open, [(unsigned char)'¡'] = Open, [(unsigned char)'¿'] = Open, [(unsigned char)'«'] = Open, // closing delimiters, no space before [','] = Close, ['.'] = Close, [';'] = Close, ['!'] = Close, ['?'] = Close, ['%'] = Close, [(unsigned char)'¢'] = Close, [(unsigned char)'»'] = Close, [')'] = Close, [']'] = Close, ['}'] = Close, // opening-closing delimiters, no space before or after ['\''] = OpenClose, ['`'] = OpenClose, ['"'] = OpenClose, [':'] = OpenClose, [' '] = OpenClose, ['\f'] = OpenClose, ['\n'] = OpenClose, ['\r'] = OpenClose, ['\t'] = OpenClose, ['\v'] = OpenClose, // isspace }; // mask if ( s == 0p ) { fmt( os, "%s", "0p" ); return os; } // null pointer if ( s[0] == '\0' ) { nosep( os ); return os; } // null string => no leading/trailing separator // first character IS NOT spacing or closing punctuation => add left separator unsigned char ch = s[0]; // must make unsigned if ( sepPrt$( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) { fmt( os, "%s", sepGetCur$( os ) ); } // if // if string starts line, must reset to determine open state because separator is off sepReset$( os ); // reset separator // last character IS spacing or opening punctuation => turn off separator for next item int len = strlen( s ); ch = s[len - 1]; // must make unsigned fmt( os, "%s", s ); // fmt resets seperator, but reset it again if ( sepPrt$( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) { sep( os ); } else { nosep( os ); } // if if ( ch == '\n' ) setNL$( os, true ); // check *AFTER* sepPrt$ call above as it resets NL flag return os; // return write( os, s, len ); } // ?|? OSTYPE_VOID_IMPL( os, const char * ) // ostype & ?|?( ostype & os, const char16_t s[] ) { // if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); // fmt( os, "%ls", s ); // return os; // } // ?|? // #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous // ostype & ?|?( ostype & os, const char32_t s[] ) { // if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); // fmt( os, "%ls", s ); // return os; // } // ?|? // #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // ostype & ?|?( ostype & os, const wchar_t s[] ) { // if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); // fmt( os, "%ls", s ); // return os; // } // ?|? ostype & ?|?( ostype & os, const void * p ) { if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); fmt( os, "%p", p ); return os; } // ?|? OSTYPE_VOID_IMPL( os, const void * ) // manipulators ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) { return manip( os ); } // ?|? void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) { manip( os ); if ( getPrt$( os ) ) ends( os ); // something printed ? setPrt$( os, false ); // turn off } // ?|? ostype & nl( ostype & os ) { (ostype &)(os | '\n'); setPrt$( os, false ); // turn off setNL$( os, true ); return nosep( os ); // prepare for next line } // nl ostype & nonl( ostype & os ) { setPrt$( os, false ); // turn off return os; } // nonl ostype & nlOn( ostype & os ) { nlOn( os ); // call void returning return os; } // nlOn ostype & nlOff( ostype & os ) { nlOff( os ); // call void returning return os; } // nlOff ostype & sepVal( ostype & os ) { return (ostype &)(os | sepGet( os )); } // sepVal ostype & sepTupleVal( ostype & os ) { return os | sepGetTuple( os ); } // sepTupleVal ostype & sep( ostype & os ) { sep( os ); // call void returning return os; } // sep ostype & nosep( ostype & os ) { nosep( os ); // call void returning return os; } // nosep ostype & sepOn( ostype & os ) { sepOn( os ); // call void returning return os; } // sepOn ostype & sepOff( ostype & os ) { sepOff( os ); // call void returning return os; } // sepOff } // distribution // tuples forall( ostype &, T, Params... | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) { ostype & ?|?( ostype & os, T arg, Params rest ) { (ostype &)(os | arg); // print first argument sepSetCur$( os, sepGetTuple( os ) ); // switch to tuple separator (ostype &)(os | rest); // print remaining arguments sepSetCur$( os, sepGet( os ) ); // switch to regular separator return os; } // ?|? void ?|?( ostype & os, T arg, Params rest ) { // (ostype &)(?|?( os, arg, rest )); ends( os ); (ostype &)(os | arg); // print first argument sepSetCur$( os, sepGetTuple( os ) ); // switch to tuple separator (ostype &)(os | rest); // print remaining arguments sepSetCur$( os, sepGet( os ) ); // switch to regular separator ends( os ); } // ?|? } // distribution // writes the range [begin, end) to the given stream forall( ostype &, elt_type | writeable( elt_type, ostype ), iterator_type | iterator( iterator_type, elt_type ) ) { void write( iterator_type begin, iterator_type end, ostype & os ) { void print( elt_type i ) { os | i; } for_each( begin, end, print ); } // ?|? void write_reverse( iterator_type begin, iterator_type end, ostype & os ) { void print( elt_type i ) { os | i; } for_each_reverse( begin, end, print ); } // ?|? } // distribution // *********************************** manipulators *********************************** // *********************************** integral *********************************** static const char * shortbin[] = { "0", "1", "10", "11", "100", "101", "110", "111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; static const char * longbin[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; // Default prefix for non-decimal prints is 0b, 0, 0x. #define INTEGRAL_FMT_IMPL( T, IFMTNP, IFMTP ) \ forall( ostype & | basic_ostream( ostype ) ) { \ ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \ if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); \ \ if ( f.base == 'b' || f.base == 'B' ) { /* bespoke binary format */ \ int bits = high1( f.val ); /* position of most significant bit */ \ if ( bits == 0 ) bits = 1; /* 0 value => force one bit to print */ \ int spaces; \ if ( ! f.flags.left ) { /* right justified ? */ \ /* Note, base prefix then zero padding or spacing then prefix. */ \ if ( f.flags.pc ) { \ spaces = f.wd - f.pc; \ if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \ if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \ if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \ spaces = f.pc - bits; \ if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \ } else { \ spaces = f.wd - bits; \ if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \ if ( f.flags.pad0 ) { \ if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \ if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \ } else { \ if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \ if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \ } /* if */ \ } /* if */ \ } else { \ if ( ! f.flags.nobsdp ) fmt( os, "0%c", f.base ); \ if ( f.flags.pc ) { \ spaces = f.pc - bits; \ if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \ spaces = f.wd - f.pc; \ } else { /* pad0 flag ignored with left flag */ \ spaces = f.wd - bits; \ } /* if */ \ if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \ } /* if */ \ int shift = floor( bits - 1, 4 ); \ typeof( f.val ) temp = f.val; \ fmt( os, "%s", shortbin[(temp >> shift) & 0xf] ); \ for () { \ shift -= 4; \ if ( shift < 0 ) break; \ temp = f.val; \ fmt( os, "%s", longbin[(temp >> shift) & 0xf] ); \ } /* for */ \ if ( f.flags.left && spaces > 0 ) fmt( os, "%*s", spaces, " " ); \ return os; \ } /* if */ \ \ char fmtstr[sizeof(IFMTP)]; /* sizeof includes '\0' */ \ if ( ! f.flags.pc ) memcpy( &fmtstr, IFMTNP, sizeof(IFMTNP) ); \ else memcpy( &fmtstr, IFMTP, sizeof(IFMTP) ); \ int star = 5; /* position before first '*' */ \ \ /* Insert flags into spaces before '*', from right to left. */ \ if ( ! f.flags.nobsdp ) { fmtstr[star] = '#'; star -= 1; } \ if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \ if ( f.flags.sign ) { fmtstr[star] = '+'; star -= 1; } \ if ( f.flags.pad0 && ! f.flags.pc ) { fmtstr[star] = '0'; star -= 1; } \ fmtstr[star] = '\''; star -= 1; /* locale */ \ fmtstr[star] = '%'; \ \ /* Special case printing 0 in hexadecimal as printf does not put the base. */ \ if ( (f.base == 'x' | f.base == 'X') && ! f.flags.nobsdp && f.val == 0 ) { \ fmt( os, f.base == 'x' ? "0x" : "0X" ); \ f.wd -= 2; \ if ( f.wd < 0 ) f.wd = 1; \ } /* if */ \ \ if ( ! f.flags.pc ) { /* no precision */ \ fmtstr[sizeof(IFMTNP)-2] = f.base; /* sizeof includes '\0' */ \ /* printf( "%s %c\n", &fmtstr[star], f.base ); */ \ fmt( os, &fmtstr[star], f.wd, f.val ); \ } else { /* precision */ \ fmtstr[sizeof(IFMTP)-2] = f.base; /* sizeof includes '\0' */ \ /* printf( "%s %c\n", &fmtstr[star], f.base ); */ \ fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); \ } /* if */ \ return os; \ } /* ?|? */ \ OSTYPE_VOID_IMPL( os, _Ostream_Manip(T) ) \ } // distribution INTEGRAL_FMT_IMPL( signed char, " *hh ", " *.*hh " ) INTEGRAL_FMT_IMPL( unsigned char, " *hh ", " *.*hh " ) INTEGRAL_FMT_IMPL( signed short int, " *h ", " *.*h " ) INTEGRAL_FMT_IMPL( unsigned short int, " *h ", " *.*h " ) INTEGRAL_FMT_IMPL( signed int, " * ", " *.* " ) INTEGRAL_FMT_IMPL( unsigned int, " * ", " *.* " ) INTEGRAL_FMT_IMPL( signed long int, " *l ", " *.*l " ) INTEGRAL_FMT_IMPL( unsigned long int, " *l ", " *.*l " ) INTEGRAL_FMT_IMPL( signed long long int, " *ll ", " *.*ll " ) INTEGRAL_FMT_IMPL( unsigned long long int, " *ll ", " *.*ll " ) #if defined( __SIZEOF_INT128__ ) // Default prefix for non-decimal prints is 0b, 0, 0x. forall( ostype & | basic_ostream( ostype ) ) static 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 ) { int wd = 1; // f.wd is never 0 because 0 implies left-pad if ( val > power ) { // subdivide value into printable 64-bit values base_128( os, val / power, power, f, maxdig, bits, cnt + 1 ); // recursive f.val = val % power; if ( cnt == 1 && f.flags.left ) { wd = f.wd; f.wd = maxdig; } // copy f.wd and reset for printing middle chunk // printf( "R val:%#lx(%lu) wd:%u pc:%u base:%c neg:%d pc:%d left:%d nobsdp:%d sign:%d pad0:%d\n", // 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 ); (ostype &)(os | f); if ( cnt == 1 ) { if ( f.flags.left ) { wd -= maxdig; f.wd = wd < 0 ? 1 : wd; } // update and restore f.wd for printing end chunk nosep( os ); // no seperator between chunks } // if } else { // print start chunk f.val = val; // f.pc is unsigned => use wd if ( f.flags.pc && f.pc > maxdig * cnt ) { wd = f.pc - maxdig * cnt; f.pc = wd < 0 ? 0 : wd; } else { f.flags.pc = false; f.pc = 0; } if ( ! f.flags.left ) { // right justify wd = f.wd - maxdig * cnt; f.wd = wd < 0 ? 1 : wd; wd = maxdig; } else { // left justify if ( cnt != 0 ) { // value >= 2^64 ? unsigned int dig, bs = 0; // compute size of prefix digits and base if ( f.base == 'd' || f.base == 'u' ) { // no base prefix dig = ceil( log10( f.val ) ); // use floating-point if ( f.base == 'd' && (f.flags.neg || f.flags.sign) ) bs = 1; // sign ? } else { dig = ceiling_div( high1( f.val ), bits ); if ( ! f.flags.nobsdp ) { // base prefix ? if ( f.base == 'o' ) { // 0 prefix for octal is not added for precision with leading zero if ( f.pc <= dig ) bs = 1; // 1 character prefix } else bs = 2; // 2 character prefix } // if } // if wd = f.wd - (f.pc > dig ? f.pc : dig) - bs; // precision > leading digits ? if ( wd < 0 ) wd = 1; f.wd = 1; } // if // all manipulators handled implicitly for value < 2^64 } // if // prior checks ensure wd not negative if ( f.flags.neg ) f.val = -f.val; // printf( "L val:%#lx(%lu) wd:%u pc:%u base:%c neg:%d pc:%d left:%d nobsdp:%d sign:%d pad0:%d\n", // 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 ); (ostype &)(os | f); // remaining middle and end chunks are padded with 0s on the left if ( ! f.flags.left ) { f.flags.pad0 = true; f.flags.pc = false; } // left pad with 0s else { f.pc = maxdig; f.flags.pc = true; } // left pad with precision if ( cnt != 0 ) nosep( os ); // no seperator between chunks f.wd = wd; // reset f.wd for next chunk f.flags.sign = false; // no leading +/- sign f.flags.nobsdp = true; // no leading base prefix } // if } // base_128 #define INTEGRAL_FMT_IMPL128( T ) \ forall( ostype & | basic_ostream( ostype ) ) { \ ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \ _Ostream_Manip(uint64_t) fmt; \ fmt.[wd, pc, base, all] = f.[wd, pc, base, all]; \ if ( f.base == 'b' | f.base == 'B' ) { \ base_128( os, f.val, (unsigned int128)1 << 64, fmt, 64, 1 ); \ } else if ( f.base == 'o' ) { \ base_128( os, f.val, (unsigned int128)1 << 63, fmt, 21, 3 ); \ } else if ( f.base == 'd' || f.base == 'u' ) { \ if ( f.base == 'd' && f.val < 0 ) { f.val = -f.val; fmt.flags.neg = true; } \ base_128( os, f.val, (unsigned int128)10_000_000_000_000_000_000UL, fmt, 19, 0 ); \ } else { \ base_128( os, f.val, (unsigned int128)1 << 64, fmt, 16, 4 ); \ } /* if */ \ return os; \ } /* ?|? */ \ OSTYPE_VOID_IMPL( os, _Ostream_Manip(T) ) \ } // distribution INTEGRAL_FMT_IMPL128( int128 ) INTEGRAL_FMT_IMPL128( unsigned int128 ) #endif // __SIZEOF_INT128__ // *********************************** floating point *********************************** static const char *suffixes[] = { "y", "z", "a", "f", "p", "n", "u", "m", "", "K", "M", "G", "T", "P", "E", "Z", "Y" }; #define SUFFIXES_START (-24) /* Smallest power for which there is a suffix defined. */ #define SUFFIXES_END (SUFFIXES_START + (int)((sizeof(suffixes) / sizeof(char *) - 1) * 3)) #define PRINT_WITH_DP2( os, format, ... ) \ { \ if ( ! f.flags.eng ) { \ len = snprintf( buf, size, format, ##__VA_ARGS__ ); \ if ( isfinite( f.val ) && ! f.flags.nobsdp ) { /* if number, print decimal point when no fraction or exponent */ \ for ( i = 0; i < len && buf[i] != '.' && buf[i] != 'e' && buf[i] != 'E' && \ buf[i] != 'p' && buf[i] != 'P'; i += 1 ); /* decimal point or scientific ? */ \ if ( i == len ) { \ if ( ! f.flags.left ) { \ buf[i] = '.'; buf[i + 1] = '\0'; \ if ( buf[0] == ' ' ) bufbeg = 1; /* decimal point within width */ \ } else { \ for ( i = 0; i < len && buf[i] != ' '; i += 1 ); /* trailing blank ? */ \ buf[i] = '.'; \ if ( i == len ) buf[i + 1] = '\0'; \ } /* if */ \ } /* if */ \ } /* if */ \ } else { \ int exp10, len2; \ eng( f.val, f.pc, exp10 ); /* changes arguments */ \ /* printf( "%g %d %d %d %s\n", f.val, f.wd, f.pc, exp10, format ); */ \ if ( ! f.flags.left && f.wd > 1 ) { \ /* Exponent size: 'e', optional minus sign, number of digits: log10(0) => undefined */ \ f.wd -= 1 + (exp10 < 0 ? 1 : 0) + lrint( floor( exp10 == 0 ? 0 : log10( abs( exp10 ) ) ) ) + 1; \ if ( f.wd < 1 ) f.wd = 1; \ } /* if */ \ len = snprintf( buf, size, format, ##__VA_ARGS__ ); \ if ( f.flags.left ) { \ for ( len -= 1; len > 0 && buf[len] == ' '; len -= 1 ); \ len += 1; \ } /* if */ \ if ( ! f.flags.nobsdp || (exp10 < SUFFIXES_START) || (exp10 > SUFFIXES_END) ) { \ len2 = snprintf( &buf[len], size - len, "e%d", (int)exp10 /* ambiguity with function exp10 */ ); \ } else { \ len2 = snprintf( &buf[len], size - len, "%s", suffixes[(exp10 - SUFFIXES_START) / 3] ); \ } /* if */ \ if ( f.flags.left && len + len2 < f.wd ) buf[len + len2] = ' '; \ } /* if */ \ fmt( os, "%s", &buf[bufbeg] ); \ } #define FLOATING_POINT_FMT_IMPL( T, DFMTNP, DFMTP ) \ forall( ostype & | basic_ostream( ostype ) ) { \ static void eng( T &value, int & pc, int & exp10 ) { \ exp10 = lrint( floor( log10( abs( value ) ) ) ); /* round to desired precision */ \ if ( exp10 < 0 ) exp10 -= 2; \ exp10 = floor( exp10, 3 ); \ value *= pow( 10.0, -exp10 ); \ if ( pc < 0 ) pc = 3; \ } /* eng */ \ \ ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \ enum { size = 48 }; \ char buf[size]; \ int bufbeg = 0, i, len; \ \ if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); \ char fmtstr[sizeof(DFMTP) + 8]; /* sizeof includes '\0' */ \ if ( ! f.flags.pc ) memcpy( &fmtstr, DFMTNP, sizeof(DFMTNP) ); \ else memcpy( &fmtstr, DFMTP, sizeof(DFMTP) ); \ int star = 5; /* position before first '*' */ \ \ /* Insert flags into spaces before '*', from right to left. */ \ if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \ if ( f.flags.sign ) { fmtstr[star] = '+'; star -= 1; } \ if ( f.flags.pad0 ) { fmtstr[star] = '0'; star -= 1; } \ fmtstr[star] = '\''; star -= 1; /* locale */ \ fmtstr[star] = '%'; \ \ if ( ! f.flags.pc ) { /* no precision */ \ fmtstr[sizeof(DFMTNP)-2] = f.base; /* sizeof includes '\0' */ \ /* printf( "%g %d %s\n", f.val, f.wd, &fmtstr[star] ); */ \ PRINT_WITH_DP2( os, &fmtstr[star], f.wd, f.val ) \ } else { /* precision */ \ fmtstr[sizeof(DFMTP)-2] = f.base; /* sizeof includes '\0' */ \ /* printf( "%g %d %d %s\n", f.val, f.wd, f.pc, &fmtstr[star] ); */ \ PRINT_WITH_DP2( os, &fmtstr[star], f.wd, f.pc, f.val ) \ } /* if */ \ return os; \ } /* ?|? */ \ \ OSTYPE_VOID_IMPL( os, _Ostream_Manip(T) ) \ } // distribution FLOATING_POINT_FMT_IMPL( double, " * ", " *.* " ) FLOATING_POINT_FMT_IMPL( long double, " *L ", " *.*L " ) // *********************************** character *********************************** forall( ostype & | basic_ostream( ostype ) ) { ostype & ?|?( ostype & os, _Ostream_Manip(char) f ) { if ( f.base != 'c' ) { // bespoke binary/octal/hex format _Ostream_Manip(unsigned char) fmtuc @= { f.val, f.wd, f.pc, f.base, {'\0'} }; fmtuc.flags.pc = f.flags.pc; fmtuc.flags.nobsdp = f.flags.nobsdp; // os | fmtuc | nonl; (ostype &)(os | fmtuc); return os; } // if if ( sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); #define CFMTNP "% * " char fmtstr[sizeof(CFMTNP)]; // sizeof includes '\0' memcpy( &fmtstr, CFMTNP, sizeof(CFMTNP) ); int star = 1; // position before first '*' // Insert flags into spaces before '*', from right to left. if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } fmtstr[star] = '%'; fmtstr[sizeof(CFMTNP)-2] = f.base; // sizeof includes '\0' // printf( "%d %s\n", f.wd, &fmtstr[star] ); fmt( os, &fmtstr[star], f.wd, f.val ); return os; } // ?|? OSTYPE_VOID_IMPL( os, _Ostream_Manip(char) ) } // distribution // *********************************** C string *********************************** forall( ostype & | basic_ostream( ostype ) ) { ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f ) { if ( ! f.val ) return os; // null pointer ? if ( f.base != 's' ) { // bespoke binary/octal/hex format _Ostream_Manip(unsigned char) fmtuc @= { 0, f.wd, f.pc, f.base, {'\0'} }; fmtuc.flags.pc = f.flags.pc; fmtuc.flags.nobsdp = f.flags.nobsdp; for ( i; 0 ~ @ : @; f.val[i] != '\0' ) { fmtuc.val = f.val[i]; // os | fmtuc | nonl; (ostype &)(os | fmtuc); } // for return os; } // if if ( f.val[0] != '\0' && // null string => no leading separator sepPrt$( os ) ) fmt( os, "%s", sepGetCur$( os ) ); #define SFMTNP "% * " #define SFMTP "% *.* " char fmtstr[sizeof(SFMTP)]; // sizeof includes '\0' if ( ! f.flags.pc ) memcpy( &fmtstr, SFMTNP, sizeof(SFMTNP) ); else memcpy( &fmtstr, SFMTP, sizeof(SFMTP) ); int star = 1; // position before first '*' // Insert flags into spaces before '*', from right to left. if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } fmtstr[star] = '%'; if ( ! f.flags.pc ) { // no precision // printf( "%d %s\n", f.wd, &fmtstr[star] ); fmtstr[sizeof(SFMTNP)-2] = f.base; // sizeof includes '\0' fmt( os, &fmtstr[star], f.wd, f.val ); } else { // precision fmtstr[sizeof(SFMTP)-2] = f.base; // sizeof includes '\0' // printf( "%d %d %s\n", f.wd, f.pc, &fmtstr[star] ); fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); } // if if ( f.val[0] == '\0' ) { nosep( os ); } // null string => no trailing separator return os; } // ?|? OSTYPE_VOID_IMPL( os, _Ostream_Manip(const char *) ) } // distribution // *********************************** istream *********************************** #define FALSE "false" #define TRUE "true" forall( istype & | basic_istream( istype ) ) { istype & ?|?( istype & is, bool & b ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int len = -1; // len not set if no match // remove optional leading whitespace at start of strings. fmt( is, " " FALSE "%n", &len ); // try false if ( len != sizeof( FALSE ) - 1 ) { // -1 removes null terminate fmt( is, " " TRUE "%n", &len ); // try true if ( len != sizeof( TRUE ) - 1 ) throwResume ExceptionInst( missing_data ); b = true; } else { b = false; } // if return is; } // ?|? istype & ?|?( istype & is, char & c ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); char temp; for () { int args = fmt( is, "%c", &temp ); if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); assert( args == 1 ); // if not EOF => a single character must be read // do not overwrite parameter with newline unless appropriate if ( temp != '\n' || getANL$( is ) ) { c = temp; break; } } // for return is; } // ?|? istype & ?|?( istype & is, signed char & sc ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args = fmt( is, "%hhi", &sc ); // can be multiple characters (100) if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? istype & ?|?( istype & is, unsigned char & usc ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args = fmt( is, "%hhi", &usc ); // can be multiple characters (-100) if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? istype & ?|?( istype & is, short int & si ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args = fmt( is, "%hi", &si ); if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? istype & ?|?( istype & is, unsigned short int & usi ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args = fmt( is, "%hi", &usi ); if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? istype & ?|?( istype & is, int & i ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args = fmt( is, "%i", &i ); if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? istype & ?|?( istype & is, unsigned int & ui ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args = fmt( is, "%i", &ui ); if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? istype & ?|?( istype & is, long int & li ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args = fmt( is, "%li", &li ); if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? istype & ?|?( istype & is, unsigned long int & ulli ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args = fmt( is, "%li", &ulli ); if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? istype & ?|?( istype & is, long long int & lli ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args = fmt( is, "%lli", &lli ); if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? istype & ?|?( istype & is, unsigned long long int & ulli ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args = fmt( is, "%lli", &ulli ); if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? #if defined( __SIZEOF_INT128__ ) istype & ?|?( istype & is, int128 & llli ) { return (istype &)(is | (unsigned int128 &)llli); } // ?|? istype & ?|?( istype & is, unsigned int128 & ullli ) { char s[40]; bool sign = false; if ( fmt( is, " %[-]", s ) == 1 ) sign = true; // skip whitespace, negative sign ? // If the input is too large, the value returned is undefined. If there is no input, no value is returned if ( fmt( is, "%39[0-9]%*[0-9]", s ) == 1 ) { // take first 39 characters, ignore remaining ullli = 0; for ( i; 0 ~ @ : @; s[i] != '\0' ) { ullli = ullli * 10 + s[i] - '0'; } // for if ( sign ) ullli = -ullli; } // if return is; } // ?|? #endif // __SIZEOF_INT128__ istype & ?|?( istype & is, float & f ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args = fmt( is, "%f", &f ); if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? istype & ?|?( istype & is, double & d ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args = fmt( is, "%lf", &d ); if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? istype & ?|?( istype & is, long double & ld ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args = fmt( is, "%Lf", &ld ); if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? istype & ?|?( istype & is, float _Complex & fc ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); float re, im; int args = fmt( is, "%f%fi", &re, &im ); if ( ! eof( is ) && args != 2 ) throwResume ExceptionInst( missing_data ); fc = re + im * _Complex_I; return is; } // ?|? istype & ?|?( istype & is, double _Complex & dc ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); double re, im; int args = fmt( is, "%lf%lfi", &re, &im ); if ( ! eof( is ) && args != 2 ) throwResume ExceptionInst( missing_data ); dc = re + im * _Complex_I; return is; } // ?|? istype & ?|?( istype & is, long double _Complex & ldc ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); long double re, im; int args = fmt( is, "%Lf%Lfi", &re, &im ); if ( ! eof( is ) && args != 2 ) throwResume ExceptionInst( missing_data ); ldc = re + im * _Complex_I; return is; } // ?|? istype & ?|?( istype & is, const char fmt[] ) { // match text if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); size_t len = strlen( fmt ); char fmtstr[len + 16]; strcpy( fmtstr, fmt ); // copy format and add %n strcpy( &fmtstr[len], "%n" ); len = -1; // scanf cursor does not move if no match fmt( is, fmtstr, &len ); if ( len == -1 ) throwResume ExceptionInst( missing_data ); return is; } // ?|? // manipulators istype & ?|?( istype & is, istype & (* manip)( istype & ) ) { return manip( is ); } // ?|? void ?|?( istype & is, istype & (* manip)( istype & ) ) { manip( is ); } // ?|? istype & nl( istype & is ) { fmt( is, "%*[^\n]" ); // ignore characters to newline if ( ! eof( is ) ) fmt( is, "%*c" ); // read newline return is; } // nl istype & nlOn( istype & is ) { nlOn( is ); // call void returning return is; } // nlOn istype & nlOff( istype & is ) { nlOff( is ); // call void returning return is; } // nlOff } // distribution // *********************************** manipulators *********************************** forall( istype & | basic_istream( istype ) ) { istype & ?|?( istype & is, _Istream_Cskip f ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); if ( f.scanset ) { int nscanset = strlen(f.scanset); char fmtstr[ sizeof("%*[]") + nscanset ]; int pos = 0; strcpy( &fmtstr[pos], "%*[" ); pos += 3; strcpy( &fmtstr[pos], f.scanset ); pos += nscanset; strcpy( &fmtstr[pos], "]" ); fmt( is, fmtstr, "" ); // skip scanset, zero or more } else { char ch; for ( f.wd ) { // skip N characters int args = fmt( is, "%c", &ch ); if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); } // for } // if return is; } istype & ?|?( istype & is, _Istream_Cquoted f ) with( f.cstr ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); int args; fini: { char rfmt[5] = { ' ', delimiters[0], '%', 'n', '\0' }; int len = -1; // may not be set in fmt args = fmt( is, rfmt, &len ); // remove leading whitespace and quote if ( eof( is ) || len == -1 ) break fini; // Change the remainder of the read into a getline by reseting the closing delimiter. if ( delimiters[1] != '\0' ) { delimiters[0] = delimiters[1]; delimiters[1] = '\0'; } // if flags.delimiter = true; return is | *(_Istream_Cstr *)&f; } // fini // read failed => no pattern match => set string to null if ( ! flags.ignore && s != 0p && args == 0 ) s[0] = '\0'; if ( args == 1 && eof( is ) ) { // data but scan ended at EOF clear( is ); // => reset EOF => detect again on next read } // if return is; } istype & ?|?( istype & is, _Istream_Cstr f ) with( f.cstr ) { if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); const char * scanset; size_t nscanset = 0; if ( flags.delimiter ) scanset = delimiters; // getline ? else scanset = f.cstr.scanset; if ( scanset ) nscanset = strlen( scanset ); char fmtstr[nscanset + 32]; // storage for scanset and format codes fmtstr[0] = '%'; int pos = 1; int args; bool check = true; if ( flags.ignore ) { check = false; fmtstr[1] = '*'; pos += 1; } int rwd = wd; if ( wd != -1 ) { // => just ignore versus ignore with width // wd is buffer bytes available (for input chars + null terminator) // rwd is count of input chars // no maximum width necessary because text ignored => width is read width if ( flags.rwd ) check = false; else rwd = wd - 1; assert( rwd > 0 ); pos += sprintf( &fmtstr[pos], "%d", rwd ); } // if if ( ! scanset ) { // %s, %*s, %ws, %*ws // fprintf( stderr, "cstr %s\n", s ); strcpy( &fmtstr[pos], "s%n" ); int len = 0; // may not be set in fmt if ( flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' else args = fmt( is, fmtstr, s, &len ); // fprintf( stderr, "cstr %s %d %d %d\n", fmtstr, args, len, f.cstr.wd ); // No data read and eof is on => true eof so raise exception. if ( len == 0 && eof( is ) ) throwResume ExceptionInst( end_of_file ); if ( check && len >= rwd && ! eof( is ) ) { // might not fit char peek; fmt( is, "%c", &peek ); // check for whitespace terminator // fprintf( stderr, "peek %d '%c'\n", args, peek ); if ( ! eof( is ) ) { // can only fail at eof ungetc( peek, is ); if ( ! isspace( peek ) ) throwResume ExceptionInst( cstring_length ); } // if } // if // FIX ME: CFA strings need to be modified to NOT change the argument for this case, then this can be removed. //fprintf( stderr, "cstr %d %d %d %d '%s'\n", flags.ignore, args, len, eof( is ), s ); //if ( ! flags.ignore && args == 0 ) s[0]= '\0'; // read failed => no pattern match => set string to null } else { if ( flags.delimiter ) { // getline int len = 0; // may not be set in fmt if ( delimiters[2] != '\0' ) { // (quoted) read single character ? sprintf( &fmtstr[pos], "c%%n" ); } else { sprintf( &fmtstr[pos], "[^%c]%%n", delimiters[0] ); } // if if ( flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' else args = fmt( is, fmtstr, s, &len ); // No data read and eof is on => true eof so raise exception. if ( len == 0 && eof( is ) ) throwResume ExceptionInst( end_of_file ); if ( check && len == rwd && ! eof( is ) ) { // might not fit char peek; fmt( is, "%c", &peek ); // check for delimiter if ( ! eof( is ) ) { if ( peek != delimiters[0] ) { ungetc( peek, is ); throwResume ExceptionInst( cstring_length ); } // if } // if } else fmt( is, "%*c" ); // remove delimiter } else { // incl %[xxx], %*[xxx], %w[xxx], %*w[xxx] // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx] sprintf( &fmtstr[pos], "[%s%s]%%n", flags.inex ? "^" : "", scanset ); // fprintf( stderr, "incl/excl %s %d\n", fmtstr, wd ); int len = 0; // may not be set in fmt if ( flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' else args = fmt( is, fmtstr, s, &len ); // fprintf( stderr, "incl/excl %s \"%s\" %d %d %d %d %d %c\n", fmtstr, s, args, wd, len, eof( is ), check, s[wd] ); // No data read and eof is on => true eof so raise exception. if ( len == 0 && eof( is ) ) throwResume ExceptionInst( end_of_file ); if ( check && len == rwd && ! eof( is ) ) { // might not fit // fprintf( stderr, "overflow\n" ); char peek; fmt( is, "%c", &peek ); // check for whitespace terminator // fprintf( stderr, "peek %d '%c'\n", args, peek ); if ( ! eof( is ) ) { ungetc( peek, is ); if ( flags.inex ^ strchr( scanset, peek ) != 0p ) throwResume ExceptionInst( cstring_length ); } // if } // if } // if if ( ! flags.ignore && args == 0 ) s[0]= '\0'; // read failed => no pattern match => set string to null } // if if ( args == 1 && eof( is ) ) { // data but scan ended at EOF clear( is ); // => reset EOF => detect again on next read } // if return is; } // ?|? } // distribution #define INPUT_FMT_IMPL( T, CODE ) \ forall( istype & | basic_istream( istype ) ) { \ istype & ?|?( istype & is, _Istream_Manip(T) f ) { \ enum { size = 16 }; \ char fmtstr[size]; \ if ( f.wd == -1 ) { \ snprintf( fmtstr, size, "%%%s%s", f.ignore ? "*" : "", CODE ); \ } else { \ snprintf( fmtstr, size, "%%%s%d%s", f.ignore ? "*" : "", f.wd, CODE ); \ } /* if */ \ /* printf( "%d %s %p\n", f.wd, fmtstr, &f.val ); */ \ fmt( is, fmtstr, &f.val ); \ return is; \ } /* ?|? */ \ } // distribution INPUT_FMT_IMPL( char, "c" ) INPUT_FMT_IMPL( signed char, "hhi" ) INPUT_FMT_IMPL( unsigned char, "hhi" ) INPUT_FMT_IMPL( signed short int, "hi" ) INPUT_FMT_IMPL( unsigned short int, "hi" ) INPUT_FMT_IMPL( signed int, "i" ) INPUT_FMT_IMPL( unsigned int, "i" ) INPUT_FMT_IMPL( signed long int, "li" ) INPUT_FMT_IMPL( unsigned long int, "li" ) INPUT_FMT_IMPL( signed long long int, "lli" ) INPUT_FMT_IMPL( unsigned long long int, "lli" ) INPUT_FMT_IMPL( float, "f" ) INPUT_FMT_IMPL( double, "lf" ) INPUT_FMT_IMPL( long double, "Lf" ) forall( istype & | basic_istream( istype ) ) { istype & ?|?( istype & is, _Istream_Manip(float _Complex) fc ) { float re, im; _Istream_Manip(float) fmtuc @= { re, fc.wd, fc.ignore }; is | fmtuc; &fmtuc.val = &im; is | fmtuc; if ( ! fc.ignore ) fc.val = re + im * _Complex_I; // re/im are uninitialized for ignore return is; } // ?|? istype & ?|?( istype & is, _Istream_Manip(double _Complex) dc ) { double re, im; _Istream_Manip(double) fmtuc @= { re, dc.wd, dc.ignore }; is | fmtuc; &fmtuc.val = &im; is | fmtuc; if ( ! dc.ignore ) dc.val = re + im * _Complex_I; // re/im are uninitialized for ignore return is; } // ?|? istype & ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) { long double re, im; _Istream_Manip(long double) fmtuc @= { re, ldc.wd, ldc.ignore }; is | fmtuc; &fmtuc.val = &im; is | fmtuc; if ( ! ldc.ignore ) ldc.val = re + im * _Complex_I; // re/im are uninitialized for ignore return is; } // ?|? } // distribution // Local Variables: // // tab-width: 4 // // compile-command: "cfa iostream.cfa" // // End: //