source: src/libcfa/iostream.c@ ed235b6

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 ed235b6 was 6de9f4a, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add I/O for signed/unsigned char/short, and output for char16_t, char32_t, wchar_t

  • Property mode set to 100644
File size: 11.6 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 Sep 11 09:21:24 2017
13// Update Count : 420
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 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
193forall( dtype ostype | ostream( ostype ) )
194ostype * ?|?( ostype * os, const char32_t * str ) {
195 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
196 fmt( os, "%ls", str );
197 return os;
198} // ?|?
199
200forall( dtype ostype | ostream( ostype ) )
201ostype * ?|?( ostype * os, const wchar_t * str ) {
202 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
203 fmt( os, "%ls", str );
204 return os;
205} // ?|?
206
207forall( dtype ostype | ostream( ostype ) )
208ostype * ?|?( ostype * os, const void * p ) {
209 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
210 fmt( os, "%p", p );
211 return os;
212} // ?|?
213
214
215// tuples
216forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype * ?|?( ostype *, Params ); } )
217ostype * ?|?( ostype * os, T arg, Params rest ) {
218 os | arg; // print first argument
219 sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator
220 os | rest; // print remaining arguments
221 sepSetCur( os, sepGet( os ) ); // switch to regular separator
222 return os;
223} // ?|?
224
225
226// manipulators
227forall( dtype ostype | ostream( ostype ) )
228ostype * ?|?( ostype * os, ostype * (* manip)( ostype * ) ) {
229 return manip( os );
230} // ?|?
231
232forall( dtype ostype | ostream( ostype ) )
233ostype * sep( ostype * os ) {
234 os | sepGet( os );
235 return os;
236} // sep
237
238forall( dtype ostype | ostream( ostype ) )
239ostype * sepTuple( ostype * os ) {
240 os | sepGetTuple( os );
241 return os;
242} // sepTuple
243
244forall( dtype ostype | ostream( ostype ) )
245ostype * endl( ostype * os ) {
246 os | '\n';
247 setNL( os, true );
248 flush( os );
249 sepOff( os ); // prepare for next line
250 return os;
251} // endl
252
253forall( dtype ostype | ostream( ostype ) )
254ostype * sepOn( ostype * os ) {
255 sepOn( os );
256 return os;
257} // sepOn
258
259forall( dtype ostype | ostream( ostype ) )
260ostype * sepOff( ostype * os ) {
261 sepOff( os );
262 return os;
263} // sepOff
264
265forall( dtype ostype | ostream( ostype ) )
266ostype * sepEnable( ostype * os ) {
267 sepEnable( os );
268 return os;
269} // sepEnable
270
271forall( dtype ostype | ostream( ostype ) )
272ostype * sepDisable( ostype * os ) {
273 sepDisable( os );
274 return os;
275} // sepDisable
276
277//---------------------------------------
278
279forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) )
280void write( iterator_type begin, iterator_type end, ostype * os ) {
281 void print( elt_type i ) { os | i; }
282 for_each( begin, end, print );
283} // ?|?
284
285forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) )
286void write_reverse( iterator_type begin, iterator_type end, ostype * os ) {
287 void print( elt_type i ) { os | i; }
288 for_each_reverse( begin, end, print );
289} // ?|?
290
291//---------------------------------------
292
293forall( dtype istype | istream( istype ) )
294istype * ?|?( istype * is, char & c ) {
295 fmt( is, "%c", &c ); // must pass pointer through varg to fmt
296 return is;
297} // ?|?
298
299forall( dtype istype | istream( istype ) )
300istype * ?|?( istype * is, signed char & sc ) {
301 fmt( is, "%hhd", &sc );
302 return is;
303} // ?|?
304
305forall( dtype istype | istream( istype ) )
306istype * ?|?( istype * is, unsigned char & usc ) {
307 fmt( is, "%hhu", &usc );
308 return is;
309} // ?|?
310
311forall( dtype istype | istream( istype ) )
312istype * ?|?( istype * is, short int & si ) {
313 fmt( is, "%hd", &si );
314 return is;
315} // ?|?
316
317forall( dtype istype | istream( istype ) )
318istype * ?|?( istype * is, unsigned short int & usi ) {
319 fmt( is, "%hu", &usi );
320 return is;
321} // ?|?
322
323forall( dtype istype | istream( istype ) )
324istype * ?|?( istype * is, int & i ) {
325 fmt( is, "%d", &i );
326 return is;
327} // ?|?
328
329forall( dtype istype | istream( istype ) )
330istype * ?|?( istype * is, unsigned int & ui ) {
331 fmt( is, "%u", &ui );
332 return is;
333} // ?|?
334
335forall( dtype istype | istream( istype ) )
336istype * ?|?( istype * is, long int & li ) {
337 fmt( is, "%ld", &li );
338 return is;
339} // ?|?
340
341forall( dtype istype | istream( istype ) )
342istype * ?|?( istype * is, unsigned long int & ulli ) {
343 fmt( is, "%lu", &ulli );
344 return is;
345} // ?|?
346
347forall( dtype istype | istream( istype ) )
348istype * ?|?( istype * is, long long int & lli ) {
349 fmt( is, "%lld", &lli );
350 return is;
351} // ?|?
352
353forall( dtype istype | istream( istype ) )
354istype * ?|?( istype * is, unsigned long long int & ulli ) {
355 fmt( is, "%llu", &ulli );
356 return is;
357} // ?|?
358
359
360forall( dtype istype | istream( istype ) )
361istype * ?|?( istype * is, float & f ) {
362 fmt( is, "%f", &f );
363 return is;
364} // ?|?
365
366forall( dtype istype | istream( istype ) )
367istype * ?|?( istype * is, double & d ) {
368 fmt( is, "%lf", &d );
369 return is;
370} // ?|?
371
372forall( dtype istype | istream( istype ) )
373istype * ?|?( istype * is, long double & ld ) {
374 fmt( is, "%Lf", &ld );
375 return is;
376} // ?|?
377
378
379forall( dtype istype | istream( istype ) )
380istype * ?|?( istype * is, float _Complex & fc ) {
381 float re, im;
382 fmt( is, "%g%gi", &re, &im );
383 fc = re + im * _Complex_I;
384 return is;
385} // ?|?
386
387forall( dtype istype | istream( istype ) )
388istype * ?|?( istype * is, double _Complex & dc ) {
389 double re, im;
390 fmt( is, "%lf%lfi", &re, &im );
391 dc = re + im * _Complex_I;
392 return is;
393} // ?|?
394
395forall( dtype istype | istream( istype ) )
396istype * ?|?( istype * is, long double _Complex & ldc ) {
397 long double re, im;
398 fmt( is, "%Lf%Lfi", &re, &im );
399 ldc = re + im * _Complex_I;
400 return is;
401} // ?|?
402
403_Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; }
404forall( dtype istype | istream( istype ) )
405istype * ?|?( istype * is, _Istream_cstrUC cstr ) {
406 fmt( is, "%s", cstr.s );
407 return is;
408} // cstr
409
410_Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; }
411forall( dtype istype | istream( istype ) )
412istype * ?|?( istype * is, _Istream_cstrC cstr ) {
413 char buf[16];
414 sprintf( buf, "%%%ds", cstr.size );
415 fmt( is, buf, cstr.s );
416 return is;
417} // cstr
418
419// Local Variables: //
420// tab-width: 4 //
421// compile-command: "cfa iostream.c" //
422// End: //
Note: See TracBrowser for help on using the repository browser.