source: src/libcfa/iostream.c@ 0fc9756b

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 0fc9756b was 53a6c2a, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

change meaning of sepOn, and replace #if with #pragma once in include files

  • Property mode set to 100644
File size: 11.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 : Thu Jul 6 18:14:17 2017
13// Update Count : 396
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 fmt( os, "%hhd", c );
37 sepOff( os );
38 return os;
39} // ?|?
40
41forall( dtype ostype | ostream( ostype ) )
42ostype * ?|?( ostype * os, unsigned char c ) {
43 fmt( os, "%hhu", c );
44 sepOff( os );
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 os | crealf( fc );
128 _Bool temp = sepDisable( os ); // disable separators within complex value
129 if ( cimagf( fc ) >= 0 ) os | '+'; // negative value prints '-'
130 os | cimagf( fc ) | 'i';
131 sepReset( os, temp ); // reset separator
132 return os;
133} // ?|?
134
135forall( dtype ostype | ostream( ostype ) )
136ostype * ?|?( ostype * os, double _Complex dc ) {
137 os | creal( dc );
138 _Bool temp = sepDisable( os ); // disable separators within complex value
139 if ( cimag( dc ) >= 0 ) os | '+'; // negative value prints '-'
140 os | cimag( dc ) | 'i';
141 sepReset( os, temp ); // reset separator
142 return os;
143} // ?|?
144
145forall( dtype ostype | ostream( ostype ) )
146ostype * ?|?( ostype * os, long double _Complex ldc ) {
147 os | creall( ldc );
148 _Bool temp = sepDisable( os ); // disable separators within complex value
149 if ( cimagl( ldc ) >= 0 ) os | '+'; // negative value prints '-'
150 os | cimagl( ldc ) | 'i';
151 sepReset( os, temp ); // reset separator
152 return os;
153} // ?|?
154
155forall( dtype ostype | ostream( ostype ) )
156ostype * ?|?( ostype * os, const char * cp ) {
157 enum { Open = 1, Close, OpenClose };
158 static const unsigned char mask[256] @= {
159 // opening delimiters, no space after
160 ['('] : Open, ['['] : Open, ['{'] : Open,
161 ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
162 [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
163 // closing delimiters, no space before
164 [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
165 ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
166 [')'] : Close, [']'] : Close, ['}'] : Close,
167 // opening-closing delimiters, no space before or after
168 ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose,
169 [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
170 }; // mask
171
172 if ( cp[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator
173
174 // first character IS NOT spacing or closing punctuation => add left separator
175 unsigned char ch = cp[0]; // must make unsigned
176 if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
177 fmt( os, "%s", sepGetCur( os ) );
178 } // if
179
180 // if string starts line, must reset to determine open state because separator is off
181 sepReset( os ); // reset separator
182
183 // last character IS spacing or opening punctuation => turn off separator for next item
184 size_t len = strlen( cp );
185 ch = cp[len - 1]; // must make unsigned
186 if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
187 sepOn( os );
188 } else {
189 sepOff( os );
190 } // if
191 if ( ch == '\n' ) setNL( os, true ); // check *AFTER* sepPrt call above as it resets NL flag
192 return write( os, cp, len );
193} // ?|?
194
195forall( dtype ostype | ostream( ostype ) )
196ostype * ?|?( ostype * os, const void * p ) {
197 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
198 fmt( os, "%p", p );
199 return os;
200} // ?|?
201
202
203// tuples
204forall( dtype ostype, otype T, ttype Params | ostream( ostype ) | writeable( T ) | { ostype * ?|?( ostype *, Params ); } )
205ostype * ?|?( ostype * os, T arg, Params rest ) {
206 os | arg; // print first argument
207 sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator
208 os | rest; // print remaining arguments
209 sepSetCur( os, sepGet( os ) ); // switch to regular separator
210 return os;
211} // ?|?
212
213
214// manipulators
215forall( dtype ostype | ostream( ostype ) )
216ostype * ?|?( ostype * os, ostype * (* manip)( ostype * ) ) {
217 return manip( os );
218} // ?|?
219
220forall( dtype ostype | ostream( ostype ) )
221ostype * sep( ostype * os ) {
222 os | sepGet( os );
223 return os;
224} // sep
225
226forall( dtype ostype | ostream( ostype ) )
227ostype * sepTuple( ostype * os ) {
228 os | sepGetTuple( os );
229 return os;
230} // sepTuple
231
232forall( dtype ostype | ostream( ostype ) )
233ostype * endl( ostype * os ) {
234 os | '\n';
235 setNL( os, true );
236 flush( os );
237 sepOff( os ); // prepare for next line
238 return os;
239} // endl
240
241forall( dtype ostype | ostream( ostype ) )
242ostype * sepOn( ostype * os ) {
243 sepOn( os );
244 return os;
245} // sepOn
246
247forall( dtype ostype | ostream( ostype ) )
248ostype * sepOff( ostype * os ) {
249 sepOff( os );
250 return os;
251} // sepOff
252
253forall( dtype ostype | ostream( ostype ) )
254ostype * sepEnable( ostype * os ) {
255 sepEnable( os );
256 return os;
257} // sepEnable
258
259forall( dtype ostype | ostream( ostype ) )
260ostype * sepDisable( ostype * os ) {
261 sepDisable( os );
262 return os;
263} // sepDisable
264
265//---------------------------------------
266
267forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
268void write( iteratortype begin, iteratortype end, ostype * os ) {
269 void print( elttype i ) { os | i; }
270 for_each( begin, end, print );
271} // ?|?
272
273forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
274void write_reverse( iteratortype begin, iteratortype end, ostype * os ) {
275 void print( elttype i ) { os | i; }
276 for_each_reverse( begin, end, print );
277} // ?|?
278
279//---------------------------------------
280
281forall( dtype istype | istream( istype ) )
282istype * ?|?( istype * is, char * c ) {
283 fmt( is, "%c", c );
284 return is;
285} // ?|?
286
287forall( dtype istype | istream( istype ) )
288istype * ?|?( istype * is, short int * si ) {
289 fmt( is, "%hd", si );
290 return is;
291} // ?|?
292
293forall( dtype istype | istream( istype ) )
294istype * ?|?( istype * is, unsigned short int * usi ) {
295 fmt( is, "%hu", usi );
296 return is;
297} // ?|?
298
299forall( dtype istype | istream( istype ) )
300istype * ?|?( istype * is, int * i ) {
301 fmt( is, "%d", i );
302 return is;
303} // ?|?
304
305forall( dtype istype | istream( istype ) )
306istype * ?|?( istype * is, unsigned int * ui ) {
307 fmt( is, "%u", ui );
308 return is;
309} // ?|?
310
311forall( dtype istype | istream( istype ) )
312istype * ?|?( istype * is, long int * li ) {
313 fmt( is, "%ld", li );
314 return is;
315} // ?|?
316
317forall( dtype istype | istream( istype ) )
318istype * ?|?( istype * is, unsigned long int * ulli ) {
319 fmt( is, "%lu", ulli );
320 return is;
321} // ?|?
322
323forall( dtype istype | istream( istype ) )
324istype * ?|?( istype * is, long long int * lli ) {
325 fmt( is, "%lld", lli );
326 return is;
327} // ?|?
328
329forall( dtype istype | istream( istype ) )
330istype * ?|?( istype * is, unsigned long long int * ulli ) {
331 fmt( is, "%llu", ulli );
332 return is;
333} // ?|?
334
335
336forall( dtype istype | istream( istype ) )
337istype * ?|?( istype * is, float * f ) {
338 fmt( is, "%f", f );
339 return is;
340} // ?|?
341
342forall( dtype istype | istream( istype ) )
343istype * ?|?( istype * is, double * d ) {
344 fmt( is, "%lf", d );
345 return is;
346} // ?|?
347
348forall( dtype istype | istream( istype ) )
349istype * ?|?( istype * is, long double * ld ) {
350 fmt( is, "%Lf", ld );
351 return is;
352} // ?|?
353
354
355forall( dtype istype | istream( istype ) )
356istype * ?|?( istype * is, float _Complex * fc ) {
357 float re, im;
358 fmt( is, "%g%gi", &re, &im );
359 *fc = re + im * _Complex_I;
360 return is;
361} // ?|?
362
363forall( dtype istype | istream( istype ) )
364istype * ?|?( istype * is, double _Complex * dc ) {
365 double re, im;
366 fmt( is, "%lf%lfi", &re, &im );
367 *dc = re + im * _Complex_I;
368 return is;
369} // ?|?
370
371forall( dtype istype | istream( istype ) )
372istype * ?|?( istype * is, long double _Complex * ldc ) {
373 long double re, im;
374 fmt( is, "%Lf%Lfi", &re, &im );
375 *ldc = re + im * _Complex_I;
376 return is;
377} // ?|?
378
379_Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; }
380forall( dtype istype | istream( istype ) )
381istype * ?|?( istype * is, _Istream_cstrUC cstr ) {
382 fmt( is, "%s", cstr.s );
383 return is;
384} // cstr
385
386_Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; }
387forall( dtype istype | istream( istype ) )
388istype * ?|?( istype * is, _Istream_cstrC cstr ) {
389 char buf[16];
390 sprintf( buf, "%%%ds", cstr.size );
391 fmt( is, buf, cstr.s );
392 return is;
393} // cstr
394
395// Local Variables: //
396// tab-width: 4 //
397// compile-command: "cfa iostream.c" //
398// End: //
Note: See TracBrowser for help on using the repository browser.