// // 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 : Sat Jul 13 08:07:59 2019 // Update Count : 821 // #include "iostream.hfa" extern "C" { #include #include // true/false //#include // strlen, strcmp 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))); #include // DBL_DIG, LDBL_DIG #include // isfinite #include // creal, cimag } // extern "C" //*********************************** ostream *********************************** forall( dtype ostype | ostream( ostype ) ) { ostype & ?|?( ostype & os, zero_t ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%d", 0n ); return os; } // ?|? void ?|?( ostype & os, zero_t z ) { (ostype &)(os | z); ends( os ); } // ?|? ostype & ?|?( ostype & os, one_t ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%d", 1n ); return os; } // ?|? void ?|?( ostype & os, one_t o ) { (ostype &)(os | o); ends( os ); } // ?|? ostype & ?|?( ostype & os, bool b ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%s", b ? "true" : "false" ); return os; } // ?|? void ?|?( ostype & os, bool b ) { (ostype &)(os | b); ends( os ); } // ?|? ostype & ?|?( ostype & os, char c ) { fmt( os, "%c", c ); if ( c == '\n' ) setNL( os, true ); return sepOff( os ); } // ?|? void ?|?( ostype & os, char c ) { (ostype &)(os | c); ends( os ); } // ?|? ostype & ?|?( ostype & os, signed char sc ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%hhd", sc ); return os; } // ?|? void ?|?( ostype & os, signed char sc ) { (ostype &)(os | sc); ends( os ); } // ?|? ostype & ?|?( ostype & os, unsigned char usc ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%hhu", usc ); return os; } // ?|? void ?|?( ostype & os, unsigned char usc ) { (ostype &)(os | usc); ends( os ); } // ?|? ostype & ?|?( ostype & os, short int si ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%hd", si ); return os; } // ?|? void & ?|?( ostype & os, short int si ) { (ostype &)(os | si); ends( os ); } // ?|? ostype & ?|?( ostype & os, unsigned short int usi ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%hu", usi ); return os; } // ?|? void & ?|?( ostype & os, unsigned short int usi ) { (ostype &)(os | usi); ends( os ); } // ?|? ostype & ?|?( ostype & os, int i ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%d", i ); return os; } // ?|? void & ?|?( ostype & os, int i ) { (ostype &)(os | i); ends( os ); } // ?|? ostype & ?|?( ostype & os, unsigned int ui ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%u", ui ); return os; } // ?|? void & ?|?( ostype & os, unsigned int ui ) { (ostype &)(os | ui); ends( os ); } // ?|? ostype & ?|?( ostype & os, long int li ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%ld", li ); return os; } // ?|? void & ?|?( ostype & os, long int li ) { (ostype &)(os | li); ends( os ); } // ?|? ostype & ?|?( ostype & os, unsigned long int uli ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%lu", uli ); return os; } // ?|? void & ?|?( ostype & os, unsigned long int uli ) { (ostype &)(os | uli); ends( os ); } // ?|? ostype & ?|?( ostype & os, long long int lli ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%lld", lli ); return os; } // ?|? void & ?|?( ostype & os, long long int lli ) { (ostype &)(os | lli); ends( os ); } // ?|? ostype & ?|?( ostype & os, unsigned long long int ulli ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%llu", ulli ); return os; } // ?|? void & ?|?( ostype & os, unsigned long long int ulli ) { (ostype &)(os | ulli); ends( os ); } // ?|? #define PrintWithDP( 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, always print decimal point */ \ for ( int i = 0;; i += 1 ) { \ if ( i == len ) { fmt( os, "." ); break; } \ if ( buf[i] == '.' ) break; \ } /* for */ \ } /* if */ \ } ostype & ?|?( ostype & os, float f ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); PrintWithDP( os, "%g", f ); return os; } // ?|? void & ?|?( ostype & os, float f ) { (ostype &)(os | f); ends( os ); } // ?|? ostype & ?|?( ostype & os, double d ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); PrintWithDP( os, "%.*lg", d, DBL_DIG ); return os; } // ?|? void & ?|?( ostype & os, double d ) { (ostype &)(os | d); ends( os ); } // ?|? ostype & ?|?( ostype & os, long double ld ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); PrintWithDP( os, "%.*Lg", ld, LDBL_DIG ); return os; } // ?|? void & ?|?( ostype & os, long double ld ) { (ostype &)(os | ld); ends( os ); } // ?|? ostype & ?|?( ostype & os, float _Complex fc ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); // os | crealf( fc ) | nonl; PrintWithDP( os, "%g", crealf( fc ) ); PrintWithDP( os, "%+g", cimagf( fc ) ); fmt( os, "i" ); return os; } // ?|? void & ?|?( ostype & os, float _Complex fc ) { (ostype &)(os | fc); ends( os ); } // ?|? ostype & ?|?( ostype & os, double _Complex dc ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); // os | creal( dc ) | nonl; PrintWithDP( os, "%.*lg", creal( dc ), DBL_DIG ); PrintWithDP( os, "%+.*lg", cimag( dc ), DBL_DIG ); fmt( os, "i" ); return os; } // ?|? void & ?|?( ostype & os, double _Complex dc ) { (ostype &)(os | dc); ends( os ); } // ?|? ostype & ?|?( ostype & os, long double _Complex ldc ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); // os | creall( ldc ) || nonl; PrintWithDP( os, "%.*Lg", creall( ldc ), LDBL_DIG ); PrintWithDP( os, "%+.*Lg", cimagl( ldc ), LDBL_DIG ); fmt( os, "i" ); return os; } // ?|? void & ?|?( ostype & os, long double _Complex ldc ) { (ostype &)(os | ldc); ends( os ); } // ?|? ostype & ?|?( ostype & os, const char * str ) { enum { Open = 1, Close, OpenClose }; static const unsigned char mask[256] @= { // 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 ( str[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator // first character IS NOT spacing or closing punctuation => add left separator unsigned char ch = str[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 size_t len = strlen( str ); ch = str[len - 1]; // must make unsigned if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) { sepOn( os ); } else { sepOff( os ); } // if if ( ch == '\n' ) setNL( os, true ); // check *AFTER* sepPrt call above as it resets NL flag return write( os, str, len ); } // ?|? void ?|?( ostype & os, const char * str ) { (ostype &)(os | str); ends( os ); } // ?|? // ostype & ?|?( ostype & os, const char16_t * str ) { // if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); // fmt( os, "%ls", str ); // return os; // } // ?|? // #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous // ostype & ?|?( ostype & os, const char32_t * str ) { // if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); // fmt( os, "%ls", str ); // return os; // } // ?|? // #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // ostype & ?|?( ostype & os, const wchar_t * str ) { // if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); // fmt( os, "%ls", str ); // return os; // } // ?|? ostype & ?|?( ostype & os, const void * p ) { if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); fmt( os, "%p", p ); return os; } // ?|? void ?|?( ostype & os, const void * p ) { (ostype &)(os | p); ends( os ); } // ?|? // manipulators ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) { (ostype &)(manip( os )); return os; } // ?|? void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) { (ostype &)(manip( os )); if ( getPrt( os ) ) ends( os ); // something printed ? setPrt( os, false ); // turn off } // ?|? ostype & sep( ostype & os ) { return (ostype &)(os | sepGet( os )); } // sep ostype & sepTuple( ostype & os ) { return os | sepGetTuple( os ); } // sepTuple ostype & nl( ostype & os ) { (ostype &)(os | '\n'); setPrt( os, false ); // turn off setNL( os, true ); flush( os ); return sepOff( os ); // prepare for next line } // nl ostype & nonl( ostype & os ) { setPrt( os, false ); // turn off return os; } // nonl ostype & sepOn( ostype & os ) { sepOn( os ); // call void returning return os; } // sepOn ostype & sepOff( ostype & os ) { sepOff( os ); // call void returning return os; } // sepOff ostype & sepEnable( ostype & os ) { sepEnable( os ); // call void returning return os; } // sepEnable ostype & sepDisable( ostype & os ) { sepDisable( os ); // call void returning return os; } // sepDisable ostype & nlOn( ostype & os ) { nlOn( os ); // call void returning return os; } // nlOn ostype & nlOff( ostype & os ) { nlOff( os ); // call void returning return os; } // nlOff } // distribution // tuples forall( dtype ostype, otype T, ttype 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( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype 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 IntegralFMTImpl( T, CODE, IFMTNP, IFMTP ) \ forall( dtype ostype | 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; \ if ( f.val == (T){0} ) bits = 1; /* force at least one bit to print */ \ else bits = sizeof(long long int) * 8 - __builtin_clzll( f.val ); /* position of most significant bit */ \ bits = bits > sizeof(f.val) * 8 ? sizeof(f.val) * 8 : bits; \ int spaces = f.wd - bits; /* can be negative */ \ if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \ /* printf( "%d %d\n", bits, spaces ); */ \ if ( ! f.flags.left ) { /* right justified ? */ \ /* Note, base prefix then zero padding or spacing then prefix. */ \ if ( f.flags.pad0 || f.flags.pc ) { \ 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 */ \ } else { \ if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \ if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \ } /* if */ \ } else if ( ! f.flags.nobsdp ) { \ fmt( os, "0%c", f.base ); \ } /* if */ \ int shift = (bits - 1) / 4 * 4; /* 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 = 4; /* 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 && f.base == CODE ) { fmtstr[star] = '+'; star -= 1; } \ if ( f.flags.pad0 && ! f.flags.pc ) { fmtstr[star] = '0'; star -= 1; } \ fmtstr[star] = '%'; \ \ if ( ! f.flags.pc ) { /* no precision */ \ /* printf( "%s\n", &fmtstr[star] ); */ \ fmtstr[sizeof(IFMTNP)-2] = f.base; /* sizeof includes '\0' */ \ fmt( os, &fmtstr[star], f.wd, f.val ); \ } else { /* precision */ \ fmtstr[sizeof(IFMTP)-2] = f.base; /* sizeof includes '\0' */ \ /* printf( "%s\n", &fmtstr[star] ); */ \ fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); \ } /* if */ \ return os; \ } /* ?|? */ \ void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \ } // distribution IntegralFMTImpl( signed char, 'd', "% *hh ", "% *.*hh " ) IntegralFMTImpl( unsigned char, 'u', "% *hh ", "% *.*hh " ) IntegralFMTImpl( signed short int, 'd', "% *h ", "% *.*h " ) IntegralFMTImpl( unsigned short int, 'u', "% *h ", "% *.*h " ) IntegralFMTImpl( signed int, 'd', "% * ", "% *.* " ) IntegralFMTImpl( unsigned int, 'u', "% * ", "% *.* " ) IntegralFMTImpl( signed long int, 'd', "% *l ", "% *.*l " ) IntegralFMTImpl( unsigned long int, 'u', "% *l ", "% *.*l " ) IntegralFMTImpl( signed long long int, 'd', "% *ll ", "% *.*ll " ) IntegralFMTImpl( unsigned long long int, 'u', "% *ll ", "% *.*ll " ) //*********************************** floating point *********************************** #define PrintWithDP2( os, format, val, ... ) \ { \ enum { size = 48 }; \ char buf[size]; \ int bufbeg = 0, i, len = snprintf( buf, size, format, ##__VA_ARGS__, val ); \ if ( isfinite( val ) && (f.base != 'g' || f.pc != 0) ) { /* if number, print decimal point */ \ for ( i = 0; i < len && buf[i] != '.' && buf[i] != 'e' && buf[i] != 'E'; i += 1 ); /* decimal point or scientific ? */ \ if ( i == len && ! f.flags.nobsdp ) { \ 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 */ \ fmt( os, "%s", &buf[bufbeg] ); \ } #define FloatingPointFMTImpl( T, DFMTNP, DFMTP ) \ forall( dtype ostype | ostream( ostype ) ) { \ ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \ if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); \ char fmtstr[sizeof(DFMTP)]; /* sizeof includes '\0' */ \ if ( ! f.flags.pc ) memcpy( &fmtstr, DFMTNP, sizeof(DFMTNP) ); \ else memcpy( &fmtstr, DFMTP, sizeof(DFMTP) ); \ int star = 4; /* 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] = '%'; \ \ 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]); */ \ PrintWithDP2( os, &fmtstr[star], f.val, f.wd ) \ } 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] ); */ \ PrintWithDP2( os, &fmtstr[star], f.val, f.wd, f.pc ) \ } /* if */ \ return os; \ } /* ?|? */ \ void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \ } // distribution FloatingPointFMTImpl( double, "% * ", "% *.* " ) FloatingPointFMTImpl( long double, "% *L ", "% *.*L " ) //*********************************** character *********************************** forall( dtype ostype | 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; } // ?|? void ?|?( ostype & os, _Ostream_Manip(char) f ) { (ostype &)(os | f); ends( os ); } } // distribution //*********************************** C string *********************************** forall( dtype ostype | 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 ( unsigned int i = 0; f.val[i] != '\0'; i += 1 ) { fmtuc.val = f.val[i]; // os | fmtuc | nonl; (ostype &)(os | fmtuc); } // for return os; } // if if ( 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 return os; } // ?|? void ?|?( ostype & os, _Ostream_Manip(const char *) f ) { (ostype &)(os | f); ends( os ); } } // distribution //*********************************** istream *********************************** forall( dtype istype | istream( istype ) ) { istype & ?|?( istype & is, bool & b ) { char val[6]; fmt( is, "%5s", val ); if ( strcmp( val, "true" ) == 0 ) b = true; else if ( strcmp( val, "false" ) == 0 ) b = false; else { fprintf( stderr, "invalid Boolean constant\n" ); abort(); // cannot use abort stream } // if return is; } // ?|? istype & ?|?( istype & is, char & c ) { char temp; for () { fmt( is, "%c", &temp ); // must pass pointer through varg to fmt // do not overwrite parameter with newline unless appropriate if ( temp != '\n' || getANL( is ) ) { c = temp; break; } if ( eof( is ) ) break; } // for return is; } // ?|? istype & ?|?( istype & is, signed char & sc ) { fmt( is, "%hhi", &sc ); return is; } // ?|? istype & ?|?( istype & is, unsigned char & usc ) { fmt( is, "%hhi", &usc ); return is; } // ?|? istype & ?|?( istype & is, short int & si ) { fmt( is, "%hi", &si ); return is; } // ?|? istype & ?|?( istype & is, unsigned short int & usi ) { fmt( is, "%hi", &usi ); return is; } // ?|? istype & ?|?( istype & is, int & i ) { fmt( is, "%i", &i ); return is; } // ?|? istype & ?|?( istype & is, unsigned int & ui ) { fmt( is, "%i", &ui ); return is; } // ?|? istype & ?|?( istype & is, long int & li ) { fmt( is, "%li", &li ); return is; } // ?|? istype & ?|?( istype & is, unsigned long int & ulli ) { fmt( is, "%li", &ulli ); return is; } // ?|? istype & ?|?( istype & is, long long int & lli ) { fmt( is, "%lli", &lli ); return is; } // ?|? istype & ?|?( istype & is, unsigned long long int & ulli ) { fmt( is, "%lli", &ulli ); return is; } // ?|? istype & ?|?( istype & is, float & f ) { fmt( is, "%f", &f ); return is; } // ?|? istype & ?|?( istype & is, double & d ) { fmt( is, "%lf", &d ); return is; } // ?|? istype & ?|?( istype & is, long double & ld ) { fmt( is, "%Lf", &ld ); return is; } // ?|? istype & ?|?( istype & is, float _Complex & fc ) { float re, im; fmt( is, "%f%fi", &re, &im ); fc = re + im * _Complex_I; return is; } // ?|? istype & ?|?( istype & is, double _Complex & dc ) { double re, im; fmt( is, "%lf%lfi", &re, &im ); dc = re + im * _Complex_I; return is; } // ?|? istype & ?|?( istype & is, long double _Complex & ldc ) { long double re, im; fmt( is, "%Lf%Lfi", &re, &im ); ldc = re + im * _Complex_I; return is; } // ?|? // istype & ?|?( istype & is, const char * fmt ) { // fmt( is, fmt, "" ); // return is; // } // ?|? istype & ?|?( istype & is, char * s ) { fmt( is, "%s", s ); return is; } // ?|? // manipulators istype & ?|?( istype & is, istype & (* manip)( istype & ) ) { return manip( is ); } // ?|? istype & nl( istype & is ) { fmt( is, "%*[^\n]" ); // ignore characters to 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( dtype istype | istream( istype ) ) istype & ?|?( istype & is, _Istream_Cstr f ) { // skip xxx if ( ! f.s ) { // printf( "skip %s %d\n", f.scanset, f.wd ); if ( f.wd == -1 ) fmt( is, f.scanset, "" ); // no input arguments else for ( f.wd ) fmt( is, "%*c" ); return is; } // if size_t len = 0; if ( f.scanset ) len = strlen( f.scanset ); char fmtstr[len + 16]; int start = 1; fmtstr[0] = '%'; if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; } if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); } // cstr %s, %*s, %ws, %*ws if ( ! f.scanset ) { fmtstr[start] = 's'; fmtstr[start + 1] = '\0'; // printf( "cstr %s\n", fmtstr ); fmt( is, fmtstr, f.s ); return is; } // if // incl %[xxx], %*[xxx], %w[xxx], %*w[xxx] // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx] fmtstr[start] = '['; start += 1; if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; } strcpy( &fmtstr[start], f.scanset ); // copy includes '\0' len += start; fmtstr[len] = ']'; fmtstr[len + 1] = '\0'; // printf( "incl/excl %s\n", fmtstr ); fmt( is, fmtstr, f.s ); return is; } // ?|? forall( dtype istype | istream( istype ) ) istype & ?|?( istype & is, _Istream_Char f ) { fmt( is, "%*c" ); // argument variable unused return is; } // ?|? #define InputFMTImpl( T, CODE ) \ forall( dtype istype | 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; \ } // ?|? InputFMTImpl( signed char, "hhi" ) InputFMTImpl( unsigned char, "hhi" ) InputFMTImpl( signed short int, "hi" ) InputFMTImpl( unsigned short int, "hi" ) InputFMTImpl( signed int, "i" ) InputFMTImpl( unsigned int, "i" ) InputFMTImpl( signed long int, "li" ) InputFMTImpl( unsigned long int, "li" ) InputFMTImpl( signed long long int, "lli" ) InputFMTImpl( unsigned long long int, "lli" ) InputFMTImpl( float, "f" ) InputFMTImpl( double, "lf" ) InputFMTImpl( long double, "Lf" ) forall( dtype istype | 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; } // ?|? forall( dtype istype | istream( istype ) ) 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; } // ?|? forall( dtype istype | istream( istype ) ) 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; } // ?|? // Local Variables: // // tab-width: 4 // // compile-command: "cfa iostream.cfa" // // End: //