| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // strstream.cfa --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Peter A. Buhr
 | 
|---|
| 10 | // Created On       : Thu Apr 22 22:24:35 2021
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Fri Aug 18 10:42:49 2023
 | 
|---|
| 13 | // Update Count     : 112
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "strstream.hfa"
 | 
|---|
| 17 | #include "fstream.hfa"                                                                  // abort
 | 
|---|
| 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 | 
 | 
|---|
| 26 | #pragma GCC visibility push(default)
 | 
|---|
| 27 | 
 | 
|---|
| 28 | // *********************************** strstream ***********************************
 | 
|---|
| 29 | 
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #define IO_MSG "I/O error: "
 | 
|---|
| 32 | 
 | 
|---|
| 33 | // private
 | 
|---|
| 34 | inline bool getNL$( ostrstream & os ) { return os.sawNL$; }
 | 
|---|
| 35 | inline bool setNL$( ostrstream & os, bool state ) { bool temp = os.sawNL$; os.sawNL$ = state; return temp; }
 | 
|---|
| 36 | inline bool getANL$( ostrstream & os ) { return os.nlOnOff$; }
 | 
|---|
| 37 | inline bool setANL$( ostrstream & os, bool state ) { bool temp = os.nlOnOff$; os.nlOnOff$ = state; return temp; }
 | 
|---|
| 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$; }
 | 
|---|
| 44 | inline bool setPrt$( ostrstream & os, bool state ) { bool temp = os.prt$; os.prt$ = state; return temp; }
 | 
|---|
| 45 | 
 | 
|---|
| 46 | // public
 | 
|---|
| 47 | void nlOn( ostrstream & os ) { os.nlOnOff$ = true; }
 | 
