source: src/libcfa/fstream.c@ e85a8631

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor 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 e85a8631 was 5d125e4, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

start code allowing structures to no fields

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