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