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