source: src/libcfa/iostream.c@ 50f0ba9

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