source: libcfa/src/iostream.cfa@ 96df1329

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 96df1329 was 9d362a0, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

simplify code

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