| [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
 | 
|---|
| [17a1b21] | 12 | // Last Modified On : Mon Mar  4 20:57:24 2019
 | 
|---|
 | 13 | // Update Count     : 593
 | 
|---|
| [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 ) {
 | 
|---|
 | 398 |                 fmt( is, "%c", &c );                                                    // must pass pointer through varg to fmt
 | 
|---|
 | 399 |                 return is;
 | 
|---|
 | 400 |         } // ?|?
 | 
|---|
 | 401 | 
 | 
|---|
 | 402 |         istype & ?|?( istype & is, signed char & sc ) {
 | 
|---|
 | 403 |                 fmt( is, "%hhd", &sc );
 | 
|---|
 | 404 |                 return is;
 | 
|---|
 | 405 |         } // ?|?
 | 
|---|
 | 406 | 
 | 
|---|
 | 407 |         istype & ?|?( istype & is, unsigned char & usc ) {
 | 
|---|
 | 408 |                 fmt( is, "%hhu", &usc );
 | 
|---|
 | 409 |                 return is;
 | 
|---|
 | 410 |         } // ?|?
 | 
|---|
 | 411 | 
 | 
|---|
 | 412 |         istype & ?|?( istype & is, short int & si ) {
 | 
|---|
 | 413 |                 fmt( is, "%hd", &si );
 | 
|---|
 | 414 |                 return is;
 | 
|---|
 | 415 |         } // ?|?
 | 
|---|
 | 416 | 
 | 
|---|
 | 417 |         istype & ?|?( istype & is, unsigned short int & usi ) {
 | 
|---|
 | 418 |                 fmt( is, "%hu", &usi );
 | 
|---|
 | 419 |                 return is;
 | 
|---|
 | 420 |         } // ?|?
 | 
|---|
 | 421 | 
 | 
|---|
 | 422 |         istype & ?|?( istype & is, int & i ) {
 | 
|---|
 | 423 |                 fmt( is, "%d", &i );
 | 
|---|
 | 424 |                 return is;
 | 
|---|
 | 425 |         } // ?|?
 | 
|---|
 | 426 | 
 | 
|---|
 | 427 |         istype & ?|?( istype & is, unsigned int & ui ) {
 | 
|---|
 | 428 |                 fmt( is, "%u", &ui );
 | 
|---|
 | 429 |                 return is;
 | 
|---|
 | 430 |         } // ?|?
 | 
|---|
 | 431 | 
 | 
|---|
 | 432 |         istype & ?|?( istype & is, long int & li ) {
 | 
|---|
 | 433 |                 fmt( is, "%ld", &li );
 | 
|---|
 | 434 |                 return is;
 | 
|---|
 | 435 |         } // ?|?
 | 
|---|
 | 436 | 
 | 
|---|
 | 437 |         istype & ?|?( istype & is, unsigned long int & ulli ) {
 | 
|---|
 | 438 |                 fmt( is, "%lu", &ulli );
 | 
|---|
 | 439 |                 return is;
 | 
|---|
 | 440 |         } // ?|?
 | 
|---|
 | 441 | 
 | 
|---|
 | 442 |         istype & ?|?( istype & is, long long int & lli ) {
 | 
|---|
 | 443 |                 fmt( is, "%lld", &lli );
 | 
|---|
 | 444 |                 return is;
 | 
|---|
 | 445 |         } // ?|?
 | 
|---|
 | 446 | 
 | 
|---|
 | 447 |         istype & ?|?( istype & is, unsigned long long int & ulli ) {
 | 
|---|
 | 448 |                 fmt( is, "%llu", &ulli );
 | 
|---|
 | 449 |                 return is;
 | 
|---|
 | 450 |         } // ?|?
 | 
|---|
 | 451 | 
 | 
|---|
 | 452 | 
 | 
|---|
 | 453 |         istype & ?|?( istype & is, float & f ) {
 | 
|---|
 | 454 |                 fmt( is, "%f", &f );
 | 
|---|
 | 455 |                 return is;
 | 
|---|
 | 456 |         } // ?|?
 | 
|---|
 | 457 | 
 | 
|---|
 | 458 |         istype & ?|?( istype & is, double & d ) {
 | 
|---|
 | 459 |                 fmt( is, "%lf", &d );
 | 
|---|
 | 460 |                 return is;
 | 
|---|
 | 461 |         } // ?|?
 | 
|---|
 | 462 | 
 | 
|---|
 | 463 |         istype & ?|?( istype & is, long double & ld ) {
 | 
|---|
 | 464 |                 fmt( is, "%Lf", &ld );
 | 
|---|
 | 465 |                 return is;
 | 
|---|
 | 466 |         } // ?|?
 | 
|---|
 | 467 | 
 | 
|---|
 | 468 | 
 | 
|---|
 | 469 |         istype & ?|?( istype & is, float _Complex & fc ) {
 | 
|---|
 | 470 |                 float re, im;
 | 
|---|
 | 471 |                 fmt( is, "%g%gi", &re, &im );
 | 
|---|
 | 472 |                 fc = re + im * _Complex_I;
 | 
|---|
 | 473 |                 return is;
 | 
|---|
 | 474 |         } // ?|?
 | 
|---|
 | 475 | 
 | 
|---|
 | 476 |         istype & ?|?( istype & is, double _Complex & dc ) {
 | 
|---|
 | 477 |                 double re, im;
 | 
|---|
 | 478 |                 fmt( is, "%lf%lfi", &re, &im );
 | 
|---|
 | 479 |                 dc = re + im * _Complex_I;
 | 
|---|
 | 480 |                 return is;
 | 
|---|
 | 481 |         } // ?|?
 | 
|---|
 | 482 | 
 | 
|---|
 | 483 |         istype & ?|?( istype & is, long double _Complex & ldc ) {
 | 
|---|
 | 484 |                 long double re, im;
 | 
|---|
 | 485 |                 fmt( is, "%Lf%Lfi", &re, &im );
 | 
|---|
 | 486 |                 ldc = re + im * _Complex_I;
 | 
|---|
 | 487 |                 return is;
 | 
|---|
 | 488 |         } // ?|?
 | 
|---|
 | 489 | 
 | 
|---|
 | 490 | 
 | 
|---|
 | 491 |         // manipulators
 | 
|---|
 | 492 |         istype & ?|?( istype & is, istype & (* manip)( istype & ) ) {
 | 
|---|
 | 493 |                 return manip( is );
 | 
|---|
 | 494 |         } // ?|?
 | 
|---|
 | 495 | 
 | 
|---|
| [200fcb3] | 496 |         istype & nl( istype & is ) {
 | 
|---|
| [3ce0d440] | 497 |                 fmt( is, "%*[ \t\f\n\r\v]" );                                   // ignore whitespace
 | 
|---|
 | 498 |                 return is;
 | 
|---|
| [200fcb3] | 499 |         } // nl
 | 
|---|
| [3ce0d440] | 500 | } // distribution
 | 
