source: src/examples/fstream.c @ 1869adf

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 1869adf was 86bd7c1f, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

licencing: eighth groups of files

  • Property mode set to 100644
File size: 2.5 KB
RevLine 
[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
12// Last Modified On : Wed May 27 18:12:33 2015
13// Update Count     : 2
14//
15
[51b7345]16#include "fstream.h"
17
18extern "C" {
19#include <stdio.h>
20#include <stdlib.h>
21}
22
[134b86a]23struct ofstream {
[86bd7c1f]24        FILE *file;
25        int fail;
[51b7345]26};
27
[42dcae7]28ofstream *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 );
32        }
33        return os;
[51b7345]34}
35
[134b86a]36int fail( ofstream *os ) {
[86bd7c1f]37        return os->fail;
[51b7345]38}
39
[42dcae7]40static ofstream *make_ofstream() {
[86bd7c1f]41        ofstream *new_stream = malloc( sizeof( ofstream ) );
42        new_stream->fail = 0;
43        return new_stream;
[51b7345]44}
45
[42dcae7]46ofstream *ofstream_stdout() {
[86bd7c1f]47        ofstream *stdout_stream = make_ofstream();
48        stdout_stream->file = stdout;
49        return stdout_stream;
[51b7345]50}
51
[42dcae7]52ofstream *ofstream_stderr() {
[86bd7c1f]53        ofstream *stderr_stream = make_ofstream();
54        stderr_stream->file = stderr;
55        return stderr_stream;
[51b7345]56}
57
[42dcae7]58ofstream *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]65void 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]72struct ifstream {
[86bd7c1f]73        FILE *file;
74        int fail;
75        int eof;
[51b7345]76};
77
[42dcae7]78ifstream *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]87ifstream *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]96int fail( ifstream *is ) {
[86bd7c1f]97        return is->fail;
[51b7345]98}
99
[134b86a]100int eof( ifstream *is ) {
[86bd7c1f]101        return is->eof;
[51b7345]102}
103
[42dcae7]104static 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]111ifstream *ifstream_stdin() {
[86bd7c1f]112        ifstream *stdin_stream = make_ifstream();
113        stdin_stream->file = stdin;
114        return stdin_stream;
[51b7345]115}
116
[42dcae7]117ifstream *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: //
Note: See TracBrowser for help on using the repository browser.