source: translator/examples/fstream.c @ 42dcae7

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 42dcae7 was 42dcae7, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

re-inserted remove and reorder hoisted aggregate, fixed example programs

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