[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 | //
|
---|
| 7 | // iostream.c --
|
---|
| 8 | //
|
---|
[90c3b1c] | 9 | // Author : Peter A. Buhr
|
---|
[86bd7c1f] | 10 | // Created On : Wed May 27 17:56:53 2015
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[b63e376] | 12 | // Last Modified On : Mon Mar 7 13:51:23 2016
|
---|
| 13 | // Update Count : 227
|
---|
[86bd7c1f] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
[d3b7937] | 16 | #include "iostream"
|
---|
| 17 |
|
---|
[51b73452] | 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
|
---|
[90c3b1c] | 23 | #include <ctype.h> // isspace, ispunct
|
---|
[51b73452] | 24 | }
|
---|
| 25 |
|
---|
| 26 | forall( dtype ostype | ostream( ostype ) )
|
---|
[cf16f94] | 27 | ostype * ?|?( ostype *os, char c ) {
|
---|
[90c3b1c] | 28 | prtfmt( os, "%c", c );
|
---|
| 29 | return os;
|
---|
| 30 | } // ?|?
|
---|
| 31 |
|
---|
| 32 | forall( dtype ostype | ostream( ostype ) )
|
---|
| 33 | ostype * ?|?( ostype *os, short int si ) {
|
---|
| 34 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
|
---|
| 35 | prtfmt( os, "%hd", si );
|
---|
| 36 | return os;
|
---|
| 37 | } // ?|?
|
---|
| 38 |
|
---|
| 39 | forall( dtype ostype | ostream( ostype ) )
|
---|
| 40 | ostype * ?|?( ostype *os, unsigned short int usi ) {
|
---|
| 41 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
|
---|
| 42 | prtfmt( os, "%hu", usi );
|
---|
| 43 | return os;
|
---|
[cf16f94] | 44 | } // ?|?
|
---|
[51b73452] | 45 |
|
---|
| 46 | forall( dtype ostype | ostream( ostype ) )
|
---|
[cf16f94] | 47 | ostype * ?|?( ostype *os, int i ) {
|
---|
[90c3b1c] | 48 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
|
---|
| 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 ) {
|
---|
| 55 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
|
---|
| 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 ) {
|
---|
| 62 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
|
---|
| 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 ) {
|
---|
| 69 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
|
---|
| 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 ) {
|
---|
| 76 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
|
---|
| 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 ) {
|
---|
| 83 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
|
---|
| 84 | prtfmt( os, "%llu", ulli );
|
---|
| 85 | return os;
|
---|
[cf16f94] | 86 | } // ?|?
|
---|
[51b73452] | 87 |
|
---|
[d3b7937] | 88 | forall( dtype ostype | ostream( ostype ) )
|
---|
| 89 | ostype * ?|?( ostype *os, float f ) {
|
---|
[90c3b1c] | 90 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
|
---|
| 91 | prtfmt( os, "%g", f );
|
---|
| 92 | return os;
|
---|
[d3b7937] | 93 | } // ?|?
|
---|
| 94 |
|
---|
[51b73452] | 95 | forall( dtype ostype | ostream( ostype ) )
|
---|
[cf16f94] | 96 | ostype * ?|?( ostype *os, double d ) {
|
---|
[90c3b1c] | 97 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
|
---|
| 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 ) {
|
---|
| 104 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
|
---|
| 105 | prtfmt( os, "%.*Lg", LDBL_DIG, ld );
|
---|
| 106 | return os;
|
---|
[cf16f94] | 107 | } // ?|?
|
---|
[51b73452] | 108 |
|
---|
[d3b7937] | 109 | forall( dtype ostype | ostream( ostype ) )
|
---|
[90c3b1c] | 110 | ostype * ?|?( ostype *os, float _Complex fc ) {
|
---|
| 111 | os | crealf( fc );
|
---|
| 112 | if ( cimagf( fc ) >= 0 ) os | '+';
|
---|
| 113 | os | "" | cimagf( fc ) | 'i';
|
---|
| 114 | return os;
|
---|
[d3b7937] | 115 | } // ?|?
|
---|
| 116 |
|
---|
| 117 | forall( dtype ostype | ostream( ostype ) )
|
---|
[90c3b1c] | 118 | ostype * ?|?( ostype *os, double _Complex dc ) {
|
---|
| 119 | os | creal( dc );
|
---|
| 120 | if ( cimag( dc ) >= 0 ) os | '+';
|
---|
| 121 | os | "" | cimag( dc ) | 'i';
|
---|
| 122 | return os;
|
---|
[d3b7937] | 123 | } // ?|?
|
---|
| 124 |
|
---|
| 125 | forall( dtype ostype | ostream( ostype ) )
|
---|
[90c3b1c] | 126 | ostype * ?|?( ostype *os, long double _Complex ldc ) {
|
---|
| 127 | os | creall( ldc );
|
---|
| 128 | if ( cimagl( ldc ) >= 0 ) os | '+';
|
---|
| 129 | os | "" | cimagl( ldc ) | 'i';
|
---|
| 130 | return os;
|
---|
[d3b7937] | 131 | } // ?|?
|
---|
| 132 |
|
---|
[e56cfdb0] | 133 | forall( dtype ostype | ostream( ostype ) )
|
---|
[90c3b1c] | 134 | ostype * ?|?( ostype *os, const char *cp ) {
|
---|
[b63e376] | 135 | enum { Open = 1, Close, OpenClose };
|
---|
| 136 | static const char mask[256] = {
|
---|
| 137 | // opening delimiters
|
---|
| 138 | ['('] : Open, ['['] : Open, ['{'] : Open,
|
---|
| 139 | ['$'] : Open, [L'£'] : Open, [L'¥'] : Open, [L'¢'] : Open, [L'¿'] : Open, [L'«'] : Open,
|
---|
| 140 | // closing delimiters
|
---|
| 141 | [','] : Close, ['.'] : Close, [':'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
|
---|
| 142 | [')'] : Close, [']'] : Close, ['}'] : Close,
|
---|
| 143 | ['%'] : Close, [L'»'] : Close,
|
---|
| 144 | // opening-closing delimiters
|
---|
| 145 | ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose,
|
---|
| 146 | }; // mask
|
---|
| 147 |
|
---|
[90c3b1c] | 148 | int len = strlen( cp );
|
---|
| 149 | // null string => no separator
|
---|
| 150 | if ( len == 0 ) { sepOff( os ); return os; }
|
---|
[b63e376] | 151 | // first character NOT spacing or closing punctuation => add left separator
|
---|
| 152 | if ( sepPrt( os ) && isspace( cp[0] ) == 0 && mask[ cp[0] ] != Close && mask[ cp[0] ] != OpenClose ) {
|
---|
[90c3b1c] | 153 | prtfmt( os, "%s", sepGet( os ) );
|
---|
| 154 | } // if
|
---|
[b63e376] | 155 | // last character IS spacing or opening punctuation => turn off separator for next item
|
---|
[90c3b1c] | 156 | unsigned int posn = len - 1;
|
---|
[b63e376] | 157 | if ( isspace( cp[posn] ) || mask[ cp[posn] ] == Open || mask[ cp[posn] ] == OpenClose ) {
|
---|
[90c3b1c] | 158 | sepOff( os );
|
---|
| 159 | } else {
|
---|
| 160 | sepOn( os );
|
---|
| 161 | } // if
|
---|
| 162 | return write( os, cp, len );
|
---|
[784deab] | 163 | } // ?|?
|
---|
| 164 |
|
---|
| 165 | forall( dtype ostype | ostream( ostype ) )
|
---|
[90c3b1c] | 166 | ostype * ?|?( ostype *os, const void *p ) {
|
---|
| 167 | if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
|
---|
| 168 | prtfmt( os, "%p", p );
|
---|
| 169 | return os;
|
---|
[cf16f94] | 170 | } // ?|?
|
---|
| 171 |
|
---|
| 172 |
|
---|
[90c3b1c] | 173 | forall( dtype ostype | ostream( ostype ) )
|
---|
| 174 | ostype * ?|?( ostype *os, ostype * (* manip)( ostype * ) ) {
|
---|
| 175 | return manip( os );
|
---|
| 176 | } // ?|?
|
---|
[cf16f94] | 177 |
|
---|
| 178 | forall( dtype ostype | ostream( ostype ) )
|
---|
| 179 | ostype * endl( ostype * os ) {
|
---|
[90c3b1c] | 180 | os | '\n';
|
---|
[6ba0659] | 181 | flush( os );
|
---|
[90c3b1c] | 182 | sepOff( os );
|
---|
[6ba0659] | 183 | return os;
|
---|
[cf16f94] | 184 | } // endl
|
---|
[e56cfdb0] | 185 |
|
---|
[90c3b1c] | 186 | forall( dtype ostype | ostream( ostype ) )
|
---|
| 187 | ostype * sepOn( ostype * os ) {
|
---|
| 188 | sepOn( os );
|
---|
| 189 | return os;
|
---|
| 190 | } // sepOn
|
---|
| 191 |
|
---|
| 192 | forall( dtype ostype | ostream( ostype ) )
|
---|
| 193 | ostype * sepOff( ostype * os ) {
|
---|
| 194 | sepOff( os );
|
---|
| 195 | return os;
|
---|
| 196 | } // sepOff
|
---|
| 197 |
|
---|
[6ba0659] | 198 | //---------------------------------------
|
---|
| 199 |
|
---|
[4040425] | 200 | forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
|
---|
[90c3b1c] | 201 | void write( iteratortype begin, iteratortype end, ostype *os ) {
|
---|
| 202 | void print( elttype i ) { os | i; }
|
---|
[e56cfdb0] | 203 | for_each( begin, end, print );
|
---|
[cf16f94] | 204 | } // ?|?
|
---|
[e56cfdb0] | 205 |
|
---|
[4040425] | 206 | forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
|
---|
[90c3b1c] | 207 | void write_reverse( iteratortype begin, iteratortype end, ostype *os ) {
|
---|
| 208 | void print( elttype i ) { os | i; }
|
---|
[e56cfdb0] | 209 | for_each_reverse( begin, end, print );
|
---|
[cf16f94] | 210 | } // ?|?
|
---|
[e56cfdb0] | 211 |
|
---|
[6ba0659] | 212 | //---------------------------------------
|
---|
[e56cfdb0] | 213 |
|
---|
[51b73452] | 214 | forall( dtype istype | istream( istype ) )
|
---|
[90c3b1c] | 215 | istype * ?|?( istype * is, char * c ) {
|
---|
| 216 | scanfmt( is, "%c", c );
|
---|
| 217 | return is;
|
---|
| 218 | } // ?|?
|
---|
| 219 |
|
---|
| 220 | forall( dtype istype | istream( istype ) )
|
---|
| 221 | istype * ?|?( istype * is, short int * si ) {
|
---|
| 222 | scanfmt( is, "%hd", si );
|
---|
| 223 | return is;
|
---|
| 224 | } // ?|?
|
---|
| 225 |
|
---|
| 226 | forall( dtype istype | istream( istype ) )
|
---|
| 227 | istype * ?|?( istype * is, unsigned short int * usi ) {
|
---|
| 228 | scanfmt( is, "%hu", usi );
|
---|
| 229 | return is;
|
---|
| 230 | } // ?|?
|
---|
| 231 |
|
---|
| 232 | forall( dtype istype | istream( istype ) )
|
---|
| 233 | istype * ?|?( istype * is, int * i ) {
|
---|
| 234 | scanfmt( is, "%d", i );
|
---|
| 235 | return is;
|
---|
| 236 | } // ?|?
|
---|
| 237 |
|
---|
| 238 | forall( dtype istype | istream( istype ) )
|
---|
| 239 | istype * ?|?( istype * is, unsigned int * ui ) {
|
---|
| 240 | scanfmt( is, "%u", ui );
|
---|
| 241 | return is;
|
---|
| 242 | } // ?|?
|
---|
| 243 |
|
---|
| 244 | forall( dtype istype | istream( istype ) )
|
---|
| 245 | istype * ?|?( istype * is, long int * li ) {
|
---|
| 246 | scanfmt( is, "%ld", li );
|
---|
| 247 | return is;
|
---|
| 248 | } // ?|?
|
---|
| 249 |
|
---|
| 250 | forall( dtype istype | istream( istype ) )
|
---|
| 251 | istype * ?|?( istype * is, unsigned long int * ulli ) {
|
---|
| 252 | scanfmt( is, "%lu", ulli );
|
---|
| 253 | return is;
|
---|
| 254 | } // ?|?
|
---|
| 255 |
|
---|
| 256 | forall( dtype istype | istream( istype ) )
|
---|
| 257 | istype * ?|?( istype * is, long long int * lli ) {
|
---|
| 258 | scanfmt( is, "%lld", lli );
|
---|
| 259 | return is;
|
---|
| 260 | } // ?|?
|
---|
| 261 |
|
---|
| 262 | forall( dtype istype | istream( istype ) )
|
---|
| 263 | istype * ?|?( istype * is, unsigned long long int * ulli ) {
|
---|
| 264 | scanfmt( is, "%llu", ulli );
|
---|
| 265 | return is;
|
---|
| 266 | } // ?|?
|
---|
| 267 |
|
---|
| 268 |
|
---|
| 269 | forall( dtype istype | istream( istype ) )
|
---|
| 270 | istype * ?|?( istype * is, float * f ) {
|
---|
| 271 | scanfmt( is, "%f", f );
|
---|
| 272 | return is;
|
---|
| 273 | } // ?|?
|
---|
| 274 |
|
---|
| 275 | forall( dtype istype | istream( istype ) )
|
---|
| 276 | istype * ?|?( istype * is, double * d ) {
|
---|
| 277 | scanfmt( is, "%lf", d );
|
---|
| 278 | return is;
|
---|
| 279 | } // ?|?
|
---|
| 280 |
|
---|
| 281 | forall( dtype istype | istream( istype ) )
|
---|
| 282 | istype * ?|?( istype * is, long double * ld ) {
|
---|
| 283 | scanfmt( is, "%Lf", ld );
|
---|
| 284 | return is;
|
---|
| 285 | } // ?|?
|
---|
| 286 |
|
---|
| 287 |
|
---|
| 288 | forall( dtype istype | istream( istype ) )
|
---|
| 289 | istype * ?|?( istype * is, float _Complex * fc ) {
|
---|
| 290 | float re, im;
|
---|
| 291 | scanfmt( is, "%g%gi", &re, &im );
|
---|
| 292 | *fc = re + im * _Complex_I;
|
---|
| 293 | return is;
|
---|
| 294 | } // ?|?
|
---|
| 295 |
|
---|
| 296 | forall( dtype istype | istream( istype ) )
|
---|
| 297 | istype * ?|?( istype * is, double _Complex * dc ) {
|
---|
| 298 | double re, im;
|
---|
| 299 | scanfmt( is, "%lf%lfi", &re, &im );
|
---|
| 300 | *dc = re + im * _Complex_I;
|
---|
| 301 | return is;
|
---|
[cf16f94] | 302 | } // ?|?
|
---|
[51b73452] | 303 |
|
---|
| 304 | forall( dtype istype | istream( istype ) )
|
---|
[90c3b1c] | 305 | istype * ?|?( istype * is, long double _Complex * ldc ) {
|
---|
| 306 | long double re, im;
|
---|
| 307 | scanfmt( is, "%Lf%Lfi", &re, &im );
|
---|
| 308 | *ldc = re + im * _Complex_I;
|
---|
| 309 | return is;
|
---|
[cf16f94] | 310 | } // ?|?
|
---|
[86bd7c1f] | 311 |
|
---|
[90c3b1c] | 312 | _Istream_str1 str( char * s ) { _Istream_str1 s = { s }; return s; }
|
---|
| 313 | forall( dtype istype | istream( istype ) )
|
---|
| 314 | istype * ?|?( istype * is, _Istream_str1 str ) {
|
---|
| 315 | scanfmt( is, "%s", str.s );
|
---|
| 316 | return is;
|
---|
| 317 | } // str
|
---|
| 318 |
|
---|
| 319 | _Istream_str2 str( char * s, int size ) { _Istream_str2 s = { s, size }; return s; }
|
---|
| 320 | forall( dtype istype | istream( istype ) )
|
---|
| 321 | istype * ?|?( istype * is, _Istream_str2 str ) {
|
---|
| 322 | char buf[16];
|
---|
| 323 | sprintf( buf, "%%%ds", str.size );
|
---|
| 324 | scanfmt( is, buf, str.s );
|
---|
| 325 | return is;
|
---|
| 326 | } // str
|
---|
| 327 |
|
---|
[86bd7c1f] | 328 | // Local Variables: //
|
---|
| 329 | // tab-width: 4 //
|
---|
| 330 | // compile-command: "cfa iostream.c" //
|
---|
| 331 | // End: //
|
---|