source: src/libcfa/fstream.c@ c14cff1

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 with_gc
Last change on this file since c14cff1 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
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 : Richard C. Bilson
10// Created On : Wed May 27 17:56:53 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Feb 17 14:03:05 2016
13// Update Count : 76
14//
15
16#include "fstream"
17
18extern "C" {
19#include <stdio.h>
20#include <stdlib.h>
21}
22
23struct ofstream {
24 FILE *file;
25};
26
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 );
63 } // if
64 return os;
65} // write
66
67static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
68ofstream *sout = &soutFile;
69static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
70ofstream *serr = &serrFile;
71
72//---------------------------------------
73
74struct ifstream {
75 FILE *file;
76};
77
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
93 return is;
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
106 return is;
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
114
115 if ( ungetc( c, is->file ) == EOF ) {
116 perror( IO_MSG "ungetc" );
117 exit( EXIT_FAILURE );
118 } // if
119 return is;
120} // ungetc
121
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
130
131void close( ifstream * is ) {
132 if ( is->file == stdin ) return;
133
134 if ( fclose( is->file ) == EOF ) {
135 perror( IO_MSG "close input" );
136 } // if
137} // close
138
139static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
140ifstream *sin = &sinFile;
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.