| 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 : Tue Apr 27 20:59:53 2021
 | 
|---|
| 13 | // Update Count     : 78
 | 
|---|
| 14 | // 
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "strstream.hfa"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include <stdio.h>                                                                              // vsnprintf
 | 
|---|
| 19 | #include <stdarg.h>                                                                             // varargs
 | 
|---|
| 20 | #include <string.h>                                                                             // strncpy, strerror
 | 
|---|
| 21 | #include <assert.h>
 | 
|---|
| 22 | #include <errno.h>                                                                              // errno
 | 
|---|
| 23 | #include <unistd.h>                                                                             // sbrk, sysconf
 | 
|---|
| 24 | 
 | 
|---|
| 25 | 
 | 
|---|
| 26 | // *********************************** strstream ***********************************
 | 
|---|
| 27 | 
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #define IO_MSG "I/O error: "
 | 
|---|
| 30 | 
 | 
|---|
| 31 | // private
 | 
|---|
| 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; }
 | 
|---|
| 42 | 
 | 
|---|
| 43 | // public
 | 
|---|
| 44 | void ?{}( ostrstream & os, char buf[], size_t size ) {
 | 
|---|
| 45 |         os.buf$ = buf;
 | 
|---|
| 46 |         os.size$ = size;
 | 
|---|
| 47 |         os.cursor$ = 0;
 | 
|---|
| 48 |         os.sepDefault$ = true;
 | 
|---|
| 49 |         os.sepOnOff$ = false;
 | 
|---|
| 50 |         os.nlOnOff$ = true;
 | 
|---|
| 51 |         os.prt$ = false;
 | 
|---|
| 52 |         os.sawNL$ = false;
 | 
|---|
| 53 |         sepSetCur$( os, sepGet( os ) );
 | 
|---|
| 54 |         sepSet( os, " " );
 | 
|---|
| 55 |         sepSetTuple( os, ", " );
 | 
|---|
| 56 | } // ?{}
 | 
|---|
| 57 | 
 | 
|---|
| 58 | void sepOn( ostrstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
 | 
|---|
| 59 | void sepOff( ostrstream & os ) { os.sepOnOff$ = false; }
 | 
|---|
| 60 | 
 | 
|---|
| 61 | bool sepDisable( ostrstream & os ) {
 | 
|---|
| 62 |         bool temp = os.sepDefault$;
 | 
|---|
| 63 |         os.sepDefault$ = false;
 | 
|---|
| 64 |         sepReset$( os );
 | 
|---|
| 65 |         return temp;
 | 
|---|
| 66 | } // sepDisable
 | 
|---|
| 67 | 
 | 
|---|
| 68 | bool sepEnable( ostrstream & os ) {
 | 
|---|
| 69 |         bool temp = os.sepDefault$;
 | 
|---|
| 70 |         os.sepDefault$ = true;
 | 
|---|
| 71 |         if ( os.sepOnOff$ ) sepReset$( os );                            // start of line ?
 | 
|---|
| 72 |         return temp;
 | 
|---|
| 73 | } // sepEnable
 | 
|---|
| 74 | 
 | 
|---|
| 75 | void nlOn( ostrstream & os ) { os.nlOnOff$ = true; }
 | 
|---|
| 76 | void nlOff( ostrstream & os ) { os.nlOnOff$ = false; }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | const char * sepGet( ostrstream & os ) { return os.separator$; }
 | 
|---|
| 79 | void sepSet( ostrstream & os, const char s[] ) {
 | 
|---|
| 80 |         assert( s );
 | 
|---|
| 81 |         strncpy( os.separator$, s, ostrstream_sepSize - 1 );
 | 
|---|
| 82 |         os.separator$[ostrstream_sepSize - 1] = '\0';
 | 
|---|
| 83 | } // sepSet
 | 
|---|
| 84 | 
 | 
|---|
| 85 | const char * sepGetTuple( ostrstream & os ) { return os.tupleSeparator$; }
 | 
|---|
| 86 | void sepSetTuple( ostrstream & os, const char s[] ) {
 | 
|---|
| 87 |         assert( s );
 | 
|---|
| 88 |         strncpy( os.tupleSeparator$, s, ostrstream_sepSize - 1 );
 | 
|---|
| 89 |         os.tupleSeparator$[ostrstream_sepSize - 1] = '\0';
 | 
|---|
| 90 | } // sepSet
 | 
|---|
| 91 | 
 | 
|---|
| 92 | void ends( ostrstream & os ) {
 | 
|---|
| 93 |         if ( getANL$( os ) ) nl( os );
 | 
|---|
| 94 |         else setPrt$( os, false );                                                      // turn off
 | 
|---|
| 95 | } // ends
 | 
|---|
| 96 | 
 | 
|---|
| 97 | int fmt( ostrstream & os, const char format[], ... ) {
 | 
|---|
| 98 |         va_list args;
 | 
|---|
| 99 |         va_start( args, format );
 | 
|---|
| 100 |         int len = vsnprintf( os.buf$ + os.cursor$, os.size$ - os.cursor$, format, args );
 | 
|---|
| 101 |         va_end( args );
 | 
|---|
| 102 |         os.cursor$ += len;
 | 
|---|
| 103 |         if ( os.cursor$ >= os.size$ ) {                                         // cursor exceeded buffer size?
 | 
|---|
| 104 |                 #define fmtmsg IO_MSG "ostrstream truncated write, buffer too small.\n"
 | 
|---|
| 105 |                 write( STDERR_FILENO, fmtmsg, sizeof(fmtmsg) - 1 );
 | 
|---|
| 106 |                 abort();
 | 
|---|
| 107 |         } // if
 | 
|---|
| 108 | 
 | 
|---|
| 109 |         setPrt$( os, true );                                                            // called in output cascade
 | 
|---|
| 110 |         sepReset$( os );                                                                        // reset separator
 | 
|---|
| 111 |         return len;
 | 
|---|
| 112 | } // fmt
 | 
|---|
| 113 | 
 | 
|---|
| 114 | ostrstream & write( ostrstream & os, FILE * stream ) {
 | 
|---|
| 115 |         if ( fwrite( os.buf$, 1, os.cursor$, stream ) != os.cursor$ ) {
 | 
|---|
| 116 |                 #define ostrwritemsg IO_MSG "ostrstream write error.\n"
 | 
|---|
| 117 |                 write( STDERR_FILENO, ostrwritemsg, sizeof(ostrwritemsg) - 1 );
 | 
|---|
| 118 |                 abort();
 | 
|---|
| 119 |         } // if
 | 
|---|
| 120 |         return os;
 | 
|---|
| 121 | } // write
 | 
|---|
| 122 | 
 | 
|---|
| 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
 | 
|---|
| 169 | 
 | 
|---|
| 170 | // Local Variables: //
 | 
|---|
| 171 | // tab-width: 4 //
 | 
|---|
| 172 | // End: //
 | 
|---|