source: src/libcfa/fstream.c@ a5de33e

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

support separator at end of line

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