Ignore:
Timestamp:
Feb 10, 2020, 11:17:38 AM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
3966d9a, 41efd33
Parents:
807a632 (diff), d231700 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    r807a632 r3b56166  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Sep 10 22:19:56 2019
    13 // Update Count     : 354
     12// Last Modified On : Fri Feb  7 19:01:01 2020
     13// Update Count     : 363
    1414//
    1515
     
    3232
    3333void ?{}( ofstream & os, void * file ) {
    34         os.file = file;
    35         os.sepDefault = true;
    36         os.sepOnOff = false;
    37         os.nlOnOff = true;
    38         os.prt = false;
    39         os.sawNL = false;
     34        os.$file = file;
     35        os.$sepDefault = true;
     36        os.$sepOnOff = false;
     37        os.$nlOnOff = true;
     38        os.$prt = false;
     39        os.$sawNL = false;
     40        $sepSetCur( os, sepGet( os ) );
    4041        sepSet( os, " " );
    41         sepSetCur( os, sepGet( os ) );
    4242        sepSetTuple( os, ", " );
    4343} // ?{}
    4444
    4545// private
    46 bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; }
    47 void sepReset( ofstream & os ) { os.sepOnOff = os.sepDefault; }
    48 void sepReset( ofstream & os, bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; }
    49 const char * sepGetCur( ofstream & os ) { return os.sepCur; }
    50 void sepSetCur( ofstream & os, const char * sepCur ) { os.sepCur = sepCur; }
    51 bool getNL( ofstream & os ) { return os.sawNL; }
    52 void setNL( ofstream & os, bool state ) { os.sawNL = state; }
    53 bool getANL( ofstream & os ) { return os.nlOnOff; }
    54 bool getPrt( ofstream & os ) { return os.prt; }
    55 void setPrt( ofstream & os, bool state ) { os.prt = state; }
     46bool $sepPrt( ofstream & os ) { $setNL( os, false ); return os.$sepOnOff; }
     47void $sepReset( ofstream & os ) { os.$sepOnOff = os.$sepDefault; }
     48void $sepReset( ofstream & os, bool reset ) { os.$sepDefault = reset; os.$sepOnOff = os.$sepDefault; }
     49const char * $sepGetCur( ofstream & os ) { return os.$sepCur; }
     50void $sepSetCur( ofstream & os, const char sepCur[] ) { os.$sepCur = sepCur; }
     51bool $getNL( ofstream & os ) { return os.$sawNL; }
     52void $setNL( ofstream & os, bool state ) { os.$sawNL = state; }
     53bool $getANL( ofstream & os ) { return os.$nlOnOff; }
     54bool $getPrt( ofstream & os ) { return os.$prt; }
     55void $setPrt( ofstream & os, bool state ) { os.$prt = state; }
    5656
    5757// public
    58 void ?{}( ofstream & os ) { os.file = 0; }
    59 
    60 void ?{}( ofstream & os, const char * name, const char * mode ) {
     58void ?{}( ofstream & os ) { os.$file = 0p; }
     59
     60void ?{}( ofstream & os, const char name[], const char mode[] ) {
    6161        open( os, name, mode );
    6262} // ?{}
    6363
    64 void ?{}( ofstream & os, const char * name ) {
     64void ?{}( ofstream & os, const char name[] ) {
    6565        open( os, name, "w" );
    6666} // ?{}
    6767
    68 void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
    69 void sepOff( ofstream & os ) { os.sepOnOff = false; }
     68void ^?{}( ofstream & os ) {
     69        close( os );
     70} // ^?{}
     71
     72void sepOn( ofstream & os ) { os.$sepOnOff = ! $getNL( os ); }
     73void sepOff( ofstream & os ) { os.$sepOnOff = false; }
    7074
    7175bool sepDisable( ofstream & os ) {
    72         bool temp = os.sepDefault;
    73         os.sepDefault = false;
    74         sepReset( os );
     76        bool temp = os.$sepDefault;
     77        os.$sepDefault = false;
     78        $sepReset( os );
    7579        return temp;
    7680} // sepDisable
    7781
    7882bool sepEnable( ofstream & os ) {
    79         bool temp = os.sepDefault;
    80         os.sepDefault = true;
    81         if ( os.sepOnOff ) sepReset( os );                                      // start of line ?
     83        bool temp = os.$sepDefault;
     84        os.$sepDefault = true;
     85        if ( os.$sepOnOff ) $sepReset( os );                            // start of line ?
    8286        return temp;
    8387} // sepEnable
    8488
    85 void nlOn( ofstream & os ) { os.nlOnOff = true; }
    86 void nlOff( ofstream & os ) { os.nlOnOff = false; }
    87 
    88 const char * sepGet( ofstream & os ) { return os.separator; }
    89 void sepSet( ofstream & os, const char * s ) {
     89void nlOn( ofstream & os ) { os.$nlOnOff = true; }
     90void nlOff( ofstream & os ) { os.$nlOnOff = false; }
     91
     92const char * sepGet( ofstream & os ) { return os.$separator; }
     93void sepSet( ofstream & os, const char s[] ) {
    9094        assert( s );
    91         strncpy( os.separator, s, sepSize - 1 );
    92         os.separator[sepSize - 1] = '\0';
     95        strncpy( os.$separator, s, sepSize - 1 );
     96        os.$separator[sepSize - 1] = '\0';
    9397} // sepSet
    9498
    95 const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator; }
    96 void sepSetTuple( ofstream & os, const char * s ) {
     99const char * sepGetTuple( ofstream & os ) { return os.$tupleSeparator; }
     100void sepSetTuple( ofstream & os, const char s[] ) {
    97101        assert( s );
    98         strncpy( os.tupleSeparator, s, sepSize - 1 );
    99         os.tupleSeparator[sepSize - 1] = '\0';
     102        strncpy( os.$tupleSeparator, s, sepSize - 1 );
     103        os.$tupleSeparator[sepSize - 1] = '\0';
    100104} // sepSet
    101105
    102106void ends( ofstream & os ) {
    103         if ( getANL( os ) ) nl( os );
    104         else setPrt( os, false );                                                       // turn off
     107        if ( $getANL( os ) ) nl( os );
     108        else $setPrt( os, false );                                                      // turn off
    105109        if ( &os == &exit ) exit( EXIT_FAILURE );
    106110        if ( &os == &abort ) abort();
     
    108112
    109113int fail( ofstream & os ) {
    110         return os.file == 0 || ferror( (FILE *)(os.file) );
     114        return os.$file == 0 || ferror( (FILE *)(os.$file) );
    111115} // fail
    112116
    113117int flush( ofstream & os ) {
    114         return fflush( (FILE *)(os.file) );
     118        return fflush( (FILE *)(os.$file) );
    115119} // flush
    116120
    117 void open( ofstream & os, const char * name, const char * mode ) {
     121void open( ofstream & os, const char name[], const char mode[] ) {
    118122        FILE * file = fopen( name, mode );
    119123        #ifdef __CFA_DEBUG__
    120         if ( file == 0 ) {
     124        if ( file == 0p ) {
    121125                abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
    122126        } // if
     
    125129} // open
    126130
    127 void open( ofstream & os, const char * name ) {
     131void open( ofstream & os, const char name[] ) {
    128132        open( os, name, "w" );
    129133} // open
    130134
    131135void close( ofstream & os ) {
    132         if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return;
    133 
    134         if ( fclose( (FILE *)(os.file) ) == EOF ) {
     136        if ( (FILE *)(os.$file) == stdout || (FILE *)(os.$file) == stderr ) return;
     137
     138        if ( fclose( (FILE *)(os.$file) ) == EOF ) {
    135139                abort | IO_MSG "close output" | nl | strerror( errno );
    136140        } // if
    137141} // close
    138142
    139 ofstream & write( ofstream & os, const char * data, size_t size ) {
     143ofstream & write( ofstream & os, const char data[], size_t size ) {
    140144        if ( fail( os ) ) {
    141145                abort | IO_MSG "attempt write I/O on failed stream";
    142146        } // if
    143147
    144         if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
     148        if ( fwrite( data, 1, size, (FILE *)(os.$file) ) != size ) {
    145149                abort | IO_MSG "write" | nl | strerror( errno );
    146150        } // if
     
    151155        va_list args;
    152156        va_start( args, format );
    153         int len = vfprintf( (FILE *)(os.file), format, args );
     157        int len = vfprintf( (FILE *)(os.$file), format, args );
    154158        if ( len == EOF ) {
    155                 if ( ferror( (FILE *)(os.file) ) ) {
     159                if ( ferror( (FILE *)(os.$file) ) ) {
    156160                        abort | IO_MSG "invalid write";
    157161                } // if
     
    159163        va_end( args );
    160164
    161         setPrt( os, true );                                                                     // called in output cascade
    162         sepReset( os );                                                                         // reset separator
     165        $setPrt( os, true );                                                            // called in output cascade
     166        $sepReset( os );                                                                        // reset separator
    163167        return len;
    164168} // fmt
     
    180184// private
    181185void ?{}( ifstream & is, void * file ) {
    182         is.file = file;
    183         is.nlOnOff = false;
     186        is.$file = file;
     187        is.$nlOnOff = false;
    184188} // ?{}
    185189
    186190// public
    187 void ?{}( ifstream & is ) {     is.file = 0; }
    188 
    189 void ?{}( ifstream & is, const char * name, const char * mode ) {
     191void ?{}( ifstream & is ) { is.$file = 0p; }
     192
     193void ?{}( ifstream & is, const char name[], const char mode[] ) {
    190194        open( is, name, mode );
    191195} // ?{}
    192196
    193 void ?{}( ifstream & is, const char * name ) {
     197void ?{}( ifstream & is, const char name[] ) {
    194198        open( is, name, "r" );
    195199} // ?{}
    196200
    197 void nlOn( ifstream & os ) { os.nlOnOff = true; }
    198 void nlOff( ifstream & os ) { os.nlOnOff = false; }
    199 bool getANL( ifstream & os ) { return os.nlOnOff; }
     201void ^?{}( ifstream & is ) {
     202        close( is );
     203} // ^?{}
     204
     205void nlOn( ifstream & os ) { os.$nlOnOff = true; }
     206void nlOff( ifstream & os ) { os.$nlOnOff = false; }
     207bool getANL( ifstream & os ) { return os.$nlOnOff; }
    200208
    201209int fail( ifstream & is ) {
    202         return is.file == 0 || ferror( (FILE *)(is.file) );
     210        return is.$file == 0p || ferror( (FILE *)(is.$file) );
    203211} // fail
    204212
    205213int eof( ifstream & is ) {
    206         return feof( (FILE *)(is.file) );
     214        return feof( (FILE *)(is.$file) );
    207215} // eof
    208216
    209 void open( ifstream & is, const char * name, const char * mode ) {
     217void open( ifstream & is, const char name[], const char mode[] ) {
    210218        FILE * file = fopen( name, mode );
    211219        #ifdef __CFA_DEBUG__
    212         if ( file == 0 ) {
     220        if ( file == 0p ) {
    213221                abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
    214222        } // if
    215223        #endif // __CFA_DEBUG__
    216         is.file = file;
    217 } // open
    218 
    219 void open( ifstream & is, const char * name ) {
     224        is.$file = file;
     225} // open
     226
     227void open( ifstream & is, const char name[] ) {
    220228        open( is, name, "r" );
    221229} // open
    222230
    223231void close( ifstream & is ) {
    224         if ( (FILE *)(is.file) == stdin ) return;
    225 
    226         if ( fclose( (FILE *)(is.file) ) == EOF ) {
     232        if ( (FILE *)(is.$file) == stdin ) return;
     233
     234        if ( fclose( (FILE *)(is.$file) ) == EOF ) {
    227235                abort | IO_MSG "close input" | nl | strerror( errno );
    228236        } // if
     
    234242        } // if
    235243
    236         if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
     244        if ( fread( data, size, 1, (FILE *)(is.$file) ) == 0 ) {
    237245                abort | IO_MSG "read" | nl | strerror( errno );
    238246        } // if
     
    245253        } // if
    246254
    247         if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
     255        if ( ungetc( c, (FILE *)(is.$file) ) == EOF ) {
    248256                abort | IO_MSG "ungetc" | nl | strerror( errno );
    249257        } // if
     
    255263
    256264        va_start( args, format );
    257         int len = vfscanf( (FILE *)(is.file), format, args );
     265        int len = vfscanf( (FILE *)(is.$file), format, args );
    258266        if ( len == EOF ) {
    259                 if ( ferror( (FILE *)(is.file) ) ) {
     267                if ( ferror( (FILE *)(is.$file) ) ) {
    260268                        abort | IO_MSG "invalid read";
    261269                } // if
Note: See TracChangeset for help on using the changeset viewer.