source: src/libcfa/iostream.c@ 2edd5502

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory 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 2edd5502 was fbfde843, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

Merge branch 'master' into ctor

Conflicts:

src/libcfa/fstream.c
src/libcfa/iostream.c

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