[86bd7c1f] | 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 | // |
---|
[bb82c03] | 7 | // fstream -- |
---|
[86bd7c1f] | 8 | // |
---|
[90c3b1c] | 9 | // Author : Peter A. Buhr |
---|
[86bd7c1f] | 10 | // Created On : Wed May 27 17:56:53 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[e474cf09] | 12 | // Last Modified On : Mon Mar 1 22:45:08 2021 |
---|
| 13 | // Update Count : 217 |
---|
[86bd7c1f] | 14 | // |
---|
| 15 | |
---|
[53a6c2a] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[e474cf09] | 18 | #include "bits/weakso_locks.hfa" // mutex_lock |
---|
[58b6d1b] | 19 | #include "iostream.hfa" |
---|
[91e52be] | 20 | #include <exception.hfa> |
---|
[51b7345] | 21 | |
---|
[65240bb] | 22 | |
---|
[8d321f9] | 23 | // *********************************** ofstream *********************************** |
---|
[65240bb] | 24 | |
---|
| 25 | |
---|
[53a6c2a] | 26 | enum { sepSize = 16 }; |
---|
[53ba273] | 27 | struct ofstream { |
---|
[d1a9ff5] | 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]; |
---|
[e474cf09] | 37 | multiple_acquisition_lock $lock; |
---|
| 38 | bool $acquired; |
---|
[53ba273] | 39 | }; // ofstream |
---|
[90c3b1c] | 40 | |
---|
[9ebd778] | 41 | // private |
---|
[d1a9ff5] | 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 ); |
---|
[9ebd778] | 52 | |
---|
| 53 | // public |
---|
[09687aa] | 54 | void sepOn( ofstream & ); |
---|
| 55 | void sepOff( ofstream & ); |
---|
[93c2e0a] | 56 | bool sepDisable( ofstream & ); |
---|
| 57 | bool sepEnable( ofstream & ); |
---|
[200fcb3] | 58 | void nlOn( ofstream & ); |
---|
| 59 | void nlOff( ofstream & ); |
---|
[9ebd778] | 60 | |
---|
[09687aa] | 61 | const char * sepGet( ofstream & ); |
---|
[e3fea42] | 62 | void sepSet( ofstream &, const char [] ); |
---|
[09687aa] | 63 | const char * sepGetTuple( ofstream & ); |
---|
[e3fea42] | 64 | void sepSetTuple( ofstream &, const char [] ); |
---|
[6152c81] | 65 | |
---|
[65240bb] | 66 | void ends( ofstream & os ); |
---|
[09687aa] | 67 | int fail( ofstream & ); |
---|
| 68 | int flush( ofstream & ); |
---|
[e3fea42] | 69 | void open( ofstream &, const char name[], const char mode[] ); |
---|
| 70 | void open( ofstream &, const char name[] ); |
---|
[09687aa] | 71 | void close( ofstream & ); |
---|
[e3fea42] | 72 | ofstream & write( ofstream &, const char data[], size_t size ); |
---|
[40cac90] | 73 | int fmt( ofstream &, const char format[], ... ) __attribute__(( format(printf, 2, 3) )); |
---|
[e474cf09] | 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 ); |
---|
[829c907] | 82 | |
---|
[09687aa] | 83 | void ?{}( ofstream & os ); |
---|
[e3fea42] | 84 | void ?{}( ofstream & os, const char name[], const char mode[] ); |
---|
| 85 | void ?{}( ofstream & os, const char name[] ); |
---|
[4cae032] | 86 | void ^?{}( ofstream & os ); |
---|
[09687aa] | 87 | |
---|
[a87d40b] | 88 | extern ofstream & sout, & stdout, & serr, & stderr; // aliases |
---|
[65240bb] | 89 | extern ofstream & exit, & abort; |
---|
| 90 | |
---|
[51b7345] | 91 | |
---|
[8d321f9] | 92 | // *********************************** ifstream *********************************** |
---|
[5cb2b8c] | 93 | |
---|
[51b7345] | 94 | |
---|
[53ba273] | 95 | struct ifstream { |
---|
[d1a9ff5] | 96 | void * $file; |
---|
| 97 | bool $nlOnOff; |
---|
[e474cf09] | 98 | multiple_acquisition_lock $lock; |
---|
| 99 | bool $acquired; |
---|
[53ba273] | 100 | }; // ifstream |
---|
[6ba0659] | 101 | |
---|
[09687aa] | 102 | // public |
---|
[0efb269] | 103 | void nlOn( ifstream & ); |
---|
| 104 | void nlOff( ifstream & ); |
---|
| 105 | bool getANL( ifstream & ); |
---|
[e474cf09] | 106 | void ends( ifstream & ); |
---|
[09687aa] | 107 | int fail( ifstream & is ); |
---|
| 108 | int eof( ifstream & is ); |
---|
[e3fea42] | 109 | void open( ifstream & is, const char name[], const char mode[] ); |
---|
| 110 | void open( ifstream & is, const char name[] ); |
---|
[09687aa] | 111 | void close( ifstream & is ); |
---|
[91d766d] | 112 | ifstream & read( ifstream & is, char * data, size_t size ); |
---|
[09687aa] | 113 | ifstream & ungetc( ifstream & is, char c ); |
---|
[40cac90] | 114 | int fmt( ifstream &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) )); |
---|
[e474cf09] | 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 ); |
---|
[09687aa] | 123 | |
---|
| 124 | void ?{}( ifstream & is ); |
---|
[e3fea42] | 125 | void ?{}( ifstream & is, const char name[], const char mode[] ); |
---|
| 126 | void ?{}( ifstream & is, const char name[] ); |
---|
[4cae032] | 127 | void ^?{}( ifstream & is ); |
---|
[51b7345] | 128 | |
---|
[a87d40b] | 129 | extern ifstream & sin, & stdin; // aliases |
---|
[51b7345] | 130 | |
---|
[91e52be] | 131 | |
---|
[8d321f9] | 132 | // *********************************** exceptions *********************************** |
---|
[91e52be] | 133 | |
---|
| 134 | |
---|
[ecfd758] | 135 | EHM_EXCEPTION(Open_Failure)( |
---|
[91e52be] | 136 | union { |
---|
| 137 | ofstream * ostream; |
---|
| 138 | ifstream * istream; |
---|
| 139 | }; |
---|
[8d321f9] | 140 | // TEMPORARY: need polymorphic exceptions |
---|
| 141 | int tag; // 1 => ostream; 0 => istream |
---|
[91e52be] | 142 | ); |
---|
| 143 | |
---|
[8d321f9] | 144 | void ?{}( Open_Failure & this, ofstream & ostream ); |
---|
| 145 | void ?{}( Open_Failure & this, ifstream & istream ); |
---|
[91e52be] | 146 | |
---|
[86bd7c1f] | 147 | // Local Variables: // |
---|
[d3b7937] | 148 | // mode: c // |
---|
[86bd7c1f] | 149 | // tab-width: 4 // |
---|
| 150 | // End: // |
---|