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

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 2e9aed4 was 1e6e08de, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

print "true" and "false" for _Bool

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