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