| 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 Oct 18 20:30:12 2023
 | 
|---|
| 13 | // Update Count     : 261
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #pragma once
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "bits/weakso_locks.hfa"                                                // mutex_lock
 | 
|---|
| 19 | #include "iostream.hfa"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | 
 | 
|---|
| 22 | // *********************************** ofstream ***********************************
 | 
|---|
| 23 | 
 | 
|---|
| 24 | 
 | 
|---|
| 25 | enum { ofstream_sepSize = 16 };
 | 
|---|
| 26 | struct ofstream {
 | 
|---|
| 27 |         void * file$;
 | 
|---|
| 28 |         bool sepDefault$;
 | 
|---|
| 29 |         bool sepOnOff$;
 | 
|---|
| 30 |         bool nlOnOff$;
 | 
|---|
| 31 |         bool prt$;                                                                                      // print text
 | 
|---|
| 32 |         bool sawNL$;
 | 
|---|
| 33 |         const char * sepCur$;
 | 
|---|
| 34 |         char separator$[ofstream_sepSize];
 | 
|---|
| 35 |         char tupleSeparator$[ofstream_sepSize];
 | 
|---|
| 36 |         multiple_acquisition_lock lock$;                                        // used by trait is_lock for mutex statement
 | 
|---|
| 37 | }; // ofstream
 | 
|---|
| 38 | 
 | 
|---|
| 39 | // Satisfies ostream
 | 
|---|
| 40 | 
 | 
|---|
| 41 | // private
 | 
|---|
| 42 | bool getNL$( ofstream & );
 | 
|---|
| 43 | bool setNL$( ofstream &, bool );
 | 
|---|
| 44 | bool getANL$( ofstream & );
 | 
|---|
| 45 | bool setANL$( ofstream &, bool );
 | 
|---|
| 46 | 
 | 
|---|
| 47 | bool sepPrt$( ofstream & );
 | 
|---|
| 48 | void sepReset$( ofstream & );
 | 
|---|
| 49 | void sepReset$( ofstream &, bool );
 | 
|---|
| 50 | const char * sepGetCur$( ofstream & );
 | 
|---|
| 51 | void sepSetCur$( ofstream &, const char [] );
 | 
|---|
| 52 | 
 | 
|---|
| 53 | bool getPrt$( ofstream & );
 | 
|---|
| 54 | bool setPrt$( ofstream &, bool );
 | 
|---|
| 55 | 
 | 
|---|
| 56 | void lock( ofstream & );
 | 
|---|
| 57 | void unlock( ofstream & );
 | 
|---|
| 58 | 
 | 
|---|
| 59 | // public
 | 
|---|
| 60 | void nlOn( ofstream & );
 | 
|---|
| 61 | void nlOff( ofstream & );
 | 
|---|
| 62 | 
 | 
|---|
| 63 | void sep( ofstream & );
 | 
|---|
| 64 | void nosep( ofstream & );
 | 
|---|
| 65 | bool sepOn( ofstream & );
 | 
|---|
| 66 | bool sepOff( ofstream & );
 | 
|---|
| 67 | const char * sepGet( ofstream & );
 | 
|---|
| 68 | void sepSet( ofstream &, const char [] );
 | 
|---|
| 69 | const char * sepGetTuple( ofstream & );
 | 
|---|
| 70 | void sepSetTuple( ofstream &, const char [] );
 | 
|---|
| 71 | 
 | 
|---|
| 72 | void ends( ofstream & );
 | 
|---|
| 73 | int fmt( ofstream &, const char format[], ... ) __attribute__(( format(printf, 2, 3) ));
 | 
|---|
| 74 | 
 | 
|---|
| 75 | bool fail( ofstream & );
 | 
|---|
| 76 | void clear( ofstream & );
 | 
|---|
| 77 | int flush( ofstream & );
 | 
|---|
| 78 | void open( ofstream &, const char name[], const char mode[] ); // FIX ME: use default = "w"
 | 
|---|
| 79 | void open( ofstream &, const char name[] );
 | 
|---|
| 80 | void close( ofstream & );
 | 
|---|
| 81 | 
 | 
|---|
| 82 | ofstream & write( ofstream &, const char data[], size_t size );
 | 
|---|
| 83 | 
 | 
|---|
| 84 | void ?{}( ofstream & );
 | 
|---|
| 85 | void ?{}( ofstream &, const char name[], const char mode[] ); // FIX ME: use default = "w"
 | 
|---|
| 86 | void ?{}( ofstream &, const char name[] );
 | 
|---|
| 87 | void ^?{}( ofstream & );
 | 
|---|
| 88 | 
 | 
|---|
| 89 | // private
 | 
|---|
| 90 | static inline ofstream & nl$( ofstream & os ) { return nl( os ); } // remember basic_ostream nl
 | 
