Changeset 666483d


Ignore:
Timestamp:
Apr 27, 2021, 10:13:14 PM (3 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
bbbd2c4
Parents:
f451177
Message:

rename strstream to ostrstream, first draft of istrstream but problem parsing

Location:
libcfa/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/strstream.cfa

    rf451177 r666483d  
    1010// Created On       : Thu Apr 22 22:24:35 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Apr 24 11:15:47 2021
    13 // Update Count     : 73
     12// Last Modified On : Tue Apr 27 20:59:53 2021
     13// Update Count     : 78
    1414//
    1515
     
    2323#include <unistd.h>                                                                             // sbrk, sysconf
    2424
     25
    2526// *********************************** strstream ***********************************
    2627
     
    2930
    3031// private
    31 bool sepPrt$( strstream & os ) { setNL$( os, false ); return os.sepOnOff$; }
    32 void sepReset$( strstream & os ) { os.sepOnOff$ = os.sepDefault$; }
    33 void sepReset$( strstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; }
    34 const char * sepGetCur$( strstream & os ) { return os.sepCur$; }
    35 void sepSetCur$( strstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; }
    36 bool getNL$( strstream & os ) { return os.sawNL$; }
    37 void setNL$( strstream & os, bool state ) { os.sawNL$ = state; }
    38 bool getANL$( strstream & os ) { return os.nlOnOff$; }
    39 bool getPrt$( strstream & os ) { return os.prt$; }
    40 void setPrt$( strstream & os, bool state ) { os.prt$ = state; }
     32bool sepPrt$( ostrstream & os ) { setNL$( os, false ); return os.sepOnOff$; }
     33void sepReset$( ostrstream & os ) { os.sepOnOff$ = os.sepDefault$; }
     34void sepReset$( ostrstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; }
     35const char * sepGetCur$( ostrstream & os ) { return os.sepCur$; }
     36void sepSetCur$( ostrstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; }
     37bool getNL$( ostrstream & os ) { return os.sawNL$; }
     38void setNL$( ostrstream & os, bool state ) { os.sawNL$ = state; }
     39bool getANL$( ostrstream & os ) { return os.nlOnOff$; }
     40bool getPrt$( ostrstream & os ) { return os.prt$; }
     41void setPrt$( ostrstream & os, bool state ) { os.prt$ = state; }
    4142
    4243// public
    43 void ?{}( strstream & os, char buf[], size_t size ) {
     44void ?{}( ostrstream & os, char buf[], size_t size ) {
    4445        os.buf$ = buf;
    4546        os.size$ = size;
     
    5556} // ?{}
    5657
    57 void sepOn( strstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
    58 void sepOff( strstream & os ) { os.sepOnOff$ = false; }
     58void sepOn( ostrstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
     59void sepOff( ostrstream & os ) { os.sepOnOff$ = false; }
    5960
    60 bool sepDisable( strstream & os ) {
     61bool sepDisable( ostrstream & os ) {
    6162        bool temp = os.sepDefault$;
    6263        os.sepDefault$ = false;
     
    6566} // sepDisable
    6667
    67 bool sepEnable( strstream & os ) {
     68bool sepEnable( ostrstream & os ) {
    6869        bool temp = os.sepDefault$;
    6970        os.sepDefault$ = true;
     
    7273} // sepEnable
    7374
    74 void nlOn( strstream & os ) { os.nlOnOff$ = true; }
    75 void nlOff( strstream & os ) { os.nlOnOff$ = false; }
     75void nlOn( ostrstream & os ) { os.nlOnOff$ = true; }
     76void nlOff( ostrstream & os ) { os.nlOnOff$ = false; }
    7677
    77 const char * sepGet( strstream & os ) { return os.separator$; }
    78 void sepSet( strstream & os, const char s[] ) {
     78const char * sepGet( ostrstream & os ) { return os.separator$; }
     79void sepSet( ostrstream & os, const char s[] ) {
    7980        assert( s );
    80         strncpy( os.separator$, s, strstream_sepSize - 1 );
    81         os.separator$[strstream_sepSize - 1] = '\0';
     81        strncpy( os.separator$, s, ostrstream_sepSize - 1 );
     82        os.separator$[ostrstream_sepSize - 1] = '\0';
    8283} // sepSet
    8384
    84 const char * sepGetTuple( strstream & os ) { return os.tupleSeparator$; }
    85 void sepSetTuple( strstream & os, const char s[] ) {
     85const char * sepGetTuple( ostrstream & os ) { return os.tupleSeparator$; }
     86void sepSetTuple( ostrstream & os, const char s[] ) {
    8687        assert( s );
    87         strncpy( os.tupleSeparator$, s, strstream_sepSize - 1 );
    88         os.tupleSeparator$[strstream_sepSize - 1] = '\0';
     88        strncpy( os.tupleSeparator$, s, ostrstream_sepSize - 1 );
     89        os.tupleSeparator$[ostrstream_sepSize - 1] = '\0';
    8990} // sepSet
    9091
    91 void ends( strstream & os ) {
     92void ends( ostrstream & os ) {
    9293        if ( getANL$( os ) ) nl( os );
    9394        else setPrt$( os, false );                                                      // turn off
    9495} // ends
    9596
    96 int fmt( strstream & os, const char format[], ... ) {
     97int fmt( ostrstream & os, const char format[], ... ) {
    9798        va_list args;
    9899        va_start( args, format );
    99100        int len = vsnprintf( os.buf$ + os.cursor$, os.size$ - os.cursor$, format, args );
     101        va_end( args );
    100102        os.cursor$ += len;
    101103        if ( os.cursor$ >= os.size$ ) {                                         // cursor exceeded buffer size?
    102                 #define fmtmsg IO_MSG "strstream truncated write, buffer too small.\n"
     104                #define fmtmsg IO_MSG "ostrstream truncated write, buffer too small.\n"
    103105                write( STDERR_FILENO, fmtmsg, sizeof(fmtmsg) - 1 );
    104106                abort();
    105107        } // if
    106         va_end( args );
    107108
    108109        setPrt$( os, true );                                                            // called in output cascade
     
    111112} // fmt
    112113
    113 int flush( strstream & ) {                                                              // match trait, not used
    114         return 0;
    115 } // flush
    116 
    117 strstream & write( strstream & os ) {
    118         return write( os, stdout );
    119 } // write
    120 strstream & write( strstream & os, FILE * stream ) {
     114ostrstream & write( ostrstream & os, FILE * stream ) {
    121115        if ( fwrite( os.buf$, 1, os.cursor$, stream ) != os.cursor$ ) {
    122                 #define writemsg IO_MSG "strstream write error.\n"
    123                 write( STDERR_FILENO, writemsg, sizeof(writemsg) - 1 );
     116                #define ostrwritemsg IO_MSG "ostrstream write error.\n"
     117                write( STDERR_FILENO, ostrwritemsg, sizeof(ostrwritemsg) - 1 );
    124118                abort();
    125119        } // if
     
    127121} // write
    128122
    129 strstream & sstr;
     123ostrstream & write( ostrstream & os ) {
     124        return write( os, stdout );
     125} // write
     126
     127
     128// *********************************** istrstream ***********************************
     129
     130
     131// public
     132void ?{}( istrstream & is, char buf[] ) {
     133        is.buf$ = buf;
     134        is.cursor$ = 0;
     135        is.nlOnOff$ = false;
     136} // ?{}
     137
     138bool getANL( istrstream & is ) { return is.nlOnOff$; }
     139void nlOn( istrstream & is ) { is.nlOnOff$ = true; }
     140void nlOff( istrstream & is ) { is.nlOnOff$ = false; }
     141
     142void ends( istrstream & is ) {
     143} // ends
     144
     145int eof( istrstream & is ) {
     146        return 0;
     147} // eof
     148
     149istrstream &ungetc( istrstream & is, char c ) {
     150        // if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) {
     151        //      abort | IO_MSG "ungetc" | nl | strerror( errno );
     152        // } // if
     153        return is;
     154} // ungetc
     155
     156int fmt( istrstream & is, const char format[], ... ) {
     157        va_list args;
     158        va_start( args, format );
     159        // This does not work because vsscanf does not return buffer position.
     160        int len = vsscanf( is.buf$ + is.cursor$, format, args );
     161        va_end( args );
     162        if ( len == EOF ) {
     163                int j;
     164                printf( "X %d%n\n", len, &j );
     165        } // if
     166        is.cursor$ += len;
     167        return len;
     168} // fmt
    130169
    131170// Local Variables: //
  • libcfa/src/strstream.hfa

    rf451177 r666483d  
    1010// Created On       : Thu Apr 22 22:20:59 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Apr 24 11:17:33 2021
    13 // Update Count     : 37
     12// Last Modified On : Tue Apr 27 20:58:50 2021
     13// Update Count     : 41
    1414//
    1515
     
    2020
    2121
    22 // *********************************** strstream ***********************************
     22// *********************************** ostrstream ***********************************
    2323
    2424
    25 enum { strstream_sepSize = 16 };
    26 struct strstream {                                                                              // satisfied basic_ostream
     25enum { ostrstream_sepSize = 16 };
     26struct ostrstream {                                                                             // satisfied basic_ostream
    2727        char * buf$;
    2828        size_t size$;
     
    3434        bool sawNL$;
    3535        const char * sepCur$;
    36         char separator$[strstream_sepSize];
    37         char tupleSeparator$[strstream_sepSize];
    38 }; // strstream
     36        char separator$[ostrstream_sepSize];
     37        char tupleSeparator$[ostrstream_sepSize];
     38}; // ostrstream
    3939
    4040// Satisfies basic_ostream
    4141
    4242// private
    43 bool sepPrt$( strstream & );
    44 void sepReset$( strstream & );
    45 void sepReset$( strstream &, bool );
    46 const char * sepGetCur$( strstream & );
    47 void sepSetCur$( strstream &, const char [] );
    48 bool getNL$( strstream & );
    49 void setNL$( strstream &, bool );
    50 bool getANL$( strstream & );
    51 bool getPrt$( strstream & );
    52 void setPrt$( strstream &, bool );
     43bool sepPrt$( ostrstream & );
     44void sepReset$( ostrstream & );
     45void sepReset$( ostrstream &, bool );
     46const char * sepGetCur$( ostrstream & );
     47void sepSetCur$( ostrstream &, const char [] );
     48bool getNL$( ostrstream & );
     49void setNL$( ostrstream &, bool );
     50bool getANL$( ostrstream & );
     51bool getPrt$( ostrstream & );
     52void setPrt$( ostrstream &, bool );
    5353
    5454// public
    55 void sepOn( strstream & );
    56 void sepOff( strstream & );
    57 bool sepDisable( strstream & );
    58 bool sepEnable( strstream & );
    59 void nlOn( strstream & );
    60 void nlOff( strstream & );
     55void sepOn( ostrstream & );
     56void sepOff( ostrstream & );
     57bool sepDisable( ostrstream & );
     58bool sepEnable( ostrstream & );
     59void nlOn( ostrstream & );
     60void nlOff( ostrstream & );
    6161
    62 const char * sepGet( strstream & );
    63 void sepSet( strstream &, const char [] );
    64 const char * sepGetTuple( strstream & );
    65 void sepSetTuple( strstream &, const char [] );
     62const char * sepGet( ostrstream & );
     63void sepSet( ostrstream &, const char [] );
     64const char * sepGetTuple( ostrstream & );
     65void sepSetTuple( ostrstream &, const char [] );
    6666
    67 void ends( strstream & );
    68 int fmt( strstream &, const char format[], ... ) __attribute__(( format(printf, 2, 3) ));
    69 int flush( strstream & );
     67void ends( ostrstream & );
     68int fmt( ostrstream &, const char format[], ... ) __attribute__(( format(printf, 2, 3) ));
    7069
    71 strstream & write( strstream & os );                                    // use stdout, default value not working
    72 strstream & write( strstream & os, FILE * stream = stdout );
     70ostrstream & write( ostrstream & os, FILE * stream ); // FIX ME: use default = stdout
     71ostrstream & write( ostrstream & os );
    7372
    74 void ?{}( strstream &, char buf[], size_t size );
     73void ?{}( ostrstream &, char buf[], size_t size );
    7574
    76 extern strstream & sstr;
     75
     76// *********************************** istrstream ***********************************
     77
     78
     79struct istrstream {
     80        char * buf$;
     81        size_t cursor$;
     82        bool nlOnOff$;
     83}; // istrstream
     84
     85// Satisfies basic_istream
     86
     87// public
     88bool getANL( istrstream & );
     89void nlOn( istrstream & );
     90void nlOff( istrstream & );
     91void ends( istrstream & );
     92int fmt( istrstream &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) ));
     93istrstream & ungetc( istrstream & is, char c );
     94int eof( istrstream & is );
     95
     96void ?{}( istrstream & is, char buf[] );
    7797
    7898// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.