Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    ra87d40b r5cb2b8c  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jul 15 18:11:26 2019
    13 // Update Count     : 349
     12// Last Modified On : Thu May 16 08:33:28 2019
     13// Update Count     : 328
    1414//
    1515
     
    2424#include <assert.h>
    2525#include <errno.h>                                                                              // errno
    26 
    27 
    28 //*********************************** ofstream ***********************************
    29 
    3026
    3127#define IO_MSG "I/O error: "
     
    4137        sepSetCur( os, sepGet( os ) );
    4238        sepSetTuple( os, ", " );
    43 } // ?{}
     39}
    4440
    4541// private
     
    6056void ?{}( ofstream & os, const char * name, const char * mode ) {
    6157        open( os, name, mode );
    62 } // ?{}
    63 
     58}
    6459void ?{}( ofstream & os, const char * name ) {
    6560        open( os, name, "w" );
    66 } // ?{}
     61}
    6762
    6863void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
     
    10095} // sepSet
    10196
    102 void 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 
    10997int fail( ofstream & os ) {
    11098        return os.file == 0 || ferror( (FILE *)(os.file) );
     
    119107        #ifdef __CFA_DEBUG__
    120108        if ( file == 0 ) {
    121                 abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
     109                abort( IO_MSG "open output file \"%s\", %s", name, strerror( errno ) );
    122110        } // if
    123111        #endif // __CFA_DEBUG__
     
    133121
    134122        if ( fclose( (FILE *)(os.file) ) == EOF ) {
    135                 abort | IO_MSG "close output" | nl | strerror( errno );
     123                abort( IO_MSG "close output %s", strerror( errno ) );
    136124        } // if
    137125} // close
     
    139127ofstream & write( ofstream & os, const char * data, size_t size ) {
    140128        if ( fail( os ) ) {
    141                 abort | IO_MSG "attempt write I/O on failed stream";
     129                abort( "attempt write I/O on failed stream\n" );
    142130        } // if
    143131
    144132        if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
    145                 abort | IO_MSG "write" | nl | strerror( errno );
     133                abort( IO_MSG "write %s", strerror( errno ) );
    146134        } // if
    147135        return os;
     
    154142        if ( len == EOF ) {
    155143                if ( ferror( (FILE *)(os.file) ) ) {
    156                         abort | IO_MSG "invalid write";
     144                        abort( "invalid write\n" );
    157145                } // if
    158146        } // if
     
    165153
    166154static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
    167 ofstream & sout = soutFile, & stdout = soutFile;
     155ofstream & sout = soutFile;
    168156static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
    169 ofstream & serr = serrFile, & stderr = serrFile;
    170 
    171 static ofstream exitFile = { (FILE *)(&_IO_2_1_stdout_) };
    172 ofstream & exit = exitFile;
    173 static ofstream abortFile = { (FILE *)(&_IO_2_1_stderr_) };
    174 ofstream & abort = abortFile;
    175 
    176 
    177 //*********************************** ifstream ***********************************
    178 
     157ofstream & 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
     164void nl( ofstream & os ) {
     165        if ( getANL( os ) ) (ofstream &)(nl( os ));                     // implementation only
     166        else setPrt( os, false );                                                       // turn off
     167}
     168
     169//---------------------------------------
    179170
    180171// private
     
    182173        is.file = file;
    183174        is.nlOnOff = false;
    184 } // ?{}
     175}
    185176
    186177// public
     
    189180void ?{}( ifstream & is, const char * name, const char * mode ) {
    190181        open( is, name, mode );
    191 } // ?{}
    192 
     182}
    193183void ?{}( ifstream & is, const char * name ) {
    194184        open( is, name, "r" );
    195 } // ?{}
     185}
    196186
    197187void nlOn( ifstream & os ) { os.nlOnOff = true; }
     
    211201        #ifdef __CFA_DEBUG__
    212202        if ( file == 0 ) {
    213                 abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
     203                abort( IO_MSG "open input file \"%s\", %s\n", name, strerror( errno ) );
    214204        } // if
    215205        #endif // __CFA_DEBUG__
     
    225215
    226216        if ( fclose( (FILE *)(is.file) ) == EOF ) {
    227                 abort | IO_MSG "close input" | nl | strerror( errno );
     217                abort( IO_MSG "close input %s", strerror( errno ) );
    228218        } // if
    229219} // close
     
    231221ifstream & read( ifstream & is, char * data, size_t size ) {
    232222        if ( fail( is ) ) {
    233                 abort | IO_MSG "attempt read I/O on failed stream";
     223                abort( "attempt read I/O on failed stream\n" );
    234224        } // if
    235225
    236226        if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
    237                 abort | IO_MSG "read" | nl | strerror( errno );
     227                abort( IO_MSG "read %s", strerror( errno ) );
    238228        } // if
    239229        return is;
     
    242232ifstream &ungetc( ifstream & is, char c ) {
    243233        if ( fail( is ) ) {
    244                 abort | IO_MSG "attempt ungetc I/O on failed stream";
     234                abort( "attempt ungetc I/O on failed stream\n" );
    245235        } // if
    246236
    247237        if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
    248                 abort | IO_MSG "ungetc" | nl | strerror( errno );
     238                abort( IO_MSG "ungetc %s", strerror( errno ) );
    249239        } // if
    250240        return is;
     
    258248        if ( len == EOF ) {
    259249                if ( ferror( (FILE *)(is.file) ) ) {
    260                         abort | IO_MSG "invalid read";
     250                        abort( "invalid read\n" );
    261251                } // if
    262252        } // if
     
    267257
    268258static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
    269 ifstream & sin = sinFile, & stdin = sinFile;
     259ifstream & sin = sinFile;
    270260
    271261// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.