source: src/libcfa/iostream.c@ 3f414ef

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 3f414ef was 2988eeb, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

iosteam can now print tuples, added = as opening delimiter for printing strings

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