source: libcfa/src/fstream.cfa @ ed3935da

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

update ofstream constructor

  • Property mode set to 100644
File size: 6.9 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
[5cb2b8c]12// Last Modified On : Thu May 16 08:33:28 2019
13// Update Count     : 328
[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
[5cb2b8c]29void ?{}( ofstream & os, void * file ) {
[09687aa]30        os.file = file;
[5cb2b8c]31        os.sepDefault = true;
32        os.sepOnOff = false;
33        os.nlOnOff = true;
34        os.prt = false;
[0efb269]35        os.sawNL = false;
[5cb2b8c]36        sepSet( os, " " );
[09687aa]37        sepSetCur( os, sepGet( os ) );
[5cb2b8c]38        sepSetTuple( os, ", " );
[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 ) {
[5cb2b8c]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__
[5cb2b8c]112        (os){ file };
[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
[5cb2b8c]154static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
[09687aa]155ofstream & sout = soutFile;
[5cb2b8c]156static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
[09687aa]157ofstream & serr = serrFile;
[51b7345]158
[5cb2b8c]159// static ofstream sexitFile = { (FILE *)(&_IO_2_1_stdout_) };
160// ofstream & sexit = sexitFile;
161// static ofstream sabortFile = { (FILE *)(&_IO_2_1_stderr_) };
162// ofstream & sabort = sabortFile;
163
164void nl( ofstream & os ) {
165        if ( getANL( os ) ) (ofstream &)(nl( os ));                     // implementation only
166        else setPrt( os, false );                                                       // turn off
167}
[90c3b1c]168
[6ba0659]169//---------------------------------------
[51b7345]170
[09687aa]171// private
172void ?{}( ifstream & is, void * file ) {
173        is.file = file;
[0efb269]174        is.nlOnOff = false;
[09687aa]175}
176
177// public
[8da74119]178void ?{}( ifstream & is ) {     is.file = 0; }
[51b7345]179
[8da74119]180void ?{}( ifstream & is, const char * name, const char * mode ) {
181        open( is, name, mode );
182}
[09687aa]183void ?{}( ifstream & is, const char * name ) {
[8da74119]184        open( is, name, "r" );
[09687aa]185}
186
[0efb269]187void nlOn( ifstream & os ) { os.nlOnOff = true; }
188void nlOff( ifstream & os ) { os.nlOnOff = false; }
189bool getANL( ifstream & os ) { return os.nlOnOff; }
190
[09687aa]191int fail( ifstream & is ) {
[8da74119]192        return is.file == 0 || ferror( (FILE *)(is.file) );
[6ba0659]193} // fail
194
[09687aa]195int eof( ifstream & is ) {
196        return feof( (FILE *)(is.file) );
[6ba0659]197} // eof
198
[8da74119]199void open( ifstream & is, const char * name, const char * mode ) {
[8a25be9]200        FILE * file = fopen( name, mode );
[93c2e0a]201        #ifdef __CFA_DEBUG__
202        if ( file == 0 ) {
[8a25be9]203                abort( IO_MSG "open input file \"%s\", %s\n", name, strerror( errno ) );
[93c2e0a]204        } // if
205        #endif // __CFA_DEBUG__
[09687aa]206        is.file = file;
[90c3b1c]207} // open
[6ba0659]208
[8da74119]209void open( ifstream & is, const char * name ) {
210        open( is, name, "r" );
211} // open
212
[09687aa]213void close( ifstream & is ) {
214        if ( (FILE *)(is.file) == stdin ) return;
[90c3b1c]215
[09687aa]216        if ( fclose( (FILE *)(is.file) ) == EOF ) {
[8a25be9]217                abort( IO_MSG "close input %s", strerror( errno ) );
[356189a]218        } // if
[90c3b1c]219} // close
220
[91d766d]221ifstream & read( ifstream & is, char * data, size_t size ) {
[6ba0659]222        if ( fail( is ) ) {
[8a25be9]223                abort( "attempt read I/O on failed stream\n" );
[6ba0659]224        } // if
225
[09687aa]226        if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
[8a25be9]227                abort( IO_MSG "read %s", strerror( errno ) );
[6ba0659]228        } // if
[86bd7c1f]229        return is;
[6ba0659]230} // read
[356189a]231
[09687aa]232ifstream &ungetc( ifstream & is, char c ) {
[6ba0659]233        if ( fail( is ) ) {
[8a25be9]234                abort( "attempt ungetc I/O on failed stream\n" );
[6ba0659]235        } // if
[51b7345]236
[09687aa]237        if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
[8a25be9]238                abort( IO_MSG "ungetc %s", strerror( errno ) );
[6ba0659]239        } // if
240        return is;
241} // ungetc
[51b7345]242
[09687aa]243int fmt( ifstream & is, const char format[], ... ) {
[5d125e4]244        va_list args;
[51b7345]245
[829c907]246        va_start( args, format );
[09687aa]247        int len = vfscanf( (FILE *)(is.file), format, args );
[90c3b1c]248        if ( len == EOF ) {
[09687aa]249                if ( ferror( (FILE *)(is.file) ) ) {
[8a25be9]250                        abort( "invalid read\n" );
[90c3b1c]251                } // if
252        } // if
[5d125e4]253        va_end( args );
[90c3b1c]254        return len;
[829c907]255} // fmt
[51b7345]256
257
[6ba0659]258static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
[09687aa]259ifstream & sin = sinFile;
[86bd7c1f]260
261// Local Variables: //
262// tab-width: 4 //
263// End: //
Note: See TracBrowser for help on using the repository browser.