source: src/libcfa/iostream.c@ cfc3e0f

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 with_gc
Last change on this file since cfc3e0f was b6dc097, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

temporarily remove printing of char16_t, char_32_t and wchar_t until they become unique types

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