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