source: src/libcfa/fstream.c@ 37f0da8

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

switch from std=c99 to std=gnu99, update latex macros, refrat and extend user manual, update limits/iostream/fstream, add rational numbers

  • Property mode set to 100644
File size: 4.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 : Wed Apr 6 17:55:27 2016
13// Update Count : 176
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 <float.h> // DBL_DIG, LDBL_DIG
24#include <complex.h> // creal, cimag
25}
26
27#define IO_MSG "I/O error: "
28
29_Bool sepPrt( ofstream * os ) { return os->sepOnOff; }
30void sepOn( ofstream * os ) { os->sepOnOff = 1; }
31void sepOff( ofstream * os ) { os->sepOnOff = 0; }
32void sepReset( ofstream * os ) { os->sepOnOff = os->sepDefault; }
33void sepReset( ofstream * os, _Bool reset ) { os->sepDefault = reset; os->sepOnOff = os->sepDefault; }
34void sepSet( ofstream * os, const char * s ) {
35 strncpy( &(os->separator[0]), s, separateSize - 1 );
36 os->separator[separateSize - 1] = '\0';
37} // sepSet
38const char * sepGet( ofstream * os ) { return &(os->separator[0]); }
39_Bool sepDisable( ofstream *os ) {
40 _Bool temp = os->sepDefault;
41 os->sepDefault = 0;
42 sepReset( os );
43 return temp;
44} // sepDisable
45_Bool sepEnable( ofstream *os ) {
46 _Bool temp = os->sepDefault;
47 os->sepDefault = 1;
48 sepReset( os );
49 return temp;
50} // sepEnable
51
52int fail( ofstream * os ) {
53 return ferror( (FILE *)(os->file) );
54} // fail
55
56int flush( ofstream * os ) {
57 return fflush( (FILE *)(os->file) );
58} // flush
59
60void open( ofstream * os, const char * name, const char * mode ) {
61 FILE *file = fopen( name, mode );
62 if ( file == 0 ) { // do not change unless successful
63 fprintf( stderr, IO_MSG "open output file \"%s\", ", name );
64 perror( 0 );
65 exit( EXIT_FAILURE );
66 } // if
67 os->file = file;
68 sepOff( os );
69 sepSet( os, " " );
70} // open
71
72void close( ofstream * os ) {
73 if ( (FILE *)(os->file) == stdout || (FILE *)(os->file) == stderr ) return;
74
75 if ( fclose( (FILE *)(os->file) ) == EOF ) {
76 perror( IO_MSG "close output" );
77 } // if
78} // close
79
80ofstream * write( ofstream * os, const char * data, unsigned long int size ) {
81 if ( fail( os ) ) {
82 fprintf( stderr, "attempt write I/O on failed stream\n" );
83 exit( EXIT_FAILURE );
84 } // if
85
86 if ( fwrite( data, 1, size, (FILE *)(os->file) ) != size ) {
87 perror( IO_MSG "write" );
88 exit( EXIT_FAILURE );
89 } // if
90 return os;
91} // write
92
93int prtfmt( ofstream * os, const char fmt[], ... ) {
94 va_list args;
95
96 va_start( args, fmt );
97 int len = vfprintf( (FILE *)(os->file), fmt, args );
98 if ( len == EOF ) {
99 if ( ferror( (FILE *)(os->file) ) ) {
100 fprintf( stderr, "invalid write\n" );
101 exit( EXIT_FAILURE );
102 } // if
103 } // if
104 va_end( args );
105 return len;
106} // prtfmt
107
108
109static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), 1, 0, { ' ', '\0' } };
110ofstream *sout = &soutFile;
111static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), 1, 0, { ' ', '\0' } };
112ofstream *serr = &serrFile;
113
114
115//---------------------------------------
116
117
118int fail( ifstream * is ) {
119 return ferror( (FILE *)(is->file) );
120} // fail
121
122int eof( ifstream * is ) {
123 return feof( (FILE *)(is->file) );
124} // eof
125
126void open( ifstream * is, const char * name, const char * mode ) {
127 FILE *t = fopen( name, mode );
128 if ( t == 0 ) { // do not change unless successful
129 fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
130 perror( 0 );
131 exit( EXIT_FAILURE );
132 } // if
133 is->file = t;
134} // open
135
136void close( ifstream * is ) {
137 if ( (FILE *)(is->file) == stdin ) return;
138
139 if ( fclose( (FILE *)(is->file) ) == EOF ) {
140 perror( IO_MSG "close input" );
141 } // if
142} // close
143
144ifstream * read( ifstream * is, char * data, unsigned long int size ) {
145 if ( fail( is ) ) {
146 fprintf( stderr, "attempt read I/O on failed stream\n" );
147 exit( EXIT_FAILURE );
148 } // if
149
150 if ( fread( data, size, 1, (FILE *)(is->file) ) == 0 ) {
151 perror( IO_MSG "read" );
152 exit( EXIT_FAILURE );
153 } // if
154 return is;
155} // read
156
157ifstream *ungetc( ifstream * is, char c ) {
158 if ( fail( is ) ) {
159 fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
160 exit( EXIT_FAILURE );
161 } // if
162
163 if ( ungetc( c, (FILE *)(is->file) ) == EOF ) {
164 perror( IO_MSG "ungetc" );
165 exit( EXIT_FAILURE );
166 } // if
167 return is;
168} // ungetc
169
170int scanfmt( ifstream * is, const char fmt[], ... ) {
171 va_list args;
172
173 va_start( args, fmt );
174 int len = vfscanf( (FILE *)(is->file), fmt, args );
175 if ( len == EOF ) {
176 if ( ferror( (FILE *)(is->file) ) ) {
177 fprintf( stderr, "invalid read\n" );
178 exit( EXIT_FAILURE );
179 } // if
180 } // if
181 va_end( args );
182 return len;
183} // prtfmt
184
185
186static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
187ifstream *sin = &sinFile;
188
189// Local Variables: //
190// tab-width: 4 //
191// End: //
Note: See TracBrowser for help on using the repository browser.