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