|---|
| 91 | // public
 | 
|---|
| 92 | ofstream & nl( ofstream & os );                                                 // override basic_ostream nl
 | 
|---|
| 93 | 
 | 
|---|
| 94 | extern ofstream & sout, & stdout, & serr, & stderr;             // aliases
 | 
|---|
| 95 | extern ofstream & exit, & abort;
 | 
|---|
| 96 | 
 | 
|---|
| 97 | 
 | 
|---|
| 98 | // *********************************** ifstream ***********************************
 | 
|---|
| 99 | 
 | 
|---|
| 100 | 
 | 
|---|
| 101 | struct ifstream {
 | 
|---|
| 102 |         void * file$;
 | 
|---|
| 103 |         bool nlOnOff$;
 | 
|---|
| 104 |         multiple_acquisition_lock lock$;                                        // used by trait is_lock for mutex statement
 | 
|---|
| 105 | }; // ifstream
 | 
|---|
| 106 | 
 | 
|---|
| 107 | // Satisfies istream
 | 
|---|
| 108 | 
 | 
|---|
| 109 | // private
 | 
|---|
| 110 | bool getANL$( ifstream & );
 | 
|---|
| 111 | bool setANL$( ifstream &, bool );
 | 
|---|
| 112 | 
 | 
|---|
| 113 | void lock( ifstream & );
 | 
|---|
| 114 | void unlock( ifstream & );
 | 
|---|
| 115 | 
 | 
|---|
| 116 | // public
 | 
|---|
| 117 | void nlOn( ifstream & );
 | 
|---|
| 118 | void nlOff( ifstream & );
 | 
|---|
| 119 | int fmt( ifstream &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) ));
 | 
|---|
| 120 | ifstream & ungetc( ifstream & is, char c );
 | 
|---|
| 121 | bool eof( ifstream & is );
 | 
|---|
| 122 | 
 | 
|---|
| 123 | bool fail( ifstream & is );
 | 
|---|
| 124 | void clear( ifstream & );
 | 
|---|
| 125 | void open( ifstream & is, const char name[], const char mode[] ); // FIX ME: use default = "r"
 | 
|---|
| 126 | void open( ifstream & is, const char name[] );
 | 
|---|
| 127 | void close( ifstream & is );
 | 
|---|
| 128 | ifstream & read( ifstream & is, char data[], size_t size );
 | 
|---|
| 129 | 
 | 
|---|
| 130 | void ?{}( ifstream & is );
 | 
|---|
| 131 | void ?{}( ifstream & is, const char name[], const char mode[] ); // FIX ME: use default = "r"
 | 
|---|
| 132 | void ?{}( ifstream & is, const char name[] );
 | 
|---|
| 133 | void ^?{}( ifstream & is );
 | 
|---|
| 134 | 
 | 
|---|
| 135 | extern ifstream & sin, & stdin;                                                 // aliases
 | 
|---|
| 136 | 
 | 
|---|
| 137 | 
 | 
|---|
| 138 | // *********************************** exceptions ***********************************
 | 
|---|
| 139 | 
 | 
|---|
| 140 | 
 | 
|---|
| 141 | exception open_failure {
 | 
|---|
| 142 |         union {
 | 
|---|
| 143 |                 ofstream * ostream;
 | 
|---|
| 144 |                 ifstream * istream;
 | 
|---|
| 145 |         };
 | 
|---|
| 146 |         // TEMPORARY: need polymorphic exceptions
 | 
|---|
| 147 |         int tag;                                                                                        // 1 => ostream; 0 => istream
 | 
|---|
| 148 | };
 | 
|---|
| 149 | 
 | 
|---|
| 150 | void ?{}( open_failure & this, ofstream & );
 | 
|---|
| 151 | void ?{}( open_failure & this, ifstream & );
 | 
|---|
| 152 | 
 | 
|---|
| 153 | exception close_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 ?{}( close_failure & this, ofstream & );
 | 
|---|
| 163 | void ?{}( close_failure & this, ifstream & );
 | 
|---|
| 164 | 
 | 
|---|
| 165 | exception write_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 ?{}( write_failure & this, ofstream & );
 | 
|---|
| 175 | void ?{}( write_failure & this, ifstream & );
 | 
|---|
| 176 | 
 | 
|---|
| 177 | exception read_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 ?{}( read_failure & this, ofstream & );
 | 
|---|
| 187 | void ?{}( read_failure & this, ifstream & );
 | 
|---|
| 188 | 
 | 
|---|
| 189 | // Local Variables: //
 | 
|---|
| 190 | // mode: c //
 | 
|---|
| 191 | // tab-width: 4 //
 | 
|---|
| 192 | // End: //
 | 
|---|