|---|
| 48 | void nlOff( ostrstream & os ) { os.nlOnOff$ = false; }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | void ?{}( ostrstream & os, char buf[], size_t size ) {
 | 
|---|
| 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 | 
 | 
|---|
| 64 | void sep( ostrstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
 | 
|---|
| 65 | void nosep( ostrstream & os ) { os.sepOnOff$ = false; }
 | 
|---|
| 66 | 
 | 
|---|
| 67 | bool sepOn( ostrstream & os ) {
 | 
|---|
| 68 |         bool temp = os.sepDefault$;
 | 
|---|
| 69 |         os.sepDefault$ = true;
 | 
|---|
| 70 |         if ( os.sepOnOff$ ) sepReset$( os );                            // start of line ?
 | 
|---|
| 71 |         return temp;
 | 
|---|
| 72 | } // sepOn
 | 
|---|
| 73 | 
 | 
|---|
| 74 | bool sepOff( ostrstream & os ) {
 | 
|---|
| 75 |         bool temp = os.sepDefault$;
 | 
|---|
| 76 |         os.sepDefault$ = false;
 | 
|---|
| 77 |         sepReset$( os );
 | 
|---|
| 78 |         return temp;
 | 
|---|
| 79 | } // sepOff
 | 
|---|
| 80 | 
 | 
|---|
| 81 | const char * sepGet( ostrstream & os ) { return os.separator$; }
 | 
|---|
| 82 | void sepSet( ostrstream & os, const char s[] ) {
 | 
|---|
| 83 |         assert( s );
 | 
|---|
| 84 |         strncpy( os.separator$, s, ostrstream_sepSize - 1 );
 | 
|---|
| 85 |         os.separator$[ostrstream_sepSize - 1] = '\0';
 | 
|---|
| 86 | } // sepSet
 | 
|---|
| 87 | 
 | 
|---|
| 88 | const char * sepGetTuple( ostrstream & os ) { return os.tupleSeparator$; }
 | 
|---|
| 89 | void sepSetTuple( ostrstream & os, const char s[] ) {
 | 
|---|
| 90 |         assert( s );
 | 
|---|
| 91 |         strncpy( os.tupleSeparator$, s, ostrstream_sepSize - 1 );
 | 
|---|
| 92 |         os.tupleSeparator$[ostrstream_sepSize - 1] = '\0';
 | 
|---|
| 93 | } // sepSet
 | 
|---|
| 94 | 
 | 
|---|
| 95 | void ends( ostrstream & os ) {
 | 
|---|
| 96 |         if ( getANL$( os ) ) nl( os );
 | 
|---|
| 97 |         else setPrt$( os, false );                                                      // turn off
 | 
|---|
| 98 | } // ends
 | 
|---|
| 99 | 
 | 
|---|
| 100 | int fmt( ostrstream & os, const char format[], ... ) {
 | 
|---|
| 101 |         va_list args;
 | 
|---|
| 102 |         va_start( args, format );
 | 
|---|
| 103 |         int len = vsnprintf( os.buf$ + os.cursor$, os.size$ - os.cursor$, format, args );
 | 
|---|
| 104 |         va_end( args );
 | 
|---|
| 105 |         os.cursor$ += len;
 | 
|---|
| 106 |         if ( os.cursor$ >= os.size$ ) {                                         // cursor exceeded buffer size?
 | 
|---|
| 107 |                 #define fmtmsg IO_MSG "ostrstream truncated write, buffer too small.\n"
 | 
|---|
| 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 | 
 | 
|---|
| 117 | ostrstream & write( ostrstream & os, FILE * stream ) {
 | 
|---|
| 118 |         if ( fwrite( os.buf$, 1, os.cursor$, stream ) != os.cursor$ ) {
 | 
|---|
| 119 |                 #define ostrwritemsg IO_MSG "ostrstream write error.\n"
 | 
|---|
| 120 |                 write( STDERR_FILENO, ostrwritemsg, sizeof(ostrwritemsg) - 1 );
 | 
|---|
| 121 |                 abort();
 | 
|---|
| 122 |         } // if
 | 
|---|
| 123 |         return os;
 | 
|---|
| 124 | } // write
 | 
|---|
| 125 | 
 | 
|---|
| 126 | ostrstream & write( ostrstream & os ) {
 | 
|---|
| 127 |         return write( os, stdout );
 | 
|---|
| 128 | } // write
 | 
|---|
| 129 | 
 | 
|---|
| 130 | 
 | 
|---|
| 131 | // *********************************** istrstream ***********************************
 | 
|---|
| 132 | 
 | 
|---|
| 133 | // private
 | 
|---|
| 134 | bool getANL$( istrstream & is ) { return is.nlOnOff$; }
 | 
|---|
| 135 | bool setANL$( istrstream & is, bool state ) { bool temp = is.nlOnOff$; is.nlOnOff$ = state; return temp;  }
 | 
|---|
| 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 | 
 | 
|---|
| 147 | void ends( istrstream & is ) {}
 | 
|---|
| 148 | bool eof( istrstream & is ) { return false; }
 | 
|---|
| 149 | 
 | 
|---|
| 150 | int fmt( istrstream & is, const char format[], ... ) with(is) {
 | 
|---|
| 151 |         va_list args;
 | 
|---|
| 152 |         va_start( args, format );
 | 
|---|
| 153 |         // THIS DOES NOT WORK BECAUSE VSSCANF RETURNS NUMBER OF VALUES READ VERSUS BUFFER POSITION SCANNED.
 | 
|---|
| 154 |         int len = vsscanf( buf$ + cursor$, format, args );
 | 
|---|
| 155 |         va_end( args );
 | 
|---|
| 156 |         if ( len == EOF ) {
 | 
|---|
| 157 |                 abort | IO_MSG "invalid read";
 | 
|---|
| 158 |         } // if
 | 
|---|
| 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
 | 
|---|
| 165 |         return len;
 | 
|---|
| 166 | } // fmt
 | 
|---|
| 167 | 
 | 
|---|
| 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 | 
 | 
|---|
| 175 | // Local Variables: //
 | 
|---|
| 176 | // tab-width: 4 //
 | 
|---|
| 177 | // End: //
 | 
|---|