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 : Sat Apr 24 11:15:47 2021
|
---|
13 | // Update Count : 73
|
---|
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 | // *********************************** strstream ***********************************
|
---|
26 |
|
---|
27 |
|
---|
28 | #define IO_MSG "I/O error: "
|
---|
29 |
|
---|
30 | // private
|
---|
31 | bool sepPrt$( strstream & os ) { setNL$( os, false ); return os.sepOnOff$; }
|
---|
32 | void sepReset$( strstream & os ) { os.sepOnOff$ = os.sepDefault$; }
|
---|
33 | void sepReset$( strstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; }
|
---|
34 | const char * sepGetCur$( strstream & os ) { return os.sepCur$; }
|
---|
35 | void sepSetCur$( strstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; }
|
---|
36 | bool getNL$( strstream & os ) { return os.sawNL$; }
|
---|
37 | void setNL$( strstream & os, bool state ) { os.sawNL$ = state; }
|
---|
38 | bool getANL$( strstream & os ) { return os.nlOnOff$; }
|
---|
39 | bool getPrt$( strstream & os ) { return os.prt$; }
|
---|
40 | void setPrt$( strstream & os, bool state ) { os.prt$ = state; }
|
---|
41 |
|
---|
42 | // public
|
---|
43 | void ?{}( strstream & os, char buf[], size_t size ) {
|
---|
44 | os.buf$ = buf;
|
---|
45 | os.size$ = size;
|
---|
46 | os.cursor$ = 0;
|
---|
47 | os.sepDefault$ = true;
|
---|
48 | os.sepOnOff$ = false;
|
---|
49 | os.nlOnOff$ = true;
|
---|
50 | os.prt$ = false;
|
---|
51 | os.sawNL$ = false;
|
---|
52 | sepSetCur$( os, sepGet( os ) );
|
---|
53 | sepSet( os, " " );
|
---|
54 | sepSetTuple( os, ", " );
|
---|
55 | } // ?{}
|
---|
56 |
|
---|
57 | void sepOn( strstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
|
---|
58 | void sepOff( strstream & os ) { os.sepOnOff$ = false; }
|
---|
59 |
|
---|
60 | bool sepDisable( strstream & os ) {
|
---|
61 | bool temp = os.sepDefault$;
|
---|
62 | os.sepDefault$ = false;
|
---|
63 | sepReset$( os );
|
---|
64 | return temp;
|
---|
65 | } // sepDisable
|
---|
66 |
|
---|
67 | bool sepEnable( strstream & os ) {
|
---|
68 | bool temp = os.sepDefault$;
|
---|
69 | os.sepDefault$ = true;
|
---|
70 | if ( os.sepOnOff$ ) sepReset$( os ); // start of line ?
|
---|
71 | return temp;
|
---|
72 | } // sepEnable
|
---|
73 |
|
---|
74 | void nlOn( strstream & os ) { os.nlOnOff$ = true; }
|
---|
75 | void nlOff( strstream & os ) { os.nlOnOff$ = false; }
|
---|
76 |
|
---|
77 | const char * sepGet( strstream & os ) { return os.separator$; }
|
---|
78 | void sepSet( strstream & os, const char s[] ) {
|
---|
79 | assert( s );
|
---|
80 | strncpy( os.separator$, s, strstream_sepSize - 1 );
|
---|
81 | os.separator$[strstream_sepSize - 1] = '\0';
|
---|
82 | } // sepSet
|
---|
83 |
|
---|
84 | const char * sepGetTuple( strstream & os ) { return os.tupleSeparator$; }
|
---|
85 | void sepSetTuple( strstream & os, const char s[] ) {
|
---|
86 | assert( s );
|
---|
87 | strncpy( os.tupleSeparator$, s, strstream_sepSize - 1 );
|
---|
88 | os.tupleSeparator$[strstream_sepSize - 1] = '\0';
|
---|
89 | } // sepSet
|
---|
90 |
|
---|
91 | void ends( strstream & os ) {
|
---|
92 | if ( getANL$( os ) ) nl( os );
|
---|
93 | else setPrt$( os, false ); // turn off
|
---|
94 | } // ends
|
---|
95 |
|
---|
96 | int fmt( strstream & os, const char format[], ... ) {
|
---|
97 | va_list args;
|
---|
98 | va_start( args, format );
|
---|
99 | int len = vsnprintf( os.buf$ + os.cursor$, os.size$ - os.cursor$, format, args );
|
---|
100 | os.cursor$ += len;
|
---|
101 | if ( os.cursor$ >= os.size$ ) { // cursor exceeded buffer size?
|
---|
102 | #define fmtmsg IO_MSG "strstream truncated write, buffer too small.\n"
|
---|
103 | write( STDERR_FILENO, fmtmsg, sizeof(fmtmsg) - 1 );
|
---|
104 | abort();
|
---|
105 | } // if
|
---|
106 | va_end( args );
|
---|
107 |
|
---|
108 | setPrt$( os, true ); // called in output cascade
|
---|
109 | sepReset$( os ); // reset separator
|
---|
110 | return len;
|
---|
111 | } // fmt
|
---|
112 |
|
---|
113 | int flush( strstream & ) { // match trait, not used
|
---|
114 | return 0;
|
---|
115 | } // flush
|
---|
116 |
|
---|
117 | strstream & write( strstream & os ) {
|
---|
118 | return write( os, stdout );
|
---|
119 | } // write
|
---|
120 | strstream & write( strstream & os, FILE * stream ) {
|
---|
121 | if ( fwrite( os.buf$, 1, os.cursor$, stream ) != os.cursor$ ) {
|
---|
122 | #define writemsg IO_MSG "strstream write error.\n"
|
---|
123 | write( STDERR_FILENO, writemsg, sizeof(writemsg) - 1 );
|
---|
124 | abort();
|
---|
125 | } // if
|
---|
126 | return os;
|
---|
127 | } // write
|
---|
128 |
|
---|
129 | strstream & sstr;
|
---|
130 |
|
---|
131 | // Local Variables: //
|
---|
132 | // tab-width: 4 //
|
---|
133 | // End: //
|
---|