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