source: libcfa/src/fstream.cfa @ 10cb642

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

add aliases stdin, stdout, stderr for sin, sout, serr

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