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