[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
|
---|
[d0cfcbe1] | 12 | // Last Modified On : Fri Aug 18 10:42:49 2023
|
---|
| 13 | // Update Count : 112
|
---|
[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 |
|
---|
| 30 |
|
---|
| 31 | #define IO_MSG "I/O error: "
|
---|
| 32 |
|
---|
| 33 | // private
|
---|
[f5d9c37] | 34 | inline bool getNL$( ostrstream & os ) { return os.sawNL$; }
|
---|
[d0cfcbe1] | 35 | inline bool setNL$( ostrstream & os, bool state ) { bool temp = os.sawNL$; os.sawNL$ = state; return temp; }
|
---|
[f5d9c37] | 36 | inline bool getANL$( ostrstream & os ) { return os.nlOnOff$; }
|
---|
[d0cfcbe1] | 37 | inline bool setANL$( ostrstream & os, bool state ) { bool temp = os.nlOnOff$; os.nlOnOff$ = state; return temp; }
|
---|
[321a1b15] | 38 | inline bool sepPrt$( ostrstream & os ) { setNL$( os, false ); return os.sepOnOff$; }
|
---|
| 39 | inline void sepReset$( ostrstream & os ) { os.sepOnOff$ = os.sepDefault$; }
|
---|
| 40 | inline void sepReset$( ostrstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; }
|
---|
| 41 | inline const char * sepGetCur$( ostrstream & os ) { return os.sepCur$; }
|
---|
| 42 | inline void sepSetCur$( ostrstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; }
|
---|
| 43 | inline bool getPrt$( ostrstream & os ) { return os.prt$; }
|
---|
[d0cfcbe1] | 44 | inline bool setPrt$( ostrstream & os, bool state ) { bool temp = os.prt$; os.prt$ = state; return temp; }
|
---|
[fec63b2] | 45 |
|
---|
| 46 | // public
|
---|
[f5d9c37] | 47 | void nlOn( ostrstream & os ) { os.nlOnOff$ = true; }
|
---|
| 48 | void nlOff( ostrstream & os ) { os.nlOnOff$ = false; }
|
---|
| 49 |
|
---|
[666483d] | 50 | void ?{}( ostrstream & os, char buf[], size_t size ) {
|
---|
[fec63b2] | 51 | os.buf$ = buf;
|
---|
| 52 | os.size$ = size;
|
---|
| 53 | os.cursor$ = 0;
|
---|
| 54 | os.sepDefault$ = true;
|
---|
| 55 | os.sepOnOff$ = false;
|
---|
| 56 | os.nlOnOff$ = true;
|
---|
| 57 | os.prt$ = false;
|
---|
| 58 | os.sawNL$ = false;
|
---|
| 59 | sepSetCur$( os, sepGet( os ) );
|
---|
| 60 | sepSet( os, " " );
|
---|
| 61 | sepSetTuple( os, ", " );
|
---|
| 62 | } // ?{}
|
---|
| 63 |
|
---|
[f5d9c37] | 64 | void sep( ostrstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
|
---|
| 65 | void nosep( ostrstream & os ) { os.sepOnOff$ = false; }
|
---|
[fec63b2] | 66 |
|
---|
[f5d9c37] | 67 | bool sepOn( ostrstream & os ) {
|
---|
[fec63b2] | 68 | bool temp = os.sepDefault$;
|
---|
| 69 | os.sepDefault$ = true;
|
---|
| 70 | if ( os.sepOnOff$ ) sepReset$( os ); // start of line ?
|
---|
| 71 | return temp;
|
---|
[f5d9c37] | 72 | } // sepOn
|
---|
[fec63b2] | 73 |
|
---|
[f5d9c37] | 74 | bool sepOff( ostrstream & os ) {
|
---|
| 75 | bool temp = os.sepDefault$;
|
---|
| 76 | os.sepDefault$ = false;
|
---|
| 77 | sepReset$( os );
|
---|
| 78 | return temp;
|
---|
| 79 | } // sepOff
|
---|
[fec63b2] | 80 |
|
---|
[666483d] | 81 | const char * sepGet( ostrstream & os ) { return os.separator$; }
|
---|
| 82 | void sepSet( ostrstream & os, const char s[] ) {
|
---|
[fec63b2] | 83 | assert( s );
|
---|
[666483d] | 84 | strncpy( os.separator$, s, ostrstream_sepSize - 1 );
|
---|
| 85 | os.separator$[ostrstream_sepSize - 1] = '\0';
|
---|
[fec63b2] | 86 | } // sepSet
|
---|
| 87 |
|
---|
[666483d] | 88 | const char * sepGetTuple( ostrstream & os ) { return os.tupleSeparator$; }
|
---|
| 89 | void sepSetTuple( ostrstream & os, const char s[] ) {
|
---|
[fec63b2] | 90 | assert( s );
|
---|
[666483d] | 91 | strncpy( os.tupleSeparator$, s, ostrstream_sepSize - 1 );
|
---|
| 92 | os.tupleSeparator$[ostrstream_sepSize - 1] = '\0';
|
---|
[fec63b2] | 93 | } // sepSet
|
---|
| 94 |
|
---|
[666483d] | 95 | void ends( ostrstream & os ) {
|
---|
[fec63b2] | 96 | if ( getANL$( os ) ) nl( os );
|
---|
| 97 | else setPrt$( os, false ); // turn off
|
---|
| 98 | } // ends
|
---|
| 99 |
|
---|
[666483d] | 100 | int fmt( ostrstream & os, const char format[], ... ) {
|
---|
[fec63b2] | 101 | va_list args;
|
---|
| 102 | va_start( args, format );
|
---|
| 103 | int len = vsnprintf( os.buf$ + os.cursor$, os.size$ - os.cursor$, format, args );
|
---|
[666483d] | 104 | va_end( args );
|
---|
[fec63b2] | 105 | os.cursor$ += len;
|
---|
| 106 | if ( os.cursor$ >= os.size$ ) { // cursor exceeded buffer size?
|
---|
[666483d] | 107 | #define fmtmsg IO_MSG "ostrstream truncated write, buffer too small.\n"
|
---|
[fec63b2] | 108 | write( STDERR_FILENO, fmtmsg, sizeof(fmtmsg) - 1 );
|
---|
| 109 | abort();
|
---|
| 110 | } // if
|
---|
| 111 |
|
---|
| 112 | setPrt$( os, true ); // called in output cascade
|
---|
| 113 | sepReset$( os ); // reset separator
|
---|
| 114 | return len;
|
---|
| 115 | } // fmt
|
---|
| 116 |
|
---|
[666483d] | 117 | ostrstream & write( ostrstream & os, FILE * stream ) {
|
---|
[fec63b2] | 118 | if ( fwrite( os.buf$, 1, os.cursor$, stream ) != os.cursor$ ) {
|
---|
[666483d] | 119 | #define ostrwritemsg IO_MSG "ostrstream write error.\n"
|
---|
| 120 | write( STDERR_FILENO, ostrwritemsg, sizeof(ostrwritemsg) - 1 );
|
---|
[fec63b2] | 121 | abort();
|
---|
| 122 | } // if
|
---|
| 123 | return os;
|
---|
| 124 | } // write
|
---|
| 125 |
|
---|
[666483d] | 126 | ostrstream & write( ostrstream & os ) {
|
---|
| 127 | return write( os, stdout );
|
---|
| 128 | } // write
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 | // *********************************** istrstream ***********************************
|
---|
| 132 |
|
---|
[321a1b15] | 133 | // private
|
---|
| 134 | bool getANL$( istrstream & is ) { return is.nlOnOff$; }
|
---|
[d0cfcbe1] | 135 | bool setANL$( istrstream & is, bool state ) { bool temp = is.nlOnOff$; is.nlOnOff$ = state; return temp; }
|
---|
[666483d] | 136 |
|
---|
| 137 | // public
|
---|
| 138 | void ?{}( istrstream & is, char buf[] ) {
|
---|
| 139 | is.buf$ = buf;
|
---|
| 140 | is.cursor$ = 0;
|
---|
| 141 | is.nlOnOff$ = false;
|
---|
| 142 | } // ?{}
|
---|
| 143 |
|
---|
| 144 | void nlOn( istrstream & is ) { is.nlOnOff$ = true; }
|
---|
| 145 | void nlOff( istrstream & is ) { is.nlOnOff$ = false; }
|
---|
| 146 |
|
---|
[321a1b15] | 147 | void ends( istrstream & is ) {}
|
---|
| 148 | bool eof( istrstream & is ) { return false; }
|
---|
[666483d] | 149 |
|
---|
[321a1b15] | 150 | int fmt( istrstream & is, const char format[], ... ) with(is) {
|
---|
[666483d] | 151 | va_list args;
|
---|
| 152 | va_start( args, format );
|
---|
[321a1b15] | 153 | // THIS DOES NOT WORK BECAUSE VSSCANF RETURNS NUMBER OF VALUES READ VERSUS BUFFER POSITION SCANNED.
|
---|
| 154 | int len = vsscanf( buf$ + cursor$, format, args );
|
---|
[666483d] | 155 | va_end( args );
|
---|
| 156 | if ( len == EOF ) {
|
---|
[321a1b15] | 157 | abort | IO_MSG "invalid read";
|
---|
[666483d] | 158 | } // if
|
---|
[321a1b15] | 159 | // SKULLDUGGERY: This hack skips over characters read by vsscanf by moving to the next whitespace but it does not
|
---|
| 160 | // handle C reads with wdi manipulators that leave the cursor at a non-whitespace character.
|
---|
| 161 | for ( ; buf$[cursor$] != ' ' && buf$[cursor$] != '\t' && buf$[cursor$] != '\0'; cursor$ += 1 ) {
|
---|
| 162 | //printf( "X \'%c\'\n", buf$[cursor$] );
|
---|
| 163 | } // for
|
---|
| 164 | if ( buf$[cursor$] != '\0' ) cursor$ += 1; // advance to whitespace
|
---|
[666483d] | 165 | return len;
|
---|
| 166 | } // fmt
|
---|
[fec63b2] | 167 |
|
---|
[321a1b15] | 168 | istrstream &ungetc( istrstream & is, char c ) {
|
---|
| 169 | // if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) {
|
---|
| 170 | // abort | IO_MSG "ungetc" | nl | strerror( errno );
|
---|
| 171 | // } // if
|
---|
| 172 | return is;
|
---|
| 173 | } // ungetc
|
---|
| 174 |
|
---|
[fec63b2] | 175 | // Local Variables: //
|
---|
| 176 | // tab-width: 4 //
|
---|
| 177 | // End: //
|
---|