| 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 : Wed Jul 28 07:35:50 2021 | 
|---|
| 13 | // Update Count     : 234 | 
|---|
| 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 | // public | 
|---|
| 56 | void sepOn( ofstream & ); | 
|---|
| 57 | void sepOff( ofstream & ); | 
|---|
| 58 | bool sepDisable( ofstream & ); | 
|---|
| 59 | bool sepEnable( ofstream & ); | 
|---|
| 60 | void nlOn( ofstream & ); | 
|---|
| 61 | void nlOff( ofstream & ); | 
|---|
| 62 |  | 
|---|
| 63 | const char * sepGet( ofstream & ); | 
|---|
| 64 | void sepSet( ofstream &, const char [] ); | 
|---|
| 65 | const char * sepGetTuple( ofstream & ); | 
|---|
| 66 | void sepSetTuple( ofstream &, const char [] ); | 
|---|
| 67 |  | 
|---|
| 68 | void ends( ofstream & ); | 
|---|
| 69 | int fmt( ofstream &, const char format[], ... ) __attribute__(( format(printf, 2, 3) )); | 
|---|
| 70 |  | 
|---|
| 71 | bool fail( ofstream & ); | 
|---|
| 72 | void clear( ofstream & ); | 
|---|
| 73 | int flush( ofstream & ); | 
|---|
| 74 | void open( ofstream &, const char name[], const char mode[] ); // FIX ME: use default = "w" | 
|---|
| 75 | void open( ofstream &, const char name[] ); | 
|---|
| 76 | void close( ofstream & ); | 
|---|
| 77 | ofstream & write( ofstream &, const char data[], size_t size ); | 
|---|
| 78 |  | 
|---|
| 79 | void acquire( ofstream & ); | 
|---|
| 80 | void release( ofstream & ); | 
|---|
| 81 |  | 
|---|
| 82 | void lock( ofstream & ); | 
|---|
| 83 | void unlock( ofstream & ); | 
|---|
| 84 |  | 
|---|
| 85 | struct osacquire { | 
|---|
| 86 | ofstream & os; | 
|---|
| 87 | }; | 
|---|
| 88 | void ?{}( osacquire & acq, ofstream & ); | 
|---|
| 89 | void ^?{}( osacquire & acq ); | 
|---|
| 90 |  | 
|---|
| 91 | void ?{}( ofstream & ); | 
|---|
| 92 | void ?{}( ofstream &, const char name[], const char mode[] ); // FIX ME: use default = "w" | 
|---|
| 93 | void ?{}( ofstream &, const char name[] ); | 
|---|
| 94 | void ^?{}( ofstream & ); | 
|---|
| 95 |  | 
|---|
| 96 | // private | 
|---|
| 97 | static inline ofstream & nl$( ofstream & os ) { return nl( os ); } // remember basic_ostream nl | 
|---|
| 98 | // public | 
|---|
| 99 | ofstream & nl( ofstream & os );                                                 // override basic_ostream nl | 
|---|
| 100 |  | 
|---|
| 101 | extern ofstream & sout, & stdout, & serr, & stderr;             // aliases | 
|---|
| 102 | extern ofstream & exit, & abort; | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | // *********************************** ifstream *********************************** | 
|---|
| 106 |  | 
|---|
| 107 |  | 
|---|
| 108 | struct ifstream { | 
|---|
| 109 | void * file$; | 
|---|
| 110 | bool nlOnOff$; | 
|---|
| 111 | multiple_acquisition_lock lock$; | 
|---|
| 112 | bool acquired$; | 
|---|
| 113 | }; // ifstream | 
|---|
| 114 |  | 
|---|
| 115 | // Satisfies istream | 
|---|
| 116 |  | 
|---|
| 117 | // public | 
|---|
| 118 | void nlOn( ifstream & ); | 
|---|
| 119 | void nlOff( ifstream & ); | 
|---|
| 120 | bool getANL( ifstream & ); | 
|---|
| 121 | void ends( ifstream & ); | 
|---|
| 122 | int fmt( ifstream &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) )); | 
|---|
| 123 |  | 
|---|
| 124 | bool fail( ifstream & is ); | 
|---|
| 125 | void clear( ifstream & ); | 
|---|
| 126 | bool eof( ifstream & is ); | 
|---|
| 127 | void open( ifstream & is, const char name[], const char mode[] ); // FIX ME: use default = "r" | 
|---|
| 128 | void open( ifstream & is, const char name[] ); | 
|---|
| 129 | void close( ifstream & is ); | 
|---|
| 130 | ifstream & read( ifstream & is, char data[], size_t size ); | 
|---|
| 131 | ifstream & ungetc( ifstream & is, char c ); | 
|---|
| 132 |  | 
|---|
| 133 | void acquire( ifstream & is ); | 
|---|
| 134 | void release( ifstream & is ); | 
|---|
| 135 |  | 
|---|
| 136 | struct isacquire { | 
|---|
| 137 | ifstream & is; | 
|---|
| 138 | }; | 
|---|
| 139 | void ?{}( isacquire & acq, ifstream & is ); | 
|---|
| 140 | void ^?{}( isacquire & acq ); | 
|---|
| 141 |  | 
|---|
| 142 | void ?{}( ifstream & is ); | 
|---|
| 143 | void ?{}( ifstream & is, const char name[], const char mode[] ); // FIX ME: use default = "r" | 
|---|
| 144 | void ?{}( ifstream & is, const char name[] ); | 
|---|
| 145 | void ^?{}( ifstream & is ); | 
|---|
| 146 |  | 
|---|
| 147 | extern ifstream & sin, & stdin;                                                 // aliases | 
|---|
| 148 |  | 
|---|
| 149 |  | 
|---|
| 150 | // *********************************** exceptions *********************************** | 
|---|
| 151 |  | 
|---|
| 152 |  | 
|---|
| 153 | exception Open_Failure { | 
|---|
| 154 | union { | 
|---|
| 155 | ofstream * ostream; | 
|---|
| 156 | ifstream * istream; | 
|---|
| 157 | }; | 
|---|
| 158 | // TEMPORARY: need polymorphic exceptions | 
|---|
| 159 | int tag;                                                                                        // 1 => ostream; 0 => istream | 
|---|
| 160 | }; | 
|---|
| 161 |  | 
|---|
| 162 | void ?{}( Open_Failure & this, ofstream & ); | 
|---|
| 163 | void ?{}( Open_Failure & this, ifstream & ); | 
|---|
| 164 |  | 
|---|
| 165 | exception Close_Failure { | 
|---|
| 166 | union { | 
|---|
| 167 | ofstream * ostream; | 
|---|
| 168 | ifstream * istream; | 
|---|
| 169 | }; | 
|---|
| 170 | // TEMPORARY: need polymorphic exceptions | 
|---|
| 171 | int tag;                                                                                        // 1 => ostream; 0 => istream | 
|---|
| 172 | }; | 
|---|
| 173 |  | 
|---|
| 174 | void ?{}( Close_Failure & this, ofstream & ); | 
|---|
| 175 | void ?{}( Close_Failure & this, ifstream & ); | 
|---|
| 176 |  | 
|---|
| 177 | exception Write_Failure { | 
|---|
| 178 | union { | 
|---|
| 179 | ofstream * ostream; | 
|---|
| 180 | ifstream * istream; | 
|---|
| 181 | }; | 
|---|
| 182 | // TEMPORARY: need polymorphic exceptions | 
|---|
| 183 | int tag;                                                                                        // 1 => ostream; 0 => istream | 
|---|
| 184 | }; | 
|---|
| 185 |  | 
|---|
| 186 | void ?{}( Write_Failure & this, ofstream & ); | 
|---|
| 187 | void ?{}( Write_Failure & this, ifstream & ); | 
|---|
| 188 |  | 
|---|
| 189 | exception Read_Failure { | 
|---|
| 190 | union { | 
|---|
| 191 | ofstream * ostream; | 
|---|
| 192 | ifstream * istream; | 
|---|
| 193 | }; | 
|---|
| 194 | // TEMPORARY: need polymorphic exceptions | 
|---|
| 195 | int tag;                                                                                        // 1 => ostream; 0 => istream | 
|---|
| 196 | }; | 
|---|
| 197 |  | 
|---|
| 198 | void ?{}( Read_Failure & this, ofstream & ); | 
|---|
| 199 | void ?{}( Read_Failure & this, ifstream & ); | 
|---|
| 200 |  | 
|---|
| 201 | // Local Variables: // | 
|---|
| 202 | // mode: c // | 
|---|
| 203 | // tab-width: 4 // | 
|---|
| 204 | // End: // | 
|---|