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 | |
---|
18 | extern "C" { |
---|
19 | #include <stdio.h> |
---|
20 | #include <stdlib.h> |
---|
21 | } |
---|
22 | |
---|
23 | struct ofstream { |
---|
24 | FILE *file; |
---|
25 | }; |
---|
26 | |
---|
27 | #define IO_MSG "I/O error " |
---|
28 | |
---|
29 | int fail( ofstream * os ) { |
---|
30 | return ferror( os->file ); |
---|
31 | } // fail |
---|
32 | |
---|
33 | int flush( ofstream * os ) { |
---|
34 | return fflush( os->file ); |
---|
35 | } // flush |
---|
36 | |
---|
37 | void 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 | |
---|
46 | void 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 | |
---|
54 | ofstream * 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 | |
---|
67 | static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) }; |
---|
68 | ofstream *sout = &soutFile; |
---|
69 | static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) }; |
---|
70 | ofstream *serr = &serrFile; |
---|
71 | |
---|
72 | //--------------------------------------- |
---|
73 | |
---|
74 | struct ifstream { |
---|
75 | FILE *file; |
---|
76 | }; |
---|
77 | |
---|
78 | int fail( ifstream * is ) { |
---|
79 | return ferror( is->file ); |
---|
80 | } // fail |
---|
81 | |
---|
82 | int eof( ifstream * is ) { |
---|
83 | return feof( is->file ); |
---|
84 | } // eof |
---|
85 | |
---|
86 | ifstream * 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 | |
---|
96 | ifstream * 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 | |
---|
109 | ifstream *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 | |
---|
122 | void 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 | |
---|
131 | void 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 | |
---|
139 | static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) }; |
---|
140 | ifstream *sin = &sinFile; |
---|
141 | |
---|
142 | // Local Variables: // |
---|
143 | // tab-width: 4 // |
---|
144 | // compile-command: "cfa fstream.c" // |
---|
145 | // End: // |
---|