[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 |
---|
[839ccbb] | 12 | // Last Modified On : Thu Nov 19 22:43:31 2015 |
---|
| 13 | // Update Count : 4 |
---|
[86bd7c1f] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include "fstream.h" |
---|
| 17 | |
---|
| 18 | extern "C" { |
---|
| 19 | #include <stdio.h> |
---|
| 20 | #include <stdlib.h> |
---|
| 21 | } |
---|
| 22 | |
---|
[134b86a] | 23 | struct ofstream { |
---|
[86bd7c1f] | 24 | FILE *file; |
---|
| 25 | int fail; |
---|
[51b7345] | 26 | }; |
---|
| 27 | |
---|
[42dcae7] | 28 | ofstream *write( ofstream *os, const char *data, streamsize_type size ) { |
---|
[86bd7c1f] | 29 | if ( ! os->fail ) { |
---|
| 30 | fwrite( data, size, 1, os->file ); |
---|
| 31 | os->fail = ferror( os->file ); |
---|
[839ccbb] | 32 | } // if |
---|
[86bd7c1f] | 33 | return os; |
---|
[839ccbb] | 34 | } // write |
---|
[51b7345] | 35 | |
---|
[134b86a] | 36 | int fail( ofstream *os ) { |
---|
[86bd7c1f] | 37 | return os->fail; |
---|
[839ccbb] | 38 | } // fail |
---|
[51b7345] | 39 | |
---|
[42dcae7] | 40 | static ofstream *make_ofstream() { |
---|
[86bd7c1f] | 41 | ofstream *new_stream = malloc( sizeof( ofstream ) ); |
---|
| 42 | new_stream->fail = 0; |
---|
| 43 | return new_stream; |
---|
[839ccbb] | 44 | } // make_ofstream |
---|
[51b7345] | 45 | |
---|
[42dcae7] | 46 | ofstream *ofstream_stdout() { |
---|
[86bd7c1f] | 47 | ofstream *stdout_stream = make_ofstream(); |
---|
| 48 | stdout_stream->file = stdout; |
---|
| 49 | return stdout_stream; |
---|
[839ccbb] | 50 | } // ofstream_stdout |
---|
[51b7345] | 51 | |
---|
[42dcae7] | 52 | ofstream *ofstream_stderr() { |
---|
[86bd7c1f] | 53 | ofstream *stderr_stream = make_ofstream(); |
---|
| 54 | stderr_stream->file = stderr; |
---|
| 55 | return stderr_stream; |
---|
[839ccbb] | 56 | } // ofstream_stderr |
---|
[51b7345] | 57 | |
---|
[42dcae7] | 58 | ofstream *ofstream_fromfile( const char *name ) { |
---|
[86bd7c1f] | 59 | ofstream *file_stream = make_ofstream(); |
---|
| 60 | file_stream->file = fopen( name, "w" ); |
---|
| 61 | file_stream->fail = file_stream->file == 0; |
---|
| 62 | return file_stream; |
---|
[51b7345] | 63 | } |
---|
| 64 | |
---|
[134b86a] | 65 | void ofstream_close( ofstream *os ) { |
---|
[86bd7c1f] | 66 | if ( os->file != stdout && os->file != stderr ) { |
---|
| 67 | os->fail = fclose( os->file ); |
---|
| 68 | } |
---|
| 69 | free( os ); |
---|
[51b7345] | 70 | } |
---|
| 71 | |
---|
[134b86a] | 72 | struct ifstream { |
---|
[86bd7c1f] | 73 | FILE *file; |
---|
| 74 | int fail; |
---|
| 75 | int eof; |
---|
[51b7345] | 76 | }; |
---|
| 77 | |
---|
[42dcae7] | 78 | ifstream *read( ifstream *is, char *data, streamsize_type size ) { |
---|
[86bd7c1f] | 79 | if ( ! is->fail && ! is->eof ) { |
---|
| 80 | fread( data, size, 1, is->file ); |
---|
| 81 | is->fail = ferror( is->file ); |
---|
| 82 | is->eof = feof( is->file ); |
---|
| 83 | } |
---|
| 84 | return is; |
---|
[51b7345] | 85 | } |
---|
| 86 | |
---|
[134b86a] | 87 | ifstream *unread( ifstream *is, char c ) { |
---|
[86bd7c1f] | 88 | if ( ! is->fail ) { |
---|
| 89 | if ( ! EOF == ungetc( c, is->file ) ) { |
---|
| 90 | is->fail = 1; |
---|
| 91 | } |
---|
[134b86a] | 92 | } |
---|
[86bd7c1f] | 93 | return is; |
---|
[51b7345] | 94 | } |
---|
| 95 | |
---|
[134b86a] | 96 | int fail( ifstream *is ) { |
---|
[86bd7c1f] | 97 | return is->fail; |
---|
[51b7345] | 98 | } |
---|
| 99 | |
---|
[134b86a] | 100 | int eof( ifstream *is ) { |
---|
[86bd7c1f] | 101 | return is->eof; |
---|
[51b7345] | 102 | } |
---|
| 103 | |
---|
[42dcae7] | 104 | static ifstream *make_ifstream() { |
---|
[86bd7c1f] | 105 | ifstream *new_stream = malloc( sizeof( ifstream ) ); |
---|
| 106 | new_stream->fail = 0; |
---|
| 107 | new_stream->eof = 0; |
---|
| 108 | return new_stream; |
---|
[51b7345] | 109 | } |
---|
| 110 | |
---|
[42dcae7] | 111 | ifstream *ifstream_stdin() { |
---|
[86bd7c1f] | 112 | ifstream *stdin_stream = make_ifstream(); |
---|
| 113 | stdin_stream->file = stdin; |
---|
| 114 | return stdin_stream; |
---|
[51b7345] | 115 | } |
---|
| 116 | |
---|
[42dcae7] | 117 | ifstream *ifstream_fromfile( const char *name ) { |
---|
[86bd7c1f] | 118 | ifstream *file_stream = make_ifstream(); |
---|
| 119 | file_stream->file = fopen( name, "r" ); |
---|
| 120 | file_stream->fail = file_stream->file == 0; |
---|
| 121 | return file_stream; |
---|
[51b7345] | 122 | } |
---|
[86bd7c1f] | 123 | |
---|
| 124 | // Local Variables: // |
---|
| 125 | // tab-width: 4 // |
---|
| 126 | // compile-command: "cfa fstream.c" // |
---|
| 127 | // End: // |
---|