source: src/libcfa/fstream.c@ 6ce67ce

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 6ce67ce was 6ba0659, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

major rewrite of I/O library and update example programs to use new I/O

  • Property mode set to 100644
File size: 3.2 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//
7// fstream.c --
8//
9// Author : Richard C. Bilson
10// Created On : Wed May 27 17:56:53 2015
11// Last Modified By : Peter A. Buhr
[6ba0659]12// Last Modified On : Wed Feb 17 14:03:05 2016
13// Update Count : 76
[86bd7c1f]14//
15
[d3b7937]16#include "fstream"
[51b73452]17
18extern "C" {
19#include <stdio.h>
20#include <stdlib.h>
21}
22
[134b86a]23struct ofstream {
[86bd7c1f]24 FILE *file;
[51b73452]25};
26
[6ba0659]27#define IO_MSG "I/O error "
28
29int fail( ofstream * os ) {
30 return ferror( os->file );
31} // fail
32
33int flush( ofstream * os ) {
34 return fflush( os->file );
35} // flush
36
37void open( ofstream ** os, const char * name, const char * mode ) {
38 FILE *t = fopen( name, mode );
39 if ( t == 0 ) { // do not change unless successful
40 perror( IO_MSG "open output" );
41 exit( EXIT_FAILURE );
42 } // if
43 (*os)->file = t;
44} // open
45
46void close( ofstream * os ) {
47 if ( os->file == stdout || os->file == stderr ) return;
48
49 if ( fclose( os->file ) == EOF ) {
50 perror( IO_MSG "close output" );
51 } // if
52} // close
53
54ofstream * write( ofstream * os, const char * data, streamsize_type size ) {
55 if ( fail( os ) ) {
56 fprintf( stderr, "attempt write I/O on failed stream\n" );
57 exit( EXIT_FAILURE );
58 } // if
59
60 if ( fwrite( data, 1, size, os->file ) != size ) {
61 perror( IO_MSG "write" );
62 exit( EXIT_FAILURE );
[839ccbb]63 } // if
[86bd7c1f]64 return os;
[839ccbb]65} // write
[51b73452]66
[6ba0659]67static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
68ofstream *sout = &soutFile;
69static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
70ofstream *serr = &serrFile;
[51b73452]71
[6ba0659]72//---------------------------------------
[51b73452]73
[134b86a]74struct ifstream {
[86bd7c1f]75 FILE *file;
[51b73452]76};
77
[6ba0659]78int fail( ifstream * is ) {
79 return ferror( is->file );
80} // fail
81
82int eof( ifstream * is ) {
83 return feof( is->file );
84} // eof
85
86ifstream * get( ifstream * is, int * data ) {
87 if ( fscanf( is->file, "%d", data ) == EOF ) {
88 if ( ferror( is->file ) ) {
89 fprintf( stderr, "invalid int read\n" );
90 exit( EXIT_FAILURE );
91 } // if
92 } // if
[86bd7c1f]93 return is;
[6ba0659]94} // read
95
96ifstream * read( ifstream * is, char * data, streamsize_type size ) {
97 if ( fail( is ) ) {
98 fprintf( stderr, "attempt read I/O on failed stream\n" );
99 exit( EXIT_FAILURE );
100 } // if
101
102 if ( fread( data, size, 1, is->file ) == 0 ) {
103 perror( IO_MSG "read" );
104 exit( EXIT_FAILURE );
105 } // if
[86bd7c1f]106 return is;
[6ba0659]107} // read
108
109ifstream *ungetc( ifstream * is, char c ) {
110 if ( fail( is ) ) {
111 fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
112 exit( EXIT_FAILURE );
113 } // if
[51b73452]114
[6ba0659]115 if ( ungetc( c, is->file ) == EOF ) {
116 perror( IO_MSG "ungetc" );
117 exit( EXIT_FAILURE );
118 } // if
119 return is;
120} // ungetc
[51b73452]121
[6ba0659]122void open( ifstream ** is, const char * name, const char * mode ) {
123 FILE *t = fopen( name, mode );
124 if ( t == 0 ) { // do not change unless successful
125 perror( IO_MSG "open input" );
126 exit( EXIT_FAILURE );
127 } // if
128 (*is)->file = t;
129} // open
[51b73452]130
[6ba0659]131void close( ifstream * is ) {
132 if ( is->file == stdin ) return;
[51b73452]133
[6ba0659]134 if ( fclose( is->file ) == EOF ) {
135 perror( IO_MSG "close input" );
136 } // if
137} // close
[51b73452]138
[6ba0659]139static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
140ifstream *sin = &sinFile;
[86bd7c1f]141
142// Local Variables: //
143// tab-width: 4 //
144// compile-command: "cfa fstream.c" //
145// End: //
Note: See TracBrowser for help on using the repository browser.