Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/examples/fstream.c

    r86bd7c1f r843054c2  
    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 
    161#include "fstream.h"
    172
     
    227
    238struct ofstream {
    24         FILE *file;
    25         int fail;
     9    FILE *file;
     10    int fail;
    2611};
    2712
    2813ofstream *write( ofstream *os, const char *data, streamsize_type size ) {
    29         if ( ! os->fail ) {
    30                 fwrite( data, size, 1, os->file );
    31                 os->fail = ferror( os->file );
    32         }
    33         return os;
     14    if ( ! os->fail ) {
     15        fwrite( data, size, 1, os->file );
     16        os->fail = ferror( os->file );
     17    }
     18    return os;
    3419}
    3520
    3621int fail( ofstream *os ) {
    37         return os->fail;
     22    return os->fail;
    3823}
    3924
    4025static ofstream *make_ofstream() {
    41         ofstream *new_stream = malloc( sizeof( ofstream ) );
    42         new_stream->fail = 0;
    43         return new_stream;
     26    ofstream *new_stream = malloc( sizeof( ofstream ) );
     27    new_stream->fail = 0;
     28    return new_stream;
    4429}
    4530
    4631ofstream *ofstream_stdout() {
    47         ofstream *stdout_stream = make_ofstream();
    48         stdout_stream->file = stdout;
    49         return stdout_stream;
     32    ofstream *stdout_stream = make_ofstream();
     33    stdout_stream->file = stdout;
     34    return stdout_stream;
    5035}
    5136
    5237ofstream *ofstream_stderr() {
    53         ofstream *stderr_stream = make_ofstream();
    54         stderr_stream->file = stderr;
    55         return stderr_stream;
     38    ofstream *stderr_stream = make_ofstream();
     39    stderr_stream->file = stderr;
     40    return stderr_stream;
    5641}
    5742
    5843ofstream *ofstream_fromfile( const char *name ) {
    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;
     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;
    6348}
    6449
    6550void ofstream_close( ofstream *os ) {
    66         if ( os->file != stdout && os->file != stderr ) {
    67                 os->fail = fclose( os->file );
    68         }
    69         free( os );
     51    if ( os->file != stdout && os->file != stderr ) {
     52        os->fail = fclose( os->file );
     53    }
     54    free( os );
    7055}
    7156
    7257struct ifstream {
    73         FILE *file;
    74         int fail;
    75         int eof;
     58    FILE *file;
     59    int fail;
     60    int eof;
    7661};
    7762
    7863ifstream *read( ifstream *is, char *data, streamsize_type size ) {
    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;
     64    if ( ! is->fail && ! is->eof ) {
     65        fread( data, size, 1, is->file );
     66        is->fail = ferror( is->file );
     67        is->eof = feof( is->file );
     68    }
     69    return is;
    8570}
    8671 
    8772ifstream *unread( ifstream *is, char c ) {
    88         if ( ! is->fail ) {
    89                 if ( ! EOF == ungetc( c, is->file ) ) {
    90                         is->fail = 1;
    91                 }
     73    if ( ! is->fail ) {
     74        if ( ! EOF == ungetc( c, is->file ) ) {
     75            is->fail = 1;
    9276        }
    93         return is;
     77    }
     78    return is;
    9479}
    9580
    9681int fail( ifstream *is ) {
    97         return is->fail;
     82    return is->fail;
    9883}
    9984
    10085int eof( ifstream *is ) {
    101         return is->eof;
     86    return is->eof;
    10287}
    10388
    10489static ifstream *make_ifstream() {
    105         ifstream *new_stream = malloc( sizeof( ifstream ) );
    106         new_stream->fail = 0;
    107         new_stream->eof = 0;
    108         return new_stream;
     90    ifstream *new_stream = malloc( sizeof( ifstream ) );
     91    new_stream->fail = 0;
     92    new_stream->eof = 0;
     93    return new_stream;
    10994}
    11095
    11196ifstream *ifstream_stdin() {
    112         ifstream *stdin_stream = make_ifstream();
    113         stdin_stream->file = stdin;
    114         return stdin_stream;
     97    ifstream *stdin_stream = make_ifstream();
     98    stdin_stream->file = stdin;
     99    return stdin_stream;
    115100}
    116101
    117102ifstream *ifstream_fromfile( const char *name ) {
    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;
     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;
    122107}
    123 
    124 // Local Variables: //
    125 // tab-width: 4 //
    126 // compile-command: "cfa fstream.c" //
    127 // End: //
Note: See TracChangeset for help on using the changeset viewer.