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