source: libcfa/src/iostream.hfa@ bb1eabc

Last change on this file since bb1eabc was bb1eabc, checked in by Peter A. Buhr <pabuhr@…>, 10 days ago

harmonize output quote manipulator with input quote manipulator, adding left/right quote parameters

  • Property mode set to 100644
File size: 25.4 KB
RevLine 
[86bd7c1f]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 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//
[3c573e9]7// iostream.hfa --
[86bd7c1f]8//
[90c3b1c]9// Author : Peter A. Buhr
[86bd7c1f]10// Created On : Wed May 27 17:56:53 2015
11// Last Modified By : Peter A. Buhr
[bb1eabc]12// Last Modified On : Sun May 3 21:55:06 2026
13// Update Count : 829
[86bd7c1f]14//
15
[53a6c2a]16#pragma once
[51b73452]17
[58b6d1b]18#include "iterator.hfa"
[2f34fde]19#include "Exception.hfa"
[e56cfdb0]20
[8d321f9]21// *********************************** ostream ***********************************
[3c573e9]22
[8a97248]23forall( ostype & )
[ae0c1c3]24struct basic_ostream_data {
[9ebd778]25 // private
[ae0c1c3]26 bool (*sepPrt$)( ostype & ); // get separator state (on/off)
27 void (*sepReset$)( ostype & ); // set separator state to default state
28 void (*sepReset$)( ostype &, bool ); // set separator and default state
[a2940184]29 const char * (*sepGetCur$)( ostype & ); // get current separator string
[ae0c1c3]30 void (*sepSetCur$)( ostype &, const char [] ); // set current separator string
[a2940184]31 bool (*getNL$)( ostype & ); // get newline
[ae0c1c3]32 bool (*setNL$)( ostype &, bool ); // set newline
33 bool (*getANL$)( ostype & ); // get auto newline (on/off)
34 bool (*setANL$)( ostype &, bool ); // set auto newline (on/off), and return previous state
35 bool (*getPrt$)( ostype & ); // get fmt called in output cascade
36 bool (*setPrt$)( ostype &, bool ); // set fmt called in output cascade
[9ebd778]37 // public
[ae0c1c3]38 void (*nlOn)( ostype & ); // turn auto-newline state on
39 void (*nlOff)( ostype & ); // turn auto-newline state off
40
41 void (*sep)( ostype & ); // turn separator state on
42 void (*nosep)( ostype & ); // turn separator state off
43 bool (*sepOn)( ostype & ); // set default state to on, and return previous state
[a2940184]44 bool (*sepOff)( ostype & ); // set default state to off, and return previous state
45 const char * (*sepGet)( ostype & ); // get separator string
[ae0c1c3]46 void (*sepSet)( ostype &, const char [] ); // set separator to string (15 character maximum)
47 const char * (*sepGetTuple)( ostype & ); // get tuple separator string
[a2940184]48 void (*sepSetTuple)( ostype &, const char [] ); // set tuple separator to string (15 character maximum)
[ae0c1c3]49
50 void (*ends)( ostype & ); // end of output statement
51 int (*fmt)( ostype &, const char format[], ... ) __attribute__(( format(printf, 2, 3) ));
[85d8153]52}; // basic_ostream
[a0bd9a2]53
[ae0c1c3]54forall( ostype & )
55struct ostream_data {
56 inline basic_ostream_data( ostype );
57 bool (*fail)( ostype & ); // operation failed?
58 void (*clearerr)( ostype & );
59 int (*flush)( ostype & );
60 void (*open)( ostype &, const char name[], const char mode[] );
61 void (*close)( ostype & );
62 ostype & (*write)( ostype &, const char [], size_t );
63}; // ostream
64
65forall( ostype & )
66trait basic_ostream {
67 basic_ostream_data(ostype) const & basic_ostream_table;
68};
69
[8a97248]70forall( ostype & | basic_ostream( ostype ) )
71trait ostream {
[ae0c1c3]72 ostream_data(ostype) const & ostream_table;
73};
[90c3b1c]74
[8a97248]75// forall( T )
76// trait writeable {
[fd54fef]77// forall( ostype & | ostream( ostype ) ) ostype & ?|?( ostype &, T );
[e1780a2]78// }; // writeable
79
[8a97248]80forall( T, ostype & | ostream( ostype ) )
[dc9dd94]81trait writeable {
[09687aa]82 ostype & ?|?( ostype &, T );
[86f384b]83}; // writeable
[51b73452]84
[4e06c1e]85// implement writable for intrinsic types
[51b73452]86
[94d2544]87#define OSTYPE_VOID( T ) void ?|?( ostype &, T )
[b12e4ad]88#define OSTYPE_VOID_IMPL( os, T ) \
[94d2544]89 void ?|?( ostype & os, T t ) { \
[ae0c1c3]90 (ostype &)(os | t); \
91 basic_ostream_table.ends( os ); \
[94d2544]92 } // ?|?
93
[85d8153]94forall( ostype & | basic_ostream( ostype ) ) {
[93c2e0a]95 ostype & ?|?( ostype &, bool );
[94d2544]96 OSTYPE_VOID( bool );
[1e6e08de]97
[3ce0d440]98 ostype & ?|?( ostype &, char );
[94d2544]99 OSTYPE_VOID( char );
[3ce0d440]100 ostype & ?|?( ostype &, signed char );
[94d2544]101 OSTYPE_VOID( signed char );
[3ce0d440]102 ostype & ?|?( ostype &, unsigned char );
[94d2544]103 OSTYPE_VOID( unsigned char );
[09687aa]104
[3ce0d440]105 ostype & ?|?( ostype &, short int );
[94d2544]106 OSTYPE_VOID( short int );
[3ce0d440]107 ostype & ?|?( ostype &, unsigned short int );
[94d2544]108 OSTYPE_VOID( unsigned short int );
[3ce0d440]109 ostype & ?|?( ostype &, int );
[94d2544]110 OSTYPE_VOID( int );
[3ce0d440]111 ostype & ?|?( ostype &, unsigned int );
[94d2544]112 OSTYPE_VOID( unsigned int );
[3ce0d440]113 ostype & ?|?( ostype &, long int );
[94d2544]114 OSTYPE_VOID( long int );
[3ce0d440]115 ostype & ?|?( ostype &, long long int );
[94d2544]116 OSTYPE_VOID( long long int );
[3ce0d440]117 ostype & ?|?( ostype &, unsigned long int );
[94d2544]118 OSTYPE_VOID( unsigned long int );
[3ce0d440]119 ostype & ?|?( ostype &, unsigned long long int );
[94d2544]120 OSTYPE_VOID( unsigned long long int );
[ef3ac46]121 #if defined( __SIZEOF_INT128__ )
[bd5b443]122 ostype & ?|?( ostype &, int128 );
[94d2544]123 OSTYPE_VOID( int128 );
[bd5b443]124 ostype & ?|?( ostype &, unsigned int128 );
[94d2544]125 OSTYPE_VOID( unsigned int128 );
[ef3ac46]126 #endif // __SIZEOF_INT128__
[09687aa]127
[65240bb]128 ostype & ?|?( ostype &, float );
[94d2544]129 OSTYPE_VOID( float );
[3ce0d440]130 ostype & ?|?( ostype &, double );
[94d2544]131 OSTYPE_VOID( double );
[3ce0d440]132 ostype & ?|?( ostype &, long double );
[94d2544]133 OSTYPE_VOID( long double );
[09687aa]134
[3ce0d440]135 ostype & ?|?( ostype &, float _Complex );
[94d2544]136 OSTYPE_VOID( float _Complex );
[3ce0d440]137 ostype & ?|?( ostype &, double _Complex );
[94d2544]138 OSTYPE_VOID( double _Complex );
[3ce0d440]139 ostype & ?|?( ostype &, long double _Complex );
[94d2544]140 OSTYPE_VOID( long double _Complex );
[09687aa]141
[e3fea42]142 ostype & ?|?( ostype &, const char [] );
[94d2544]143 OSTYPE_VOID( const char [] );
[e7a8f65]144 // ostype & ?|?( ostype &, const char16_t [] );
[ef3ac46]145 #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
[e7a8f65]146 // ostype & ?|?( ostype &, const char32_t [] );
[ef3ac46]147 #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
[e7a8f65]148 // ostype & ?|?( ostype &, const wchar_t [] );
[3ce0d440]149 ostype & ?|?( ostype &, const void * );
[94d2544]150 OSTYPE_VOID( const void * );
[3ce0d440]151
[0a2e0e21]152 // FIX-ME: does not work so using macros
[c635047]153 // forall( T | { ostype & ?|?( ostype &, T ); } )
154 // void ?|?( ostype & os, T );
155
[3ce0d440]156 // manipulators
157 ostype & ?|?( ostype &, ostype & (*)( ostype & ) );
[94d2544]158 OSTYPE_VOID( ostype & (*)( ostype & ) );
[f5d9c37]159
[200fcb3]160 ostype & nl( ostype & );
161 ostype & nonl( ostype & );
[f5d9c37]162 ostype & nlOn( ostype & );
163 ostype & nlOff( ostype & );
164
165 ostype & sepVal( ostype & );
166 ostype & sepTupleVal( ostype & );
[3ce0d440]167 ostype & sep( ostype & );
[f5d9c37]168 ostype & nosep( ostype & );
[3ce0d440]169 ostype & sepOn( ostype & );
170 ostype & sepOff( ostype & );
[85d8153]171} // distribution
172
[c443d1d]173// tuples
[c0363be]174forall( ostype &, T, List ... | writeable( T, ostype ) | { ostype & ?|?( ostype &, List ); } ) {
175 ostype & ?|?( ostype & os, T arg, List rest );
176 void ?|?( ostype & os, T arg, List rest );
[200fcb3]177} // distribution
[c443d1d]178
[e56cfdb0]179// writes the range [begin, end) to the given stream
[fd54fef]180forall( ostype &, elt_type | writeable( elt_type, ostype ), iterator_type | iterator( iterator_type, elt_type ) ) {
[200fcb3]181 void write( iterator_type begin, iterator_type end, ostype & os );
182 void write_reverse( iterator_type begin, iterator_type end, ostype & os );
183} // distribution
[51b73452]184
[8d321f9]185// *********************************** manipulators ***********************************
[3c573e9]186
[a0bd9a2]187struct _Ostream_Flags {
[dc9dd94]188 unsigned char eng:1; // engineering notation
189 unsigned char neg:1; // val is negative
190 unsigned char pc:1; // precision specified
191 unsigned char left:1; // left justify
192 unsigned char nobsdp:1; // base prefix / decimal point
193 unsigned char sign:1; // plus / minus sign
194 unsigned char pad0:1; // zero pad
[b19b362]195 unsigned char quote:1; // print quotes
[a0bd9a2]196};
197
[96ef156]198// FIXME: Should be an anonymous inner union of _Ostream_Manip.
199// Hoisting manually to work around warning of #294.
200union _Ostream_Manip_Mode {
201 unsigned char all;
202 _Ostream_Flags flags;
203};
204
[fd54fef]205forall( T )
[3c573e9]206struct _Ostream_Manip {
207 T val; // polymorphic base-type
[424dfc4]208 int wd, pc; // width, precision: signed for computations
[3c573e9]209 char base; // numeric base / floating-point style
[96ef156]210 inline _Ostream_Manip_Mode;
[bb1eabc]211 char qleft, qright; // quote delimiters (PLACE HERE SO OPTIONAL IN INITIALIZATION)
[3c573e9]212}; // _Ostream_Manip
213
[8d321f9]214// *********************************** integral ***********************************
[3c573e9]215
[94d2544]216#define INTEGRAL_FMT_DECL( T, CODE ) \
[3c573e9]217static inline { \
[1a7203d]218 _Ostream_Manip(T) bin( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'b', { .all = 0 } }; } \
219 _Ostream_Manip(T) oct( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'o', { .all = 0 } }; } \
220 _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'x', { .all = 0 } }; } \
221 _Ostream_Manip(T) wd( unsigned int wd, T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = wd, .pc = 0, .base = CODE, { .all = 0 } }; } \
222 _Ostream_Manip(T) wd( unsigned int wd, unsigned int pc, T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = wd, .pc = pc, .base = CODE, { .flags.pc = true } }; } \
[8abe4090]223 _Ostream_Manip(T) & wd( unsigned int wd, _Ostream_Manip(T) & fmt ) { fmt.wd = wd; return fmt; } \
224 _Ostream_Manip(T) & wd( unsigned int wd, unsigned int pc, _Ostream_Manip(T) & fmt ) { fmt.wd = wd; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \
[3c573e9]225 _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
226 _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; } \
227 _Ostream_Manip(T) & nobase( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
[1a7203d]228 _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = CODE, { .flags.sign = true } }; } \
[3c573e9]229 _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
[b19b362]230 _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
[04396aa]231} /* distribution */ \
[85d8153]232forall( ostype & | basic_ostream( ostype ) ) { \
[3c573e9]233 ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
[c635047]234 OSTYPE_VOID( _Ostream_Manip(T) ); \
[61c7239]235} // ?|?
[3c573e9]236
[94d2544]237INTEGRAL_FMT_DECL( signed char, 'd' )
238INTEGRAL_FMT_DECL( unsigned char, 'u' )
239INTEGRAL_FMT_DECL( signed short int, 'd' )
240INTEGRAL_FMT_DECL( unsigned short int, 'u' )
241INTEGRAL_FMT_DECL( signed int, 'd' )
242INTEGRAL_FMT_DECL( unsigned int, 'u' )
243INTEGRAL_FMT_DECL( signed long int, 'd' )
244INTEGRAL_FMT_DECL( unsigned long int, 'u' )
245INTEGRAL_FMT_DECL( signed long long int, 'd' )
246INTEGRAL_FMT_DECL( unsigned long long int, 'u' )
[bd5b443]247#if defined( __SIZEOF_INT128__ )
[94d2544]248INTEGRAL_FMT_DECL( int128, 'd' )
249INTEGRAL_FMT_DECL( unsigned int128, 'u' )
[2b22b5c4]250#endif // __SIZEOF_INT128__
[3c573e9]251
[8d321f9]252// *********************************** floating point ***********************************
[3c573e9]253
254// Default suffix for values with no fraction is "."
[94d2544]255#define FLOATING_POINT_FMT_DECL( T ) \
[3c573e9]256static inline { \
[1a7203d]257 _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'a', { .all = 0 } }; } \
258 _Ostream_Manip(T) sci( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'e', { .all = 0 } }; } \
259 _Ostream_Manip(T) eng( T val ) { return (_Ostream_Manip(T))@{ .val = val, 1, -1, .base = 'g', { .flags.eng = true } }; } \
260 _Ostream_Manip(T) wd( unsigned int wd, T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = wd, .pc = 0, .base = 'g', { .all = 0 } }; } \
261 _Ostream_Manip(T) wd( unsigned int wd, unsigned int pc, T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = wd, .pc = pc, .base = 'f', { .flags.pc = true } }; } \
262 _Ostream_Manip(T) ws( unsigned int wd, unsigned int pc, T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = wd, .pc = pc, .base = 'g', { .flags.pc = true } }; } \
[8abe4090]263 _Ostream_Manip(T) & wd( unsigned int wd, _Ostream_Manip(T) & fmt ) { if ( fmt.flags.eng ) fmt.base = 'f'; fmt.wd = wd; return fmt; } \
264 _Ostream_Manip(T) & wd( unsigned int wd, unsigned int pc, _Ostream_Manip(T) & fmt ) { \
265 if ( fmt.flags.eng ) fmt.base = 'f'; fmt.wd = wd; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \
266 _Ostream_Manip(T) & ws( unsigned int wd, unsigned int pc, _Ostream_Manip(T) & fmt ) { fmt.wd = wd; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \
[3c573e9]267 _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
[1a7203d]268 _Ostream_Manip(T) upcase( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'G', { .all = 0 } }; } \
[3c573e9]269 _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { fmt.base -= 32; /* upper case */ return fmt; } \
270 _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
[1a7203d]271 _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'g', { .flags.sign = true } }; } \
[3c573e9]272 _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
[1a7203d]273 _Ostream_Manip(T) nodp( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'g', { .flags.nobsdp = true } }; } \
[3c573e9]274 _Ostream_Manip(T) & nodp( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
[1a7203d]275 _Ostream_Manip(T) unit( T val ) { return (_Ostream_Manip(T))@{ .val = val, .wd = 1, .pc = 0, .base = 'g', { .flags.nobsdp = true } }; } \
[fd4c009]276 _Ostream_Manip(T) & unit( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
[04396aa]277} /* distribution */ \
[85d8153]278forall( ostype & | basic_ostream( ostype ) ) { \
[3c573e9]279 ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
[c635047]280 OSTYPE_VOID( _Ostream_Manip(T) ); \
[61c7239]281} // ?|?
[3c573e9]282
[94d2544]283FLOATING_POINT_FMT_DECL( double )
284FLOATING_POINT_FMT_DECL( long double )
[3c573e9]285
[8d321f9]286// *********************************** character ***********************************
[3c573e9]287
288static inline {
[1a7203d]289 _Ostream_Manip(char) bin( char c ) { return (_Ostream_Manip(char))@{ .val = c, .wd = 1, .pc = 0, .base = 'b', { .all = 0 } }; }
290 _Ostream_Manip(char) oct( char c ) { return (_Ostream_Manip(char))@{ .val = c, .wd = 1, .pc = 0, .base = 'o', { .all = 0 } }; }
291 _Ostream_Manip(char) hex( char c ) { return (_Ostream_Manip(char))@{ .val = c, .wd = 1, .pc = 0, .base = 'x', { .all = 0 } }; }
[bb1eabc]292 _Ostream_Manip(char) quote( char c, const char qleft = '\'', const char qright = '\0' ) {
293 return (_Ostream_Manip(char))@{ .val = c, .wd = 1, .pc = 0, .base = 'c', { .flags.quote = true }, .qleft = qleft, .qright = qright }; }
[b19b362]294 _Ostream_Manip(char) wd( unsigned int wd, char c ) { return (_Ostream_Manip(char))@{ .val = c, .wd = wd, .pc = 0, .base = 'c', { .all = 0 } }; }
[8abe4090]295 _Ostream_Manip(char) & wd( unsigned int wd, _Ostream_Manip(char) & fmt ) { fmt.wd = wd; return fmt; }
[3c573e9]296 _Ostream_Manip(char) & left( _Ostream_Manip(char) & fmt ) { fmt.flags.left = true; return fmt; }
297 _Ostream_Manip(char) & upcase( _Ostream_Manip(char) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; }
298 _Ostream_Manip(char) & nobase( _Ostream_Manip(char) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
[bb1eabc]299 _Ostream_Manip(char) & quote( _Ostream_Manip(char) & fmt, const char qleft = '\'', const char qright = '\0' ) {
300 fmt.flags.quote = true; fmt.qleft = qleft, fmt.qright = qright; return fmt; }
[3c573e9]301} // distribution
[85d8153]302forall( ostype & | basic_ostream( ostype ) ) {
[3c573e9]303 ostype & ?|?( ostype & os, _Ostream_Manip(char) f );
[c635047]304 OSTYPE_VOID( _Ostream_Manip(char) ); \
[61c7239]305} // ?|?
[3c573e9]306
[8d321f9]307// *********************************** C string ***********************************
[3c573e9]308
309static inline {
[1a7203d]310 _Ostream_Manip(const char *) bin( const char s[] ) { return (_Ostream_Manip(const char *))@{ .val = s, .wd = 1, .pc = 0, .base = 'b', { .all = 0 } }; }
311 _Ostream_Manip(const char *) oct( const char s[] ) { return (_Ostream_Manip(const char *))@{ .val = s, .wd = 1, .pc = 0, .base = 'o', { .all = 0 } }; }
312 _Ostream_Manip(const char *) hex( const char s[] ) { return (_Ostream_Manip(const char *))@{ .val = s, .wd = 1, .pc = 0, .base = 'x', { .all = 0 } }; }
[bb1eabc]313 _Ostream_Manip(const char *) quote( const char s[], const char qleft = '"', const char qright = '\0' ) {
314 return (_Ostream_Manip(const char *))@{ .val = s, .wd = 1, .pc = 0, .base = 's', { .flags.quote = true }, .qleft = qleft, .qright = qright }; }
[b19b362]315 _Ostream_Manip(const char *) wd( unsigned int wd, const char s[] ) { return (_Ostream_Manip(const char *))@{ .val = s, .wd = wd, .pc = 0, .base = 's', { .all = 0 } }; }
316 _Ostream_Manip(const char *) wd( unsigned int wd, unsigned int pc, const char s[] ) { return (_Ostream_Manip(const char *))@{ .val = s, .wd = wd, .pc = pc, .base = 's', { .flags.pc = true } }; }
[8abe4090]317 _Ostream_Manip(const char *) & wd( unsigned int wd, _Ostream_Manip(const char *) & fmt ) { fmt.wd = wd; return fmt; }
318 _Ostream_Manip(const char *) & wd( unsigned int wd, unsigned int pc, _Ostream_Manip(const char *) & fmt ) { fmt.wd = wd; fmt.pc = pc; fmt.flags.pc = true; return fmt; }
[3c573e9]319 _Ostream_Manip(const char *) & left( _Ostream_Manip(const char *) & fmt ) { fmt.flags.left = true; return fmt; }
[b19b362]320 _Ostream_Manip(const char *) & upcase( _Ostream_Manip(const char *) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; }
[3c573e9]321 _Ostream_Manip(const char *) & nobase( _Ostream_Manip(const char *) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
[bb1eabc]322 _Ostream_Manip(const char *) & quote( _Ostream_Manip(const char *) & fmt, const char qleft = '"', const char qright = '\0' ) {
323 fmt.flags.quote = true; fmt.qleft = qleft; fmt.qright = qright; return fmt; }
[3c573e9]324} // distribution
[85d8153]325forall( ostype & | basic_ostream( ostype ) ) {
[3c573e9]326 ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f );
[c635047]327 OSTYPE_VOID( _Ostream_Manip(const char *) ); \
[61c7239]328} // ?|?
[3c573e9]329
[0528d79]330
[8d321f9]331// *********************************** istream ***********************************
[3c573e9]332
[0528d79]333
[8a97248]334forall( istype & )
[ae0c1c3]335struct basic_istream_data {
[c8371b5]336 // private
[ae0c1c3]337 bool (*getANL$)( istype & ); // get scan newline (on/off)
338 bool (*setANL$)( istype &, bool ); // set scan newline (on/off)
[c8371b5]339 // public
[ae0c1c3]340 void (*nlOn)( istype & ); // read newline
341 void (*nlOff)( istype & ); // scan newline
342 int (*fmt)( istype &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) ));
343 istype & (*ungetc)( char, istype & );
344 bool (*eof)( istype & );
345 void (*clearerr)( istype & );
[ef3ac46]346}; // basic_istream
347
[ae0c1c3]348forall( istype & )
349struct istream_data {
350 inline basic_istream_data( istype );
351 bool (*fail)( istype & );
352 void (*open)( istype & is, const char name[], const char mode[] );
353 void (*open)( istype & is, const char name[] );
354 void (*close)( istype & is );
355 istype & (*read)( istype &, char [], size_t );
356}; // istream
357
358forall( istype & )
359trait basic_istream {
360 basic_istream_data(istype) const & basic_istream_table;
361};
362
[8a97248]363forall( istype & | basic_istream( istype ) )
364trait istream {
[ae0c1c3]365 istream_data(istype) const & istream_table;
366};
[51b73452]367
[8a97248]368forall( T )
369trait readable {
[fd54fef]370 forall( istype & | istream( istype ) ) istype & ?|?( istype &, T );
[86f384b]371}; // readable
[51b73452]372
[ef3ac46]373forall( istype & | basic_istream( istype ) ) {
[93c2e0a]374 istype & ?|?( istype &, bool & );
[3ce0d440]375
376 istype & ?|?( istype &, char & );
377 istype & ?|?( istype &, signed char & );
378 istype & ?|?( istype &, unsigned char & );
379
380 istype & ?|?( istype &, short int & );
381 istype & ?|?( istype &, unsigned short int & );
382 istype & ?|?( istype &, int & );
383 istype & ?|?( istype &, unsigned int & );
384 istype & ?|?( istype &, long int & );
385 istype & ?|?( istype &, unsigned long int & );
[21baa40]386 istype & ?|?( istype &, long long int & );
[3ce0d440]387 istype & ?|?( istype &, unsigned long long int & );
[ef3ac46]388 #if defined( __SIZEOF_INT128__ )
[21baa40]389 istype & ?|?( istype &, int128 & );
390 istype & ?|?( istype &, unsigned int128 & );
[ef3ac46]391 #endif // __SIZEOF_INT128__
[3ce0d440]392
393 istype & ?|?( istype &, float & );
394 istype & ?|?( istype &, double & );
395 istype & ?|?( istype &, long double & );
396
397 istype & ?|?( istype &, float _Complex & );
398 istype & ?|?( istype &, double _Complex & );
399 istype & ?|?( istype &, long double _Complex & );
400
[c015e2d]401 // This is too restrictive as it prevents building a format in a string and using that format.
402 // inline istype & ?|?( istype &, char [] ) { // possible error, too restrictive to change
403 // _Static_assert( false, "reading a character array without a maximum length is unsafe. Use input manipulator \"wdi( N, s )\", where \"char s[N]\" or fmt( s )." );
404 // }
405 istype & ?|?( istype &, const char [] ); // match text
[61c7239]406
[3ce0d440]407 // manipulators
408 istype & ?|?( istype &, istype & (*)( istype & ) );
[200fcb3]409 istype & nl( istype & is );
[3c5dee4]410 istype & nlOn( istype & );
411 istype & nlOff( istype & );
[ef3ac46]412} // distribution
413
[2f34fde]414// *********************************** exceptions ***********************************
415
[3ac5fd8]416ExceptionDecl( end_of_file ); // read encounters end of file
[211def2]417ExceptionDecl( missing_data ); // read finds no appropriate data
418ExceptionDecl( cstring_length ); // character string size exceeded
419ExceptionDecl( data_range ); // value too large for numerical type
[2f34fde]420
[8d321f9]421// *********************************** manipulators ***********************************
[51b73452]422
[e0dc038]423// skip does not compose with other C string manipulators.
[0f107e4]424struct _Istream_Cskip {
425 const char * scanset;
426 unsigned wd; // scan width
427}; // _Istream_Cskip
428
429static inline {
[1a7203d]430 _Istream_Cskip skip( const char scanset[] ) { return (_Istream_Cskip)@{ .scanset = scanset, .wd = 0 }; }
431 _Istream_Cskip skip( unsigned int wd ) { return (_Istream_Cskip)@{ .scanset = 0p, .wd = wd }; }
[0f107e4]432} // distribution
433
[38de914]434struct _Istream_str_base {
[e7a8f65]435 union {
436 const char * scanset;
[bb1eabc]437 char delimiters[3]; // [0] => qleft or terminator, [1] => qright or getline '\0', [2] => char '\0' or char * '\1'
[e7a8f65]438 };
[3c573e9]439 int wd; // width
[61c7239]440 union {
441 unsigned char all;
442 struct {
443 unsigned char ignore:1; // do not change input argument
444 unsigned char inex:1; // include/exclude characters in scanset
[baa1d5d]445 unsigned char delimiter:1; // delimit character(s)
[2f34fde]446 unsigned char rwd:1; // read width
[61c7239]447 } flags;
448 };
[38de914]449}; // _Istream_str_base
450
[5764204]451struct _Istream_Cwidth {
[38de914]452 char * s;
453 inline _Istream_str_base;
[211def2]454}; // _Istream_Cwidth
[61c7239]455
[5764204]456// Restrict nesting of input manipulators to those combinations that make sense.
457
[30548de]458struct _Istream_Cquote {
[5764204]459 _Istream_Cwidth cstr;
[30548de]460}; // _Istream_Cquote
[8c13ca8]461
[211def2]462struct _Istream_Cstr {
463 _Istream_Cwidth cstr;
464}; // _Istream_Cstr
465
[04396aa]466static inline {
[5764204]467 // width must include room for null terminator, (gcc) scanf does not allow a 0 width => wd > 1 (1 char and null) and rd > 0 (1 char);
468 _Istream_Cwidth wdi( unsigned int wd, char s[] ) {
[211def2]469 if ( wd <= 1 ) throwResume ExceptionInst( cstring_length ); // minimum 1 character and null terminator
[1a7203d]470 return (_Istream_Cwidth)@{ .s = s, { {.scanset = 0p}, .wd = wd, {.all = 0} } };
[5764204]471 }
472 _Istream_Cwidth wdi( unsigned int wd, unsigned int rwd, char s[] ) {
[211def2]473 if ( wd <= 1 || wd <= rwd ) throwResume ExceptionInst( cstring_length ); // minimum 1 character, null terminator, plus subset
[1a7203d]474 return (_Istream_Cwidth)@{ .s = s, { {.scanset = 0p}, .wd = rwd, {.flags.rwd = true} } };
[5ad2c6c7]475 }
[211def2]476 _Istream_Cstr & getline( _Istream_Cwidth & f, const char delimiter = '\n' ) {
477 f.delimiters[0] = delimiter; f.delimiters[1] = '\0'; f.flags.delimiter = true; return (_Istream_Cstr &)f;
478 }
[30548de]479 _Istream_Cquote quote( char & ch, const char Ldelimiter = '\'', const char Rdelimiter = '\0' ) {
480 return (_Istream_Cquote)@{ { .s = &ch, { {.delimiters = { Ldelimiter, Rdelimiter, '\1' }}, .wd = 1, {.flags.rwd = true} } } };
[baa1d5d]481 }
[30548de]482 _Istream_Cquote & quote( _Istream_Cwidth & f, const char Ldelimiter = '"', const char Rdelimiter = '\0' ) {
[5764204]483 f.delimiters[0] = Ldelimiter; f.delimiters[1] = Rdelimiter; f.delimiters[2] = '\0';
[30548de]484 return (_Istream_Cquote &)f;
[8c13ca8]485 }
[5764204]486 _Istream_Cstr & incl( const char scanset[], _Istream_Cwidth & f ) { f.scanset = scanset; f.flags.inex = false; return (_Istream_Cstr &)f; }
487 _Istream_Cstr & excl( const char scanset[], _Istream_Cwidth & f ) { f.scanset = scanset; f.flags.inex = true; return (_Istream_Cstr &)f; }
[1a7203d]488 _Istream_Cstr ignore( const char s[] ) { return (_Istream_Cstr)@{ { .s = (char *)s, { {.scanset = 0p}, .wd = -1, {.flags.ignore = true} } } }; }
[5764204]489 _Istream_Cstr & ignore( _Istream_Cwidth & f ) { f.flags.ignore = true; return (_Istream_Cstr &)f; }
[30548de]490 _Istream_Cquote & ignore( _Istream_Cquote & f ) { f.cstr.flags.ignore = true; return (_Istream_Cquote &)f; }
[5764204]491 _Istream_Cstr & ignore( _Istream_Cstr & f ) { f.cstr.flags.ignore = true; return (_Istream_Cstr &)f; }
[04396aa]492} // distribution
[8c13ca8]493
[ef3ac46]494forall( istype & | basic_istream( istype ) ) {
[8c13ca8]495 istype & ?|?( istype & is, _Istream_Cskip f );
[30548de]496 istype & ?|?( istype & is, _Istream_Cquote f );
[5764204]497 istype & ?|?( istype & is, _Istream_Cstr f );
[211def2]498 static inline istype & ?|?( istype & is, _Istream_Cwidth f ) { return is | *(_Istream_Cstr *)&f; }
[8c13ca8]499} // distribution
[86a8be5]500
[96ef156]501// FIXME: `| sized(T)` seems to be working around a bug, but it is logically unnecessary.
502// Including sized(T) causes a warning that is telling us to get rid of it.
503#pragma GCC diagnostic push
504#pragma GCC diagnostic ignored "-Wunused-parameter"
505forall( T & | sized(T) )
[3c573e9]506struct _Istream_Manip {
507 T & val; // polymorphic base-type
508 int wd; // width
[61c7239]509 bool ignore; // do not change input argument
[3c573e9]510}; // _Istream_Manip
[96ef156]511#pragma GCC diagnostic pop
[3c573e9]512
[94d2544]513#define INPUT_FMT_DECL( T ) \
[04396aa]514static inline { \
[1a7203d]515 _Istream_Manip(T) wdi( unsigned int wd, T & val ) { return (_Istream_Manip(T))@{ .val = val, .wd = wd, .ignore = false }; } \
516 _Istream_Manip(T) ignore( const T & val ) { return (_Istream_Manip(T))@{ .val = (T &)val, .wd = -1, .ignore = true }; } \
[04396aa]517 _Istream_Manip(T) & ignore( _Istream_Manip(T) & fmt ) { fmt.ignore = true; return fmt; } \
518} /* distribution */ \
[ef3ac46]519forall( istype & | basic_istream( istype ) ) { \
[3c573e9]520 istype & ?|?( istype & is, _Istream_Manip(T) f ); \
[61c7239]521} // ?|?
[3c573e9]522
[baa1d5d]523INPUT_FMT_DECL( char )
[94d2544]524INPUT_FMT_DECL( signed char )
525INPUT_FMT_DECL( unsigned char )
526INPUT_FMT_DECL( signed short int )
527INPUT_FMT_DECL( unsigned short int )
528INPUT_FMT_DECL( signed int )
529INPUT_FMT_DECL( unsigned int )
530INPUT_FMT_DECL( signed long int )
531INPUT_FMT_DECL( unsigned long int )
532INPUT_FMT_DECL( signed long long int )
533INPUT_FMT_DECL( unsigned long long int )
534
535INPUT_FMT_DECL( float )
536INPUT_FMT_DECL( double )
537INPUT_FMT_DECL( long double )
538
539INPUT_FMT_DECL( float _Complex )
540INPUT_FMT_DECL( double _Complex )
541INPUT_FMT_DECL( long double _Complex )
[3c573e9]542
[0528d79]543// *********************************** enumerations ***********************************
544
545forall( istype & | istream( istype ), E | CfaEnum( E ) | Serial(E) )
546istype & ?|?( istype &, E & );
547
548forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) {
549 ostype & ?|?( ostype &, E );
550 OSTYPE_VOID( E );
551}
552
[8d321f9]553// *********************************** time ***********************************
[3c573e9]554
[200fcb3]555#include <time_t.hfa> // Duration (constructors) / Time (constructors)
[10a97adb]556
[fd54fef]557forall( ostype & | ostream( ostype ) ) {
[200fcb3]558 ostype & ?|?( ostype & os, Duration dur );
[c635047]559 OSTYPE_VOID( Duration );
[200fcb3]560 ostype & ?|?( ostype & os, Time time );
[c635047]561 OSTYPE_VOID( Time );
[200fcb3]562} // distribution
[10a97adb]563
[86bd7c1f]564// Local Variables: //
565// tab-width: 4 //
566// End: //
Note: See TracBrowser for help on using the repository browser.