Changeset 666483d
- Timestamp:
- Apr 27, 2021, 10:13:14 PM (4 years ago)
- 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
- Location:
- libcfa/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/strstream.cfa
rf451177 r666483d 10 10 // Created On : Thu Apr 22 22:24:35 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Apr 24 11:15:47202113 // Update Count : 7 312 // Last Modified On : Tue Apr 27 20:59:53 2021 13 // Update Count : 78 14 14 // 15 15 … … 23 23 #include <unistd.h> // sbrk, sysconf 24 24 25 25 26 // *********************************** strstream *********************************** 26 27 … … 29 30 30 31 // 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; }32 bool sepPrt$( ostrstream & os ) { setNL$( os, false ); return os.sepOnOff$; } 33 void sepReset$( ostrstream & os ) { os.sepOnOff$ = os.sepDefault$; } 34 void sepReset$( ostrstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; } 35 const char * sepGetCur$( ostrstream & os ) { return os.sepCur$; } 36 void sepSetCur$( ostrstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; } 37 bool getNL$( ostrstream & os ) { return os.sawNL$; } 38 void setNL$( ostrstream & os, bool state ) { os.sawNL$ = state; } 39 bool getANL$( ostrstream & os ) { return os.nlOnOff$; } 40 bool getPrt$( ostrstream & os ) { return os.prt$; } 41 void setPrt$( ostrstream & os, bool state ) { os.prt$ = state; } 41 42 42 43 // public 43 void ?{}( strstream & os, char buf[], size_t size ) {44 void ?{}( ostrstream & os, char buf[], size_t size ) { 44 45 os.buf$ = buf; 45 46 os.size$ = size; … … 55 56 } // ?{} 56 57 57 void sepOn( strstream & os ) { os.sepOnOff$ = ! getNL$( os ); }58 void sepOff( strstream & os ) { os.sepOnOff$ = false; }58 void sepOn( ostrstream & os ) { os.sepOnOff$ = ! getNL$( os ); } 59 void sepOff( ostrstream & os ) { os.sepOnOff$ = false; } 59 60 60 bool sepDisable( strstream & os ) {61 bool sepDisable( ostrstream & os ) { 61 62 bool temp = os.sepDefault$; 62 63 os.sepDefault$ = false; … … 65 66 } // sepDisable 66 67 67 bool sepEnable( strstream & os ) {68 bool sepEnable( ostrstream & os ) { 68 69 bool temp = os.sepDefault$; 69 70 os.sepDefault$ = true; … … 72 73 } // sepEnable 73 74 74 void nlOn( strstream & os ) { os.nlOnOff$ = true; }75 void nlOff( strstream & os ) { os.nlOnOff$ = false; }75 void nlOn( ostrstream & os ) { os.nlOnOff$ = true; } 76 void nlOff( ostrstream & os ) { os.nlOnOff$ = false; } 76 77 77 const char * sepGet( strstream & os ) { return os.separator$; }78 void sepSet( strstream & os, const char s[] ) {78 const char * sepGet( ostrstream & os ) { return os.separator$; } 79 void sepSet( ostrstream & os, const char s[] ) { 79 80 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'; 82 83 } // sepSet 83 84 84 const char * sepGetTuple( strstream & os ) { return os.tupleSeparator$; }85 void sepSetTuple( strstream & os, const char s[] ) {85 const char * sepGetTuple( ostrstream & os ) { return os.tupleSeparator$; } 86 void sepSetTuple( ostrstream & os, const char s[] ) { 86 87 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'; 89 90 } // sepSet 90 91 91 void ends( strstream & os ) {92 void ends( ostrstream & os ) { 92 93 if ( getANL$( os ) ) nl( os ); 93 94 else setPrt$( os, false ); // turn off 94 95 } // ends 95 96 96 int fmt( strstream & os, const char format[], ... ) {97 int fmt( ostrstream & os, const char format[], ... ) { 97 98 va_list args; 98 99 va_start( args, format ); 99 100 int len = vsnprintf( os.buf$ + os.cursor$, os.size$ - os.cursor$, format, args ); 101 va_end( args ); 100 102 os.cursor$ += len; 101 103 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" 103 105 write( STDERR_FILENO, fmtmsg, sizeof(fmtmsg) - 1 ); 104 106 abort(); 105 107 } // if 106 va_end( args );107 108 108 109 setPrt$( os, true ); // called in output cascade … … 111 112 } // fmt 112 113 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 ) { 114 ostrstream & write( ostrstream & os, FILE * stream ) { 121 115 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 ); 124 118 abort(); 125 119 } // if … … 127 121 } // write 128 122 129 strstream & sstr; 123 ostrstream & write( ostrstream & os ) { 124 return write( os, stdout ); 125 } // write 126 127 128 // *********************************** istrstream *********************************** 129 130 131 // public 132 void ?{}( istrstream & is, char buf[] ) { 133 is.buf$ = buf; 134 is.cursor$ = 0; 135 is.nlOnOff$ = false; 136 } // ?{} 137 138 bool getANL( istrstream & is ) { return is.nlOnOff$; } 139 void nlOn( istrstream & is ) { is.nlOnOff$ = true; } 140 void nlOff( istrstream & is ) { is.nlOnOff$ = false; } 141 142 void ends( istrstream & is ) { 143 } // ends 144 145 int eof( istrstream & is ) { 146 return 0; 147 } // eof 148 149 istrstream &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 156 int 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 130 169 131 170 // Local Variables: // -
libcfa/src/strstream.hfa
rf451177 r666483d 10 10 // Created On : Thu Apr 22 22:20:59 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Apr 24 11:17:33202113 // Update Count : 3712 // Last Modified On : Tue Apr 27 20:58:50 2021 13 // Update Count : 41 14 14 // 15 15 … … 20 20 21 21 22 // *********************************** strstream ***********************************22 // *********************************** ostrstream *********************************** 23 23 24 24 25 enum { strstream_sepSize = 16 };26 struct strstream { // satisfied basic_ostream25 enum { ostrstream_sepSize = 16 }; 26 struct ostrstream { // satisfied basic_ostream 27 27 char * buf$; 28 28 size_t size$; … … 34 34 bool sawNL$; 35 35 const char * sepCur$; 36 char separator$[ strstream_sepSize];37 char tupleSeparator$[ strstream_sepSize];38 }; // strstream36 char separator$[ostrstream_sepSize]; 37 char tupleSeparator$[ostrstream_sepSize]; 38 }; // ostrstream 39 39 40 40 // Satisfies basic_ostream 41 41 42 42 // 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 );43 bool sepPrt$( ostrstream & ); 44 void sepReset$( ostrstream & ); 45 void sepReset$( ostrstream &, bool ); 46 const char * sepGetCur$( ostrstream & ); 47 void sepSetCur$( ostrstream &, const char [] ); 48 bool getNL$( ostrstream & ); 49 void setNL$( ostrstream &, bool ); 50 bool getANL$( ostrstream & ); 51 bool getPrt$( ostrstream & ); 52 void setPrt$( ostrstream &, bool ); 53 53 54 54 // 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 & );55 void sepOn( ostrstream & ); 56 void sepOff( ostrstream & ); 57 bool sepDisable( ostrstream & ); 58 bool sepEnable( ostrstream & ); 59 void nlOn( ostrstream & ); 60 void nlOff( ostrstream & ); 61 61 62 const char * sepGet( strstream & );63 void sepSet( strstream &, const char [] );64 const char * sepGetTuple( strstream & );65 void sepSetTuple( strstream &, const char [] );62 const char * sepGet( ostrstream & ); 63 void sepSet( ostrstream &, const char [] ); 64 const char * sepGetTuple( ostrstream & ); 65 void sepSetTuple( ostrstream &, const char [] ); 66 66 67 void ends( strstream & ); 68 int fmt( strstream &, const char format[], ... ) __attribute__(( format(printf, 2, 3) )); 69 int flush( strstream & ); 67 void ends( ostrstream & ); 68 int fmt( ostrstream &, const char format[], ... ) __attribute__(( format(printf, 2, 3) )); 70 69 71 strstream & write( strstream & os ); // use stdout, default value not working 72 strstream & write( strstream & os, FILE * stream = stdout);70 ostrstream & write( ostrstream & os, FILE * stream ); // FIX ME: use default = stdout 71 ostrstream & write( ostrstream & os ); 73 72 74 void ?{}( strstream &, char buf[], size_t size );73 void ?{}( ostrstream &, char buf[], size_t size ); 75 74 76 extern strstream & sstr; 75 76 // *********************************** istrstream *********************************** 77 78 79 struct istrstream { 80 char * buf$; 81 size_t cursor$; 82 bool nlOnOff$; 83 }; // istrstream 84 85 // Satisfies basic_istream 86 87 // public 88 bool getANL( istrstream & ); 89 void nlOn( istrstream & ); 90 void nlOff( istrstream & ); 91 void ends( istrstream & ); 92 int fmt( istrstream &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) )); 93 istrstream & ungetc( istrstream & is, char c ); 94 int eof( istrstream & is ); 95 96 void ?{}( istrstream & is, char buf[] ); 77 97 78 98 // Local Variables: //
Note: See TracChangeset
for help on using the changeset viewer.