source: src/libcfa/fstream.c @ ed9c0a8

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 ed9c0a8 was 91c389a, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change usages of assert to assert.h

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