source: libcfa/src/strstream.cfa @ ef3ac46

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ef3ac46 was fec63b2, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

add first draft of strstream type

  • Property mode set to 100644
File size: 3.9 KB
Line 
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
31bool sepPrt$( strstream & os ) { setNL$( os, false ); return os.sepOnOff$; }
32void sepReset$( strstream & os ) { os.sepOnOff$ = os.sepDefault$; }
33void sepReset$( strstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; }
34const char * sepGetCur$( strstream & os ) { return os.sepCur$; }
35void sepSetCur$( strstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; }
36bool getNL$( strstream & os ) { return os.sawNL$; }
37void setNL$( strstream & os, bool state ) { os.sawNL$ = state; }
38bool getANL$( strstream & os ) { return os.nlOnOff$; }
39bool getPrt$( strstream & os ) { return os.prt$; }
40void setPrt$( strstream & os, bool state ) { os.prt$ = state; }
41
42// public
43void ?{}( 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
57void sepOn( strstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
58void sepOff( strstream & os ) { os.sepOnOff$ = false; }
59
60bool sepDisable( strstream & os ) {
61        bool temp = os.sepDefault$;
62        os.sepDefault$ = false;
63        sepReset$( os );
64        return temp;
65} // sepDisable
66
67bool 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
74void nlOn( strstream & os ) { os.nlOnOff$ = true; }
75void nlOff( strstream & os ) { os.nlOnOff$ = false; }
76
77const char * sepGet( strstream & os ) { return os.separator$; }
78void 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
84const char * sepGetTuple( strstream & os ) { return os.tupleSeparator$; }
85void 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
91void ends( strstream & os ) {
92        if ( getANL$( os ) ) nl( os );
93        else setPrt$( os, false );                                                      // turn off
94} // ends
95
96int 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
113int flush( strstream & ) {                                                              // match trait, not used
114        return 0;
115} // flush
116
117strstream & write( strstream & os ) {
118        return write( os, stdout );
119} // write
120strstream & 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
129strstream & sstr;
130
131// Local Variables: //
132// tab-width: 4 //
133// End: //
Note: See TracBrowser for help on using the repository browser.