[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 |
---|
[1630f18] | 12 | // Last Modified On : Fri Feb 24 21:09:59 2017 |
---|
| 13 | // Update Count : 307 |
---|
[86bd7c1f] | 14 | // |
---|
[51b7345] | 15 | |
---|
[d3b7937] | 16 | #include "iostream" |
---|
| 17 | |
---|
[51b7345] | 18 | extern "C" { |
---|
| 19 | #include <stdio.h> |
---|
[839ccbb] | 20 | #include <string.h> // strlen |
---|
[52f85e0] | 21 | #include <float.h> // DBL_DIG, LDBL_DIG |
---|
[d3b7937] | 22 | #include <complex.h> // creal, cimag |
---|
[51b7345] | 23 | } |
---|
| 24 | |
---|
| 25 | forall( dtype ostype | ostream( ostype ) ) |
---|
[cf16f94] | 26 | ostype * ?|?( ostype *os, char c ) { |
---|
[90c3b1c] | 27 | prtfmt( os, "%c", c ); |
---|
[53ba273] | 28 | sepOff( os ); |
---|
[90c3b1c] | 29 | return os; |
---|
| 30 | } // ?|? |
---|
| 31 | |
---|
[1630f18] | 32 | forall( dtype ostype | ostream( ostype ) ) |
---|
| 33 | ostype * ?|?( ostype *os, signed char c ) { |
---|
| 34 | prtfmt( os, "%hhd", c ); |
---|
| 35 | sepOff( os ); |
---|
| 36 | return os; |
---|
| 37 | } // ?|? |
---|
| 38 | |
---|
| 39 | forall( dtype ostype | ostream( ostype ) ) |
---|
| 40 | ostype * ?|?( ostype *os, unsigned char c ) { |
---|
| 41 | prtfmt( os, "%hhu", c ); |
---|
| 42 | sepOff( os ); |
---|
| 43 | return os; |
---|
| 44 | } // ?|? |
---|
| 45 | |
---|
[90c3b1c] | 46 | forall( dtype ostype | ostream( ostype ) ) |
---|
| 47 | ostype * ?|?( ostype *os, short int si ) { |
---|
[53ba273] | 48 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); |
---|
[90c3b1c] | 49 | prtfmt( os, "%hd", si ); |
---|
| 50 | return os; |
---|
| 51 | } // ?|? |
---|
| 52 | |
---|
| 53 | forall( dtype ostype | ostream( ostype ) ) |
---|
| 54 | ostype * ?|?( ostype *os, unsigned short int usi ) { |
---|
[53ba273] | 55 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); |
---|
[90c3b1c] | 56 | prtfmt( os, "%hu", usi ); |
---|
| 57 | return os; |
---|
[cf16f94] | 58 | } // ?|? |
---|
[51b7345] | 59 | |
---|
| 60 | forall( dtype ostype | ostream( ostype ) ) |
---|
[cf16f94] | 61 | ostype * ?|?( ostype *os, int i ) { |
---|
[53ba273] | 62 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); |
---|
[90c3b1c] | 63 | prtfmt( os, "%d", i ); |
---|
| 64 | return os; |
---|
[784deab] | 65 | } // ?|? |
---|
| 66 | |
---|
| 67 | forall( dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 68 | ostype * ?|?( ostype *os, unsigned int ui ) { |
---|
[53ba273] | 69 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); |
---|
[90c3b1c] | 70 | prtfmt( os, "%u", ui ); |
---|
| 71 | return os; |
---|
[784deab] | 72 | } // ?|? |
---|
| 73 | |
---|
| 74 | forall( dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 75 | ostype * ?|?( ostype *os, long int li ) { |
---|
[53ba273] | 76 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); |
---|
[90c3b1c] | 77 | prtfmt( os, "%ld", li ); |
---|
| 78 | return os; |
---|
[784deab] | 79 | } // ?|? |
---|
| 80 | |
---|
| 81 | forall( dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 82 | ostype * ?|?( ostype *os, unsigned long int uli ) { |
---|
[53ba273] | 83 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); |
---|
[90c3b1c] | 84 | prtfmt( os, "%lu", uli ); |
---|
| 85 | return os; |
---|
[784deab] | 86 | } // ?|? |
---|
| 87 | |
---|
| 88 | forall( dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 89 | ostype * ?|?( ostype *os, long long int lli ) { |
---|
[53ba273] | 90 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); |
---|
[90c3b1c] | 91 | prtfmt( os, "%lld", lli ); |
---|
| 92 | return os; |
---|
[784deab] | 93 | } // ?|? |
---|
| 94 | |
---|
| 95 | forall( dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 96 | ostype * ?|?( ostype *os, unsigned long long int ulli ) { |
---|
[53ba273] | 97 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); |
---|
[90c3b1c] | 98 | prtfmt( os, "%llu", ulli ); |
---|
| 99 | return os; |
---|
[cf16f94] | 100 | } // ?|? |
---|
[51b7345] | 101 | |
---|
[d3b7937] | 102 | forall( dtype ostype | ostream( ostype ) ) |
---|
| 103 | ostype * ?|?( ostype *os, float f ) { |
---|
[53ba273] | 104 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); |
---|
[90c3b1c] | 105 | prtfmt( os, "%g", f ); |
---|
| 106 | return os; |
---|
[d3b7937] | 107 | } // ?|? |
---|
| 108 | |
---|
[51b7345] | 109 | forall( dtype ostype | ostream( ostype ) ) |
---|
[cf16f94] | 110 | ostype * ?|?( ostype *os, double d ) { |
---|
[53ba273] | 111 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); |
---|
[90c3b1c] | 112 | prtfmt( os, "%.*lg", DBL_DIG, d ); |
---|
| 113 | return os; |
---|
[cf16f94] | 114 | } // ?|? |
---|
[134b86a] | 115 | |
---|
| 116 | forall( dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 117 | ostype * ?|?( ostype *os, long double ld ) { |
---|
[53ba273] | 118 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); |
---|
[90c3b1c] | 119 | prtfmt( os, "%.*Lg", LDBL_DIG, ld ); |
---|
| 120 | return os; |
---|
[cf16f94] | 121 | } // ?|? |
---|
[51b7345] | 122 | |
---|
[d3b7937] | 123 | forall( dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 124 | ostype * ?|?( ostype *os, float _Complex fc ) { |
---|
| 125 | os | crealf( fc ); |
---|
[53ba273] | 126 | _Bool temp = sepDisable( os ); // disable separators within complex value |
---|
| 127 | if ( cimagf( fc ) >= 0 ) os | '+'; // negative value prints '-' |
---|
| 128 | os | cimagf( fc ) | 'i'; |
---|
| 129 | sepReset( os, temp ); // reset separator |
---|
[90c3b1c] | 130 | return os; |
---|
[d3b7937] | 131 | } // ?|? |
---|
| 132 | |
---|
| 133 | forall( dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 134 | ostype * ?|?( ostype *os, double _Complex dc ) { |
---|
| 135 | os | creal( dc ); |
---|
[53ba273] | 136 | _Bool temp = sepDisable( os ); // disable separators within complex value |
---|
| 137 | if ( cimag( dc ) >= 0 ) os | '+'; // negative value prints '-' |
---|
| 138 | os | cimag( dc ) | 'i'; |
---|
| 139 | sepReset( os, temp ); // reset separator |
---|
[90c3b1c] | 140 | return os; |
---|
[d3b7937] | 141 | } // ?|? |
---|
| 142 | |
---|
| 143 | forall( dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 144 | ostype * ?|?( ostype *os, long double _Complex ldc ) { |
---|
| 145 | os | creall( ldc ); |
---|
[53ba273] | 146 | _Bool temp = sepDisable( os ); // disable separators within complex value |
---|
| 147 | if ( cimagl( ldc ) >= 0 ) os | '+'; // negative value prints '-' |
---|
| 148 | os | cimagl( ldc ) | 'i'; |
---|
| 149 | sepReset( os, temp ); // reset separator |
---|
[90c3b1c] | 150 | return os; |
---|
[d3b7937] | 151 | } // ?|? |
---|
| 152 | |
---|
[e56cfdb0] | 153 | forall( dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 154 | ostype * ?|?( ostype *os, const char *cp ) { |
---|
[b63e376] | 155 | enum { Open = 1, Close, OpenClose }; |
---|
[53ba273] | 156 | static const unsigned char mask[256] = { |
---|
[b63e376] | 157 | // opening delimiters |
---|
| 158 | ['('] : Open, ['['] : Open, ['{'] : Open, |
---|
[e945826] | 159 | ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open, |
---|
| 160 | [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open, |
---|
[b63e376] | 161 | // closing delimiters |
---|
| 162 | [','] : Close, ['.'] : Close, [':'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close, |
---|
| 163 | [')'] : Close, [']'] : Close, ['}'] : Close, |
---|
[53ba273] | 164 | ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close, |
---|
[b63e376] | 165 | // opening-closing delimiters |
---|
| 166 | ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, |
---|
[e945826] | 167 | [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace |
---|
[b63e376] | 168 | }; // mask |
---|
| 169 | |
---|
[b72bad4f] | 170 | if ( cp[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator |
---|
| 171 | |
---|
[53ba273] | 172 | // first character IS NOT spacing or closing punctuation => add left separator |
---|
| 173 | unsigned char ch = cp[0]; // must make unsigned |
---|
| 174 | if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) { |
---|
[90c3b1c] | 175 | prtfmt( os, "%s", sepGet( os ) ); |
---|
| 176 | } // if |
---|
[b72bad4f] | 177 | |
---|
| 178 | // if string starts line, must reset to determine open state because separator is off |
---|
| 179 | sepReset( os ); // reset separator |
---|
| 180 | |
---|
[b63e376] | 181 | // last character IS spacing or opening punctuation => turn off separator for next item |
---|
[b72bad4f] | 182 | unsigned int len = strlen( cp ), posn = len - 1; |
---|
[53ba273] | 183 | ch = cp[posn]; // must make unsigned |
---|
[b72bad4f] | 184 | if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) { |
---|
[90c3b1c] | 185 | sepOn( os ); |
---|
[b72bad4f] | 186 | } else { |
---|
| 187 | sepOff( os ); |
---|
[90c3b1c] | 188 | } // if |
---|
| 189 | return write( os, cp, len ); |
---|
[784deab] | 190 | } // ?|? |
---|
| 191 | |
---|
| 192 | forall( dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 193 | ostype * ?|?( ostype *os, const void *p ) { |
---|
[53ba273] | 194 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); |
---|
[90c3b1c] | 195 | prtfmt( os, "%p", p ); |
---|
| 196 | return os; |
---|
[cf16f94] | 197 | } // ?|? |
---|
| 198 | |
---|
| 199 | |
---|
[a5a71d0] | 200 | forall( dtype ostype | ostream( ostype ) ) |
---|
[4e06c1e] | 201 | ostype * ?|?( ostype * os, ostype * (* manip)( ostype * ) ) { |
---|
[90c3b1c] | 202 | return manip( os ); |
---|
| 203 | } // ?|? |
---|
[cf16f94] | 204 | |
---|
[a5a71d0] | 205 | forall( dtype ostype | ostream( ostype ) ) |
---|
[cf16f94] | 206 | ostype * endl( ostype * os ) { |
---|
[90c3b1c] | 207 | os | '\n'; |
---|
[6ba0659] | 208 | flush( os ); |
---|
[90c3b1c] | 209 | sepOff( os ); |
---|
[6ba0659] | 210 | return os; |
---|
[cf16f94] | 211 | } // endl |
---|
[e56cfdb0] | 212 | |
---|
[a5a71d0] | 213 | forall( dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 214 | ostype * sepOn( ostype * os ) { |
---|
| 215 | sepOn( os ); |
---|
| 216 | return os; |
---|
| 217 | } // sepOn |
---|
| 218 | |
---|
[a5a71d0] | 219 | forall( dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 220 | ostype * sepOff( ostype * os ) { |
---|
| 221 | sepOff( os ); |
---|
| 222 | return os; |
---|
| 223 | } // sepOff |
---|
| 224 | |
---|
[70a06f6] | 225 | forall( dtype ostype | ostream( ostype ) ) |
---|
[53ba273] | 226 | ostype * sepEnable( ostype * os ) { |
---|
| 227 | sepEnable( os ); |
---|
| 228 | return os; |
---|
| 229 | } // sepEnable |
---|
| 230 | |
---|
[70a06f6] | 231 | forall( dtype ostype | ostream( ostype ) ) |
---|
[53ba273] | 232 | ostype * sepDisable( ostype * os ) { |
---|
| 233 | sepDisable( os ); |
---|
| 234 | return os; |
---|
| 235 | } // sepDisable |
---|
| 236 | |
---|
[6ba0659] | 237 | //--------------------------------------- |
---|
| 238 | |
---|
[4040425] | 239 | forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 240 | void write( iteratortype begin, iteratortype end, ostype *os ) { |
---|
| 241 | void print( elttype i ) { os | i; } |
---|
[e56cfdb0] | 242 | for_each( begin, end, print ); |
---|
[cf16f94] | 243 | } // ?|? |
---|
[e56cfdb0] | 244 | |
---|
[4040425] | 245 | forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) ) |
---|
[90c3b1c] | 246 | void write_reverse( iteratortype begin, iteratortype end, ostype *os ) { |
---|
| 247 | void print( elttype i ) { os | i; } |
---|
[e56cfdb0] | 248 | for_each_reverse( begin, end, print ); |
---|
[cf16f94] | 249 | } // ?|? |
---|
[e56cfdb0] | 250 | |
---|
[6ba0659] | 251 | //--------------------------------------- |
---|
[e56cfdb0] | 252 | |
---|
[51b7345] | 253 | forall( dtype istype | istream( istype ) ) |
---|
[90c3b1c] | 254 | istype * ?|?( istype * is, char * c ) { |
---|
| 255 | scanfmt( is, "%c", c ); |
---|
| 256 | return is; |
---|
| 257 | } // ?|? |
---|
| 258 | |
---|
| 259 | forall( dtype istype | istream( istype ) ) |
---|
| 260 | istype * ?|?( istype * is, short int * si ) { |
---|
| 261 | scanfmt( is, "%hd", si ); |
---|
| 262 | return is; |
---|
| 263 | } // ?|? |
---|
| 264 | |
---|
| 265 | forall( dtype istype | istream( istype ) ) |
---|
| 266 | istype * ?|?( istype * is, unsigned short int * usi ) { |
---|
| 267 | scanfmt( is, "%hu", usi ); |
---|
| 268 | return is; |
---|
| 269 | } // ?|? |
---|
| 270 | |
---|
| 271 | forall( dtype istype | istream( istype ) ) |
---|
| 272 | istype * ?|?( istype * is, int * i ) { |
---|
| 273 | scanfmt( is, "%d", i ); |
---|
| 274 | return is; |
---|
| 275 | } // ?|? |
---|
| 276 | |
---|
| 277 | forall( dtype istype | istream( istype ) ) |
---|
| 278 | istype * ?|?( istype * is, unsigned int * ui ) { |
---|
| 279 | scanfmt( is, "%u", ui ); |
---|
| 280 | return is; |
---|
| 281 | } // ?|? |
---|
| 282 | |
---|
| 283 | forall( dtype istype | istream( istype ) ) |
---|
| 284 | istype * ?|?( istype * is, long int * li ) { |
---|
| 285 | scanfmt( is, "%ld", li ); |
---|
| 286 | return is; |
---|
[cf16f94] | 287 | } // ?|? |
---|
[51b7345] | 288 | |
---|
| 289 | forall( dtype istype | istream( istype ) ) |
---|
[90c3b1c] | 290 | istype * ?|?( istype * is, unsigned long int * ulli ) { |
---|
| 291 | scanfmt( is, "%lu", ulli ); |
---|
| 292 | return is; |
---|
| 293 | } // ?|? |
---|
| 294 | |
---|
| 295 | forall( dtype istype | istream( istype ) ) |
---|
| 296 | istype * ?|?( istype * is, long long int * lli ) { |
---|
| 297 | scanfmt( is, "%lld", lli ); |
---|
| 298 | return is; |
---|
| 299 | } // ?|? |
---|
| 300 | |
---|
| 301 | forall( dtype istype | istream( istype ) ) |
---|
| 302 | istype * ?|?( istype * is, unsigned long long int * ulli ) { |
---|
| 303 | scanfmt( is, "%llu", ulli ); |
---|
| 304 | return is; |
---|
| 305 | } // ?|? |
---|
| 306 | |
---|
| 307 | |
---|
| 308 | forall( dtype istype | istream( istype ) ) |
---|
| 309 | istype * ?|?( istype * is, float * f ) { |
---|
| 310 | scanfmt( is, "%f", f ); |
---|
| 311 | return is; |
---|
[cf16f94] | 312 | } // ?|? |
---|
[86bd7c1f] | 313 | |
---|
[90c3b1c] | 314 | forall( dtype istype | istream( istype ) ) |
---|
| 315 | istype * ?|?( istype * is, double * d ) { |
---|
| 316 | scanfmt( is, "%lf", d ); |
---|
| 317 | return is; |
---|
| 318 | } // ?|? |
---|
| 319 | |
---|
| 320 | forall( dtype istype | istream( istype ) ) |
---|
| 321 | istype * ?|?( istype * is, long double * ld ) { |
---|
| 322 | scanfmt( is, "%Lf", ld ); |
---|
| 323 | return is; |
---|
| 324 | } // ?|? |
---|
| 325 | |
---|
| 326 | |
---|
| 327 | forall( dtype istype | istream( istype ) ) |
---|
| 328 | istype * ?|?( istype * is, float _Complex * fc ) { |
---|
| 329 | float re, im; |
---|
| 330 | scanfmt( is, "%g%gi", &re, &im ); |
---|
| 331 | *fc = re + im * _Complex_I; |
---|
| 332 | return is; |
---|
| 333 | } // ?|? |
---|
| 334 | |
---|
| 335 | forall( dtype istype | istream( istype ) ) |
---|
| 336 | istype * ?|?( istype * is, double _Complex * dc ) { |
---|
| 337 | double re, im; |
---|
| 338 | scanfmt( is, "%lf%lfi", &re, &im ); |
---|
| 339 | *dc = re + im * _Complex_I; |
---|
| 340 | return is; |
---|
[cf16f94] | 341 | } // ?|? |
---|
[51b7345] | 342 | |
---|
| 343 | forall( dtype istype | istream( istype ) ) |
---|
[90c3b1c] | 344 | istype * ?|?( istype * is, long double _Complex * ldc ) { |
---|
| 345 | long double re, im; |
---|
| 346 | scanfmt( is, "%Lf%Lfi", &re, &im ); |
---|
| 347 | *ldc = re + im * _Complex_I; |
---|
| 348 | return is; |
---|
[cf16f94] | 349 | } // ?|? |
---|
[86bd7c1f] | 350 | |
---|
[e24f13a] | 351 | _Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; } |
---|
[90c3b1c] | 352 | forall( dtype istype | istream( istype ) ) |
---|
[53ba273] | 353 | istype * ?|?( istype * is, _Istream_cstrUC cstr ) { |
---|
| 354 | scanfmt( is, "%s", cstr.s ); |
---|
[90c3b1c] | 355 | return is; |
---|
[53ba273] | 356 | } // cstr |
---|
[90c3b1c] | 357 | |
---|
[e24f13a] | 358 | _Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; } |
---|
[90c3b1c] | 359 | forall( dtype istype | istream( istype ) ) |
---|
[53ba273] | 360 | istype * ?|?( istype * is, _Istream_cstrC cstr ) { |
---|
[90c3b1c] | 361 | char buf[16]; |
---|
[53ba273] | 362 | sprintf( buf, "%%%ds", cstr.size ); |
---|
| 363 | scanfmt( is, buf, cstr.s ); |
---|
[90c3b1c] | 364 | return is; |
---|
[53ba273] | 365 | } // cstr |
---|
[90c3b1c] | 366 | |
---|
[86bd7c1f] | 367 | // Local Variables: // |
---|
| 368 | // tab-width: 4 // |
---|
| 369 | // compile-command: "cfa iostream.c" // |
---|
| 370 | // End: // |
---|