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