source: src/libcfa/iostream.c @ ed0e67a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since ed0e67a was e24f13a, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

modify makefile to allow different compilers, use constructor cast in iostream

  • Property mode set to 100644
File size: 9.9 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 May 26 10:08:31 2016
13// Update Count     : 305
14//
15
16#include "iostream"
17
18extern "C" {
19#include <stdio.h>
20#include <string.h>                                                                             // strlen
21#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
22#include <complex.h>                                                                    // creal, cimag
23}
24
25forall( dtype ostype | ostream( ostype ) )
26ostype * ?|?( ostype *os, char c ) {
27        prtfmt( os, "%c", c );
28        sepOff( os );
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 ) );
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 ) );
42        prtfmt( os, "%hu", usi );
43        return os;
44} // ?|?
45
46forall( dtype ostype | ostream( ostype ) )
47ostype * ?|?( ostype *os, int i ) {
48        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
49        prtfmt( os, "%d", i );
50        return os;
51} // ?|?
52
53forall( dtype ostype | ostream( ostype ) )
54ostype * ?|?( ostype *os, unsigned int ui ) {
55        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
56        prtfmt( os, "%u", ui );
57        return os;
58} // ?|?
59
60forall( dtype ostype | ostream( ostype ) )
61ostype * ?|?( ostype *os, long int li ) {
62        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
63        prtfmt( os, "%ld", li );
64        return os;
65} // ?|?
66
67forall( dtype ostype | ostream( ostype ) )
68ostype * ?|?( ostype *os, unsigned long int uli ) {
69        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
70        prtfmt( os, "%lu", uli );
71        return os;
72} // ?|?
73
74forall( dtype ostype | ostream( ostype ) )
75ostype * ?|?( ostype *os, long long int lli ) {
76        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
77        prtfmt( os, "%lld", lli );
78        return os;
79} // ?|?
80
81forall( dtype ostype | ostream( ostype ) )
82ostype * ?|?( ostype *os, unsigned long long int ulli ) {
83        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
84        prtfmt( os, "%llu", ulli );
85        return os;
86} // ?|?
87
88forall( dtype ostype | ostream( ostype ) )
89ostype * ?|?( ostype *os, float f ) {
90        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
91        prtfmt( os, "%g", f );
92        return os;
93} // ?|?
94
95forall( dtype ostype | ostream( ostype ) )
96ostype * ?|?( ostype *os, double d ) {
97        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
98        prtfmt( os, "%.*lg", DBL_DIG, d );
99        return os;
100} // ?|?
101
102forall( dtype ostype | ostream( ostype ) )
103ostype * ?|?( ostype *os, long double ld ) {
104        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
105        prtfmt( os, "%.*Lg", LDBL_DIG, ld );
106        return os;
107} // ?|?
108
109forall( dtype ostype | ostream( ostype ) )
110ostype * ?|?( ostype *os, float _Complex fc ) {
111        os | crealf( fc );
112        _Bool temp = sepDisable( os );                                          // disable separators within complex value
113        if ( cimagf( fc ) >= 0 ) os | '+';                                      // negative value prints '-'
114        os | cimagf( fc ) | 'i';
115        sepReset( os, temp );                                                           // reset separator
116        return os;
117} // ?|?
118
119forall( dtype ostype | ostream( ostype ) )
120ostype * ?|?( ostype *os, double _Complex dc ) {
121        os | creal( dc );
122        _Bool temp = sepDisable( os );                                          // disable separators within complex value
123        if ( cimag( dc ) >= 0 ) os | '+';                                       // negative value prints '-'
124        os | cimag( dc ) | 'i';
125        sepReset( os, temp );                                                           // reset separator
126        return os;
127} // ?|?
128
129forall( dtype ostype | ostream( ostype ) )
130ostype * ?|?( ostype *os, long double _Complex ldc ) {
131        os | creall( ldc );
132        _Bool temp = sepDisable( os );                                          // disable separators within complex value
133        if ( cimagl( ldc ) >= 0 ) os | '+';                                     // negative value prints '-'
134        os | cimagl( ldc ) | 'i';
135        sepReset( os, temp );                                                           // reset separator
136        return os;
137} // ?|?
138
139forall( dtype ostype | ostream( ostype ) )
140ostype * ?|?( ostype *os, const char *cp ) {
141        enum { Open = 1, Close, OpenClose };
142        static const unsigned char mask[256] = {
143                // opening delimiters
144                ['('] : Open, ['['] : Open, ['{'] : Open,
145                ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
146                [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
147                // closing delimiters
148                [','] : Close, ['.'] : Close, [':'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
149                [')'] : Close, [']'] : Close, ['}'] : Close,
150                ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
151                // opening-closing delimiters
152                ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose,
153                [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
154        }; // mask
155
156  if ( cp[0] == '\0' ) { sepOff( os ); return os; }             // null string => no separator
157
158        // first character IS NOT spacing or closing punctuation => add left separator
159        unsigned char ch = cp[0];                                                       // must make unsigned
160        if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
161                prtfmt( os, "%s", sepGet( os ) );
162        } // if
163
164        // if string starts line, must reset to determine open state because separator is off
165        sepReset( os );                                                                         // reset separator
166
167        // last character IS spacing or opening punctuation => turn off separator for next item
168        unsigned int len = strlen( cp ), posn = len - 1;
169        ch = cp[posn];                                                                          // must make unsigned
170        if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
171                sepOn( os );
172        } else {
173                sepOff( os );
174        } // if
175        return write( os, cp, len );
176} // ?|?
177
178forall( dtype ostype | ostream( ostype ) )
179ostype * ?|?( ostype *os, const void *p ) {
180        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
181        prtfmt( os, "%p", p );
182        return os;
183} // ?|?
184
185
186forall( dtype ostype | ostream( ostype ) )
187ostype * ?|?( ostype *os, ostype * (* manip)( ostype * ) ) {
188        return manip( os );
189} // ?|?
190
191forall( dtype ostype | ostream( ostype ) )
192ostype * endl( ostype * os ) {
193        os | '\n';
194        flush( os );
195        sepOff( os );
196        return os;
197} // endl
198
199forall( dtype ostype | ostream( ostype ) )
200ostype * sepOn( ostype * os ) {
201        sepOn( os );
202        return os;
203} // sepOn
204
205forall( dtype ostype | ostream( ostype ) )
206ostype * sepOff( ostype * os ) {
207        sepOff( os );
208        return os;
209} // sepOff
210
211forall( dtype ostype | ostream( ostype ) )
212ostype * sepEnable( ostype * os ) {
213        sepEnable( os );
214        return os;
215} // sepEnable
216
217forall( dtype ostype | ostream( ostype ) )
218ostype * sepDisable( ostype * os ) {
219        sepDisable( os );
220        return os;
221} // sepDisable
222
223//---------------------------------------
224
225forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
226void write( iteratortype begin, iteratortype end, ostype *os ) {
227        void print( elttype i ) { os | i; }
228        for_each( begin, end, print );
229} // ?|?
230
231forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
232void write_reverse( iteratortype begin, iteratortype end, ostype *os ) {
233        void print( elttype i ) { os | i; }
234        for_each_reverse( begin, end, print );
235} // ?|?
236
237//---------------------------------------
238
239forall( dtype istype | istream( istype ) )
240istype * ?|?( istype * is, char * c ) {
241        scanfmt( is, "%c", c );
242        return is;
243} // ?|?
244
245forall( dtype istype | istream( istype ) )
246istype * ?|?( istype * is, short int * si ) {
247        scanfmt( is, "%hd", si );
248        return is;
249} // ?|?
250
251forall( dtype istype | istream( istype ) )
252istype * ?|?( istype * is, unsigned short int * usi ) {
253        scanfmt( is, "%hu", usi );
254        return is;
255} // ?|?
256
257forall( dtype istype | istream( istype ) )
258istype * ?|?( istype * is, int * i ) {
259        scanfmt( is, "%d", i );
260        return is;
261} // ?|?
262
263forall( dtype istype | istream( istype ) )
264istype * ?|?( istype * is, unsigned int * ui ) {
265        scanfmt( is, "%u", ui );
266        return is;
267} // ?|?
268
269forall( dtype istype | istream( istype ) )
270istype * ?|?( istype * is, long int * li ) {
271        scanfmt( is, "%ld", li );
272        return is;
273} // ?|?
274
275forall( dtype istype | istream( istype ) )
276istype * ?|?( istype * is, unsigned long int * ulli ) {
277        scanfmt( is, "%lu", ulli );
278        return is;
279} // ?|?
280
281forall( dtype istype | istream( istype ) )
282istype * ?|?( istype * is, long long int * lli ) {
283        scanfmt( is, "%lld", lli );
284        return is;
285} // ?|?
286
287forall( dtype istype | istream( istype ) )
288istype * ?|?( istype * is, unsigned long long int * ulli ) {
289        scanfmt( is, "%llu", ulli );
290        return is;
291} // ?|?
292
293
294forall( dtype istype | istream( istype ) )
295istype * ?|?( istype * is, float * f ) {
296        scanfmt( is, "%f", f );
297        return is;
298} // ?|?
299
300forall( dtype istype | istream( istype ) )
301istype * ?|?( istype * is, double * d ) {
302        scanfmt( is, "%lf", d );
303        return is;
304} // ?|?
305
306forall( dtype istype | istream( istype ) )
307istype * ?|?( istype * is, long double * ld ) {
308        scanfmt( is, "%Lf", ld );
309        return is;
310} // ?|?
311
312
313forall( dtype istype | istream( istype ) )
314istype * ?|?( istype * is, float _Complex * fc ) {
315        float re, im;
316        scanfmt( is, "%g%gi", &re, &im );
317        *fc = re + im * _Complex_I;
318        return is;
319} // ?|?
320
321forall( dtype istype | istream( istype ) )
322istype * ?|?( istype * is, double _Complex * dc ) {
323        double re, im;
324        scanfmt( is, "%lf%lfi", &re, &im );
325        *dc = re + im * _Complex_I;
326        return is;
327} // ?|?
328
329forall( dtype istype | istream( istype ) )
330istype * ?|?( istype * is, long double _Complex * ldc ) {
331        long double re, im;
332        scanfmt( is, "%Lf%Lfi", &re, &im );
333        *ldc = re + im * _Complex_I;
334        return is;
335} // ?|?
336
337_Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; }
338forall( dtype istype | istream( istype ) )
339istype * ?|?( istype * is, _Istream_cstrUC cstr ) {
340        scanfmt( is, "%s", cstr.s );
341        return is;
342} // cstr
343
344_Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; }
345forall( dtype istype | istream( istype ) )
346istype * ?|?( istype * is, _Istream_cstrC cstr ) {
347        char buf[16];
348        sprintf( buf, "%%%ds", cstr.size );
349        scanfmt( is, buf, cstr.s );
350        return is;
351} // cstr
352
353// Local Variables: //
354// tab-width: 4 //
355// compile-command: "cfa iostream.c" //
356// End: //
Note: See TracBrowser for help on using the repository browser.