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