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