// // Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // strstream.cfa -- // // Author : Peter A. Buhr // Created On : Thu Apr 22 22:24:35 2021 // Last Modified By : Peter A. Buhr // Last Modified On : Sat Apr 24 11:15:47 2021 // Update Count : 73 // #include "strstream.hfa" #include // vsnprintf #include // varargs #include // strncpy, strerror #include #include // errno #include // sbrk, sysconf // *********************************** strstream *********************************** #define IO_MSG "I/O error: " // private bool sepPrt$( strstream & os ) { setNL$( os, false ); return os.sepOnOff$; } void sepReset$( strstream & os ) { os.sepOnOff$ = os.sepDefault$; } void sepReset$( strstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; } const char * sepGetCur$( strstream & os ) { return os.sepCur$; } void sepSetCur$( strstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; } bool getNL$( strstream & os ) { return os.sawNL$; } void setNL$( strstream & os, bool state ) { os.sawNL$ = state; } bool getANL$( strstream & os ) { return os.nlOnOff$; } bool getPrt$( strstream & os ) { return os.prt$; } void setPrt$( strstream & os, bool state ) { os.prt$ = state; } // public void ?{}( strstream & os, char buf[], size_t size ) { os.buf$ = buf; os.size$ = size; os.cursor$ = 0; os.sepDefault$ = true; os.sepOnOff$ = false; os.nlOnOff$ = true; os.prt$ = false; os.sawNL$ = false; sepSetCur$( os, sepGet( os ) ); sepSet( os, " " ); sepSetTuple( os, ", " ); } // ?{} void sepOn( strstream & os ) { os.sepOnOff$ = ! getNL$( os ); } void sepOff( strstream & os ) { os.sepOnOff$ = false; } bool sepDisable( strstream & os ) { bool temp = os.sepDefault$; os.sepDefault$ = false; sepReset$( os ); return temp; } // sepDisable bool sepEnable( strstream & os ) { bool temp = os.sepDefault$; os.sepDefault$ = true; if ( os.sepOnOff$ ) sepReset$( os ); // start of line ? return temp; } // sepEnable void nlOn( strstream & os ) { os.nlOnOff$ = true; } void nlOff( strstream & os ) { os.nlOnOff$ = false; } const char * sepGet( strstream & os ) { return os.separator$; } void sepSet( strstream & os, const char s[] ) { assert( s ); strncpy( os.separator$, s, strstream_sepSize - 1 ); os.separator$[strstream_sepSize - 1] = '\0'; } // sepSet const char * sepGetTuple( strstream & os ) { return os.tupleSeparator$; } void sepSetTuple( strstream & os, const char s[] ) { assert( s ); strncpy( os.tupleSeparator$, s, strstream_sepSize - 1 ); os.tupleSeparator$[strstream_sepSize - 1] = '\0'; } // sepSet void ends( strstream & os ) { if ( getANL$( os ) ) nl( os ); else setPrt$( os, false ); // turn off } // ends int fmt( strstream & os, const char format[], ... ) { va_list args; va_start( args, format ); int len = vsnprintf( os.buf$ + os.cursor$, os.size$ - os.cursor$, format, args ); os.cursor$ += len; if ( os.cursor$ >= os.size$ ) { // cursor exceeded buffer size? #define fmtmsg IO_MSG "strstream truncated write, buffer too small.\n" write( STDERR_FILENO, fmtmsg, sizeof(fmtmsg) - 1 ); abort(); } // if va_end( args ); setPrt$( os, true ); // called in output cascade sepReset$( os ); // reset separator return len; } // fmt int flush( strstream & ) { // match trait, not used return 0; } // flush strstream & write( strstream & os ) { return write( os, stdout ); } // write strstream & write( strstream & os, FILE * stream ) { if ( fwrite( os.buf$, 1, os.cursor$, stream ) != os.cursor$ ) { #define writemsg IO_MSG "strstream write error.\n" write( STDERR_FILENO, writemsg, sizeof(writemsg) - 1 ); abort(); } // if return os; } // write strstream & sstr; // Local Variables: // // tab-width: 4 // // End: //