source: libcfa/src/fstream.cfa @ 2185df1

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resnenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 2185df1 was 0e0f128c, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[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//
[356189a]7// fstream.c --
[86bd7c1f]8//
[90c3b1c]9// Author           : Peter A. Buhr
[86bd7c1f]10// Created On       : Wed May 27 17:56:53 2015
[5d125e4]11// Last Modified By : Peter A. Buhr
[93c2e0a]12// Last Modified On : Fri Aug 10 18:19:40 2018
13// Update Count     : 284
[86bd7c1f]14//
15
[58b6d1b]16#include "fstream.hfa"
[51b7345]17
[90c3b1c]18#include <stdio.h>                                                                              // vfprintf, vfscanf
19#include <stdlib.h>                                                                             // exit
20#include <stdarg.h>                                                                             // varargs
21#include <string.h>                                                                             // strlen
[6152c81]22#include <stdbool.h>                                                                    // true/false
[90c3b1c]23#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
24#include <complex.h>                                                                    // creal, cimag
[91c389a]25#include <assert.h>
[51b7345]26
[53ba273]27#define IO_MSG "I/O error: "
[6ba0659]28
[93c2e0a]29void ?{}( ofstream & os, void * file, bool sepDefault, bool sepOnOff, const char * separator, const char * tupleSeparator ) {
[09687aa]30        os.file = file;
31        os.sepDefault = sepDefault;
32        os.sepOnOff = sepOnOff;
33        sepSet( os, separator );
34        sepSetCur( os, sepGet( os ) );
35        sepSetTuple( os, tupleSeparator );
[0583064b]36}
37
[9ebd778]38// private
[93c2e0a]39bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; }
[09687aa]40void sepReset( ofstream & os ) { os.sepOnOff = os.sepDefault; }
[93c2e0a]41void sepReset( ofstream & os, bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; }
[09687aa]42const char * sepGetCur( ofstream & os ) { return os.sepCur; }
43void sepSetCur( ofstream & os, const char * sepCur ) { os.sepCur = sepCur; }
[93c2e0a]44bool getNL( ofstream & os ) { return os.sawNL; }
45void setNL( ofstream & os, bool state ) { os.sawNL = state; }
[829c907]46
[9ebd778]47// public
[8da74119]48void ?{}( ofstream & os ) { os.file = 0; }
[829c907]49
[09687aa]50void ?{}( ofstream & os, const char * name, const char * mode ) {
51        open( os, name, mode );
52}
[8da74119]53void ?{}( ofstream & os, const char * name ) {
54        open( os, name, "w" );
55}
[09687aa]56
57void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
58void sepOff( ofstream & os ) { os.sepOnOff = false; }
59
[93c2e0a]60bool sepDisable( ofstream & os ) {
61        bool temp = os.sepDefault;
[09687aa]62        os.sepDefault = false;
[53ba273]63        sepReset( os );
64        return temp;
65} // sepDisable
[6152c81]66
[93c2e0a]67bool sepEnable( ofstream & os ) {
68        bool temp = os.sepDefault;
[09687aa]69        os.sepDefault = true;
70        if ( os.sepOnOff ) sepReset( os );                                      // start of line ?
[53ba273]71        return temp;
72} // sepEnable
[90c3b1c]73
[09687aa]74const char * sepGet( ofstream & os ) { return os.separator; }
75void sepSet( ofstream & os, const char * s ) {
[9ebd778]76        assert( s );
[09687aa]77        strncpy( os.separator, s, sepSize - 1 );
78        os.separator[sepSize - 1] = '\0';
[9ebd778]79} // sepSet
80
[09687aa]81const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator; }
82void sepSetTuple( ofstream & os, const char * s ) {
[9ebd778]83        assert( s );
[09687aa]84        strncpy( os.tupleSeparator, s, sepSize - 1 );
85        os.tupleSeparator[sepSize - 1] = '\0';
[9ebd778]86} // sepSet
87
[09687aa]88int fail( ofstream & os ) {
[8da74119]89        return os.file == 0 || ferror( (FILE *)(os.file) );
[6ba0659]90} // fail
91
[09687aa]92int flush( ofstream & os ) {
93        return fflush( (FILE *)(os.file) );
[6ba0659]94} // flush
95
[09687aa]96void open( ofstream & os, const char * name, const char * mode ) {
[90c3b1c]97        FILE *file = fopen( name, mode );
[93c2e0a]98        #ifdef __CFA_DEBUG__
99        if ( file == 0 ) {
100                fprintf( stderr, IO_MSG "open output file \"%s\", ", name );
101                perror( 0 );
102                exit( EXIT_FAILURE );
103        } // if
104        #endif // __CFA_DEBUG__
[09687aa]105        (os){ file, true, false, " ", ", " };
[6ba0659]106} // open
107
[8da74119]108void open( ofstream & os, const char * name ) {
109        open( os, name, "w" );
110} // open
111
[09687aa]112void close( ofstream & os ) {
113        if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return;
[6ba0659]114
[09687aa]115        if ( fclose( (FILE *)(os.file) ) == EOF ) {
[6ba0659]116                perror( IO_MSG "close output" );
[356189a]117        } // if
[6ba0659]118} // close
119
[91d766d]120ofstream & write( ofstream & os, const char * data, size_t size ) {
[6ba0659]121        if ( fail( os ) ) {
122                fprintf( stderr, "attempt write I/O on failed stream\n" );
123                exit( EXIT_FAILURE );
124        } // if
125
[09687aa]126        if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
[6ba0659]127                perror( IO_MSG "write" );
128                exit( EXIT_FAILURE );
[839ccbb]129        } // if
[86bd7c1f]130        return os;
[839ccbb]131} // write
[51b7345]132
[09687aa]133int fmt( ofstream & os, const char format[], ... ) {
[5d125e4]134        va_list args;
[829c907]135        va_start( args, format );
[09687aa]136        int len = vfprintf( (FILE *)(os.file), format, args );
[90c3b1c]137        if ( len == EOF ) {
[09687aa]138                if ( ferror( (FILE *)(os.file) ) ) {
[90c3b1c]139                        fprintf( stderr, "invalid write\n" );
140                        exit( EXIT_FAILURE );
141                } // if
142        } // if
[5d125e4]143        va_end( args );
[b72bad4f]144
145        sepReset( os );                                                                         // reset separator
[90c3b1c]146        return len;
[829c907]147} // fmt
148
[d395012]149static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, " ", ", " };
[09687aa]150ofstream & sout = soutFile;
[d395012]151static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, " ", ", " };
[09687aa]152ofstream & serr = serrFile;
[51b7345]153
[90c3b1c]154
[6ba0659]155//---------------------------------------
[51b7345]156
[09687aa]157// private
158void ?{}( ifstream & is, void * file ) {
159        is.file = file;
160}
161
162// public
[8da74119]163void ?{}( ifstream & is ) {     is.file = 0; }
[51b7345]164
[8da74119]165void ?{}( ifstream & is, const char * name, const char * mode ) {
166        open( is, name, mode );
167}
[09687aa]168void ?{}( ifstream & is, const char * name ) {
[8da74119]169        open( is, name, "r" );
[09687aa]170}
171
172int fail( ifstream & is ) {
[8da74119]173        return is.file == 0 || ferror( (FILE *)(is.file) );
[6ba0659]174} // fail
175
[09687aa]176int eof( ifstream & is ) {
177        return feof( (FILE *)(is.file) );
[6ba0659]178} // eof
179
[8da74119]180void open( ifstream & is, const char * name, const char * mode ) {
181        FILE *file = fopen( name, mode );
[93c2e0a]182        #ifdef __CFA_DEBUG__
183        if ( file == 0 ) {
184                fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
185                perror( 0 );
186                exit( EXIT_FAILURE );
187        } // if
188        #endif // __CFA_DEBUG__
[09687aa]189        is.file = file;
[90c3b1c]190} // open
[6ba0659]191
[8da74119]192void open( ifstream & is, const char * name ) {
193        open( is, name, "r" );
194} // open
195
[09687aa]196void close( ifstream & is ) {
197        if ( (FILE *)(is.file) == stdin ) return;
[90c3b1c]198
[09687aa]199        if ( fclose( (FILE *)(is.file) ) == EOF ) {
[90c3b1c]200                perror( IO_MSG "close input" );
[356189a]201        } // if
[90c3b1c]202} // close
203
[91d766d]204ifstream & read( ifstream & is, char * data, size_t size ) {
[6ba0659]205        if ( fail( is ) ) {
206                fprintf( stderr, "attempt read I/O on failed stream\n" );
207                exit( EXIT_FAILURE );
208        } // if
209
[09687aa]210        if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
[6ba0659]211                perror( IO_MSG "read" );
212                exit( EXIT_FAILURE );
213        } // if
[86bd7c1f]214        return is;
[6ba0659]215} // read
[356189a]216
[09687aa]217ifstream &ungetc( ifstream & is, char c ) {
[6ba0659]218        if ( fail( is ) ) {
219                fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
220                exit( EXIT_FAILURE );
221        } // if
[51b7345]222
[09687aa]223        if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
[6ba0659]224                perror( IO_MSG "ungetc" );
225                exit( EXIT_FAILURE );
226        } // if
227        return is;
228} // ungetc
[51b7345]229
[09687aa]230int fmt( ifstream & is, const char format[], ... ) {
[5d125e4]231        va_list args;
[51b7345]232
[829c907]233        va_start( args, format );
[09687aa]234        int len = vfscanf( (FILE *)(is.file), format, args );
[90c3b1c]235        if ( len == EOF ) {
[09687aa]236                if ( ferror( (FILE *)(is.file) ) ) {
[90c3b1c]237                        fprintf( stderr, "invalid read\n" );
238                        exit( EXIT_FAILURE );
239                } // if
240        } // if
[5d125e4]241        va_end( args );
[90c3b1c]242        return len;
[829c907]243} // fmt
[51b7345]244
245
[6ba0659]246static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
[09687aa]247ifstream & sin = sinFile;
[86bd7c1f]248
249// Local Variables: //
250// tab-width: 4 //
251// End: //
Note: See TracBrowser for help on using the repository browser.