| 1 | // "cfa -E fstream.c > fstream_out.c"
|
|---|
| 2 | // "cfa -c -o fstream.o fstream.c"
|
|---|
| 3 |
|
|---|
| 4 | #include "fstream.h"
|
|---|
| 5 |
|
|---|
| 6 | extern "C" {
|
|---|
| 7 | #include <stdio.h>
|
|---|
| 8 | #include <stdlib.h>
|
|---|
| 9 | }
|
|---|
| 10 |
|
|---|
| 11 | struct ofstream {
|
|---|
| 12 | FILE *file;
|
|---|
| 13 | int fail;
|
|---|
| 14 | };
|
|---|
| 15 |
|
|---|
| 16 | ofstream * write( ofstream *os, const char *data, streamsize_type size ) {
|
|---|
| 17 | if( !os->fail ) {
|
|---|
| 18 | fwrite( data, size, 1, os->file );
|
|---|
| 19 | os->fail = ferror( os->file );
|
|---|
| 20 | }
|
|---|
| 21 | return os;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | int fail( ofstream *os ) {
|
|---|
| 25 | return os->fail;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | static ofstream * make_ofstream() {
|
|---|
| 29 | ofstream *new_stream = malloc( sizeof( ofstream ) );
|
|---|
| 30 | new_stream->fail = 0;
|
|---|
| 31 | return new_stream;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | ofstream * ofstream_stdout() {
|
|---|
| 35 | ofstream *stdout_stream = make_ofstream();
|
|---|
| 36 | stdout_stream->file = stdout;
|
|---|
| 37 | return stdout_stream;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | ofstream * ofstream_stderr() {
|
|---|
| 41 | ofstream *stderr_stream = make_ofstream();
|
|---|
| 42 | stderr_stream->file = stderr;
|
|---|
| 43 | return stderr_stream;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | ofstream * ofstream_fromfile( const char *name ) {
|
|---|
| 47 | ofstream *file_stream = make_ofstream();
|
|---|
| 48 | file_stream->file = fopen( name, "w" );
|
|---|
| 49 | file_stream->fail = file_stream->file == 0;
|
|---|
| 50 | return file_stream;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | void ofstream_close( ofstream *os ) {
|
|---|
| 54 | if( os->file != stdout && os->file != stderr ) {
|
|---|
| 55 | os->fail = fclose( os->file );
|
|---|
| 56 | }
|
|---|
| 57 | free( os );
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | struct ifstream {
|
|---|
| 61 | FILE *file;
|
|---|
| 62 | int fail;
|
|---|
| 63 | int eof;
|
|---|
| 64 | };
|
|---|
| 65 |
|
|---|
| 66 | ifstream * read( ifstream *is, char *data, streamsize_type size ) {
|
|---|
| 67 | if( !is->fail && !is->eof ) {
|
|---|
| 68 | fread( data, size, 1, is->file );
|
|---|
| 69 | is->fail = ferror( is->file );
|
|---|
| 70 | is->eof = feof( is->file );
|
|---|
| 71 | }
|
|---|
| 72 | return is;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | ifstream *unread( ifstream *is, char c ) {
|
|---|
| 76 | if( !is->fail ) {
|
|---|
| 77 | if( EOF == ungetc( c, is->file ) ) {
|
|---|
| 78 | is->fail = 1;
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | return is;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | int fail( ifstream *is ) {
|
|---|
| 85 | return is->fail;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | int eof( ifstream *is ) {
|
|---|
| 89 | return is->eof;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | static ifstream * make_ifstream() {
|
|---|
| 93 | ifstream *new_stream = malloc( sizeof( ifstream ) );
|
|---|
| 94 | new_stream->fail = 0;
|
|---|
| 95 | new_stream->eof = 0;
|
|---|
| 96 | return new_stream;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | ifstream * ifstream_stdin() {
|
|---|
| 100 | ifstream *stdin_stream = make_ifstream();
|
|---|
| 101 | stdin_stream->file = stdin;
|
|---|
| 102 | return stdin_stream;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | ifstream * ifstream_fromfile( const char *name ) {
|
|---|
| 106 | ifstream *file_stream = make_ifstream();
|
|---|
| 107 | file_stream->file = fopen( name, "r" );
|
|---|
| 108 | file_stream->fail = file_stream->file == 0;
|
|---|
| 109 | return file_stream;
|
|---|
| 110 | }
|
|---|