source: src/libcfa/iostream.c@ a180ded

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since a180ded was 09687aa, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

complete conversion of iostream/fstream to use references

  • Property mode set to 100644
File size: 11.8 KB
RevLine 
[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
[09687aa]12// Last Modified On : Wed Dec 6 23:03:43 2017
13// Update Count : 426
[86bd7c1f]14//
[51b73452]15
[d3b7937]16#include "iostream"
17
[51b73452]18extern "C" {
19#include <stdio.h>
[53a6c2a]20#include <stdbool.h> // true/false
[839ccbb]21#include <string.h> // strlen
[52f85e0]22#include <float.h> // DBL_DIG, LDBL_DIG
[d3b7937]23#include <complex.h> // creal, cimag
[51b73452]24}
25
26forall( dtype ostype | ostream( ostype ) )
[09687aa]27ostype & ?|?( ostype & os, char ch ) {
[53a6c2a]28 fmt( os, "%c", ch );
29 if ( ch == '\n' ) setNL( os, true );
[53ba273]30 sepOff( os );
[90c3b1c]31 return os;
32} // ?|?
33
[1630f18]34forall( dtype ostype | ostream( ostype ) )
[09687aa]35ostype & ?|?( ostype & os, signed char c ) {
[3e239ea]36 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
[829c907]37 fmt( os, "%hhd", c );
[1630f18]38 return os;
39} // ?|?
40
41forall( dtype ostype | ostream( ostype ) )
[09687aa]42ostype & ?|?( ostype & os, unsigned char c ) {
[3e239ea]43 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
[829c907]44 fmt( os, "%hhu", c );
[1630f18]45 return os;
46} // ?|?
47
[90c3b1c]48forall( dtype ostype | ostream( ostype ) )
[09687aa]49ostype & ?|?( ostype & os, short int si ) {
[829c907]50 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
51 fmt( os, "%hd", si );
[90c3b1c]52 return os;
53} // ?|?
54
55forall( dtype ostype | ostream( ostype ) )
[09687aa]56ostype & ?|?( ostype & os, unsigned short int usi ) {
[829c907]57 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
58 fmt( os, "%hu", usi );
[90c3b1c]59 return os;
[cf16f94]60} // ?|?
[51b73452]61
62forall( dtype ostype | ostream( ostype ) )
[09687aa]63ostype & ?|?( ostype & os, int i ) {
[829c907]64 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
65 fmt( os, "%d", i );
[90c3b1c]66 return os;
[784deab]67} // ?|?
68
69forall( dtype ostype | ostream( ostype ) )
[09687aa]70ostype & ?|?( ostype & os, unsigned int ui ) {
[829c907]71 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
72 fmt( os, "%u", ui );
[90c3b1c]73 return os;
[784deab]74} // ?|?
75
76forall( dtype ostype | ostream( ostype ) )
[09687aa]77ostype & ?|?( ostype & os, long int li ) {
[829c907]78 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
79 fmt( os, "%ld", li );
[90c3b1c]80 return os;
[784deab]81} // ?|?
82
83forall( dtype ostype | ostream( ostype ) )
[09687aa]84ostype & ?|?( ostype & os, unsigned long int uli ) {
[829c907]85 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
86 fmt( os, "%lu", uli );
[90c3b1c]87 return os;
[784deab]88} // ?|?
89
90forall( dtype ostype | ostream( ostype ) )
[09687aa]91ostype & ?|?( ostype & os, long long int lli ) {
[829c907]92 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
93 fmt( os, "%lld", lli );
[90c3b1c]94 return os;
[784deab]95} // ?|?
96
97forall( dtype ostype | ostream( ostype ) )
[09687aa]98ostype & ?|?( ostype & os, unsigned long long int ulli ) {
[829c907]99 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
100 fmt( os, "%llu", ulli );
[90c3b1c]101 return os;
[cf16f94]102} // ?|?
[51b73452]103
[d3b7937]104forall( dtype ostype | ostream( ostype ) )
[09687aa]105ostype & ?|?( ostype & os, float f ) {
[829c907]106 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
107 fmt( os, "%g", f );
[90c3b1c]108 return os;
[d3b7937]109} // ?|?
110
[51b73452]111forall( dtype ostype | ostream( ostype ) )
[09687aa]112ostype & ?|?( ostype & os, double d ) {
[829c907]113 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
114 fmt( os, "%.*lg", DBL_DIG, d );
[90c3b1c]115 return os;
[cf16f94]116} // ?|?
[134b86a]117
118forall( dtype ostype | ostream( ostype ) )
[09687aa]119ostype & ?|?( ostype & os, long double ld ) {
[829c907]120 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
121 fmt( os, "%.*Lg", LDBL_DIG, ld );
[90c3b1c]122 return os;
[cf16f94]123} // ?|?
[51b73452]124
[d3b7937]125forall( dtype ostype | ostream( ostype ) )
[09687aa]126ostype & ?|?( ostype & os, float _Complex fc ) {
[17954f0e]127 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
128 fmt( os, "%g%+gi", crealf( fc ), cimagf( fc ) );
[90c3b1c]129 return os;
[d3b7937]130} // ?|?
131
132forall( dtype ostype | ostream( ostype ) )
[09687aa]133ostype & ?|?( ostype & os, double _Complex dc ) {
[17954f0e]134 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
135 fmt( os, "%.*lg%+.*lgi", DBL_DIG, creal( dc ), DBL_DIG, cimag( dc ) );
[90c3b1c]136 return os;
[d3b7937]137} // ?|?
138
139forall( dtype ostype | ostream( ostype ) )
[09687aa]140ostype & ?|?( ostype & os, long double _Complex ldc ) {
[17954f0e]141 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
142 fmt( os, "%.*Lg%+.*Lgi", LDBL_DIG, creall( ldc ), LDBL_DIG, cimagl( ldc ) );
[90c3b1c]143 return os;
[d3b7937]144} // ?|?
145
[e56cfdb0]146forall( dtype ostype | ostream( ostype ) )
[09687aa]147ostype & ?|?( ostype & os, const char * str ) {
[b63e376]148 enum { Open = 1, Close, OpenClose };
[a4dd728]149 static const unsigned char mask[256] @= {
[c443d1d]150 // opening delimiters, no space after
[b63e376]151 ['('] : Open, ['['] : Open, ['{'] : Open,
[cb91437]152 ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
[e945826]153 [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
[c443d1d]154 // closing delimiters, no space before
[b65a9fc]155 [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
[53ba273]156 ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
[829c907]157 [')'] : Close, [']'] : Close, ['}'] : Close,
[c443d1d]158 // opening-closing delimiters, no space before or after
[b65a9fc]159 ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose,
[e945826]160 [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
[b63e376]161 }; // mask
162
[6de9f4a]163 if ( str[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator
[b72bad4f]164
[53ba273]165 // first character IS NOT spacing or closing punctuation => add left separator
[6de9f4a]166 unsigned char ch = str[0]; // must make unsigned
[53ba273]167 if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
[829c907]168 fmt( os, "%s", sepGetCur( os ) );
[90c3b1c]169 } // if
[b72bad4f]170
171 // if string starts line, must reset to determine open state because separator is off
172 sepReset( os ); // reset separator
173
[b63e376]174 // last character IS spacing or opening punctuation => turn off separator for next item
[6de9f4a]175 size_t len = strlen( str );
176 ch = str[len - 1]; // must make unsigned
[b72bad4f]177 if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
[90c3b1c]178 sepOn( os );
[b72bad4f]179 } else {
180 sepOff( os );
[90c3b1c]181 } // if
[53a6c2a]182 if ( ch == '\n' ) setNL( os, true ); // check *AFTER* sepPrt call above as it resets NL flag
[6de9f4a]183 return write( os, str, len );
184} // ?|?
185
186forall( dtype ostype | ostream( ostype ) )
[09687aa]187ostype & ?|?( ostype & os, const char16_t * str ) {
[6de9f4a]188 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
189 fmt( os, "%ls", str );
190 return os;
191} // ?|?
192
[0db817e]193#if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
[6de9f4a]194forall( dtype ostype | ostream( ostype ) )
[09687aa]195ostype & ?|?( ostype & os, const char32_t * str ) {
[6de9f4a]196 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
197 fmt( os, "%ls", str );
198 return os;
199} // ?|?
[0db817e]200#endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
[6de9f4a]201
202forall( dtype ostype | ostream( ostype ) )
[09687aa]203ostype & ?|?( ostype & os, const wchar_t * str ) {
[6de9f4a]204 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
205 fmt( os, "%ls", str );
206 return os;
[784deab]207} // ?|?
208
209forall( dtype ostype | ostream( ostype ) )
[09687aa]210ostype & ?|?( ostype & os, const void * p ) {
[829c907]211 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
212 fmt( os, "%p", p );
[90c3b1c]213 return os;
[cf16f94]214} // ?|?
215
216
[c443d1d]217// tuples
[09687aa]218forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } )
219ostype & ?|?( ostype & os, T arg, Params rest ) {
[0583064b]220 os | arg; // print first argument
[86f384b]221 sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator
[0583064b]222 os | rest; // print remaining arguments
[829c907]223 sepSetCur( os, sepGet( os ) ); // switch to regular separator
[cb91437]224 return os;
[c443d1d]225} // ?|?
226
227
228// manipulators
[a5a71d0]229forall( dtype ostype | ostream( ostype ) )
[09687aa]230ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
[90c3b1c]231 return manip( os );
232} // ?|?
[cf16f94]233
[53a6c2a]234forall( dtype ostype | ostream( ostype ) )
[09687aa]235ostype & sep( ostype & os ) {
[53a6c2a]236 os | sepGet( os );
237 return os;
238} // sep
239
240forall( dtype ostype | ostream( ostype ) )
[09687aa]241ostype & sepTuple( ostype & os ) {
[53a6c2a]242 os | sepGetTuple( os );
243 return os;
244} // sepTuple
245
[a5a71d0]246forall( dtype ostype | ostream( ostype ) )
[09687aa]247ostype & endl( ostype & os ) {
[90c3b1c]248 os | '\n';
[53a6c2a]249 setNL( os, true );
[6ba0659]250 flush( os );
[0583064b]251 sepOff( os ); // prepare for next line
[6ba0659]252 return os;
[cf16f94]253} // endl
[e56cfdb0]254
[a5a71d0]255forall( dtype ostype | ostream( ostype ) )
[09687aa]256ostype & sepOn( ostype & os ) {
[90c3b1c]257 sepOn( os );
258 return os;
259} // sepOn
260
[a5a71d0]261forall( dtype ostype | ostream( ostype ) )
[09687aa]262ostype & sepOff( ostype & os ) {
[90c3b1c]263 sepOff( os );
264 return os;
265} // sepOff
266
[70a06f6]267forall( dtype ostype | ostream( ostype ) )
[09687aa]268ostype & sepEnable( ostype & os ) {
[53ba273]269 sepEnable( os );
270 return os;
271} // sepEnable
272
[70a06f6]273forall( dtype ostype | ostream( ostype ) )
[09687aa]274ostype & sepDisable( ostype & os ) {
[53ba273]275 sepDisable( os );
276 return os;
277} // sepDisable
278
[6ba0659]279//---------------------------------------
280
[e1780a2]281forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) )
[09687aa]282void write( iterator_type begin, iterator_type end, ostype & os ) {
[e1780a2]283 void print( elt_type i ) { os | i; }
[e56cfdb0]284 for_each( begin, end, print );
[cf16f94]285} // ?|?
[e56cfdb0]286
[e1780a2]287forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) )
[09687aa]288void write_reverse( iterator_type begin, iterator_type end, ostype & os ) {
[e1780a2]289 void print( elt_type i ) { os | i; }
[e56cfdb0]290 for_each_reverse( begin, end, print );
[cf16f94]291} // ?|?
[e56cfdb0]292
[6ba0659]293//---------------------------------------
[e56cfdb0]294
[51b73452]295forall( dtype istype | istream( istype ) )
[09687aa]296istype & ?|?( istype & is, char & c ) {
[7bc4e6b]297 fmt( is, "%c", &c ); // must pass pointer through varg to fmt
[90c3b1c]298 return is;
299} // ?|?
300
[6de9f4a]301forall( dtype istype | istream( istype ) )
[09687aa]302istype & ?|?( istype & is, signed char & sc ) {
[6de9f4a]303 fmt( is, "%hhd", &sc );
304 return is;
305} // ?|?
306
307forall( dtype istype | istream( istype ) )
[09687aa]308istype & ?|?( istype & is, unsigned char & usc ) {
[6de9f4a]309 fmt( is, "%hhu", &usc );
310 return is;
311} // ?|?
312
[90c3b1c]313forall( dtype istype | istream( istype ) )
[09687aa]314istype & ?|?( istype & is, short int & si ) {
[7bc4e6b]315 fmt( is, "%hd", &si );
[90c3b1c]316 return is;
317} // ?|?
318
319forall( dtype istype | istream( istype ) )
[09687aa]320istype & ?|?( istype & is, unsigned short int & usi ) {
[7bc4e6b]321 fmt( is, "%hu", &usi );
[90c3b1c]322 return is;
323} // ?|?
324
325forall( dtype istype | istream( istype ) )
[09687aa]326istype & ?|?( istype & is, int & i ) {
[7bc4e6b]327 fmt( is, "%d", &i );
[90c3b1c]328 return is;
329} // ?|?
330
331forall( dtype istype | istream( istype ) )
[09687aa]332istype & ?|?( istype & is, unsigned int & ui ) {
[7bc4e6b]333 fmt( is, "%u", &ui );
[90c3b1c]334 return is;
335} // ?|?
336
337forall( dtype istype | istream( istype ) )
[09687aa]338istype & ?|?( istype & is, long int & li ) {
[7bc4e6b]339 fmt( is, "%ld", &li );
[90c3b1c]340 return is;
[cf16f94]341} // ?|?
[51b73452]342
343forall( dtype istype | istream( istype ) )
[09687aa]344istype & ?|?( istype & is, unsigned long int & ulli ) {
[7bc4e6b]345 fmt( is, "%lu", &ulli );
[90c3b1c]346 return is;
347} // ?|?
348
349forall( dtype istype | istream( istype ) )
[09687aa]350istype & ?|?( istype & is, long long int & lli ) {
[7bc4e6b]351 fmt( is, "%lld", &lli );
[90c3b1c]352 return is;
353} // ?|?
354
355forall( dtype istype | istream( istype ) )
[09687aa]356istype & ?|?( istype & is, unsigned long long int & ulli ) {
[7bc4e6b]357 fmt( is, "%llu", &ulli );
[90c3b1c]358 return is;
359} // ?|?
360
361
362forall( dtype istype | istream( istype ) )
[09687aa]363istype & ?|?( istype & is, float & f ) {
[7bc4e6b]364 fmt( is, "%f", &f );
[90c3b1c]365 return is;
[cf16f94]366} // ?|?
[86bd7c1f]367
[90c3b1c]368forall( dtype istype | istream( istype ) )
[09687aa]369istype & ?|?( istype & is, double & d ) {
[7bc4e6b]370 fmt( is, "%lf", &d );
[90c3b1c]371 return is;
372} // ?|?
373
374forall( dtype istype | istream( istype ) )
[09687aa]375istype & ?|?( istype & is, long double & ld ) {
[7bc4e6b]376 fmt( is, "%Lf", &ld );
[90c3b1c]377 return is;
378} // ?|?
379
380
381forall( dtype istype | istream( istype ) )
[09687aa]382istype & ?|?( istype & is, float _Complex & fc ) {
[90c3b1c]383 float re, im;
[829c907]384 fmt( is, "%g%gi", &re, &im );
[7bc4e6b]385 fc = re + im * _Complex_I;
[90c3b1c]386 return is;
387} // ?|?
388
389forall( dtype istype | istream( istype ) )
[09687aa]390istype & ?|?( istype & is, double _Complex & dc ) {
[90c3b1c]391 double re, im;
[829c907]392 fmt( is, "%lf%lfi", &re, &im );
[7bc4e6b]393 dc = re + im * _Complex_I;
[90c3b1c]394 return is;
[cf16f94]395} // ?|?
[51b73452]396
397forall( dtype istype | istream( istype ) )
[09687aa]398istype & ?|?( istype & is, long double _Complex & ldc ) {
[90c3b1c]399 long double re, im;
[829c907]400 fmt( is, "%Lf%Lfi", &re, &im );
[7bc4e6b]401 ldc = re + im * _Complex_I;
[90c3b1c]402 return is;
[cf16f94]403} // ?|?
[86bd7c1f]404
[e24f13a]405_Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; }
[90c3b1c]406forall( dtype istype | istream( istype ) )
[09687aa]407istype & ?|?( istype & is, _Istream_cstrUC cstr ) {
[829c907]408 fmt( is, "%s", cstr.s );
[90c3b1c]409 return is;
[53ba273]410} // cstr
[90c3b1c]411
[e24f13a]412_Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; }
[90c3b1c]413forall( dtype istype | istream( istype ) )
[09687aa]414istype & ?|?( istype & is, _Istream_cstrC cstr ) {
[90c3b1c]415 char buf[16];
[53ba273]416 sprintf( buf, "%%%ds", cstr.size );
[829c907]417 fmt( is, buf, cstr.s );
[90c3b1c]418 return is;
[53ba273]419} // cstr
[90c3b1c]420
[86bd7c1f]421// Local Variables: //
422// tab-width: 4 //
423// compile-command: "cfa iostream.c" //
424// End: //
Note: See TracBrowser for help on using the repository browser.