Changes in libcfa/src/iostream.cfa [852a2f06:4f37255]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/iostream.cfa
r852a2f06 r4f37255 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Mar 11 14:35:35 202013 // Update Count : 8 6012 // Last Modified On : Sat Jul 13 08:07:59 2019 13 // Update Count : 821 14 14 // 15 15 … … 19 19 #include <stdio.h> 20 20 #include <stdbool.h> // true/false 21 #include <stdint.h> // UINT64_MAX22 21 //#include <string.h> // strlen, strcmp 23 22 extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1))); … … 36 35 forall( dtype ostype | ostream( ostype ) ) { 37 36 ostype & ?|?( ostype & os, zero_t ) { 38 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );37 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 39 38 fmt( os, "%d", 0n ); 40 39 return os; … … 45 44 46 45 ostype & ?|?( ostype & os, one_t ) { 47 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );46 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 48 47 fmt( os, "%d", 1n ); 49 48 return os; … … 54 53 55 54 ostype & ?|?( ostype & os, bool b ) { 56 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );55 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 57 56 fmt( os, "%s", b ? "true" : "false" ); 58 57 return os; … … 64 63 ostype & ?|?( ostype & os, char c ) { 65 64 fmt( os, "%c", c ); 66 if ( c == '\n' ) $setNL( os, true );65 if ( c == '\n' ) setNL( os, true ); 67 66 return sepOff( os ); 68 67 } // ?|? … … 72 71 73 72 ostype & ?|?( ostype & os, signed char sc ) { 74 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );73 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 75 74 fmt( os, "%hhd", sc ); 76 75 return os; … … 81 80 82 81 ostype & ?|?( ostype & os, unsigned char usc ) { 83 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );82 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 84 83 fmt( os, "%hhu", usc ); 85 84 return os; … … 90 89 91 90 ostype & ?|?( ostype & os, short int si ) { 92 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );91 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 93 92 fmt( os, "%hd", si ); 94 93 return os; … … 99 98 100 99 ostype & ?|?( ostype & os, unsigned short int usi ) { 101 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );100 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 102 101 fmt( os, "%hu", usi ); 103 102 return os; … … 108 107 109 108 ostype & ?|?( ostype & os, int i ) { 110 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );109 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 111 110 fmt( os, "%d", i ); 112 111 return os; … … 117 116 118 117 ostype & ?|?( ostype & os, unsigned int ui ) { 119 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );118 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 120 119 fmt( os, "%u", ui ); 121 120 return os; … … 126 125 127 126 ostype & ?|?( ostype & os, long int li ) { 128 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );127 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 129 128 fmt( os, "%ld", li ); 130 129 return os; … … 135 134 136 135 ostype & ?|?( ostype & os, unsigned long int uli ) { 137 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );136 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 138 137 fmt( os, "%lu", uli ); 139 138 return os; … … 144 143 145 144 ostype & ?|?( ostype & os, long long int lli ) { 146 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );145 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 147 146 fmt( os, "%lld", lli ); 148 147 return os; … … 153 152 154 153 ostype & ?|?( ostype & os, unsigned long long int ulli ) { 155 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );154 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 156 155 fmt( os, "%llu", ulli ); 157 156 return os; … … 160 159 (ostype &)(os | ulli); ends( os ); 161 160 } // ?|? 162 163 #if defined( __SIZEOF_INT128__ )164 // UINT64_MAX 18_446_744_073_709_551_615_ULL165 #define P10_UINT64 10_000_000_000_000_000_000_ULL // 19 zeroes166 167 static void base10_128( ostype & os, unsigned int128 val ) {168 if ( val > UINT64_MAX ) {169 base10_128( os, val / P10_UINT64 ); // recursive170 fmt( os, "%.19lu", (uint64_t)(val % P10_UINT64) );171 } else {172 fmt( os, "%lu", (uint64_t)val );173 } // if174 } // base10_128175 176 static void base10_128( ostype & os, int128 val ) {177 if ( val < 0 ) {178 fmt( os, "-" ); // leading negative sign179 val = -val;180 } // if181 base10_128( os, (unsigned int128)val ); // print zero/positive value182 } // base10_128183 184 ostype & ?|?( ostype & os, int128 llli ) {185 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );186 base10_128( os, llli );187 return os;188 } // ?|?189 void & ?|?( ostype & os, int128 llli ) {190 (ostype &)(os | llli); ends( os );191 } // ?|?192 193 ostype & ?|?( ostype & os, unsigned int128 ullli ) {194 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );195 base10_128( os, ullli );196 return os;197 } // ?|?198 void & ?|?( ostype & os, unsigned int128 ullli ) {199 (ostype &)(os | ullli); ends( os );200 } // ?|?201 #endif // __SIZEOF_INT128__202 161 203 162 #define PrintWithDP( os, format, val, ... ) \ … … 216 175 217 176 ostype & ?|?( ostype & os, float f ) { 218 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );177 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 219 178 PrintWithDP( os, "%g", f ); 220 179 return os; … … 225 184 226 185 ostype & ?|?( ostype & os, double d ) { 227 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );186 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 228 187 PrintWithDP( os, "%.*lg", d, DBL_DIG ); 229 188 return os; … … 234 193 235 194 ostype & ?|?( ostype & os, long double ld ) { 236 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );195 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 237 196 PrintWithDP( os, "%.*Lg", ld, LDBL_DIG ); 238 197 return os; … … 243 202 244 203 ostype & ?|?( ostype & os, float _Complex fc ) { 245 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );204 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 246 205 // os | crealf( fc ) | nonl; 247 206 PrintWithDP( os, "%g", crealf( fc ) ); … … 255 214 256 215 ostype & ?|?( ostype & os, double _Complex dc ) { 257 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );216 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 258 217 // os | creal( dc ) | nonl; 259 218 PrintWithDP( os, "%.*lg", creal( dc ), DBL_DIG ); … … 267 226 268 227 ostype & ?|?( ostype & os, long double _Complex ldc ) { 269 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );228 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 270 229 // os | creall( ldc ) || nonl; 271 230 PrintWithDP( os, "%.*Lg", creall( ldc ), LDBL_DIG ); … … 278 237 } // ?|? 279 238 280 ostype & ?|?( ostype & os, const char str[]) {239 ostype & ?|?( ostype & os, const char * str ) { 281 240 enum { Open = 1, Close, OpenClose }; 282 241 static const unsigned char mask[256] @= { … … 298 257 // first character IS NOT spacing or closing punctuation => add left separator 299 258 unsigned char ch = str[0]; // must make unsigned 300 if ( $sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {301 fmt( os, "%s", $sepGetCur( os ) );259 if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) { 260 fmt( os, "%s", sepGetCur( os ) ); 302 261 } // if 303 262 304 263 // if string starts line, must reset to determine open state because separator is off 305 $sepReset( os );// reset separator264 sepReset( os ); // reset separator 306 265 307 266 // last character IS spacing or opening punctuation => turn off separator for next item 308 267 size_t len = strlen( str ); 309 268 ch = str[len - 1]; // must make unsigned 310 if ( $sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {269 if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) { 311 270 sepOn( os ); 312 271 } else { 313 272 sepOff( os ); 314 273 } // if 315 if ( ch == '\n' ) $setNL( os, true ); // check *AFTER* $sepPrt call above as it resets NL flag274 if ( ch == '\n' ) setNL( os, true ); // check *AFTER* sepPrt call above as it resets NL flag 316 275 return write( os, str, len ); 317 276 } // ?|? 318 319 void ?|?( ostype & os, const char str[] ) { 277 void ?|?( ostype & os, const char * str ) { 320 278 (ostype &)(os | str); ends( os ); 321 279 } // ?|? 322 280 323 281 // ostype & ?|?( ostype & os, const char16_t * str ) { 324 // if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );282 // if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 325 283 // fmt( os, "%ls", str ); 326 284 // return os; … … 329 287 // #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous 330 288 // ostype & ?|?( ostype & os, const char32_t * str ) { 331 // if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );289 // if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 332 290 // fmt( os, "%ls", str ); 333 291 // return os; … … 336 294 337 295 // ostype & ?|?( ostype & os, const wchar_t * str ) { 338 // if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );296 // if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 339 297 // fmt( os, "%ls", str ); 340 298 // return os; … … 342 300 343 301 ostype & ?|?( ostype & os, const void * p ) { 344 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );302 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 345 303 fmt( os, "%p", p ); 346 304 return os; … … 357 315 void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) { 358 316 (ostype &)(manip( os )); 359 if ( $getPrt( os ) ) ends( os );// something printed ?360 $setPrt( os, false ); // turn off317 if ( getPrt( os ) ) ends( os ); // something printed ? 318 setPrt( os, false ); // turn off 361 319 } // ?|? 362 320 … … 371 329 ostype & nl( ostype & os ) { 372 330 (ostype &)(os | '\n'); 373 $setPrt( os, false ); // turn off374 $setNL( os, true );331 setPrt( os, false ); // turn off 332 setNL( os, true ); 375 333 flush( os ); 376 334 return sepOff( os ); // prepare for next line … … 378 336 379 337 ostype & nonl( ostype & os ) { 380 $setPrt( os, false ); // turn off338 setPrt( os, false ); // turn off 381 339 return os; 382 340 } // nonl … … 417 375 ostype & ?|?( ostype & os, T arg, Params rest ) { 418 376 (ostype &)(os | arg); // print first argument 419 $sepSetCur( os, sepGetTuple( os ) );// switch to tuple separator377 sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator 420 378 (ostype &)(os | rest); // print remaining arguments 421 $sepSetCur( os, sepGet( os ) ); // switch to regular separator379 sepSetCur( os, sepGet( os ) ); // switch to regular separator 422 380 return os; 423 381 } // ?|? … … 425 383 // (ostype &)(?|?( os, arg, rest )); ends( os ); 426 384 (ostype &)(os | arg); // print first argument 427 $sepSetCur( os, sepGetTuple( os ) );// switch to tuple separator385 sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator 428 386 (ostype &)(os | rest); // print remaining arguments 429 $sepSetCur( os, sepGet( os ) ); // switch to regular separator387 sepSetCur( os, sepGet( os ) ); // switch to regular separator 430 388 ends( os ); 431 389 } // ?|? … … 456 414 forall( dtype ostype | ostream( ostype ) ) { \ 457 415 ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \ 458 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \416 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); \ 459 417 \ 460 418 if ( f.base == 'b' || f.base == 'B' ) { /* bespoke binary format */ \ … … 505 463 \ 506 464 if ( ! f.flags.pc ) { /* no precision */ \ 465 /* printf( "%s\n", &fmtstr[star] ); */ \ 507 466 fmtstr[sizeof(IFMTNP)-2] = f.base; /* sizeof includes '\0' */ \ 508 /* printf( "%s %c %c\n", &fmtstr[star], f.base, CODE ); */ \509 467 fmt( os, &fmtstr[star], f.wd, f.val ); \ 510 468 } else { /* precision */ \ 511 469 fmtstr[sizeof(IFMTP)-2] = f.base; /* sizeof includes '\0' */ \ 512 /* printf( "%s %c %c\n", &fmtstr[star], f.base, CODE); */ \470 /* printf( "%s\n", &fmtstr[star] ); */ \ 513 471 fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); \ 514 472 } /* if */ \ … … 528 486 IntegralFMTImpl( signed long long int, 'd', "% *ll ", "% *.*ll " ) 529 487 IntegralFMTImpl( unsigned long long int, 'u', "% *ll ", "% *.*ll " ) 530 531 532 #if defined( __SIZEOF_INT128__ )533 // Default prefix for non-decimal prints is 0b, 0, 0x.534 #define IntegralFMTImpl128( T, SIGNED, CODE, IFMTNP, IFMTP ) \535 forall( dtype ostype | ostream( ostype ) ) \536 static void base10_128( ostype & os, _Ostream_Manip(T) f ) { \537 if ( f.val > UINT64_MAX ) { \538 unsigned long long int lsig = f.val % P10_UINT64; \539 f.val /= P10_UINT64; /* msig */ \540 base10_128( os, f ); /* recursion */ \541 _Ostream_Manip(unsigned long long int) fmt @= { lsig, 0, 19, 'u', { .all : 0 } }; \542 fmt.flags.nobsdp = true; \543 /* printf( "fmt1 %c %lld %d\n", fmt.base, fmt.val, fmt.all ); */ \544 sepOff( os ); \545 (ostype &)(os | fmt); \546 } else { \547 /* printf( "fmt2 %c %lld %d\n", f.base, (unsigned long long int)f.val, f.all ); */ \548 _Ostream_Manip(SIGNED long long int) fmt @= { (SIGNED long long int)f.val, f.wd, f.pc, f.base, { .all : f.all } }; \549 (ostype &)(os | fmt); \550 } /* if */ \551 } /* base10_128 */ \552 forall( dtype ostype | ostream( ostype ) ) { \553 ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \554 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \555 \556 if ( f.base == 'b' | f.base == 'B' | f.base == 'o' | f.base == 'x' | f.base == 'X' ) { \557 unsigned long long int msig = (unsigned long long int)(f.val >> 64); \558 unsigned long long int lsig = (unsigned long long int)(f.val); \559 _Ostream_Manip(SIGNED long long int) fmt @= { msig, f.wd, f.pc, f.base, { .all : f.all } }; \560 _Ostream_Manip(unsigned long long int) fmt2 @= { lsig, 0, 0, f.base, { .all : 0 } }; \561 if ( msig == 0 ) { \562 fmt.val = lsig; \563 (ostype &)(os | fmt); \564 } else { \565 fmt2.flags.pad0 = fmt2.flags.nobsdp = true; \566 if ( f.base == 'b' | f.base == 'B' ) { \567 if ( f.wd > 64 ) fmt.wd = f.wd - 64; \568 if ( f.flags.pc && f.pc > 64 ) fmt.pc = f.pc - 64; \569 fmt2.wd = 64; \570 (ostype &)(os | fmt | "" | fmt2); \571 } else if ( f.base == 'o' ) { \572 fmt.val = (unsigned long long int)fmt.val >> 2; \573 if ( f.wd > 21 ) fmt.wd = f.wd - 21; \574 if ( f.flags.pc && f.pc > 21 ) fmt.pc = f.pc - 21; \575 fmt2.wd = 1; \576 fmt2.val = ((msig & 0x3) << 1) + 1; \577 (ostype &)(os | fmt | "" | fmt2); \578 sepOff( os ); \579 fmt2.wd = 21; \580 fmt2.val = lsig & 0x7fffffffffffffff; \581 (ostype &)(os | fmt2); \582 } else { \583 if ( f.flags.left ) { \584 if ( f.wd > 16 ) fmt2.wd = f.wd - 16; \585 fmt.wd = 16; \586 } else { \587 if ( f.wd > 16 ) fmt.wd = f.wd - 16; \588 if ( f.flags.pc && f.pc > 16 ) fmt.pc = f.pc - 16; \589 fmt2.wd = 16; \590 } /* if */ \591 (ostype &)(os | fmt | "" | fmt2); \592 } /* if */ \593 } /* if */ \594 } else { \595 if ( CODE == 'd' ) { \596 if ( f.val < 0 ) { fmt( os, "-" ); sepOff( os ); f.val = -f.val; f.flags.sign = false; } \597 } /* if */ \598 base10_128( os, f ); \599 } /* if */ \600 return os; \601 } /* ?|? */ \602 void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \603 } // distribution604 605 IntegralFMTImpl128( int128, signed, 'd', "% *ll ", "% *.*ll " )606 IntegralFMTImpl128( unsigned int128, unsigned, 'u', "% *ll ", "% *.*ll " )607 #endif // __SIZEOF_INT128__608 488 609 489 //*********************************** floating point *********************************** … … 633 513 forall( dtype ostype | ostream( ostype ) ) { \ 634 514 ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \ 635 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \515 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); \ 636 516 char fmtstr[sizeof(DFMTP)]; /* sizeof includes '\0' */ \ 637 517 if ( ! f.flags.pc ) memcpy( &fmtstr, DFMTNP, sizeof(DFMTNP) ); \ … … 656 536 return os; \ 657 537 } /* ?|? */ \ 658 \659 538 void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \ 660 539 } // distribution … … 676 555 } // if 677 556 678 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );557 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 679 558 680 559 #define CFMTNP "% * " … … 692 571 return os; 693 572 } // ?|? 694 695 573 void ?|?( ostype & os, _Ostream_Manip(char) f ) { (ostype &)(os | f); ends( os ); } 696 574 } // distribution … … 714 592 } // if 715 593 716 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );594 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); 717 595 718 596 #define SFMTNP "% * " … … 738 616 return os; 739 617 } // ?|? 740 741 618 void ?|?( ostype & os, _Ostream_Manip(const char *) f ) { (ostype &)(os | f); ends( os ); } 742 619 } // distribution … … 858 735 } // ?|? 859 736 860 // istype & ?|?( istype & is, const char fmt[]) {737 // istype & ?|?( istype & is, const char * fmt ) { 861 738 // fmt( is, fmt, "" ); 862 739 // return is;
Note:
See TracChangeset
for help on using the changeset viewer.