source: libcfa/src/fstream.cfa @ 712348a

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 712348a was 0efb269, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add feature to input streams to read/not-read newline characters

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