source: src/libcfa/fstream.c @ 0583064b

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

additions to printing tuples, iostream, nad user documentations

  • Property mode set to 100644
File size: 5.6 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
[0583064b]12// Last Modified On : Thu Mar 23 08:20:41 2017
13// Update Count     : 226
[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
[53ba273]40_Bool sepPrt( ofstream * os ) { return os->sepOnOff; }
41void sepOn( ofstream * os ) { os->sepOnOff = 1; }
42void sepOff( ofstream * os ) { os->sepOnOff = 0; }
43void sepReset( ofstream * os ) { os->sepOnOff = os->sepDefault; }
44void sepReset( ofstream * os, _Bool reset ) { os->sepDefault = reset; os->sepOnOff = os->sepDefault; }
[829c907]45
46const char * sepGetCur( ofstream * os ) { return os->sepCur; }
47void sepSetCur( ofstream * os, const char * sepCur ) { os->sepCur = sepCur; }
48
[cb91437]49const char * sepGet( ofstream * os ) { return os->separator; }
[6152c81]50
[90c3b1c]51void sepSet( ofstream * os, const char * s ) {
[cb91437]52        assert( s );
53        strncpy( os->separator, s, separateSize - 1 );
[90c3b1c]54        os->separator[separateSize - 1] = '\0';
55} // sepSet
[6152c81]56
[cb91437]57const char * sepGetTuple( ofstream * os ) { return os->tupleSeparator; }
[829c907]58
59void sepSetTuple( ofstream * os, const char * s ) {
[cb91437]60        assert( s );
61        strncpy( os->tupleSeparator, s, separateSize - 1 );
[829c907]62        os->tupleSeparator[separateSize - 1] = '\0';
63} // sepSet
64
[53ba273]65_Bool sepDisable( ofstream *os ) {
66        _Bool temp = os->sepDefault;
[6152c81]67        os->sepDefault = false;
[53ba273]68        sepReset( os );
69        return temp;
70} // sepDisable
[6152c81]71
[53ba273]72_Bool sepEnable( ofstream *os ) {
73        _Bool temp = os->sepDefault;
[6152c81]74        os->sepDefault = true;
[53ba273]75        sepReset( os );
76        return temp;
77} // sepEnable
[90c3b1c]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
[0583064b]94        ?{}( os, file, 1, 0, " ", ", " );
[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
134static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), 1, 0, " ", ", " };
[6ba0659]135ofstream *sout = &soutFile;
[829c907]136static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), 1, 0, " ", ", " };
[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 ) {
152        FILE *t = fopen( name, mode );
153        if ( t == 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
[90c3b1c]158        is->file = t;
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.