source: src/libcfa/iostream.c @ 1630f18

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

print signed and unsigned char

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