[cce4648] | 1 | //
|
---|
[fec63b2] | 2 | // Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo
|
---|
[cce4648] | 3 | //
|
---|
[fec63b2] | 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
[cce4648] | 7 | // strstream.cfa --
|
---|
| 8 | //
|
---|
[fec63b2] | 9 | // Author : Peter A. Buhr
|
---|
| 10 | // Created On : Thu Apr 22 22:24:35 2021
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[768d091] | 12 | // Last Modified On : Mon Apr 14 20:45:00 2025
|
---|
| 13 | // Update Count : 116
|
---|
[cce4648] | 14 | //
|
---|
[fec63b2] | 15 |
|
---|
| 16 | #include "strstream.hfa"
|
---|
[321a1b15] | 17 | #include "fstream.hfa" // abort
|
---|
[fec63b2] | 18 |
|
---|
| 19 | #include <stdio.h> // vsnprintf
|
---|
| 20 | #include <stdarg.h> // varargs
|
---|
| 21 | #include <string.h> // strncpy, strerror
|
---|
| 22 | #include <assert.h>
|
---|
| 23 | #include <errno.h> // errno
|
---|
| 24 | #include <unistd.h> // sbrk, sysconf
|
---|
| 25 |
|
---|
[cce4648] | 26 | #pragma GCC visibility push(default)
|
---|
[666483d] | 27 |
|
---|
[fec63b2] | 28 | // *********************************** strstream ***********************************
|
---|
| 29 |
|
---|
[ae0c1c3] | 30 | static basic_ostream_data(ostrstream) ostrstream_basic_data = {
|
---|
| 31 | sepPrt$,
|
---|
| 32 | sepReset$,
|
---|
| 33 | sepReset$,
|
---|
| 34 | sepGetCur$,
|
---|
| 35 | sepSetCur$,
|
---|
| 36 | getNL$,
|
---|
| 37 | setNL$,
|
---|
| 38 | getANL$,
|
---|
| 39 | setANL$,
|
---|
| 40 | getPrt$,
|
---|
| 41 | setPrt$,
|
---|
| 42 | nlOn,
|
---|
| 43 | nlOff,
|
---|
| 44 | sep,
|
---|
| 45 | nosep,
|
---|
| 46 | sepOn,
|
---|
| 47 | sepOff,
|
---|
| 48 | sepGet,
|
---|
| 49 | sepSet,
|
---|
| 50 | sepGetTuple,
|
---|
| 51 | sepSetTuple,
|
---|
| 52 | ends,
|
---|
| 53 | fmt,
|
---|
| 54 | };
|
---|
| 55 |
|
---|
| 56 | basic_ostream_data(ostrstream) const & basic_ostream_table = ostrstream_basic_data;
|
---|
[fec63b2] | 57 |
|
---|
| 58 | #define IO_MSG "I/O error: "
|
---|
| 59 |
|
---|
| 60 | // private
|
---|
[f5d9c37] | 61 | inline bool getNL$( ostrstream & os ) { return os.sawNL$; }
|
---|
[d0cfcbe1] | 62 | inline bool setNL$( ostrstream & os, bool state ) { bool temp = os.sawNL$; os.sawNL$ = state; return temp; }
|
---|
[f5d9c37] | 63 | inline bool getANL$( ostrstream & os ) { return os.nlOnOff$; }
|
---|
[d0cfcbe1] | 64 | inline bool setANL$( ostrstream & os, bool state ) { bool temp = os.nlOnOff$; os.nlOnOff$ = state; return temp; }
|
---|
[321a1b15] | 65 | inline bool sepPrt$( ostrstream & os ) { setNL$( os, false ); return os.sepOnOff$; }
|
---|
| 66 | inline void sepReset$( ostrstream & os ) { os.sepOnOff$ = os.sepDefault$; }
|
---|
| 67 | inline void sepReset$( ostrstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; }
|
---|
| 68 | inline const char * sepGetCur$( ostrstream & os ) { return os.sepCur$; }
|
---|
| 69 | inline void sepSetCur$( ostrstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; }
|
---|
| 70 | inline bool getPrt$( ostrstream & os ) { return os.prt$; }
|
---|
[d0cfcbe1] | 71 | inline bool setPrt$( ostrstream & os, bool state ) { bool temp = os.prt$; os.prt$ = state; return temp; }
|
---|
[fec63b2] | 72 |
|
---|
| 73 | // public
|
---|
[f5d9c37] | 74 | void nlOn( ostrstream & os ) { os.nlOnOff$ = true; }
|
---|
| 75 | void nlOff( ostrstream & os ) { os.nlOnOff$ = false; }
|
---|
| 76 |
|
---|
[666483d] | 77 | void ?{}( ostrstream & os, char buf[], size_t size ) {
|
---|
[fec63b2] | 78 | os.buf$ = buf;
|
---|
| 79 | os.size$ = size;
|
---|
| 80 | os.cursor$ = 0;
|
---|
| 81 | os.sepDefault$ = true;
|
---|
| 82 | os.sepOnOff$ = false;
|
---|
| 83 | os.nlOnOff$ = true;
|
---|
| 84 | os.prt$ = false;
|
---|
| 85 | os.sawNL$ = false;
|
---|
| 86 | sepSetCur$( os, sepGet( os ) );
|
---|
| 87 | sepSet( os, " " );
|
---|
| 88 | sepSetTuple( os, ", " );
|
---|
| 89 | } // ?{}
|
---|
| 90 |
|
---|
[f5d9c37] | 91 | void sep( ostrstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
|
---|
| 92 | void nosep( ostrstream & os ) { os.sepOnOff$ = false; }
|
---|
[fec63b2] | 93 |
|
---|
[f5d9c37] | 94 | bool sepOn( ostrstream & os ) {
|
---|
[fec63b2] | 95 | bool temp = os.sepDefault$;
|
---|
| 96 | os.sepDefault$ = true;
|
---|
| 97 | if ( os.sepOnOff$ ) sepReset$( os ); // start of line ?
|
---|
| 98 | return temp;
|
---|
[f5d9c37] | 99 | } // sepOn
|
---|
[fec63b2] | 100 |
|
---|
[f5d9c37] | 101 | bool sepOff( ostrstream & os ) {
|
---|
| 102 | bool temp = os.sepDefault$;
|
---|
| 103 | os.sepDefault$ = false;
|
---|
| 104 | sepReset$( os );
|
---|
| 105 | return temp;
|
---|
| 106 | } // sepOff
|
---|
[fec63b2] | 107 |
|
---|
[666483d] | 108 | const char * sepGet( ostrstream & os ) { return os.separator$; }
|
---|
| 109 | void sepSet( ostrstream & os, const char s[] ) {
|
---|
[fec63b2] | 110 | assert( s );
|
---|
[666483d] | 111 | strncpy( os.separator$, s, ostrstream_sepSize - 1 );
|
---|
| 112 | os.separator$[ostrstream_sepSize - 1] = '\0';
|
---|
[fec63b2] | 113 | } // sepSet
|
---|
| 114 |
|
---|
[666483d] | 115 | const char * sepGetTuple( ostrstream & os ) { return os.tupleSeparator$; }
|
---|
| 116 | void sepSetTuple( ostrstream & os, const char s[] ) {
|
---|
[fec63b2] | 117 | assert( s );
|
---|
[666483d] | 118 | strncpy( os.tupleSeparator$, s, ostrstream_sepSize - 1 );
|
---|
| 119 | os.tupleSeparator$[ostrstream_sepSize - 1] = '\0';
|
---|
[fec63b2] | 120 | } // sepSet
|
---|
| 121 |
|
---|
[666483d] | 122 | void ends( ostrstream & os ) {
|
---|
[fec63b2] | 123 | if ( getANL$( os ) ) nl( os );
|
---|
| 124 | else setPrt$( os, false ); // turn off
|
---|
| 125 | } // ends
|
---|
| 126 |
|
---|
[666483d] | 127 | int fmt( ostrstream & os, const char format[], ... ) {
|
---|
[fec63b2] | 128 | va_list args;
|
---|
| 129 | va_start( args, format );
|
---|
| 130 | int len = vsnprintf( os.buf$ + os.cursor$, os.size$ - os.cursor$, format, args );
|
---|
[666483d] | 131 | va_end( args );
|
---|
[fec63b2] | 132 | os.cursor$ += len;
|
---|
| 133 | if ( os.cursor$ >= os.size$ ) { // cursor exceeded buffer size?
|
---|
[666483d] | 134 | #define fmtmsg IO_MSG "ostrstream truncated write, buffer too small.\n"
|
---|
[fec63b2] | 135 | write( STDERR_FILENO, fmtmsg, sizeof(fmtmsg) - 1 );
|
---|
| 136 | abort();
|
---|
| 137 | } // if
|
---|
| 138 |
|
---|
| 139 | setPrt$( os, true ); // called in output cascade
|
---|
| 140 | sepReset$( os ); // reset separator
|
---|
| 141 | return len;
|
---|
| 142 | } // fmt
|
---|
| 143 |
|
---|
[666483d] | 144 | ostrstream & write( ostrstream & os, FILE * stream ) {
|
---|
[fec63b2] | 145 | if ( fwrite( os.buf$, 1, os.cursor$, stream ) != os.cursor$ ) {
|
---|
[666483d] | 146 | #define ostrwritemsg IO_MSG "ostrstream write error.\n"
|
---|
| 147 | write( STDERR_FILENO, ostrwritemsg, sizeof(ostrwritemsg) - 1 );
|
---|
[fec63b2] | 148 | abort();
|
---|
| 149 | } // if
|
---|
| 150 | return os;
|
---|
| 151 | } // write
|
---|
| 152 |
|
---|
[666483d] | 153 | ostrstream & write( ostrstream & os ) {
|
---|
| 154 | return write( os, stdout );
|
---|
| 155 | } // write
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | // *********************************** istrstream ***********************************
|
---|
| 159 |
|
---|
[ae0c1c3] | 160 | static basic_istream_data(istrstream) istrstream_basic_data = {
|
---|
| 161 | getANL$,
|
---|
| 162 | setANL$,
|
---|
| 163 | nlOn,
|
---|
| 164 | nlOff,
|
---|
| 165 | fmt,
|
---|
| 166 | ungetc,
|
---|
| 167 | eof,
|
---|
| 168 | clearerr,
|
---|
| 169 | };
|
---|
| 170 |
|
---|
| 171 | basic_istream_data(istrstream) const & basic_istream_data = istrstream_basic_data;
|
---|
| 172 |
|
---|
[321a1b15] | 173 | // private
|
---|
| 174 | bool getANL$( istrstream & is ) { return is.nlOnOff$; }
|
---|
[d0cfcbe1] | 175 | bool setANL$( istrstream & is, bool state ) { bool temp = is.nlOnOff$; is.nlOnOff$ = state; return temp; }
|
---|
[666483d] | 176 |
|
---|
| 177 | // public
|
---|
| 178 | void ?{}( istrstream & is, char buf[] ) {
|
---|
| 179 | is.buf$ = buf;
|
---|
| 180 | is.cursor$ = 0;
|
---|
| 181 | is.nlOnOff$ = false;
|
---|
| 182 | } // ?{}
|
---|
| 183 |
|
---|
| 184 | void nlOn( istrstream & is ) { is.nlOnOff$ = true; }
|
---|
| 185 | void nlOff( istrstream & is ) { is.nlOnOff$ = false; }
|
---|
| 186 |
|
---|
[321a1b15] | 187 | int fmt( istrstream & is, const char format[], ... ) with(is) {
|
---|
[666483d] | 188 | va_list args;
|
---|
| 189 | va_start( args, format );
|
---|
[321a1b15] | 190 | // THIS DOES NOT WORK BECAUSE VSSCANF RETURNS NUMBER OF VALUES READ VERSUS BUFFER POSITION SCANNED.
|
---|
| 191 | int len = vsscanf( buf$ + cursor$, format, args );
|
---|
[666483d] | 192 | va_end( args );
|
---|
| 193 | if ( len == EOF ) {
|
---|
[321a1b15] | 194 | abort | IO_MSG "invalid read";
|
---|
[666483d] | 195 | } // if
|
---|
[321a1b15] | 196 | // SKULLDUGGERY: This hack skips over characters read by vsscanf by moving to the next whitespace but it does not
|
---|
| 197 | // handle C reads with wdi manipulators that leave the cursor at a non-whitespace character.
|
---|
| 198 | for ( ; buf$[cursor$] != ' ' && buf$[cursor$] != '\t' && buf$[cursor$] != '\0'; cursor$ += 1 ) {
|
---|
| 199 | //printf( "X \'%c\'\n", buf$[cursor$] );
|
---|
| 200 | } // for
|
---|
| 201 | if ( buf$[cursor$] != '\0' ) cursor$ += 1; // advance to whitespace
|
---|
[666483d] | 202 | return len;
|
---|
| 203 | } // fmt
|
---|
[fec63b2] | 204 |
|
---|
[a1a1f37d] | 205 | istrstream &ungetc( char c, istrstream & is ) {
|
---|
[321a1b15] | 206 | // if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) {
|
---|
| 207 | // abort | IO_MSG "ungetc" | nl | strerror( errno );
|
---|
| 208 | // } // if
|
---|
| 209 | return is;
|
---|
| 210 | } // ungetc
|
---|
| 211 |
|
---|
[768d091] | 212 | bool eof( istrstream & is ) { return false; }
|
---|
| 213 | void clearerr( istrstream & ) {} // no error flags to clear
|
---|
| 214 | void ends( istrstream & is ) {}
|
---|
| 215 |
|
---|
[fec63b2] | 216 | // Local Variables: //
|
---|
| 217 | // tab-width: 4 //
|
---|
| 218 | // End: //
|
---|