source: src/libcfa/fstream.c@ 5bd0aad

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