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 : Mon Feb 1 14:20:30 2016
|
---|
13 | // Update Count : 60
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "iostream"
|
---|
17 |
|
---|
18 | extern "C" {
|
---|
19 | #include <stdio.h>
|
---|
20 | #include <string.h> // strlen
|
---|
21 | #include <complex.h> // creal, cimag
|
---|
22 | }
|
---|
23 |
|
---|
24 | forall( dtype ostype | ostream( ostype ) )
|
---|
25 | ostype * ?|?( ostype *os, char c ) {
|
---|
26 | return write( os, &c, 1 );
|
---|
27 | } // ?|?
|
---|
28 |
|
---|
29 | forall( dtype ostype | ostream( ostype ) )
|
---|
30 | ostype * ?|?( ostype *os, int i ) {
|
---|
31 | char buffer[32];
|
---|
32 | return write( os, buffer, sprintf( buffer, "%d", i ) );
|
---|
33 | } // ?|?
|
---|
34 |
|
---|
35 | forall( dtype ostype | ostream( ostype ) )
|
---|
36 | ostype * ?|?( ostype *os, unsigned int i ) {
|
---|
37 | char buffer[32];
|
---|
38 | return write( os, buffer, sprintf( buffer, "%u", i ) );
|
---|
39 | } // ?|?
|
---|
40 |
|
---|
41 | forall( dtype ostype | ostream( ostype ) )
|
---|
42 | ostype * ?|?( ostype *os, long int i ) {
|
---|
43 | char buffer[32];
|
---|
44 | return write( os, buffer, sprintf( buffer, "%ld", i ) );
|
---|
45 | } // ?|?
|
---|
46 |
|
---|
47 | forall( dtype ostype | ostream( ostype ) )
|
---|
48 | ostype * ?|?( ostype *os, long long int i ) {
|
---|
49 | char buffer[32];
|
---|
50 | return write( os, buffer, sprintf( buffer, "%lld", i ) );
|
---|
51 | } // ?|?
|
---|
52 |
|
---|
53 | forall( dtype ostype | ostream( ostype ) )
|
---|
54 | ostype * ?|?( ostype *os, unsigned long int i ) {
|
---|
55 | char buffer[32];
|
---|
56 | return write( os, buffer, sprintf( buffer, "%lu", i ) );
|
---|
57 | } // ?|?
|
---|
58 |
|
---|
59 | forall( dtype ostype | ostream( ostype ) )
|
---|
60 | ostype * ?|?( ostype *os, unsigned long long int i ) {
|
---|
61 | char buffer[32];
|
---|
62 | return write( os, buffer, sprintf( buffer, "%llu", i ) );
|
---|
63 | } // ?|?
|
---|
64 |
|
---|
65 | forall( dtype ostype | ostream( ostype ) )
|
---|
66 | ostype * ?|?( ostype *os, float f ) {
|
---|
67 | char buffer[32];
|
---|
68 | return write( os, buffer, sprintf( buffer, "%g", f ) );
|
---|
69 | } // ?|?
|
---|
70 |
|
---|
71 | forall( dtype ostype | ostream( ostype ) )
|
---|
72 | ostype * ?|?( ostype *os, double d ) {
|
---|
73 | char buffer[32];
|
---|
74 | return write( os, buffer, sprintf( buffer, "%g", d ) );
|
---|
75 | } // ?|?
|
---|
76 |
|
---|
77 | forall( dtype ostype | ostream( ostype ) )
|
---|
78 | ostype * ?|?( ostype *os, long double d ) {
|
---|
79 | char buffer[32];
|
---|
80 | return write( os, buffer, sprintf( buffer, "%Lg", d ) );
|
---|
81 | } // ?|?
|
---|
82 |
|
---|
83 | forall( dtype ostype | ostream( ostype ) )
|
---|
84 | ostype * ?|?( ostype *os, float _Complex c ) {
|
---|
85 | return os | crealf( c ) | (cimagf( c ) < 0 ? "" : "+") | cimagf( c ) | 'i';
|
---|
86 | } // ?|?
|
---|
87 |
|
---|
88 | forall( dtype ostype | ostream( ostype ) )
|
---|
89 | ostype * ?|?( ostype *os, double _Complex c ) {
|
---|
90 | return os | creal( c ) | (cimag( c ) < 0 ? "" : "+") | cimag( c ) | 'i';
|
---|
91 | } // ?|?
|
---|
92 |
|
---|
93 | forall( dtype ostype | ostream( ostype ) )
|
---|
94 | ostype * ?|?( ostype *os, long double _Complex c ) {
|
---|
95 | return os | creall( c ) | (cimagl( c ) < 0 ? "" : "+") | cimagl( c ) | 'i';
|
---|
96 | } // ?|?
|
---|
97 |
|
---|
98 | forall( dtype ostype | ostream( ostype ) )
|
---|
99 | ostype * ?|?( ostype *os, const void *p ) {
|
---|
100 | char buffer[32];
|
---|
101 | return write( os, buffer, sprintf( buffer, "%p", p ) );
|
---|
102 | } // ?|?
|
---|
103 |
|
---|
104 | forall( dtype ostype | ostream( ostype ) )
|
---|
105 | ostype * ?|?( ostype *os, const char *cp ) {
|
---|
106 | return write( os, cp, strlen( cp ) );
|
---|
107 | } // ?|?
|
---|
108 |
|
---|
109 |
|
---|
110 | forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) )
|
---|
111 | retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) {
|
---|
112 | return manip(os);
|
---|
113 | }
|
---|
114 |
|
---|
115 | forall( dtype ostype | ostream( ostype ) )
|
---|
116 | ostype * endl( ostype * os ) {
|
---|
117 | os | "\n";
|
---|
118 | // flush
|
---|
119 | return os;
|
---|
120 | } // endl
|
---|
121 |
|
---|
122 | forall( type elt_type | writeable( elt_type ),
|
---|
123 | type iterator_type | iterator( iterator_type, elt_type ),
|
---|
124 | dtype os_type | ostream( os_type ) )
|
---|
125 | void write( iterator_type begin, iterator_type end, os_type *os ) {
|
---|
126 | void print( elt_type i ) {
|
---|
127 | os | i | ' ';
|
---|
128 | }
|
---|
129 | for_each( begin, end, print );
|
---|
130 | } // ?|?
|
---|
131 |
|
---|
132 | forall( type elt_type | writeable( elt_type ),
|
---|
133 | type iterator_type | iterator( iterator_type, elt_type ),
|
---|
134 | dtype os_type | ostream( os_type ) )
|
---|
135 | void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
|
---|
136 | void print( elt_type i ) { os | i | ' '; }
|
---|
137 | for_each_reverse( begin, end, print );
|
---|
138 | } // ?|?
|
---|
139 |
|
---|
140 |
|
---|
141 | forall( dtype istype | istream( istype ) )
|
---|
142 | istype * ?|?( istype *is, char *cp ) {
|
---|
143 | return read( is, cp, 1 );
|
---|
144 | } // ?|?
|
---|
145 |
|
---|
146 | forall( dtype istype | istream( istype ) )
|
---|
147 | istype * ?|?( istype *is, int *ip ) {
|
---|
148 | char cur;
|
---|
149 |
|
---|
150 | // skip some whitespace
|
---|
151 | do {
|
---|
152 | is | &cur;
|
---|
153 | if ( fail( is ) || eof( is ) ) return is;
|
---|
154 | } while ( !( cur >= '0' && cur <= '9' ) );
|
---|
155 |
|
---|
156 | // accumulate digits
|
---|
157 | *ip = 0;
|
---|
158 | while ( cur >= '0' && cur <= '9' ) {
|
---|
159 | *ip = *ip * 10 + ( cur - '0' );
|
---|
160 | is | &cur;
|
---|
161 | if ( fail( is ) || eof( is ) ) return is;
|
---|
162 | }
|
---|
163 |
|
---|
164 | unread( is, cur );
|
---|
165 | return is;
|
---|
166 | } // ?|?
|
---|
167 |
|
---|
168 | // Local Variables: //
|
---|
169 | // tab-width: 4 //
|
---|
170 | // compile-command: "cfa iostream.c" //
|
---|
171 | // End: //
|
---|