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 | // fstream -- |
---|
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 Oct 1 07:40:37 2021 |
---|
13 | // Update Count : 238 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include "bits/weakso_locks.hfa" // mutex_lock |
---|
19 | #include "iostream.hfa" |
---|
20 | #include <exception.hfa> |
---|
21 | |
---|
22 | |
---|
23 | // *********************************** ofstream *********************************** |
---|
24 | |
---|
25 | |
---|
26 | enum { ofstream_sepSize = 16 }; |
---|
27 | struct ofstream { |
---|
28 | void * file$; |
---|
29 | bool sepDefault$; |
---|
30 | bool sepOnOff$; |
---|
31 | bool nlOnOff$; |
---|
32 | bool prt$; // print text |
---|
33 | bool sawNL$; |
---|
34 | const char * sepCur$; |
---|
35 | char separator$[ofstream_sepSize]; |
---|
36 | char tupleSeparator$[ofstream_sepSize]; |
---|
37 | multiple_acquisition_lock lock$; |
---|
38 | bool acquired$; |
---|
39 | }; // ofstream |
---|
40 | |
---|
41 | // Satisfies ostream |
---|
42 | |
---|
43 | // private |
---|
44 | bool sepPrt$( ofstream & ); |
---|
45 | void sepReset$( ofstream & ); |
---|
46 | void sepReset$( ofstream &, bool ); |
---|
47 | const char * sepGetCur$( ofstream & ); |
---|
48 | void sepSetCur$( ofstream &, const char [] ); |
---|
49 | bool getNL$( ofstream & ); |
---|
50 | void setNL$( ofstream &, bool ); |
---|
51 | bool getANL$( ofstream & ); |
---|
52 | bool getPrt$( ofstream & ); |
---|
53 | void setPrt$( ofstream &, bool ); |
---|
54 | |
---|
55 | void lock( ofstream & ); |
---|
56 | void unlock( ofstream & ); |
---|
57 | |
---|
58 | // public |
---|
59 | void sepOn( ofstream & ); |
---|
60 | void sepOff( ofstream & ); |
---|
61 | bool sepDisable( ofstream & ); |
---|
62 | bool sepEnable( ofstream & ); |
---|
63 | void nlOn( ofstream & ); |
---|
64 | void nlOff( ofstream & ); |
---|
65 | |
---|
66 | const char * sepGet( ofstream & ); |
---|
67 | void sepSet( ofstream &, const char [] ); |
---|
68 | const char * sepGetTuple( ofstream & ); |
---|
69 | void sepSetTuple( ofstream &, const char [] ); |
---|
70 | |
---|
71 | void ends( ofstream & ); |
---|
72 | int fmt( ofstream &, const char format[], ... ) __attribute__(( format(printf, 2, 3) )); |
---|
73 | |
---|
74 | bool fail( ofstream & ); |
---|
75 | void clear( ofstream & ); |
---|
76 | int flush( ofstream & ); |
---|
77 | void open( ofstream &, const char name[], const char mode[] ); // FIX ME: use default = "w" |
---|
78 | void open( ofstream &, const char name[] ); |
---|
79 | void close( ofstream & ); |
---|
80 | ofstream & write( ofstream &, const char data[], size_t size ); |
---|
81 | |
---|
82 | void acquire( ofstream & ); |
---|
83 | |
---|
84 | struct osacquire { |
---|
85 | ofstream & os; |
---|
86 | }; |
---|
87 | void ?{}( osacquire & acq, ofstream & ); |
---|
88 | void ^?{}( osacquire & acq ); |
---|
89 | |
---|
90 | void ?{}( ofstream & ); |
---|
91 | void ?{}( ofstream &, const char name[], const char mode[] ); // FIX ME: use default = "w" |
---|
92 | void ?{}( ofstream &, const char name[] ); |
---|
93 | void ^?{}( ofstream & ); |
---|
94 | |
---|
95 | // private |
---|
96 | static inline ofstream & nl$( ofstream & os ) { return nl( os ); } // remember basic_ostream nl |
---|
97 | // public |
---|
98 | ofstream & nl( ofstream & os ); // override basic_ostream nl |
---|
99 | |
---|
100 | extern ofstream & sout, & stdout, & serr, & stderr; // aliases |
---|
101 | extern ofstream & exit, & abort; |
---|
102 | |
---|
103 | |
---|
104 | // *********************************** ifstream *********************************** |
---|
105 | |
---|
106 | |
---|
107 | struct ifstream { |
---|
108 | void * file$; |
---|
109 | bool nlOnOff$; |
---|
110 | multiple_acquisition_lock lock$; |
---|
111 | bool acquired$; |
---|
112 | }; // ifstream |
---|
113 | |
---|
114 | // Satisfies istream |
---|
115 | |
---|
116 | // private |
---|
117 | void lock( ifstream & ); |
---|
118 | void unlock( ifstream & ); |
---|
119 | |
---|
120 | // public |
---|
121 | void nlOn( ifstream & ); |
---|
122 | void nlOff( ifstream & ); |
---|
123 | bool getANL( ifstream & ); |
---|
124 | void ends( ifstream & ); |
---|
125 | int fmt( ifstream &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) )); |
---|
126 | |
---|
127 | bool fail( ifstream & is ); |
---|
128 | void clear( ifstream & ); |
---|
129 | bool eof( ifstream & is ); |
---|
130 | void open( ifstream & is, const char name[], const char mode[] ); // FIX ME: use default = "r" |
---|
131 | void open( ifstream & is, const char name[] ); |
---|
132 | void close( ifstream & is ); |
---|
133 | ifstream & read( ifstream & is, char data[], size_t size ); |
---|
134 | ifstream & ungetc( ifstream & is, char c ); |
---|
135 | |
---|
136 | void acquire( ifstream & is ); |
---|
137 | |
---|
138 | struct isacquire { |
---|
139 | ifstream & is; |
---|
140 | }; |
---|
141 | void ?{}( isacquire & acq, ifstream & is ); |
---|
142 | void ^?{}( isacquire & acq ); |
---|
143 | |
---|
144 | void ?{}( ifstream & is ); |
---|
145 | void ?{}( ifstream & is, const char name[], const char mode[] ); // FIX ME: use default = "r" |
---|
146 | void ?{}( ifstream & is, const char name[] ); |
---|
147 | void ^?{}( ifstream & is ); |
---|
148 | |
---|
149 | extern ifstream & sin, & stdin; // aliases |
---|
150 | |
---|
151 | |
---|
152 | // *********************************** exceptions *********************************** |
---|
153 | |
---|
154 | |
---|
155 | exception Open_Failure { |
---|
156 | union { |
---|
157 | ofstream * ostream; |
---|
158 | ifstream * istream; |
---|
159 | }; |
---|
160 | // TEMPORARY: need polymorphic exceptions |
---|
161 | int tag; // 1 => ostream; 0 => istream |
---|
162 | }; |
---|
163 | |
---|
164 | void ?{}( Open_Failure & this, ofstream & ); |
---|
165 | void ?{}( Open_Failure & this, ifstream & ); |
---|
166 | |
---|
167 | exception Close_Failure { |
---|
168 | union { |
---|
169 | ofstream * ostream; |
---|
170 | ifstream * istream; |
---|
171 | }; |
---|
172 | // TEMPORARY: need polymorphic exceptions |
---|
173 | int tag; // 1 => ostream; 0 => istream |
---|
174 | }; |
---|
175 | |
---|
176 | void ?{}( Close_Failure & this, ofstream & ); |
---|
177 | void ?{}( Close_Failure & this, ifstream & ); |
---|
178 | |
---|
179 | exception Write_Failure { |
---|
180 | union { |
---|
181 | ofstream * ostream; |
---|
182 | ifstream * istream; |
---|
183 | }; |
---|
184 | // TEMPORARY: need polymorphic exceptions |
---|
185 | int tag; // 1 => ostream; 0 => istream |
---|
186 | }; |
---|
187 | |
---|
188 | void ?{}( Write_Failure & this, ofstream & ); |
---|
189 | void ?{}( Write_Failure & this, ifstream & ); |
---|
190 | |
---|
191 | exception Read_Failure { |
---|
192 | union { |
---|
193 | ofstream * ostream; |
---|
194 | ifstream * istream; |
---|
195 | }; |
---|
196 | // TEMPORARY: need polymorphic exceptions |
---|
197 | int tag; // 1 => ostream; 0 => istream |
---|
198 | }; |
---|
199 | |
---|
200 | void ?{}( Read_Failure & this, ofstream & ); |
---|
201 | void ?{}( Read_Failure & this, ifstream & ); |
---|
202 | |
---|
203 | // Local Variables: // |
---|
204 | // mode: c // |
---|
205 | // tab-width: 4 // |
---|
206 | // End: // |
---|