source: src/libcfa/iostream.c @ 9d7b3ea

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 9d7b3ea was 6ba0659, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

major rewrite of I/O library and update example programs to use new I/O

  • Property mode set to 100644
File size: 4.4 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           : Richard C. Bilson
10// Created On       : Wed May 27 17:56:53 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Feb 17 14:19:56 2016
13// Update Count     : 76
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        return write( os, &c, 1 );
28} // ?|?
29
30forall( dtype ostype | ostream( ostype ) )
31ostype * ?|?( ostype *os, int i ) {
32        char buffer[32];
33        return write( os, buffer, sprintf( buffer, "%d", i ) );
34} // ?|?
35
36forall( dtype ostype | ostream( ostype ) )
37ostype * ?|?( ostype *os, unsigned int i ) {
38        char buffer[32];
39        return write( os, buffer, sprintf( buffer, "%u", i ) );
40} // ?|?
41
42forall( dtype ostype | ostream( ostype ) )
43ostype * ?|?( ostype *os, long int i ) {
44        char buffer[32];
45        return write( os, buffer, sprintf( buffer, "%ld", i ) );
46} // ?|?
47
48forall( dtype ostype | ostream( ostype ) )
49ostype * ?|?( ostype *os, long long int i ) {
50        char buffer[32];
51        return write( os, buffer, sprintf( buffer, "%lld", i ) );
52} // ?|?
53
54forall( dtype ostype | ostream( ostype ) )
55ostype * ?|?( ostype *os, unsigned long int i ) {
56        char buffer[32];
57        return write( os, buffer, sprintf( buffer, "%lu", i ) );
58} // ?|?
59
60forall( dtype ostype | ostream( ostype ) )
61ostype * ?|?( ostype *os, unsigned long long int i ) {
62        char buffer[32];
63        return write( os, buffer, sprintf( buffer, "%llu", i ) );
64} // ?|?
65
66forall( dtype ostype | ostream( ostype ) )
67ostype * ?|?( ostype *os, float f ) {
68        char buffer[32];
69        return write( os, buffer, sprintf( buffer, "%g", f ) );
70} // ?|?
71
72forall( dtype ostype | ostream( ostype ) )
73ostype * ?|?( ostype *os, double d ) {
74        char buffer[32];
75        return write( os, buffer, sprintf( buffer, "%.*lg", DBL_DIG, d ) );
76} // ?|?
77
78forall( dtype ostype | ostream( ostype ) )
79ostype * ?|?( ostype *os, long double d ) {
80        char buffer[32];
81        return write( os, buffer, sprintf( buffer, "%.*Lg", LDBL_DIG, d ) );
82} // ?|?
83
84forall( dtype ostype | ostream( ostype ) )
85ostype * ?|?( ostype *os, float _Complex c ) {
86        return os | crealf( c ) | (cimagf( c ) < 0 ? "" : "+") | cimagf( c ) | 'i';
87} // ?|?
88
89forall( dtype ostype | ostream( ostype ) )
90ostype * ?|?( ostype *os, double _Complex c ) {
91        return os | creal( c ) | (cimag( c ) < 0 ? "" : "+") | cimag( c ) | 'i';
92} // ?|?
93
94forall( dtype ostype | ostream( ostype ) )
95ostype * ?|?( ostype *os, long double _Complex c ) {
96        return os | creall( c ) | (cimagl( c ) < 0 ? "" : "+") | cimagl( c ) | 'i';
97} // ?|?
98
99forall( dtype ostype | ostream( ostype ) )
100ostype * ?|?( ostype *os, const void *p ) {
101        char buffer[32];
102        return write( os, buffer, sprintf( buffer, "%p", p ) );
103} // ?|?
104
105forall( dtype ostype | ostream( ostype ) )
106ostype * ?|?( ostype *os, const char *cp ) {
107        return write( os, cp, strlen( cp ) );
108} // ?|?
109
110
111forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) ) 
112retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) {
113  return manip( os );
114}
115
116forall( dtype ostype | ostream( ostype ) ) 
117ostype * endl( ostype * os ) {
118        os | "\n";
119        flush( os );
120        return os;
121} // endl
122
123//---------------------------------------
124
125forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
126                dtype os_type | ostream( os_type ) )
127void write( iterator_type begin, iterator_type end, os_type *os ) {
128        void print( elt_type i ) { os | i | ' '; }
129        for_each( begin, end, print );
130} // ?|?
131
132forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
133                dtype os_type | ostream( os_type ) )
134void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
135        void print( elt_type i ) { os | i | ' '; }
136        for_each_reverse( begin, end, print );
137} // ?|?
138
139//---------------------------------------
140
141forall( dtype istype | istream( istype ) )
142istype * ?|?( istype *is, char *cp ) {
143        return read( is, cp, 1 );
144} // ?|?
145
146forall( dtype istype | istream( istype ) )
147istype * ?|?( istype *is, int *ip ) {
148        return get( is, ip );
149} // ?|?
150
151// Local Variables: //
152// tab-width: 4 //
153// compile-command: "cfa iostream.c" //
154// End: //
Note: See TracBrowser for help on using the repository browser.