|---|
| [44574f2] | 501 | 
 | 
|---|
| [e24f13a] | 502 | _Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; }
 | 
|---|
| [90c3b1c] | 503 | forall( dtype istype | istream( istype ) )
 | 
|---|
| [09687aa] | 504 | istype & ?|?( istype & is, _Istream_cstrUC cstr ) {
 | 
|---|
| [829c907] | 505 |         fmt( is, "%s", cstr.s );
 | 
|---|
| [90c3b1c] | 506 |         return is;
 | 
|---|
| [53ba273] | 507 | } // cstr
 | 
|---|
| [90c3b1c] | 508 | 
 | 
|---|
| [e24f13a] | 509 | _Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; }
 | 
|---|
| [90c3b1c] | 510 | forall( dtype istype | istream( istype ) )
 | 
|---|
| [09687aa] | 511 | istype & ?|?( istype & is, _Istream_cstrC cstr ) {
 | 
|---|
| [90c3b1c] | 512 |         char buf[16];
 | 
|---|
| [53ba273] | 513 |         sprintf( buf, "%%%ds", cstr.size );
 | 
|---|
| [829c907] | 514 |         fmt( is, buf, cstr.s );
 | 
|---|
| [90c3b1c] | 515 |         return is;
 | 
|---|
| [53ba273] | 516 | } // cstr
 | 
|---|
| [90c3b1c] | 517 | 
 | 
|---|
| [86bd7c1f] | 518 | // Local Variables: //
 | 
|---|
 | 519 | // tab-width: 4 //
 | 
|---|
| [084520f] | 520 | // compile-command: "cfa iostream.cfa" //
 | 
|---|
| [86bd7c1f] | 521 | // End: //
 | 
|---|