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