Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    r5cb2b8c ra87d40b  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu May 16 08:33:28 2019
    13 // Update Count     : 328
     12// Last Modified On : Mon Jul 15 18:11:26 2019
     13// Update Count     : 349
    1414//
    1515
     
    2424#include <assert.h>
    2525#include <errno.h>                                                                              // errno
     26
     27
     28//*********************************** ofstream ***********************************
     29
    2630
    2731#define IO_MSG "I/O error: "
     
    3741        sepSetCur( os, sepGet( os ) );
    3842        sepSetTuple( os, ", " );
    39 }
     43} // ?{}
    4044
    4145// private
     
    5660void ?{}( ofstream & os, const char * name, const char * mode ) {
    5761        open( os, name, mode );
    58 }
     62} // ?{}
     63
    5964void ?{}( ofstream & os, const char * name ) {
    6065        open( os, name, "w" );
    61 }
     66} // ?{}
    6267
    6368void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
     
    95100} // sepSet
    96101
     102void ends( ofstream & os ) {
     103        if ( getANL( os ) ) nl( os );
     104        else setPrt( os, false );                                                       // turn off
     105        if ( &os == &exit ) exit( EXIT_FAILURE );
     106        if ( &os == &abort ) abort();
     107} // ends
     108
    97109int fail( ofstream & os ) {
    98110        return os.file == 0 || ferror( (FILE *)(os.file) );
     
    107119        #ifdef __CFA_DEBUG__
    108120        if ( file == 0 ) {
    109                 abort( IO_MSG "open output file \"%s\", %s", name, strerror( errno ) );
     121                abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
    110122        } // if
    111123        #endif // __CFA_DEBUG__
     
    121133
    122134        if ( fclose( (FILE *)(os.file) ) == EOF ) {
    123                 abort( IO_MSG "close output %s", strerror( errno ) );
     135                abort | IO_MSG "close output" | nl | strerror( errno );
    124136        } // if
    125137} // close
     
    127139ofstream & write( ofstream & os, const char * data, size_t size ) {
    128140        if ( fail( os ) ) {
    129                 abort( "attempt write I/O on failed stream\n" );
     141                abort | IO_MSG "attempt write I/O on failed stream";
    130142        } // if
    131143
    132144        if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
    133                 abort( IO_MSG "write %s", strerror( errno ) );
     145                abort | IO_MSG "write" | nl | strerror( errno );
    134146        } // if
    135147        return os;
     
    142154        if ( len == EOF ) {
    143155                if ( ferror( (FILE *)(os.file) ) ) {
    144                         abort( "invalid write\n" );
     156                        abort | IO_MSG "invalid write";
    145157                } // if
    146158        } // if
     
    153165
    154166static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
    155 ofstream & sout = soutFile;
     167ofstream & sout = soutFile, & stdout = soutFile;
    156168static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
    157 ofstream & serr = serrFile;
    158 
    159 // static ofstream sexitFile = { (FILE *)(&_IO_2_1_stdout_) };
    160 // ofstream & sexit = sexitFile;
    161 // static ofstream sabortFile = { (FILE *)(&_IO_2_1_stderr_) };
    162 // ofstream & sabort = sabortFile;
    163 
    164 void nl( ofstream & os ) {
    165         if ( getANL( os ) ) (ofstream &)(nl( os ));                     // implementation only
    166         else setPrt( os, false );                                                       // turn off
    167 }
    168 
    169 //---------------------------------------
     169ofstream & serr = serrFile, & stderr = serrFile;
     170
     171static ofstream exitFile = { (FILE *)(&_IO_2_1_stdout_) };
     172ofstream & exit = exitFile;
     173static ofstream abortFile = { (FILE *)(&_IO_2_1_stderr_) };
     174ofstream & abort = abortFile;
     175
     176
     177//*********************************** ifstream ***********************************
     178
    170179
    171180// private
     
    173182        is.file = file;
    174183        is.nlOnOff = false;
    175 }
     184} // ?{}
    176185
    177186// public
     
    180189void ?{}( ifstream & is, const char * name, const char * mode ) {
    181190        open( is, name, mode );
    182 }
     191} // ?{}
     192
    183193void ?{}( ifstream & is, const char * name ) {
    184194        open( is, name, "r" );
    185 }
     195} // ?{}
    186196
    187197void nlOn( ifstream & os ) { os.nlOnOff = true; }
     
    201211        #ifdef __CFA_DEBUG__
    202212        if ( file == 0 ) {
    203                 abort( IO_MSG "open input file \"%s\", %s\n", name, strerror( errno ) );
     213                abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
    204214        } // if
    205215        #endif // __CFA_DEBUG__
     
    215225
    216226        if ( fclose( (FILE *)(is.file) ) == EOF ) {
    217                 abort( IO_MSG "close input %s", strerror( errno ) );
     227                abort | IO_MSG "close input" | nl | strerror( errno );
    218228        } // if
    219229} // close
     
    221231ifstream & read( ifstream & is, char * data, size_t size ) {
    222232        if ( fail( is ) ) {
    223                 abort( "attempt read I/O on failed stream\n" );
     233                abort | IO_MSG "attempt read I/O on failed stream";
    224234        } // if
    225235
    226236        if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
    227                 abort( IO_MSG "read %s", strerror( errno ) );
     237                abort | IO_MSG "read" | nl | strerror( errno );
    228238        } // if
    229239        return is;
     
    232242ifstream &ungetc( ifstream & is, char c ) {
    233243        if ( fail( is ) ) {
    234                 abort( "attempt ungetc I/O on failed stream\n" );
     244                abort | IO_MSG "attempt ungetc I/O on failed stream";
    235245        } // if
    236246
    237247        if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
    238                 abort( IO_MSG "ungetc %s", strerror( errno ) );
     248                abort | IO_MSG "ungetc" | nl | strerror( errno );
    239249        } // if
    240250        return is;
     
    248258        if ( len == EOF ) {
    249259                if ( ferror( (FILE *)(is.file) ) ) {
    250                         abort( "invalid read\n" );
     260                        abort | IO_MSG "invalid read";
    251261                } // if
    252262        } // if
     
    257267
    258268static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
    259 ifstream & sin = sinFile;
     269ifstream & sin = sinFile, & stdin = sinFile;
    260270
    261271// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.