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