source: libcfa/src/strstream.cfa@ f5856ecd

Last change on this file since f5856ecd was 1034059, checked in by Peter A. Buhr <pabuhr@…>, 4 weeks ago

add "clear" function to strstring

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[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
[1034059]12// Last Modified On : Sat Oct 11 15:10:56 2025
13// Update Count : 119
[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]30static 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
56basic_ostream_data(ostrstream) const & basic_ostream_table = ostrstream_basic_data;
[fec63b2]57
58#define IO_MSG "I/O error: "
59
60// private
[f5d9c37]61inline bool getNL$( ostrstream & os ) { return os.sawNL$; }
[d0cfcbe1]62inline bool setNL$( ostrstream & os, bool state ) { bool temp = os.sawNL$; os.sawNL$ = state; return temp; }
[f5d9c37]63inline bool getANL$( ostrstream & os ) { return os.nlOnOff$; }
[d0cfcbe1]64inline bool setANL$( ostrstream & os, bool state ) { bool temp = os.nlOnOff$; os.nlOnOff$ = state; return temp; }
[321a1b15]65inline bool sepPrt$( ostrstream & os ) { setNL$( os, false ); return os.sepOnOff$; }
66inline void sepReset$( ostrstream & os ) { os.sepOnOff$ = os.sepDefault$; }
67inline void sepReset$( ostrstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; }
68inline const char * sepGetCur$( ostrstream & os ) { return os.sepCur$; }
69inline void sepSetCur$( ostrstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; }
70inline bool getPrt$( ostrstream & os ) { return os.prt$; }
[d0cfcbe1]71inline bool setPrt$( ostrstream & os, bool state ) { bool temp = os.prt$; os.prt$ = state; return temp; }
[fec63b2]72
73// public
[f5d9c37]74void nlOn( ostrstream & os ) { os.nlOnOff$ = true; }
75void nlOff( ostrstream & os ) { os.nlOnOff$ = false; }
76
[666483d]77void ?{}( 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]91void sep( ostrstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
92void nosep( ostrstream & os ) { os.sepOnOff$ = false; }
[fec63b2]93
[f5d9c37]94bool 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]101bool sepOff( ostrstream & os ) {
102 bool temp = os.sepDefault$;
103 os.sepDefault$ = false;
104 sepReset$( os );
105 return temp;
106} // sepOff
[fec63b2]107
[666483d]108const char * sepGet( ostrstream & os ) { return os.separator$; }
109void 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]115const char * sepGetTuple( ostrstream & os ) { return os.tupleSeparator$; }
116void 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]122void ends( ostrstream & os ) {
[fec63b2]123 if ( getANL$( os ) ) nl( os );
124 else setPrt$( os, false ); // turn off
125} // ends
126
[666483d]127int 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]144ostrstream & 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
[1034059]150 os.cursor$ = 0;
[fec63b2]151 return os;
152} // write
153
[666483d]154ostrstream & write( ostrstream & os ) {
155 return write( os, stdout );
156} // write
157
[1034059]158void clear( ostrstream & os ) {
159 os.cursor$ = 0;
160} // clear
[666483d]161
162// *********************************** istrstream ***********************************
163
[ae0c1c3]164static basic_istream_data(istrstream) istrstream_basic_data = {
165 getANL$,
166 setANL$,
167 nlOn,
168 nlOff,
169 fmt,
170 ungetc,
171 eof,
172 clearerr,
173};
174
175basic_istream_data(istrstream) const & basic_istream_data = istrstream_basic_data;
176
[321a1b15]177// private
178bool getANL$( istrstream & is ) { return is.nlOnOff$; }
[d0cfcbe1]179bool setANL$( istrstream & is, bool state ) { bool temp = is.nlOnOff$; is.nlOnOff$ = state; return temp; }
[666483d]180
181// public
182void ?{}( istrstream & is, char buf[] ) {
183 is.buf$ = buf;
184 is.cursor$ = 0;
185 is.nlOnOff$ = false;
186} // ?{}
187
188void nlOn( istrstream & is ) { is.nlOnOff$ = true; }
189void nlOff( istrstream & is ) { is.nlOnOff$ = false; }
190
[321a1b15]191int fmt( istrstream & is, const char format[], ... ) with(is) {
[666483d]192 va_list args;
193 va_start( args, format );
[321a1b15]194 // THIS DOES NOT WORK BECAUSE VSSCANF RETURNS NUMBER OF VALUES READ VERSUS BUFFER POSITION SCANNED.
195 int len = vsscanf( buf$ + cursor$, format, args );
[666483d]196 va_end( args );
197 if ( len == EOF ) {
[321a1b15]198 abort | IO_MSG "invalid read";
[666483d]199 } // if
[321a1b15]200 // SKULLDUGGERY: This hack skips over characters read by vsscanf by moving to the next whitespace but it does not
201 // handle C reads with wdi manipulators that leave the cursor at a non-whitespace character.
202 for ( ; buf$[cursor$] != ' ' && buf$[cursor$] != '\t' && buf$[cursor$] != '\0'; cursor$ += 1 ) {
203 //printf( "X \'%c\'\n", buf$[cursor$] );
204 } // for
205 if ( buf$[cursor$] != '\0' ) cursor$ += 1; // advance to whitespace
[666483d]206 return len;
207} // fmt
[fec63b2]208
[a1a1f37d]209istrstream &ungetc( char c, istrstream & is ) {
[321a1b15]210 // if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) {
211 // abort | IO_MSG "ungetc" | nl | strerror( errno );
212 // } // if
213 return is;
214} // ungetc
215
[768d091]216bool eof( istrstream & is ) { return false; }
217void clearerr( istrstream & ) {} // no error flags to clear
218void ends( istrstream & is ) {}
219
[fec63b2]220// Local Variables: //
221// tab-width: 4 //
222// End: //
Note: See TracBrowser for help on using the repository browser.