source: src/libcfa/iostream.c@ ff56efdf

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 ff56efdf was 17954f0e, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

simplify printing complex values

  • Property mode set to 100644
File size: 10.7 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//
[a5a71d0]7// iostream.c --
[86bd7c1f]8//
[90c3b1c]9// Author : Peter A. Buhr
[86bd7c1f]10// Created On : Wed May 27 17:56:53 2015
[e24f13a]11// Last Modified By : Peter A. Buhr
[17954f0e]12// Last Modified On : Sun Jul 16 21:12:03 2017
13// Update Count : 398
[86bd7c1f]14//
[51b73452]15
[d3b7937]16#include "iostream"
17
[51b73452]18extern "C" {
19#include <stdio.h>
[53a6c2a]20#include <stdbool.h> // true/false
[839ccbb]21#include <string.h> // strlen
[52f85e0]22#include <float.h> // DBL_DIG, LDBL_DIG
[d3b7937]23#include <complex.h> // creal, cimag
[51b73452]24}
25
26forall( dtype ostype | ostream( ostype ) )
[53a6c2a]27ostype * ?|?( ostype * os, char ch ) {
28 fmt( os, "%c", ch );
29 if ( ch == '\n' ) setNL( os, true );
[53ba273]30 sepOff( os );
[90c3b1c]31 return os;
32} // ?|?
33
[1630f18]34forall( dtype ostype | ostream( ostype ) )
[829c907]35ostype * ?|?( ostype * os, signed char c ) {
36 fmt( os, "%hhd", c );
[1630f18]37 sepOff( os );
38 return os;
39} // ?|?
40
41forall( dtype ostype | ostream( ostype ) )
[829c907]42ostype * ?|?( ostype * os, unsigned char c ) {
43 fmt( os, "%hhu", c );
[1630f18]44 sepOff( os );
45 return os;
46} // ?|?
47
[90c3b1c]48forall( dtype ostype | ostream( ostype ) )
[829c907]49ostype * ?|?( ostype * os, short int si ) {
50 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
51 fmt( os, "%hd", si );
[90c3b1c]52 return os;
53} // ?|?
54
55forall( dtype ostype | ostream( ostype ) )
[829c907]56ostype * ?|?( ostype * os, unsigned short int usi ) {
57 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
58 fmt( os, "%hu", usi );
[90c3b1c]59 return os;
[cf16f94]60} // ?|?
[51b73452]61
62forall( dtype ostype | ostream( ostype ) )
[829c907]63ostype * ?|?( ostype * os, int i ) {
64 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
65 fmt( os, "%d", i );
[90c3b1c]66 return os;
[784deab]67} // ?|?
68
69forall( dtype ostype | ostream( ostype ) )
[829c907]70ostype * ?|?( ostype * os, unsigned int ui ) {
71 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
72 fmt( os, "%u", ui );
[90c3b1c]73 return os;
[784deab]74} // ?|?
75
76forall( dtype ostype | ostream( ostype ) )
[829c907]77ostype * ?|?( ostype * os, long int li ) {
78 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
79 fmt( os, "%ld", li );
[90c3b1c]80 return os;
[784deab]81} // ?|?
82
83forall( dtype ostype | ostream( ostype ) )
[829c907]84ostype * ?|?( ostype * os, unsigned long int uli ) {
85 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
86 fmt( os, "%lu", uli );
[90c3b1c]87 return os;
[784deab]88} // ?|?
89
90forall( dtype ostype | ostream( ostype ) )
[829c907]91ostype * ?|?( ostype * os, long long int lli ) {
92 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
93 fmt( os, "%lld", lli );
[90c3b1c]94 return os;
[784deab]95} // ?|?
96
97forall( dtype ostype | ostream( ostype ) )
[829c907]98ostype * ?|?( ostype * os, unsigned long long int ulli ) {
99 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
100 fmt( os, "%llu", ulli );
[90c3b1c]101 return os;
[cf16f94]102} // ?|?
[51b73452]103
[d3b7937]104forall( dtype ostype | ostream( ostype ) )
[829c907]105ostype * ?|?( ostype * os, float f ) {
106 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
107 fmt( os, "%g", f );
[90c3b1c]108 return os;
[d3b7937]109} // ?|?
110
[51b73452]111forall( dtype ostype | ostream( ostype ) )
[829c907]112ostype * ?|?( ostype * os, double d ) {
113 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
114 fmt( os, "%.*lg", DBL_DIG, d );
[90c3b1c]115 return os;
[cf16f94]116} // ?|?
[134b86a]117
118forall( dtype ostype | ostream( ostype ) )
[829c907]119ostype * ?|?( ostype * os, long double ld ) {
120 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
121 fmt( os, "%.*Lg", LDBL_DIG, ld );
[90c3b1c]122 return os;
[cf16f94]123} // ?|?
[51b73452]124
[d3b7937]125forall( dtype ostype | ostream( ostype ) )
[829c907]126ostype * ?|?( ostype * os, float _Complex fc ) {
[17954f0e]127 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
128 fmt( os, "%g%+gi", crealf( fc ), cimagf( fc ) );
[90c3b1c]129 return os;
[d3b7937]130} // ?|?
131
132forall( dtype ostype | ostream( ostype ) )
[829c907]133ostype * ?|?( ostype * os, double _Complex dc ) {
[17954f0e]134 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
135 fmt( os, "%.*lg%+.*lgi", DBL_DIG, creal( dc ), DBL_DIG, cimag( dc ) );
[90c3b1c]136 return os;
[d3b7937]137} // ?|?
138
139forall( dtype ostype | ostream( ostype ) )
[829c907]140ostype * ?|?( ostype * os, long double _Complex ldc ) {
[17954f0e]141 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
142 fmt( os, "%.*Lg%+.*Lgi", LDBL_DIG, creall( ldc ), LDBL_DIG, cimagl( ldc ) );
[90c3b1c]143 return os;
[d3b7937]144} // ?|?
145
[e56cfdb0]146forall( dtype ostype | ostream( ostype ) )
[829c907]147ostype * ?|?( ostype * os, const char * cp ) {
[b63e376]148 enum { Open = 1, Close, OpenClose };
[a4dd728]149 static const unsigned char mask[256] @= {
[c443d1d]150 // opening delimiters, no space after
[b63e376]151 ['('] : Open, ['['] : Open, ['{'] : Open,
[cb91437]152 ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
[e945826]153 [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
[c443d1d]154 // closing delimiters, no space before
[b65a9fc]155 [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
[53ba273]156 ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
[829c907]157 [')'] : Close, [']'] : Close, ['}'] : Close,
[c443d1d]158 // opening-closing delimiters, no space before or after
[b65a9fc]159 ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose,
[e945826]160 [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
[b63e376]161 }; // mask
162
[b72bad4f]163 if ( cp[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator
164
[53ba273]165 // first character IS NOT spacing or closing punctuation => add left separator
166 unsigned char ch = cp[0]; // must make unsigned
167 if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
[829c907]168 fmt( os, "%s", sepGetCur( os ) );
[90c3b1c]169 } // if
[b72bad4f]170
171 // if string starts line, must reset to determine open state because separator is off
172 sepReset( os ); // reset separator
173
[b63e376]174 // last character IS spacing or opening punctuation => turn off separator for next item
[53a6c2a]175 size_t len = strlen( cp );
176 ch = cp[len - 1]; // must make unsigned
[b72bad4f]177 if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
[90c3b1c]178 sepOn( os );
[b72bad4f]179 } else {
180 sepOff( os );
[90c3b1c]181 } // if
[53a6c2a]182 if ( ch == '\n' ) setNL( os, true ); // check *AFTER* sepPrt call above as it resets NL flag
[90c3b1c]183 return write( os, cp, len );
[784deab]184} // ?|?
185
186forall( dtype ostype | ostream( ostype ) )
[829c907]187ostype * ?|?( ostype * os, const void * p ) {
188 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
189 fmt( os, "%p", p );
[90c3b1c]190 return os;
[cf16f94]191} // ?|?
192
193
[c443d1d]194// tuples
195forall( dtype ostype, otype T, ttype Params | ostream( ostype ) | writeable( T ) | { ostype * ?|?( ostype *, Params ); } )
196ostype * ?|?( ostype * os, T arg, Params rest ) {
[0583064b]197 os | arg; // print first argument
[86f384b]198 sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator
[0583064b]199 os | rest; // print remaining arguments
[829c907]200 sepSetCur( os, sepGet( os ) ); // switch to regular separator
[cb91437]201 return os;
[c443d1d]202} // ?|?
203
204
205// manipulators
[a5a71d0]206forall( dtype ostype | ostream( ostype ) )
[4e06c1e]207ostype * ?|?( ostype * os, ostype * (* manip)( ostype * ) ) {
[90c3b1c]208 return manip( os );
209} // ?|?
[cf16f94]210
[53a6c2a]211forall( dtype ostype | ostream( ostype ) )
212ostype * sep( ostype * os ) {
213 os | sepGet( os );
214 return os;
215} // sep
216
217forall( dtype ostype | ostream( ostype ) )
218ostype * sepTuple( ostype * os ) {
219 os | sepGetTuple( os );
220 return os;
221} // sepTuple
222
[a5a71d0]223forall( dtype ostype | ostream( ostype ) )
[cf16f94]224ostype * endl( ostype * os ) {
[90c3b1c]225 os | '\n';
[53a6c2a]226 setNL( os, true );
[6ba0659]227 flush( os );
[0583064b]228 sepOff( os ); // prepare for next line
[6ba0659]229 return os;
[cf16f94]230} // endl
[e56cfdb0]231
[a5a71d0]232forall( dtype ostype | ostream( ostype ) )
[90c3b1c]233ostype * sepOn( ostype * os ) {
234 sepOn( os );
235 return os;
236} // sepOn
237
[a5a71d0]238forall( dtype ostype | ostream( ostype ) )
[90c3b1c]239ostype * sepOff( ostype * os ) {
240 sepOff( os );
241 return os;
242} // sepOff
243
[70a06f6]244forall( dtype ostype | ostream( ostype ) )
[53ba273]245ostype * sepEnable( ostype * os ) {
246 sepEnable( os );
247 return os;
248} // sepEnable
249
[70a06f6]250forall( dtype ostype | ostream( ostype ) )
[53ba273]251ostype * sepDisable( ostype * os ) {
252 sepDisable( os );
253 return os;
254} // sepDisable
255
[6ba0659]256//---------------------------------------
257
[4040425]258forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
[829c907]259void write( iteratortype begin, iteratortype end, ostype * os ) {
[90c3b1c]260 void print( elttype i ) { os | i; }
[e56cfdb0]261 for_each( begin, end, print );
[cf16f94]262} // ?|?
[e56cfdb0]263
[4040425]264forall( otype elttype | writeable( elttype ), otype iteratortype | iterator( iteratortype, elttype ), dtype ostype | ostream( ostype ) )
[829c907]265void write_reverse( iteratortype begin, iteratortype end, ostype * os ) {
[90c3b1c]266 void print( elttype i ) { os | i; }
[e56cfdb0]267 for_each_reverse( begin, end, print );
[cf16f94]268} // ?|?
[e56cfdb0]269
[6ba0659]270//---------------------------------------
[e56cfdb0]271
[51b73452]272forall( dtype istype | istream( istype ) )
[90c3b1c]273istype * ?|?( istype * is, char * c ) {
[829c907]274 fmt( is, "%c", c );
[90c3b1c]275 return is;
276} // ?|?
277
278forall( dtype istype | istream( istype ) )
279istype * ?|?( istype * is, short int * si ) {
[829c907]280 fmt( is, "%hd", si );
[90c3b1c]281 return is;
282} // ?|?
283
284forall( dtype istype | istream( istype ) )
285istype * ?|?( istype * is, unsigned short int * usi ) {
[829c907]286 fmt( is, "%hu", usi );
[90c3b1c]287 return is;
288} // ?|?
289
290forall( dtype istype | istream( istype ) )
291istype * ?|?( istype * is, int * i ) {
[829c907]292 fmt( is, "%d", i );
[90c3b1c]293 return is;
294} // ?|?
295
296forall( dtype istype | istream( istype ) )
297istype * ?|?( istype * is, unsigned int * ui ) {
[829c907]298 fmt( is, "%u", ui );
[90c3b1c]299 return is;
300} // ?|?
301
302forall( dtype istype | istream( istype ) )
303istype * ?|?( istype * is, long int * li ) {
[829c907]304 fmt( is, "%ld", li );
[90c3b1c]305 return is;
[cf16f94]306} // ?|?
[51b73452]307
308forall( dtype istype | istream( istype ) )
[90c3b1c]309istype * ?|?( istype * is, unsigned long int * ulli ) {
[829c907]310 fmt( is, "%lu", ulli );
[90c3b1c]311 return is;
312} // ?|?
313
314forall( dtype istype | istream( istype ) )
315istype * ?|?( istype * is, long long int * lli ) {
[829c907]316 fmt( is, "%lld", lli );
[90c3b1c]317 return is;
318} // ?|?
319
320forall( dtype istype | istream( istype ) )
321istype * ?|?( istype * is, unsigned long long int * ulli ) {
[829c907]322 fmt( is, "%llu", ulli );
[90c3b1c]323 return is;
324} // ?|?
325
326
327forall( dtype istype | istream( istype ) )
328istype * ?|?( istype * is, float * f ) {
[829c907]329 fmt( is, "%f", f );
[90c3b1c]330 return is;
[cf16f94]331} // ?|?
[86bd7c1f]332
[90c3b1c]333forall( dtype istype | istream( istype ) )
334istype * ?|?( istype * is, double * d ) {
[829c907]335 fmt( is, "%lf", d );
[90c3b1c]336 return is;
337} // ?|?
338
339forall( dtype istype | istream( istype ) )
340istype * ?|?( istype * is, long double * ld ) {
[829c907]341 fmt( is, "%Lf", ld );
[90c3b1c]342 return is;
343} // ?|?
344
345
346forall( dtype istype | istream( istype ) )
347istype * ?|?( istype * is, float _Complex * fc ) {
348 float re, im;
[829c907]349 fmt( is, "%g%gi", &re, &im );
[90c3b1c]350 *fc = re + im * _Complex_I;
351 return is;
352} // ?|?
353
354forall( dtype istype | istream( istype ) )
355istype * ?|?( istype * is, double _Complex * dc ) {
356 double re, im;
[829c907]357 fmt( is, "%lf%lfi", &re, &im );
[90c3b1c]358 *dc = re + im * _Complex_I;
359 return is;
[cf16f94]360} // ?|?
[51b73452]361
362forall( dtype istype | istream( istype ) )
[90c3b1c]363istype * ?|?( istype * is, long double _Complex * ldc ) {
364 long double re, im;
[829c907]365 fmt( is, "%Lf%Lfi", &re, &im );
[90c3b1c]366 *ldc = re + im * _Complex_I;
367 return is;
[cf16f94]368} // ?|?
[86bd7c1f]369
[e24f13a]370_Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; }
[90c3b1c]371forall( dtype istype | istream( istype ) )
[53ba273]372istype * ?|?( istype * is, _Istream_cstrUC cstr ) {
[829c907]373 fmt( is, "%s", cstr.s );
[90c3b1c]374 return is;
[53ba273]375} // cstr
[90c3b1c]376
[e24f13a]377_Istream_cstrC cstr( char * str, int size ) { return (_Istream_cstrC){ str, size }; }
[90c3b1c]378forall( dtype istype | istream( istype ) )
[53ba273]379istype * ?|?( istype * is, _Istream_cstrC cstr ) {
[90c3b1c]380 char buf[16];
[53ba273]381 sprintf( buf, "%%%ds", cstr.size );
[829c907]382 fmt( is, buf, cstr.s );
[90c3b1c]383 return is;
[53ba273]384} // cstr
[90c3b1c]385
[86bd7c1f]386// Local Variables: //
387// tab-width: 4 //
388// compile-command: "cfa iostream.c" //
389// End: //
Note: See TracBrowser for help on using the repository browser.