| 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 | //
|
|---|
| 7 | // iostream.c --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Peter A. Buhr
|
|---|
| 10 | // Created On : Wed May 27 17:56:53 2015
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Sun May 19 10:48:27 2019
|
|---|
| 13 | // Update Count : 654
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "iostream.hfa"
|
|---|
| 17 |
|
|---|
| 18 | extern "C" {
|
|---|
| 19 | #include <stdio.h>
|
|---|
| 20 | #include <stdbool.h> // true/false
|
|---|
| 21 | //#include <string.h> // strlen, strcmp
|
|---|
| 22 | extern int strcmp (const char *__s1, const char *__s2) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
|
|---|
| 23 | extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
|
|---|
| 24 | #include <float.h> // DBL_DIG, LDBL_DIG
|
|---|
| 25 | #include <math.h> // isfinite
|
|---|
| 26 | #include <complex.h> // creal, cimag
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | forall( dtype ostype | ostream( ostype ) ) {
|
|---|
| 30 | ostype & ?|?( ostype & os, zero_t ) {
|
|---|
| 31 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 32 | fmt( os, "%d", 0n );
|
|---|
| 33 | return os;
|
|---|
| 34 | } // ?|?
|
|---|
| 35 | void ?|?( ostype & os, zero_t z ) {
|
|---|
| 36 | (ostype &)(os | z); nl( os );
|
|---|
| 37 | } // ?|?
|
|---|
| 38 |
|
|---|
| 39 | ostype & ?|?( ostype & os, one_t ) {
|
|---|
| 40 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 41 | fmt( os, "%d", 1n );
|
|---|
| 42 | return os;
|
|---|
| 43 | } // ?|?
|
|---|
| 44 | void ?|?( ostype & os, one_t o ) {
|
|---|
| 45 | (ostype &)(os | o); nl( os );
|
|---|
| 46 | } // ?|?
|
|---|
| 47 |
|
|---|
| 48 | ostype & ?|?( ostype & os, bool b ) {
|
|---|
| 49 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 50 | fmt( os, "%s", b ? "true" : "false" );
|
|---|
| 51 | return os;
|
|---|
| 52 | } // ?|?
|
|---|
| 53 | void ?|?( ostype & os, bool b ) {
|
|---|
| 54 | (ostype &)(os | b); nl( os );
|
|---|
| 55 | } // ?|?
|
|---|
| 56 |
|
|---|
| 57 | ostype & ?|?( ostype & os, char c ) {
|
|---|
| 58 | fmt( os, "%c", c );
|
|---|
| 59 | if ( c == '\n' ) setNL( os, true );
|
|---|
| 60 | return sepOff( os );
|
|---|
| 61 | } // ?|?
|
|---|
| 62 | void ?|?( ostype & os, char c ) {
|
|---|
| 63 | (ostype &)(os | c); nl( os );
|
|---|
| 64 | } // ?|?
|
|---|
| 65 |
|
|---|
| 66 | ostype & ?|?( ostype & os, signed char sc ) {
|
|---|
| 67 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 68 | fmt( os, "%hhd", sc );
|
|---|
| 69 | return os;
|
|---|
| 70 | } // ?|?
|
|---|
| 71 | void ?|?( ostype & os, signed char sc ) {
|
|---|
| 72 | (ostype &)(os | sc); nl( os );
|
|---|
| 73 | } // ?|?
|
|---|
| 74 |
|
|---|
| 75 | ostype & ?|?( ostype & os, unsigned char usc ) {
|
|---|
| 76 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 77 | fmt( os, "%hhu", usc );
|
|---|
| 78 | return os;
|
|---|
| 79 | } // ?|?
|
|---|
| 80 | void ?|?( ostype & os, unsigned char usc ) {
|
|---|
| 81 | (ostype &)(os | usc); nl( os );
|
|---|
| 82 | } // ?|?
|
|---|
| 83 |
|
|---|
| 84 | ostype & ?|?( ostype & os, short int si ) {
|
|---|
| 85 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 86 | fmt( os, "%hd", si );
|
|---|
| 87 | return os;
|
|---|
| 88 | } // ?|?
|
|---|
| 89 | void & ?|?( ostype & os, short int si ) {
|
|---|
| 90 | (ostype &)(os | si); nl( os );
|
|---|
| 91 | } // ?|?
|
|---|
| 92 |
|
|---|
| 93 | ostype & ?|?( ostype & os, unsigned short int usi ) {
|
|---|
| 94 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 95 | fmt( os, "%hu", usi );
|
|---|
| 96 | return os;
|
|---|
| 97 | } // ?|?
|
|---|
| 98 | void & ?|?( ostype & os, unsigned short int usi ) {
|
|---|
| 99 | (ostype &)(os | usi); nl( os );
|
|---|
| 100 | } // ?|?
|
|---|
| 101 |
|
|---|
| 102 | ostype & ?|?( ostype & os, int i ) {
|
|---|
| 103 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 104 | fmt( os, "%d", i );
|
|---|
| 105 | return os;
|
|---|
| 106 | } // ?|?
|
|---|
| 107 | void & ?|?( ostype & os, int i ) {
|
|---|
| 108 | (ostype &)(os | i); nl( os );
|
|---|
| 109 | } // ?|?
|
|---|
| 110 |
|
|---|
| 111 | ostype & ?|?( ostype & os, unsigned int ui ) {
|
|---|
| 112 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 113 | fmt( os, "%u", ui );
|
|---|
| 114 | return os;
|
|---|
| 115 | } // ?|?
|
|---|
| 116 | void & ?|?( ostype & os, unsigned int ui ) {
|
|---|
| 117 | (ostype &)(os | ui); nl( os );
|
|---|
| 118 | } // ?|?
|
|---|
| 119 |
|
|---|
| 120 | ostype & ?|?( ostype & os, long int li ) {
|
|---|
| 121 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 122 | fmt( os, "%ld", li );
|
|---|
| 123 | return os;
|
|---|
| 124 | } // ?|?
|
|---|
| 125 | void & ?|?( ostype & os, long int li ) {
|
|---|
| 126 | (ostype &)(os | li); nl( os );
|
|---|
| 127 | } // ?|?
|
|---|
| 128 |
|
|---|
| 129 | ostype & ?|?( ostype & os, unsigned long int uli ) {
|
|---|
| 130 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 131 | fmt( os, "%lu", uli );
|
|---|
| 132 | return os;
|
|---|
| 133 | } // ?|?
|
|---|
| 134 | void & ?|?( ostype & os, unsigned long int uli ) {
|
|---|
| 135 | (ostype &)(os | uli); nl( os );
|
|---|
| 136 | } // ?|?
|
|---|
| 137 |
|
|---|
| 138 | ostype & ?|?( ostype & os, long long int lli ) {
|
|---|
| 139 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 140 | fmt( os, "%lld", lli );
|
|---|
| 141 | return os;
|
|---|
| 142 | } // ?|?
|
|---|
| 143 | void & ?|?( ostype & os, long long int lli ) {
|
|---|
| 144 | (ostype &)(os | lli); nl( os );
|
|---|
| 145 | } // ?|?
|
|---|
| 146 |
|
|---|
| 147 | ostype & ?|?( ostype & os, unsigned long long int ulli ) {
|
|---|
| 148 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 149 | fmt( os, "%llu", ulli );
|
|---|
| 150 | return os;
|
|---|
| 151 | } // ?|?
|
|---|
| 152 | void & ?|?( ostype & os, unsigned long long int ulli ) {
|
|---|
| 153 | (ostype &)(os | ulli); nl( os );
|
|---|
| 154 | } // ?|?
|
|---|
| 155 |
|
|---|
| 156 | static void checkDecPt( ostype & os, const char * buf, int len ) {
|
|---|
| 157 | for ( int i = 0;; i += 1 ) {
|
|---|
| 158 | if ( i == len ) { fmt( os, "." ); break; }
|
|---|
| 159 | if ( buf[i] == '.' ) break;
|
|---|
| 160 | } // for
|
|---|
| 161 | } // checkDecPt
|
|---|
| 162 |
|
|---|
| 163 | ostype & ?|?( ostype & os, float f ) {
|
|---|
| 164 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 165 | char buf[48];
|
|---|
| 166 | int len = snprintf( buf, 48, "%g", f );
|
|---|
| 167 | fmt( os, "%s", buf );
|
|---|
| 168 | if ( isfinite( f ) ) checkDecPt( os, buf, len ); // always print decimal point
|
|---|
| 169 | return os;
|
|---|
| 170 | } // ?|?
|
|---|
| 171 | void & ?|?( ostype & os, float f ) {
|
|---|
| 172 | (ostype &)(os | f); nl( os );
|
|---|
| 173 | } // ?|?
|
|---|
| 174 |
|
|---|
| 175 | ostype & ?|?( ostype & os, double d ) {
|
|---|
| 176 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 177 | char buf[48];
|
|---|
| 178 | int len = snprintf( buf, 48, "%.*lg", DBL_DIG, d );
|
|---|
| 179 | fmt( os, "%s", buf );
|
|---|
| 180 | if ( isfinite( d ) ) checkDecPt( os, buf, len ); // always print decimal point
|
|---|
| 181 | return os;
|
|---|
| 182 | } // ?|?
|
|---|
| 183 | void & ?|?( ostype & os, double d ) {
|
|---|
| 184 | (ostype &)(os | d); nl( os );
|
|---|
| 185 | } // ?|?
|
|---|
| 186 |
|
|---|
| 187 | ostype & ?|?( ostype & os, long double ld ) {
|
|---|
| 188 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 189 | char buf[48];
|
|---|
| 190 | int len = snprintf( buf, 48, "%.*Lg", LDBL_DIG, ld );
|
|---|
| 191 | fmt( os, "%s", buf );
|
|---|
| 192 | if ( isfinite( ld ) ) checkDecPt( os, buf, len ); // always print decimal point
|
|---|
| 193 | return os;
|
|---|
| 194 | } // ?|?
|
|---|
| 195 | void & ?|?( ostype & os, long double ld ) {
|
|---|
| 196 | (ostype &)(os | ld); nl( os );
|
|---|
| 197 | } // ?|?
|
|---|
| 198 |
|
|---|
| 199 | ostype & ?|?( ostype & os, float _Complex fc ) {
|
|---|
| 200 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 201 | // os | crealf( fc ) | nonl;
|
|---|
| 202 | float f = crealf( fc );
|
|---|
| 203 | char buf[48];
|
|---|
| 204 | int len = snprintf( buf, 48, "%g", f );
|
|---|
| 205 | fmt( os, "%s", buf );
|
|---|
| 206 | if ( isfinite( f ) ) checkDecPt( os, buf, len ); // always print decimal point
|
|---|
| 207 | f = cimagf( fc );
|
|---|
| 208 | len = snprintf( buf, 48, "%+g", f );
|
|---|
| 209 | fmt( os, "%s", buf );
|
|---|
| 210 | if ( isfinite( f ) ) checkDecPt( os, buf, len ); // always print decimal point
|
|---|
| 211 | fmt( os, "i" );
|
|---|
| 212 | return os;
|
|---|
| 213 | } // ?|?
|
|---|
| 214 | void & ?|?( ostype & os, float _Complex fc ) {
|
|---|
| 215 | (ostype &)(os | fc); nl( os );
|
|---|
| 216 | } // ?|?
|
|---|
| 217 |
|
|---|
| 218 | ostype & ?|?( ostype & os, double _Complex dc ) {
|
|---|
| 219 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 220 | // os | creal( dc ) | nonl;
|
|---|
| 221 | double d = creal( dc );
|
|---|
| 222 | char buf[48];
|
|---|
| 223 | int len = snprintf( buf, 48, "%.*lg", DBL_DIG, d );
|
|---|
| 224 | fmt( os, "%s", buf );
|
|---|
| 225 | if ( isfinite( d ) ) checkDecPt( os, buf, len ); // always print decimal point
|
|---|
| 226 | d = cimag( dc );
|
|---|
| 227 | len = snprintf( buf, 48, "%+.*lg", DBL_DIG, d );
|
|---|
| 228 | fmt( os, "%s", buf );
|
|---|
| 229 | if ( isfinite( d ) ) checkDecPt( os, buf, len ); // always print decimal point
|
|---|
| 230 | fmt( os, "i" );
|
|---|
| 231 | return os;
|
|---|
| 232 | } // ?|?
|
|---|
| 233 | void & ?|?( ostype & os, double _Complex dc ) {
|
|---|
| 234 | (ostype &)(os | dc); nl( os );
|
|---|
| 235 | } // ?|?
|
|---|
| 236 |
|
|---|
| 237 | ostype & ?|?( ostype & os, long double _Complex ldc ) {
|
|---|
| 238 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 239 | // os | creall( ldc ) || nonl;
|
|---|
| 240 | long double ld = creall( ldc );
|
|---|
| 241 | char buf[48];
|
|---|
| 242 | int len = snprintf( buf, 48, "%.*Lg", LDBL_DIG, ld );
|
|---|
| 243 | fmt( os, "%s", buf );
|
|---|
| 244 | if ( isfinite( ld ) ) checkDecPt( os, buf, len ); // always print decimal point
|
|---|
| 245 | ld = cimagl( ldc );
|
|---|
| 246 | len = snprintf( buf, 48, "%+.*Lg", LDBL_DIG, ld );
|
|---|
| 247 | fmt( os, "%s", buf );
|
|---|
| 248 | if ( isfinite( ld ) ) checkDecPt( os, buf, len ); // always print decimal point
|
|---|
| 249 | fmt( os, "i" );
|
|---|
| 250 | return os;
|
|---|
| 251 | } // ?|?
|
|---|
| 252 | void & ?|?( ostype & os, long double _Complex ldc ) {
|
|---|
| 253 | (ostype &)(os | ldc); nl( os );
|
|---|
| 254 | } // ?|?
|
|---|
| 255 |
|
|---|
| 256 | ostype & ?|?( ostype & os, const char * str ) {
|
|---|
| 257 | enum { Open = 1, Close, OpenClose };
|
|---|
| 258 | static const unsigned char mask[256] @= {
|
|---|
| 259 | // opening delimiters, no space after
|
|---|
| 260 | ['('] : Open, ['['] : Open, ['{'] : Open,
|
|---|
| 261 | ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
|
|---|
| 262 | [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
|
|---|
| 263 | // closing delimiters, no space before
|
|---|
| 264 | [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
|
|---|
| 265 | ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
|
|---|
| 266 | [')'] : Close, [']'] : Close, ['}'] : Close,
|
|---|
| 267 | // opening-closing delimiters, no space before or after
|
|---|
| 268 | ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose,
|
|---|
| 269 | [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
|
|---|
| 270 | }; // mask
|
|---|
| 271 |
|
|---|
| 272 | if ( str[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator
|
|---|
| 273 |
|
|---|
| 274 | // first character IS NOT spacing or closing punctuation => add left separator
|
|---|
| 275 | unsigned char ch = str[0]; // must make unsigned
|
|---|
| 276 | if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
|
|---|
| 277 | fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 278 | } // if
|
|---|
| 279 |
|
|---|
| 280 | // if string starts line, must reset to determine open state because separator is off
|
|---|
| 281 | sepReset( os ); // reset separator
|
|---|
| 282 |
|
|---|
| 283 | // last character IS spacing or opening punctuation => turn off separator for next item
|
|---|
| 284 | size_t len = strlen( str );
|
|---|
| 285 | ch = str[len - 1]; // must make unsigned
|
|---|
| 286 | if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
|
|---|
| 287 | sepOn( os );
|
|---|
| 288 | } else {
|
|---|
| 289 | sepOff( os );
|
|---|
| 290 | } // if
|
|---|
| 291 | if ( ch == '\n' ) setNL( os, true ); // check *AFTER* sepPrt call above as it resets NL flag
|
|---|
| 292 | return write( os, str, len );
|
|---|
| 293 | } // ?|?
|
|---|
| 294 | void ?|?( ostype & os, const char * str ) {
|
|---|
| 295 | (ostype &)(os | str); nl( os );
|
|---|
| 296 | } // ?|?
|
|---|
| 297 |
|
|---|
| 298 | // ostype & ?|?( ostype & os, const char16_t * str ) {
|
|---|
| 299 | // if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 300 | // fmt( os, "%ls", str );
|
|---|
| 301 | // return os;
|
|---|
| 302 | // } // ?|?
|
|---|
| 303 |
|
|---|
| 304 | // #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
|
|---|
| 305 | // ostype & ?|?( ostype & os, const char32_t * str ) {
|
|---|
| 306 | // if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 307 | // fmt( os, "%ls", str );
|
|---|
| 308 | // return os;
|
|---|
| 309 | // } // ?|?
|
|---|
| 310 | // #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
|
|---|
| 311 |
|
|---|
| 312 | // ostype & ?|?( ostype & os, const wchar_t * str ) {
|
|---|
| 313 | // if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 314 | // fmt( os, "%ls", str );
|
|---|
| 315 | // return os;
|
|---|
| 316 | // } // ?|?
|
|---|
| 317 |
|
|---|
| 318 | ostype & ?|?( ostype & os, const void * p ) {
|
|---|
| 319 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 320 | fmt( os, "%p", p );
|
|---|
| 321 | return os;
|
|---|
| 322 | } // ?|?
|
|---|
| 323 | void ?|?( ostype & os, const void * p ) {
|
|---|
| 324 | (ostype &)(os | p); nl( os );
|
|---|
| 325 | } // ?|?
|
|---|
| 326 |
|
|---|
| 327 | // manipulators
|
|---|
| 328 | ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
|
|---|
| 329 | (ostype &)(manip( os ));
|
|---|
| 330 | return os;
|
|---|
| 331 | } // ?|?
|
|---|
| 332 | void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
|
|---|
| 333 | (ostype &)(manip( os ));
|
|---|
| 334 | if ( getPrt( os ) ) nl( os ); // something printed ?
|
|---|
| 335 | setPrt( os, false ); // turn off
|
|---|
| 336 | } // ?|?
|
|---|
| 337 |
|
|---|
| 338 | ostype & sep( ostype & os ) {
|
|---|
| 339 | return (ostype &)(os | sepGet( os ));
|
|---|
| 340 | } // sep
|
|---|
| 341 |
|
|---|
| 342 | ostype & sepTuple( ostype & os ) {
|
|---|
| 343 | return os | sepGetTuple( os );
|
|---|
| 344 | } // sepTuple
|
|---|
| 345 |
|
|---|
| 346 | ostype & nl( ostype & os ) {
|
|---|
| 347 | (ostype &)(os | '\n');
|
|---|
| 348 | setPrt( os, false ); // turn off
|
|---|
| 349 | setNL( os, true );
|
|---|
| 350 | flush( os );
|
|---|
| 351 | return sepOff( os ); // prepare for next line
|
|---|
| 352 | } // nl
|
|---|
| 353 |
|
|---|
| 354 | void nl( ostype & os ) {
|
|---|
| 355 | if ( getANL( os ) ) (ostype &)(nl( os )); // implementation only
|
|---|
| 356 | else setPrt( os, false ); // turn off
|
|---|
| 357 | } // nl
|
|---|
| 358 |
|
|---|
| 359 | ostype & nonl( ostype & os ) {
|
|---|
| 360 | setPrt( os, false ); // turn off
|
|---|
| 361 | return os;
|
|---|
| 362 | } // nonl
|
|---|
| 363 |
|
|---|
| 364 | ostype & sepOn( ostype & os ) {
|
|---|
| 365 | sepOn( os ); // call void returning
|
|---|
| 366 | return os;
|
|---|
| 367 | } // sepOn
|
|---|
| 368 |
|
|---|
| 369 | ostype & sepOff( ostype & os ) {
|
|---|
| 370 | sepOff( os ); // call void returning
|
|---|
| 371 | return os;
|
|---|
| 372 | } // sepOff
|
|---|
| 373 |
|
|---|
| 374 | ostype & sepEnable( ostype & os ) {
|
|---|
| 375 | sepEnable( os ); // call void returning
|
|---|
| 376 | return os;
|
|---|
| 377 | } // sepEnable
|
|---|
| 378 |
|
|---|
| 379 | ostype & sepDisable( ostype & os ) {
|
|---|
| 380 | sepDisable( os ); // call void returning
|
|---|
| 381 | return os;
|
|---|
| 382 | } // sepDisable
|
|---|
| 383 |
|
|---|
| 384 | ostype & nlOn( ostype & os ) {
|
|---|
| 385 | nlOn( os ); // call void returning
|
|---|
| 386 | return os;
|
|---|
| 387 | } // nlOn
|
|---|
| 388 |
|
|---|
| 389 | ostype & nlOff( ostype & os ) {
|
|---|
| 390 | nlOff( os ); // call void returning
|
|---|
| 391 | return os;
|
|---|
| 392 | } // nlOff
|
|---|
| 393 | } // distribution
|
|---|
| 394 |
|
|---|
| 395 | // tuples
|
|---|
| 396 | forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
|
|---|
| 397 | ostype & ?|?( ostype & os, T arg, Params rest ) {
|
|---|
| 398 | (ostype &)(os | arg); // print first argument
|
|---|
| 399 | sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator
|
|---|
| 400 | (ostype &)(os | rest); // print remaining arguments
|
|---|
| 401 | sepSetCur( os, sepGet( os ) ); // switch to regular separator
|
|---|
| 402 | return os;
|
|---|
| 403 | } // ?|?
|
|---|
| 404 | void ?|?( ostype & os, T arg, Params rest ) {
|
|---|
| 405 | // (ostype &)(?|?( os, arg, rest )); nl( os );
|
|---|
| 406 | (ostype &)(os | arg); // print first argument
|
|---|
| 407 | sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator
|
|---|
| 408 | (ostype &)(os | rest); // print remaining arguments
|
|---|
| 409 | sepSetCur( os, sepGet( os ) ); // switch to regular separator
|
|---|
| 410 | nl( os );
|
|---|
| 411 | } // ?|?
|
|---|
| 412 | } // distribution
|
|---|
| 413 |
|
|---|
| 414 | //---------------------------------------
|
|---|
| 415 |
|
|---|
| 416 | // writes the range [begin, end) to the given stream
|
|---|
| 417 | forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) {
|
|---|
| 418 | void write( iterator_type begin, iterator_type end, ostype & os ) {
|
|---|
| 419 | void print( elt_type i ) { os | i; }
|
|---|
| 420 | for_each( begin, end, print );
|
|---|
| 421 | } // ?|?
|
|---|
| 422 |
|
|---|
| 423 | void write_reverse( iterator_type begin, iterator_type end, ostype & os ) {
|
|---|
| 424 | void print( elt_type i ) { os | i; }
|
|---|
| 425 | for_each_reverse( begin, end, print );
|
|---|
| 426 | } // ?|?
|
|---|
| 427 | } // distribution
|
|---|
| 428 |
|
|---|
| 429 | //---------------------------------------
|
|---|
| 430 |
|
|---|
| 431 | forall( dtype istype | istream( istype ) ) {
|
|---|
| 432 | istype & ?|?( istype & is, bool & b ) {
|
|---|
| 433 | char val[6];
|
|---|
| 434 | fmt( is, "%5s", val );
|
|---|
| 435 | if ( strcmp( val, "true" ) == 0 ) b = true;
|
|---|
| 436 | else if ( strcmp( val, "false" ) == 0 ) b = false;
|
|---|
| 437 | else {
|
|---|
| 438 | fprintf( stderr, "invalid Boolean constant\n" );
|
|---|
| 439 | abort();
|
|---|
| 440 | } // if
|
|---|
| 441 | return is;
|
|---|
| 442 | } // ?|?
|
|---|
| 443 |
|
|---|
| 444 | istype & ?|?( istype & is, char & c ) {
|
|---|
| 445 | char temp;
|
|---|
| 446 | for () {
|
|---|
| 447 | fmt( is, "%c", &temp ); // must pass pointer through varg to fmt
|
|---|
| 448 | // do not overwrite parameter with newline unless appropriate
|
|---|
| 449 | if ( temp != '\n' || getANL( is ) ) { c = temp; break; }
|
|---|
| 450 | if ( eof( is ) ) break;
|
|---|
| 451 | } // for
|
|---|
| 452 | return is;
|
|---|
| 453 | } // ?|?
|
|---|
| 454 |
|
|---|
| 455 | istype & ?|?( istype & is, signed char & sc ) {
|
|---|
| 456 | fmt( is, "%hhd", &sc );
|
|---|
| 457 | return is;
|
|---|
| 458 | } // ?|?
|
|---|
| 459 |
|
|---|
| 460 | istype & ?|?( istype & is, unsigned char & usc ) {
|
|---|
| 461 | fmt( is, "%hhu", &usc );
|
|---|
| 462 | return is;
|
|---|
| 463 | } // ?|?
|
|---|
| 464 |
|
|---|
| 465 | istype & ?|?( istype & is, short int & si ) {
|
|---|
| 466 | fmt( is, "%hd", &si );
|
|---|
| 467 | return is;
|
|---|
| 468 | } // ?|?
|
|---|
| 469 |
|
|---|
| 470 | istype & ?|?( istype & is, unsigned short int & usi ) {
|
|---|
| 471 | fmt( is, "%hu", &usi );
|
|---|
| 472 | return is;
|
|---|
| 473 | } // ?|?
|
|---|
| 474 |
|
|---|
| 475 | istype & ?|?( istype & is, int & i ) {
|
|---|
| 476 | fmt( is, "%d", &i );
|
|---|
| 477 | return is;
|
|---|
| 478 | } // ?|?
|
|---|
| 479 |
|
|---|
| 480 | istype & ?|?( istype & is, unsigned int & ui ) {
|
|---|
| 481 | fmt( is, "%u", &ui );
|
|---|
| 482 | return is;
|
|---|
| 483 | } // ?|?
|
|---|
| 484 |
|
|---|
| 485 | istype & ?|?( istype & is, long int & li ) {
|
|---|
| 486 | fmt( is, "%ld", &li );
|
|---|
| 487 | return is;
|
|---|
| 488 | } // ?|?
|
|---|
| 489 |
|
|---|
| 490 | istype & ?|?( istype & is, unsigned long int & ulli ) {
|
|---|
| 491 | fmt( is, "%lu", &ulli );
|
|---|
| 492 | return is;
|
|---|
| 493 | } // ?|?
|
|---|
| 494 |
|
|---|
| 495 | istype & ?|?( istype & is, long long int & lli ) {
|
|---|
| 496 | fmt( is, "%lld", &lli );
|
|---|
| 497 | return is;
|
|---|
| 498 | } // ?|?
|
|---|
| 499 |
|
|---|
| 500 | istype & ?|?( istype & is, unsigned long long int & ulli ) {
|
|---|
| 501 | fmt( is, "%llu", &ulli );
|
|---|
| 502 | return is;
|
|---|
| 503 | } // ?|?
|
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 | istype & ?|?( istype & is, float & f ) {
|
|---|
| 507 | fmt( is, "%f", &f );
|
|---|
| 508 | return is;
|
|---|
| 509 | } // ?|?
|
|---|
| 510 |
|
|---|
| 511 | istype & ?|?( istype & is, double & d ) {
|
|---|
| 512 | fmt( is, "%lf", &d );
|
|---|
| 513 | return is;
|
|---|
| 514 | } // ?|?
|
|---|
| 515 |
|
|---|
| 516 | istype & ?|?( istype & is, long double & ld ) {
|
|---|
| 517 | fmt( is, "%Lf", &ld );
|
|---|
| 518 | return is;
|
|---|
| 519 | } // ?|?
|
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 | istype & ?|?( istype & is, float _Complex & fc ) {
|
|---|
| 523 | float re, im;
|
|---|
| 524 | fmt( is, "%g%gi", &re, &im );
|
|---|
| 525 | fc = re + im * _Complex_I;
|
|---|
| 526 | return is;
|
|---|
| 527 | } // ?|?
|
|---|
| 528 |
|
|---|
| 529 | istype & ?|?( istype & is, double _Complex & dc ) {
|
|---|
| 530 | double re, im;
|
|---|
| 531 | fmt( is, "%lf%lfi", &re, &im );
|
|---|
| 532 | dc = re + im * _Complex_I;
|
|---|
| 533 | return is;
|
|---|
| 534 | } // ?|?
|
|---|
| 535 |
|
|---|
| 536 | istype & ?|?( istype & is, long double _Complex & ldc ) {
|
|---|
| 537 | long double re, im;
|
|---|
| 538 | fmt( is, "%Lf%Lfi", &re, &im );
|
|---|
| 539 | ldc = re + im * _Complex_I;
|
|---|
| 540 | return is;
|
|---|
| 541 | } // ?|?
|
|---|
| 542 |
|
|---|
| 543 | // manipulators
|
|---|
| 544 | istype & ?|?( istype & is, istype & (* manip)( istype & ) ) {
|
|---|
| 545 | return manip( is );
|
|---|
| 546 | } // ?|?
|
|---|
| 547 |
|
|---|
| 548 | istype & nl( istype & is ) {
|
|---|
| 549 | fmt( is, "%*[^\n]" ); // ignore characters to newline
|
|---|
| 550 | return is;
|
|---|
| 551 | } // nl
|
|---|
| 552 |
|
|---|
| 553 | istype & nlOn( istype & is ) {
|
|---|
| 554 | nlOn( is ); // call void returning
|
|---|
| 555 | return is;
|
|---|
| 556 | } // nlOn
|
|---|
| 557 |
|
|---|
| 558 | istype & nlOff( istype & is ) {
|
|---|
| 559 | nlOff( is ); // call void returning
|
|---|
| 560 | return is;
|
|---|
| 561 | } // nlOff
|
|---|
| 562 | } // distribution
|
|---|
| 563 |
|
|---|
| 564 | _Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; }
|
|---|
| 565 | forall( dtype istype | istream( istype ) )
|
|---|
| 566 | istype & ?|?( istype & is, _Istream_cstrUC cstr ) {
|
|---|
| 567 | fmt( is, "%s", cstr.s );
|
|---|
| 568 | return is;
|
|---|
| 569 | } // cstr
|
|---|
| 570 |
|
|---|
| 571 | _Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; }
|
|---|
| 572 | forall( dtype istype | istream( istype ) )
|
|---|
| 573 | istype & ?|?( istype & is, _Istream_cstrC cstr ) {
|
|---|
| 574 | char buf[16];
|
|---|
| 575 | sprintf( buf, "%%%ds", cstr.size );
|
|---|
| 576 | fmt( is, buf, cstr.s );
|
|---|
| 577 | return is;
|
|---|
| 578 | } // cstr
|
|---|
| 579 |
|
|---|
| 580 | // Local Variables: //
|
|---|
| 581 | // tab-width: 4 //
|
|---|
| 582 | // compile-command: "cfa iostream.cfa" //
|
|---|
| 583 | // End: //
|
|---|