source: src/libcfa/fstream.c @ a0b3e32

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 a0b3e32 was 53a6c2a, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change meaning of sepOn, and replace #if with #pragma once in include files

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