Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/fstream.c

    r6ba0659 rd3b7937  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Feb 17 14:03:05 2016
    13 // Update Count     : 76
     12// Last Modified On : Tue Jan 26 17:12:59 2016
     13// Update Count     : 6
    1414//
    1515
     
    2323struct ofstream {
    2424        FILE *file;
     25        int fail;
    2526};
    2627
    27 #define IO_MSG "I/O error "
    28 
    29 int fail( ofstream * os ) {
    30         return ferror( os->file );
    31 } // fail
    32 
    33 int flush( ofstream * os ) {
    34         return fflush( os->file );
    35 } // flush
    36 
    37 void open( ofstream ** os, const char * name, const char * mode ) {
    38         FILE *t = fopen( name, mode );
    39         if ( t == 0 ) {                                                                         // do not change unless successful
    40                 perror( IO_MSG "open output" );
    41                 exit( EXIT_FAILURE );
    42         } // if
    43         (*os)->file = t;
    44 } // open
    45 
    46 void close( ofstream * os ) {
    47         if ( os->file == stdout || os->file == stderr ) return;
    48 
    49         if ( fclose( os->file ) == EOF ) {
    50                 perror( IO_MSG "close output" );
    51         } // if
    52 } // close
    53 
    54 ofstream * write( ofstream * os, const char * data, streamsize_type size ) {
    55         if ( fail( os ) ) {
    56                 fprintf( stderr, "attempt write I/O on failed stream\n" );
    57                 exit( EXIT_FAILURE );
    58         } // if
    59 
    60         if ( fwrite( data, 1, size, os->file ) != size ) {
    61                 perror( IO_MSG "write" );
    62                 exit( EXIT_FAILURE );
     28ofstream *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 );
    6332        } // if
    6433        return os;
    6534} // write
    6635
    67 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
    68 ofstream *sout = &soutFile;
    69 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
    70 ofstream *serr = &serrFile;
     36int fail( ofstream *os ) {
     37        return os->fail;
     38} // fail
    7139
    72 //---------------------------------------
     40static ofstream *make_ofstream() {
     41        ofstream *new_stream = malloc( sizeof( ofstream ) );
     42        new_stream->fail = 0;
     43        return new_stream;
     44} // make_ofstream
     45
     46ofstream *ofstream_stdout() {
     47        ofstream *stdout_stream = make_ofstream();
     48        stdout_stream->file = stdout;
     49        return stdout_stream;
     50} // ofstream_stdout
     51
     52ofstream *ofstream_stderr() {
     53        ofstream *stderr_stream = make_ofstream();
     54        stderr_stream->file = stderr;
     55        return stderr_stream;
     56} // ofstream_stderr
     57
     58ofstream *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;
     63}
     64
     65void ofstream_close( ofstream *os ) {
     66        if ( os->file != stdout && os->file != stderr ) {
     67                os->fail = fclose( os->file );
     68        }
     69        free( os );
     70}
    7371
    7472struct ifstream {
    7573        FILE *file;
     74        int fail;
     75        int eof;
    7676};
    7777
    78 int fail( ifstream * is ) {
    79         return ferror( is->file );
    80 } // fail
     78ifstream *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;
     85}
     86 
     87ifstream *unread( ifstream *is, char c ) {
     88        if ( ! is->fail ) {
     89                if ( ! EOF == ungetc( c, is->file ) ) {
     90                        is->fail = 1;
     91                }
     92        }
     93        return is;
     94}
    8195
    82 int eof( ifstream * is ) {
    83         return feof( is->file );
    84 } // eof
     96int fail( ifstream *is ) {
     97        return is->fail;
     98}
    8599
    86 ifstream * get( ifstream * is, int * data ) {
    87         if ( fscanf( is->file, "%d", data ) == EOF ) {
    88                 if ( ferror( is->file ) ) {
    89                         fprintf( stderr, "invalid int read\n" );
    90                         exit( EXIT_FAILURE );
    91                 } // if
    92         } // if
    93         return is;
    94 } // read
     100int eof( ifstream *is ) {
     101        return is->eof;
     102}
    95103
    96 ifstream * read( ifstream * is, char * data, streamsize_type size ) {
    97         if ( fail( is ) ) {
    98                 fprintf( stderr, "attempt read I/O on failed stream\n" );
    99                 exit( EXIT_FAILURE );
    100         } // if
     104static ifstream *make_ifstream() {
     105        ifstream *new_stream = malloc( sizeof( ifstream ) );
     106        new_stream->fail = 0;
     107        new_stream->eof = 0;
     108        return new_stream;
     109}
    101110
    102         if ( fread( data, size, 1, is->file ) == 0 ) {
    103                 perror( IO_MSG "read" );
    104                 exit( EXIT_FAILURE );
    105         } // if
    106         return is;
    107 } // read
    108  
    109 ifstream *ungetc( ifstream * is, char c ) {
    110         if ( fail( is ) ) {
    111                 fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
    112                 exit( EXIT_FAILURE );
    113         } // if
     111ifstream *ifstream_stdin() {
     112        ifstream *stdin_stream = make_ifstream();
     113        stdin_stream->file = stdin;
     114        return stdin_stream;
     115}
    114116
    115         if ( ungetc( c, is->file ) == EOF ) {
    116                 perror( IO_MSG "ungetc" );
    117                 exit( EXIT_FAILURE );
    118         } // if
    119         return is;
    120 } // ungetc
    121 
    122 void open( ifstream ** is, const char * name, const char * mode ) {
    123         FILE *t = fopen( name, mode );
    124         if ( t == 0 ) {                                                                         // do not change unless successful
    125                 perror( IO_MSG "open input" );
    126                 exit( EXIT_FAILURE );
    127         } // if
    128         (*is)->file = t;
    129 } // open
    130 
    131 void close( ifstream * is ) {
    132         if ( is->file == stdin ) return;
    133 
    134         if ( fclose( is->file ) == EOF ) {
    135                 perror( IO_MSG "close input" );
    136         } // if
    137 } // close
    138 
    139 static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
    140 ifstream *sin = &sinFile;
     117ifstream *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;
     122}
    141123
    142124// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.