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

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 2e60a1a was 4040425, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

change keyword type to otype and context to trait

  • Property mode set to 100644
File size: 8.4 KB
RevLine 
[86bd7c1f]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//
[90c3b1c]9// Author : Peter A. Buhr
[86bd7c1f]10// Created On : Wed May 27 17:56:53 2015
11// Last Modified By : Peter A. Buhr
[4040425]12// Last Modified On : Wed Mar 2 18:06:35 2016
13// Update Count : 208
[86bd7c1f]14//
[51b73452]15
[d3b7937]16#include "iostream"
17
[51b73452]18extern "C" {
19#include <stdio.h>
[839ccbb]20#include <string.h> // strlen
[52f85e0]21#include <float.h> // DBL_DIG, LDBL_DIG
[d3b7937]22#include <complex.h> // creal, cimag
[90c3b1c]23#include <ctype.h> // isspace, ispunct
[51b73452]24}
25
26forall( dtype ostype | ostream( ostype ) )
[cf16f94]27ostype * ?|?( ostype *os, char c ) {
[90c3b1c]28 prtfmt( os, "%c", c );
29 return os;
30} // ?|?
31
32forall( dtype ostype | ostream( ostype ) )
33ostype * ?|?( ostype *os, short int si ) {
34 if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
35 prtfmt( os, "%hd", si );
36 return os;
37} // ?|?
38
39forall( dtype ostype | ostream( ostype ) )
40ostype * ?|?( ostype *os, unsigned short int usi ) {
41 if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
42 prtfmt( os, "%hu", usi );
43 return os;
[cf16f94]44} // ?|?
[51b73452]45
46forall( dtype ostype | ostream( ostype ) )
[cf16f94]47ostype * ?|?( ostype *os, int i ) {
[90c3b1c]48 if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
49 prtfmt( os, "%d", i );
50 return os;
[784deab]51} // ?|?
52
53forall( dtype ostype | ostream( ostype ) )
[90c3b1c]54ostype * ?|?( ostype *os, unsigned int ui ) {
55 if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
56 prtfmt( os, "%u", ui );
57 return os;
[784deab]58} // ?|?
59
60forall( dtype ostype | ostream( ostype ) )
[90c3b1c]61ostype * ?|?( ostype *os, long int li ) {
62 if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
63 prtfmt( os, "%ld", li );
64 return os;
[784deab]65} // ?|?
66
67forall( dtype ostype | ostream( ostype ) )
[90c3b1c]68ostype * ?|?( ostype *os, unsigned long int uli ) {
69 if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
70 prtfmt( os, "%lu", uli );
71 return os;
[784deab]72} // ?|?
73
74forall( dtype ostype | ostream( ostype ) )
[90c3b1c]75ostype * ?|?( ostype *os, long long int lli ) {
76 if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
77 prtfmt( os, "%lld", lli );
78 return os;
[784deab]79} // ?|?
80
81forall( dtype ostype | ostream( ostype ) )
[90c3b1c]82ostype * ?|?( ostype *os, unsigned long long int ulli ) {
83 if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
84 prtfmt( os, "%llu", ulli );
85 return os;
[cf16f94]86} // ?|?
[51b73452]87
[d3b7937]88forall( dtype ostype | ostream( ostype ) )
89ostype * ?|?( ostype *os, float f ) {
[90c3b1c]90 if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
91 prtfmt( os, "%g", f );
92 return os;
[d3b7937]93} // ?|?
94
[51b73452]95forall( dtype ostype | ostream( ostype ) )
[cf16f94]96ostype * ?|?( ostype *os, double d ) {
[90c3b1c]97 if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
98 prtfmt( os, "%.*lg", DBL_DIG, d );
99 return os;
[cf16f94]100} // ?|?
[134b86a]101
102forall( dtype ostype | ostream( ostype ) )
[90c3b1c]103ostype * ?|?( ostype *os, long double ld ) {
104 if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
105 prtfmt( os, "%.*Lg", LDBL_DIG, ld );
106 return os;
[cf16f94]107} // ?|?
[51b73452]108
[d3b7937]109forall( dtype ostype | ostream( ostype ) )
[90c3b1c]110ostype * ?|?( ostype *os, float _Complex fc ) {
111 os | crealf( fc );
112 if ( cimagf( fc ) >= 0 ) os | '+';
113 os | "" | cimagf( fc ) | 'i';
114 return os;
[d3b7937]115} // ?|?
116
117forall( dtype ostype | ostream( ostype ) )
[90c3b1c]118ostype * ?|?( ostype *os, double _Complex dc ) {
119 os | creal( dc );
120 if ( cimag( dc ) >= 0 ) os | '+';
121 os | "" | cimag( dc ) | 'i';
122 return os;
[d3b7937]123} // ?|?
124
125forall( dtype ostype | ostream( ostype ) )
[90c3b1c]126ostype * ?|?( ostype *os, long double _Complex ldc ) {
127 os | creall( ldc );
128 if ( cimagl( ldc ) >= 0 ) os | '+';
129 os | "" | cimagl( ldc ) | 'i';
130 return os;
[d3b7937]131} // ?|?
132
[e56cfdb0]133forall( dtype ostype | ostream( ostype ) )
[90c3b1c]134ostype * ?|?( ostype *os, const char *cp ) {
135 int len = strlen( cp );
136 // null string => no separator
137 if ( len == 0 ) { sepOff( os ); return os; }
138 // first character NOT spacing or special punctuation => add left separator
139 if ( sepPrt( os ) && isspace( cp[0] ) == 0 && cp[0] != '.' && cp[0] != ',' ) {
140 prtfmt( os, "%s", sepGet( os ) );
141 } // if
142 // last character is spacing or special punctuation => turn off separator for next item
143 unsigned int posn = len - 1;
144 if ( isspace( cp[posn] ) || cp[posn] == ':' || cp[posn] == '$' ) {
145 sepOff( os );
146 } else {
147 sepOn( os );
148 } // if
149 return write( os, cp, len );
[784deab]150} // ?|?
151
152forall( dtype ostype | ostream( ostype ) )
[90c3b1c]153ostype * ?|?( ostype *os, const void *p ) {
154 if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
155 prtfmt( os, "%p", p );
156 return os;
[cf16f94]157} // ?|?
158
159
[90c3b1c]160forall( dtype ostype | ostream( ostype ) )
161ostype * ?|?( ostype *os, ostype * (* manip)( ostype * ) ) {
162 return manip( os );
163} // ?|?
[cf16f94]164
165forall( dtype ostype | ostream( ostype ) )
166ostype * endl( ostype * os ) {
[90c3b1c]167 os | '\n';
[6ba0659]168 flush( os );
[90c3b1c]169 sepOff( os );
[6ba0659]170 return os;
[cf16f94]171} // endl
[e56cfdb0]172
[90c3b1c]173forall( dtype ostype | ostream( ostype ) )
174ostype * sepOn( ostype * os ) {
175 sepOn( os );
176 return os;
177} // sepOn
178
179forall( dtype ostype | ostream( ostype ) )
180ostype * sepOff( ostype * os ) {
181 sepOff( os );
182 return os;
183} // sepOff
184
[6ba0659]185//---------------------------------------
186
[4040425]187forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
[90c3b1c]188void write( iteratortype begin, iteratortype end, ostype *os ) {
189 void print( elttype i ) { os | i; }
[e56cfdb0]190 for_each( begin, end, print );
[cf16f94]191} // ?|?
[e56cfdb0]192
[4040425]193forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
[90c3b1c]194void write_reverse( iteratortype begin, iteratortype end, ostype *os ) {
195 void print( elttype i ) { os | i; }
[e56cfdb0]196 for_each_reverse( begin, end, print );
[cf16f94]197} // ?|?
[e56cfdb0]198
[6ba0659]199//---------------------------------------
[e56cfdb0]200
[51b73452]201forall( dtype istype | istream( istype ) )
[90c3b1c]202istype * ?|?( istype * is, char * c ) {
203 scanfmt( is, "%c", c );
204 return is;
205} // ?|?
206
207forall( dtype istype | istream( istype ) )
208istype * ?|?( istype * is, short int * si ) {
209 scanfmt( is, "%hd", si );
210 return is;
211} // ?|?
212
213forall( dtype istype | istream( istype ) )
214istype * ?|?( istype * is, unsigned short int * usi ) {
215 scanfmt( is, "%hu", usi );
216 return is;
217} // ?|?
218
219forall( dtype istype | istream( istype ) )
220istype * ?|?( istype * is, int * i ) {
221 scanfmt( is, "%d", i );
222 return is;
223} // ?|?
224
225forall( dtype istype | istream( istype ) )
226istype * ?|?( istype * is, unsigned int * ui ) {
227 scanfmt( is, "%u", ui );
228 return is;
229} // ?|?
230
231forall( dtype istype | istream( istype ) )
232istype * ?|?( istype * is, long int * li ) {
233 scanfmt( is, "%ld", li );
234 return is;
235} // ?|?
236
237forall( dtype istype | istream( istype ) )
238istype * ?|?( istype * is, unsigned long int * ulli ) {
239 scanfmt( is, "%lu", ulli );
240 return is;
241} // ?|?
242
243forall( dtype istype | istream( istype ) )
244istype * ?|?( istype * is, long long int * lli ) {
245 scanfmt( is, "%lld", lli );
246 return is;
247} // ?|?
248
249forall( dtype istype | istream( istype ) )
250istype * ?|?( istype * is, unsigned long long int * ulli ) {
251 scanfmt( is, "%llu", ulli );
252 return is;
253} // ?|?
254
255
256forall( dtype istype | istream( istype ) )
257istype * ?|?( istype * is, float * f ) {
258 scanfmt( is, "%f", f );
259 return is;
260} // ?|?
261
262forall( dtype istype | istream( istype ) )
263istype * ?|?( istype * is, double * d ) {
264 scanfmt( is, "%lf", d );
265 return is;
266} // ?|?
267
268forall( dtype istype | istream( istype ) )
269istype * ?|?( istype * is, long double * ld ) {
270 scanfmt( is, "%Lf", ld );
271 return is;
272} // ?|?
273
274
275forall( dtype istype | istream( istype ) )
276istype * ?|?( istype * is, float _Complex * fc ) {
277 float re, im;
278 scanfmt( is, "%g%gi", &re, &im );
279 *fc = re + im * _Complex_I;
280 return is;
281} // ?|?
282
283forall( dtype istype | istream( istype ) )
284istype * ?|?( istype * is, double _Complex * dc ) {
285 double re, im;
286 scanfmt( is, "%lf%lfi", &re, &im );
287 *dc = re + im * _Complex_I;
288 return is;
[cf16f94]289} // ?|?
[51b73452]290
291forall( dtype istype | istream( istype ) )
[90c3b1c]292istype * ?|?( istype * is, long double _Complex * ldc ) {
293 long double re, im;
294 scanfmt( is, "%Lf%Lfi", &re, &im );
295 *ldc = re + im * _Complex_I;
296 return is;
[cf16f94]297} // ?|?
[86bd7c1f]298
[90c3b1c]299_Istream_str1 str( char * s ) { _Istream_str1 s = { s }; return s; }
300forall( dtype istype | istream( istype ) )
301istype * ?|?( istype * is, _Istream_str1 str ) {
302 scanfmt( is, "%s", str.s );
303 return is;
304} // str
305
306_Istream_str2 str( char * s, int size ) { _Istream_str2 s = { s, size }; return s; }
307forall( dtype istype | istream( istype ) )
308istype * ?|?( istype * is, _Istream_str2 str ) {
309 char buf[16];
310 sprintf( buf, "%%%ds", str.size );
311 scanfmt( is, buf, str.s );
312 return is;
313} // str
314
[86bd7c1f]315// Local Variables: //
316// tab-width: 4 //
317// compile-command: "cfa iostream.c" //
318// End: //
Note: See TracBrowser for help on using the repository browser.