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 : Mon Mar 1 22:45:08 2021 |
---|
13 | // Update Count : 217 |
---|
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 { 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[sepSize]; |
---|
36 | char $tupleSeparator[sepSize]; |
---|
37 | multiple_acquisition_lock $lock; |
---|
38 | bool $acquired; |
---|
39 | }; // ofstream |
---|
40 | |
---|
41 | // private |
---|
42 | bool $sepPrt( ofstream & ); |
---|
43 | void $sepReset( ofstream & ); |
---|
44 | void $sepReset( ofstream &, bool ); |
---|
45 | const char * $sepGetCur( ofstream & ); |
---|
46 | void $sepSetCur( ofstream &, const char [] ); |
---|
47 | bool $getNL( ofstream & ); |
---|
48 | void $setNL( ofstream &, bool ); |
---|
49 | bool $getANL( ofstream & ); |
---|
50 | bool $getPrt( ofstream & ); |
---|
51 | void $setPrt( ofstream &, bool ); |
---|
52 | |
---|
53 | // public |
---|
54 | void sepOn( ofstream & ); |
---|
55 | void sepOff( ofstream & ); |
---|
56 | bool sepDisable( ofstream & ); |
---|
57 | bool sepEnable( ofstream & ); |
---|
58 | void nlOn( ofstream & ); |
---|
59 | void nlOff( ofstream & ); |
---|
60 | |
---|
61 | const char * sepGet( ofstream & ); |
---|
62 | void sepSet( ofstream &, const char [] ); |
---|
63 | const char * sepGetTuple( ofstream & ); |
---|
64 | void sepSetTuple( ofstream &, const char [] ); |
---|
65 | |
---|
66 | void ends( ofstream & os ); |
---|
67 | int fail( ofstream & ); |
---|
68 | int flush( ofstream & ); |
---|
69 | void open( ofstream &, const char name[], const char mode[] ); |
---|
70 | void open( ofstream &, const char name[] ); |
---|
71 | void close( ofstream & ); |
---|
72 | ofstream & write( ofstream &, const char data[], size_t size ); |
---|
73 | int fmt( ofstream &, const char format[], ... ) __attribute__(( format(printf, 2, 3) )); |
---|
74 | void acquire( ofstream & os ); |
---|
75 | void release( ofstream & os ); |
---|
76 | |
---|
77 | struct osacquire { |
---|
78 | ofstream & os; |
---|
79 | }; |
---|
80 | void ?{}( osacquire & acq, ofstream & os ); |
---|
81 | void ^?{}( osacquire & acq ); |
---|
82 | |
---|
83 | void ?{}( ofstream & os ); |
---|
84 | void ?{}( ofstream & os, const char name[], const char mode[] ); |
---|
85 | void ?{}( ofstream & os, const char name[] ); |
---|
86 | void ^?{}( ofstream & os ); |
---|
87 | |
---|
88 | extern ofstream & sout, & stdout, & serr, & stderr; // aliases |
---|
89 | extern ofstream & exit, & abort; |
---|
90 | |
---|
91 | |
---|
92 | // *********************************** ifstream *********************************** |
---|
93 | |
---|
94 | |
---|
95 | struct ifstream { |
---|
96 | void * $file; |
---|
97 | bool $nlOnOff; |
---|
98 | multiple_acquisition_lock $lock; |
---|
99 | bool $acquired; |
---|
100 | }; // ifstream |
---|
101 | |
---|
102 | // public |
---|
103 | void nlOn( ifstream & ); |
---|
104 | void nlOff( ifstream & ); |
---|
105 | bool getANL( ifstream & ); |
---|
106 | void ends( ifstream & ); |
---|
107 | int fail( ifstream & is ); |
---|
108 | int eof( ifstream & is ); |
---|
109 | void open( ifstream & is, const char name[], const char mode[] ); |
---|
110 | void open( ifstream & is, const char name[] ); |
---|
111 | void close( ifstream & is ); |
---|
112 | ifstream & read( ifstream & is, char * data, size_t size ); |
---|
113 | ifstream & ungetc( ifstream & is, char c ); |
---|
114 | int fmt( ifstream &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) )); |
---|
115 | void acquire( ifstream & is ); |
---|
116 | void release( ifstream & is ); |
---|
117 | |
---|
118 | struct isacquire { |
---|
119 | ifstream & is; |
---|
120 | }; |
---|
121 | void ?{}( isacquire & acq, ifstream & is ); |
---|
122 | void ^?{}( isacquire & acq ); |
---|
123 | |
---|
124 | void ?{}( ifstream & is ); |
---|
125 | void ?{}( ifstream & is, const char name[], const char mode[] ); |
---|
126 | void ?{}( ifstream & is, const char name[] ); |
---|
127 | void ^?{}( ifstream & is ); |
---|
128 | |
---|
129 | extern ifstream & sin, & stdin; // aliases |
---|
130 | |
---|
131 | |
---|
132 | // *********************************** exceptions *********************************** |
---|
133 | |
---|
134 | |
---|
135 | DATA_EXCEPTION(Open_Failure)( |
---|
136 | union { |
---|
137 | ofstream * ostream; |
---|
138 | ifstream * istream; |
---|
139 | }; |
---|
140 | // TEMPORARY: need polymorphic exceptions |
---|
141 | int tag; // 1 => ostream; 0 => istream |
---|
142 | ); |
---|
143 | |
---|
144 | void ?{}( Open_Failure & this, ofstream & ostream ); |
---|
145 | void ?{}( Open_Failure & this, ifstream & istream ); |
---|
146 | |
---|
147 | // Local Variables: // |
---|
148 | // mode: c // |
---|
149 | // tab-width: 4 // |
---|
150 | // End: // |
---|