source: src/libcfa/fstream.c @ 6ba16fa

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 6ba16fa was 09687aa, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

complete conversion of iostream/fstream to use references

  • Property mode set to 100644
File size: 6.0 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
[09687aa]12// Last Modified On : Thu Dec  7 08:35:01 2017
13// Update Count     : 270
[86bd7c1f]14//
15
[d3b7937]16#include "fstream"
[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
[09687aa]29void ?{}( ofstream & os, void * file, _Bool sepDefault, _Bool sepOnOff, const char * separator, const char * tupleSeparator ) {
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
[09687aa]39_Bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; }
40void sepReset( ofstream & os ) { os.sepOnOff = os.sepDefault; }
41void sepReset( ofstream & os, _Bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; }
42const char * sepGetCur( ofstream & os ) { return os.sepCur; }
43void sepSetCur( ofstream & os, const char * sepCur ) { os.sepCur = sepCur; }
44_Bool getNL( ofstream & os ) { return os.sawNL; }
45void setNL( ofstream & os, _Bool state ) { os.sawNL = state; }
[829c907]46
[9ebd778]47// public
[09687aa]48void ?{}( ofstream & os ) {}
[829c907]49
[09687aa]50void ?{}( ofstream & os, const char * name, const char * mode ) {
51        open( os, name, mode );
52}
53
54void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
55void sepOff( ofstream & os ) { os.sepOnOff = false; }
56
57_Bool sepDisable( ofstream & os ) {
58        _Bool temp = os.sepDefault;
59        os.sepDefault = false;
[53ba273]60        sepReset( os );
61        return temp;
62} // sepDisable
[6152c81]63
[09687aa]64_Bool sepEnable( ofstream & os ) {
65        _Bool temp = os.sepDefault;
66        os.sepDefault = true;
67        if ( os.sepOnOff ) sepReset( os );                                      // start of line ?
[53ba273]68        return temp;
69} // sepEnable
[90c3b1c]70
[09687aa]71const char * sepGet( ofstream & os ) { return os.separator; }
72void sepSet( ofstream & os, const char * s ) {
[9ebd778]73        assert( s );
[09687aa]74        strncpy( os.separator, s, sepSize - 1 );
75        os.separator[sepSize - 1] = '\0';
[9ebd778]76} // sepSet
77
[09687aa]78const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator; }
79void sepSetTuple( ofstream & os, const char * s ) {
[9ebd778]80        assert( s );
[09687aa]81        strncpy( os.tupleSeparator, s, sepSize - 1 );
82        os.tupleSeparator[sepSize - 1] = '\0';
[9ebd778]83} // sepSet
84
[09687aa]85int fail( ofstream & os ) {
86        return ferror( (FILE *)(os.file) );
[6ba0659]87} // fail
88
[09687aa]89int flush( ofstream & os ) {
90        return fflush( (FILE *)(os.file) );
[6ba0659]91} // flush
92
[09687aa]93void open( ofstream & os, const char * name, const char * mode ) {
[90c3b1c]94        FILE *file = fopen( name, mode );
95        if ( file == 0 ) {                                                                      // do not change unless successful
[53ba273]96                fprintf( stderr, IO_MSG "open output file \"%s\", ", name );
97                perror( 0 );
[6ba0659]98                exit( EXIT_FAILURE );
99        } // if
[09687aa]100        (os){ file, true, false, " ", ", " };
[6ba0659]101} // open
102
[09687aa]103void close( ofstream & os ) {
104        if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return;
[6ba0659]105
[09687aa]106        if ( fclose( (FILE *)(os.file) ) == EOF ) {
[6ba0659]107                perror( IO_MSG "close output" );
[356189a]108        } // if
[6ba0659]109} // close
110
[09687aa]111ofstream & write( ofstream & os, const char * data, unsigned long int size ) {
[6ba0659]112        if ( fail( os ) ) {
113                fprintf( stderr, "attempt write I/O on failed stream\n" );
114                exit( EXIT_FAILURE );
115        } // if
116
[09687aa]117        if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
[6ba0659]118                perror( IO_MSG "write" );
119                exit( EXIT_FAILURE );
[839ccbb]120        } // if
[86bd7c1f]121        return os;
[839ccbb]122} // write
[51b7345]123
[09687aa]124int fmt( ofstream & os, const char format[], ... ) {
[5d125e4]125        va_list args;
[829c907]126        va_start( args, format );
[09687aa]127        int len = vfprintf( (FILE *)(os.file), format, args );
[90c3b1c]128        if ( len == EOF ) {
[09687aa]129                if ( ferror( (FILE *)(os.file) ) ) {
[90c3b1c]130                        fprintf( stderr, "invalid write\n" );
131                        exit( EXIT_FAILURE );
132                } // if
133        } // if
[5d125e4]134        va_end( args );
[b72bad4f]135
136        sepReset( os );                                                                         // reset separator
[90c3b1c]137        return len;
[829c907]138} // fmt
139
[d395012]140static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, " ", ", " };
[09687aa]141ofstream & sout = soutFile;
[d395012]142static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, " ", ", " };
[09687aa]143ofstream & serr = serrFile;
[51b7345]144
[90c3b1c]145
[6ba0659]146//---------------------------------------
[51b7345]147
[09687aa]148// private
149void ?{}( ifstream & is, void * file ) {
150        is.file = file;
151}
152
153// public
154void ?{}( ifstream & is ) {}
[51b7345]155
[09687aa]156void ?{}( ifstream & is, const char * name ) {
157        open( is, name );
158}
159
160int fail( ifstream & is ) {
161        return ferror( (FILE *)(is.file) );
[6ba0659]162} // fail
163
[09687aa]164int eof( ifstream & is ) {
165        return feof( (FILE *)(is.file) );
[6ba0659]166} // eof
167
[09687aa]168void open( ifstream & is, const char * name ) {
169        FILE *file = fopen( name, "r" );
[53a6c2a]170        if ( file == 0 ) {                                                                      // do not change unless successful
[53ba273]171                fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
172                perror( 0 );
[90c3b1c]173                exit( EXIT_FAILURE );
[6ba0659]174        } // if
[09687aa]175        is.file = file;
[90c3b1c]176} // open
[6ba0659]177
[09687aa]178void close( ifstream & is ) {
179        if ( (FILE *)(is.file) == stdin ) return;
[90c3b1c]180
[09687aa]181        if ( fclose( (FILE *)(is.file) ) == EOF ) {
[90c3b1c]182                perror( IO_MSG "close input" );
[356189a]183        } // if
[90c3b1c]184} // close
185
[09687aa]186ifstream & read( ifstream & is, char * data, unsigned long int size ) {
[6ba0659]187        if ( fail( is ) ) {
188                fprintf( stderr, "attempt read I/O on failed stream\n" );
189                exit( EXIT_FAILURE );
190        } // if
191
[09687aa]192        if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
[6ba0659]193                perror( IO_MSG "read" );
194                exit( EXIT_FAILURE );
195        } // if
[86bd7c1f]196        return is;
[6ba0659]197} // read
[356189a]198
[09687aa]199ifstream &ungetc( ifstream & is, char c ) {
[6ba0659]200        if ( fail( is ) ) {
201                fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
202                exit( EXIT_FAILURE );
203        } // if
[51b7345]204
[09687aa]205        if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
[6ba0659]206                perror( IO_MSG "ungetc" );
207                exit( EXIT_FAILURE );
208        } // if
209        return is;
210} // ungetc
[51b7345]211
[09687aa]212int fmt( ifstream & is, const char format[], ... ) {
[5d125e4]213        va_list args;
[51b7345]214
[829c907]215        va_start( args, format );
[09687aa]216        int len = vfscanf( (FILE *)(is.file), format, args );
[90c3b1c]217        if ( len == EOF ) {
[09687aa]218                if ( ferror( (FILE *)(is.file) ) ) {
[90c3b1c]219                        fprintf( stderr, "invalid read\n" );
220                        exit( EXIT_FAILURE );
221                } // if
222        } // if
[5d125e4]223        va_end( args );
[90c3b1c]224        return len;
[829c907]225} // fmt
[51b7345]226
227
[6ba0659]228static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
[09687aa]229ifstream & sin = sinFile;
[86bd7c1f]230
231// Local Variables: //
232// tab-width: 4 //
233// End: //
Note: See TracBrowser for help on using the repository browser.