source: src/libcfa/fstream.c@ 87d13cd

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 87d13cd was 829c907, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

add tuple separator to sout

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