Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.hfa

    r3c5dee4 r61c7239  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // iostream --
     7// iostream.hfa --
    88//
    99// Author           : Peter A. Buhr
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat May 11 10:31:27 2019
    13 // Update Count     : 232
     12// Last Modified On : Sat Jun  8 17:28:44 2019
     13// Update Count     : 312
    1414//
    1515
     
    1717
    1818#include "iterator.hfa"
     19
     20
     21//*********************************** Ostream ***********************************
     22
    1923
    2024trait ostream( dtype ostype ) {
     
    146150} // distribution
    147151
    148 //---------------------------------------
     152//*********************************** Manipulators ***********************************
     153
     154forall( otype T )
     155struct _Ostream_Manip {
     156        T val;                                                                                          // polymorphic base-type
     157        unsigned char wd, pc;                                                           // width, precision
     158        char base;                                                                                      // numeric base / floating-point style
     159        union {
     160                unsigned char all;
     161                struct {
     162                        unsigned char pc:1;                                                     // precision specified
     163                        unsigned char left:1;                                           // left justify
     164                        unsigned char nobsdp:1;                                         // base prefix / decimal point
     165                        unsigned char sign:1;                                           // plus / minus sign
     166                        unsigned char pad0:1;                                           // zero pad
     167                } flags;
     168        };
     169}; // _Ostream_Manip
     170
     171//*********************************** Integral ***********************************
     172
     173// See 6.7.9. 19) The initialization shall occur in initializer list order, each initializer provided for a particular
     174// subobject overriding any previously listed initializer for the same subobject; ***all subobjects that are not
     175// initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.***
     176
     177#define IntegralFMTDecl( T, CODE ) \
     178static inline { \
     179        _Ostream_Manip(T) bin( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'b', { .all : 0 } }; } \
     180        _Ostream_Manip(T) oct( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'o', { .all : 0 } }; } \
     181        _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'x', { .all : 0 } }; } \
     182        _Ostream_Manip(T) wd( unsigned char w, T val ) { return (_Ostream_Manip(T))@{ val, w, 0, CODE, { .all : 0 } }; } \
     183        _Ostream_Manip(T) wd( unsigned char w, unsigned char pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, CODE, { .flags.pc : true } }; } \
     184        _Ostream_Manip(T) & wd( unsigned char w, _Ostream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \
     185        _Ostream_Manip(T) & wd( unsigned char w, unsigned char pc, _Ostream_Manip(T) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \
     186        _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
     187        _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; } \
     188        _Ostream_Manip(T) & nobase( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
     189        _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
     190        _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, CODE, { .flags.sign : true } }; } \
     191        _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
     192} \
     193forall( dtype ostype | ostream( ostype ) ) { \
     194        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
     195        void ?|?( ostype & os, _Ostream_Manip(T) f ); \
     196} // ?|?
     197
     198IntegralFMTDecl( signed char, 'd' )
     199IntegralFMTDecl( unsigned char, 'u' )
     200IntegralFMTDecl( signed short int, 'd' )
     201IntegralFMTDecl( unsigned short int, 'u' )
     202IntegralFMTDecl( signed int, 'd' )
     203IntegralFMTDecl( unsigned int, 'u' )
     204IntegralFMTDecl( signed long int, 'd' )
     205IntegralFMTDecl( unsigned long int, 'u' )
     206IntegralFMTDecl( signed long long int, 'd' )
     207IntegralFMTDecl( unsigned long long int, 'u' )
     208
     209//*********************************** Floating Point ***********************************
     210
     211// Default suffix for values with no fraction is "."
     212#define FloatingPointFMTDecl( T ) \
     213static inline { \
     214        _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'a', { .all : 0 } }; } \
     215        _Ostream_Manip(T) sci( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'e', { .all : 0 } }; } \
     216        _Ostream_Manip(T) wd( unsigned char w, T val ) { return (_Ostream_Manip(T))@{ val, w, 0, 'f', { .all : 0 } }; } \
     217        _Ostream_Manip(T) wd( unsigned char w, unsigned char pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, 'f', { .flags.pc : true } }; } \
     218        _Ostream_Manip(T) ws( unsigned char w, unsigned char pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, 'g', { .flags.pc : true } }; } \
     219        _Ostream_Manip(T) & wd( unsigned char w, _Ostream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \
     220        _Ostream_Manip(T) & wd( unsigned char w, unsigned char pc, _Ostream_Manip(T) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \
     221        _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
     222        _Ostream_Manip(T) upcase( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'G', { .all : 0 } }; } \
     223        _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { fmt.base -= 32; /* upper case */ return fmt; } \
     224        _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
     225        _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.sign : true } }; } \
     226        _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
     227        _Ostream_Manip(T) nodp( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.nobsdp : true } }; } \
     228        _Ostream_Manip(T) & nodp( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
     229} \
     230forall( dtype ostype | ostream( ostype ) ) { \
     231        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
     232        void ?|?( ostype & os, _Ostream_Manip(T) f ); \
     233} // ?|?
     234
     235FloatingPointFMTDecl( double )
     236FloatingPointFMTDecl( long double )
     237
     238//*********************************** Character ***********************************
     239
     240static inline {
     241        _Ostream_Manip(char) bin( char val ) { return (_Ostream_Manip(char))@{ val, 1, 0, 'b', { .all : 0 } }; }
     242        _Ostream_Manip(char) oct( char val ) { return (_Ostream_Manip(char))@{ val, 1, 0, 'o', { .all : 0 } }; }
     243        _Ostream_Manip(char) hex( char val ) { return (_Ostream_Manip(char))@{ val, 1, 0, 'x', { .all : 0 } }; }
     244        _Ostream_Manip(char) wd( unsigned char w, char val ) { return (_Ostream_Manip(char))@{ val, w, 0, 'c', { .all : 0 } }; }
     245        _Ostream_Manip(char) & wd( unsigned char w, _Ostream_Manip(char) & fmt ) { fmt.wd = w; return fmt; }
     246        _Ostream_Manip(char) & left( _Ostream_Manip(char) & fmt ) { fmt.flags.left = true; return fmt; }
     247        _Ostream_Manip(char) & upcase( _Ostream_Manip(char) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; }
     248        _Ostream_Manip(char) & nobase( _Ostream_Manip(char) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
     249} // distribution
     250forall( dtype ostype | ostream( ostype ) ) {
     251        ostype & ?|?( ostype & os, _Ostream_Manip(char) f );
     252        void ?|?( ostype & os, _Ostream_Manip(char) f );
     253} // ?|?
     254
     255//*********************************** C String ***********************************
     256
     257static inline {
     258        _Ostream_Manip(const char *) bin( const char * val ) { return (_Ostream_Manip(const char *))@{ val, 1, 0, 'b', { .all : 0 } }; }
     259        _Ostream_Manip(const char *) oct( const char * val ) { return (_Ostream_Manip(const char *))@{ val, 1, 0, 'o', { .all : 0 } }; }
     260        _Ostream_Manip(const char *) hex( const char * val ) { return (_Ostream_Manip(const char *))@{ val, 1, 0, 'x', { .all : 0 } }; }
     261        _Ostream_Manip(const char *) wd( unsigned char w, const char * val ) { return (_Ostream_Manip(const char *))@{ val, w, 0, 's', { .all : 0 } }; }
     262        _Ostream_Manip(const char *) wd( unsigned char w, unsigned char pc, const char * val ) { return (_Ostream_Manip(const char *))@{ val, w, pc, 's', { .flags.pc : true } }; }
     263        _Ostream_Manip(const char *) & wd( unsigned char w, _Ostream_Manip(const char *) & fmt ) { fmt.wd = w; return fmt; }
     264        _Ostream_Manip(const char *) & wd( unsigned char w, unsigned char pc, _Ostream_Manip(const char *) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; }
     265        _Ostream_Manip(const char *) & left( _Ostream_Manip(const char *) & fmt ) { fmt.flags.left = true; return fmt; }
     266        _Ostream_Manip(const char *) & nobase( _Ostream_Manip(const char *) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
     267} // distribution
     268forall( dtype ostype | ostream( ostype ) ) {
     269        ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f );
     270        void ?|?( ostype & os, _Ostream_Manip(const char *) f );
     271} // ?|?
     272
     273
     274//*********************************** Istream ***********************************
     275
    149276
    150277trait istream( dtype istype ) {
     
    189316        istype & ?|?( istype &, long double _Complex & );
    190317
     318        // Cannot have char & and char * => cstr manipulator
     319        // istype & ?|?( istype &, char * );
     320
    191321        // manipulators
    192322        istype & ?|?( istype &, istype & (*)( istype & ) );
     
    196326} // distribution
    197327
    198 struct _Istream_cstrUC { char * s; };
    199 _Istream_cstrUC cstr( char * );
    200 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, _Istream_cstrUC );
    201 
    202 struct _Istream_cstrC { char * s; int size; };
    203 _Istream_cstrC cstr( char *, int size );
    204 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, _Istream_cstrC );
     328//*********************************** Manipulators ***********************************
     329
     330struct _Istream_Cstr {
     331        char * s;
     332        const char * scanset;
     333        int wd;                                                                                         // width
     334        union {
     335                unsigned char all;
     336                struct {
     337                        unsigned char ignore:1;                                         // do not change input argument
     338                        unsigned char inex:1;                                           // include/exclude characters in scanset
     339                } flags;
     340        };
     341}; // _Istream_Cstr
     342
     343static inline _Istream_Cstr skip( const char * scanset ) { return (_Istream_Cstr){ 0p, scanset, -1, { .all : 0 } }; }
     344static inline _Istream_Cstr incl( const char * scanset, char * s ) { return (_Istream_Cstr){ s, scanset, -1, { .flags.inex : false } }; }
     345static inline _Istream_Cstr incl( const char * scanset, _Istream_Cstr & fmt ) { fmt.flags.inex = false; return fmt; }
     346static inline _Istream_Cstr excl( const char * scanset, char * s ) { return (_Istream_Cstr){ s, scanset, -1, { .flags.inex : true } }; }
     347static inline _Istream_Cstr excl( const char * scanset, _Istream_Cstr & fmt ) { fmt.flags.inex = true; return fmt; }
     348static inline _Istream_Cstr cstr( char * s ) { return (_Istream_Cstr){ s, 0p, -1, { .all : 0 } }; }
     349static inline _Istream_Cstr ignore( const char * s ) { return (_Istream_Cstr)@{ s, 0p, -1, { .flags.ignore : true } }; }
     350static inline _Istream_Cstr ignore( _Istream_Cstr & fmt ) { fmt.flags.ignore = true; return fmt; }
     351static inline _Istream_Cstr wd( unsigned int w, char * s ) { return (_Istream_Cstr)@{ s, 0p, w, { .all : 0 } }; }
     352static inline _Istream_Cstr wd( unsigned int w, _Istream_Cstr & fmt ) { fmt.wd = w; return fmt; }
     353forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, _Istream_Cstr );
     354
     355forall( otype T )
     356struct _Istream_Manip {
     357        T & val;                                                                                        // polymorphic base-type
     358        int wd;                                                                                         // width
     359        bool ignore;                                                                            // do not change input argument
     360}; // _Istream_Manip
     361
     362#define InputFMTDecl( T ) \
     363static inline _Istream_Manip(T) ignore( const T & val ) { return (_Istream_Manip(T))@{ (T &)val, -1, true }; } \
     364static inline _Istream_Manip(T) ignore( _Istream_Manip(T) & fmt ) { fmt.ignore = true; return fmt; } \
     365static inline _Istream_Manip(T) wd( unsigned int w, T & val ) { return (_Istream_Manip(T))@{ val, w, false }; } \
     366forall( dtype istype | istream( istype ) ) { \
     367        istype & ?|?( istype & is, _Istream_Manip(T) f ); \
     368} // ?|?
     369
     370InputFMTDecl( char )
     371InputFMTDecl( signed char )
     372InputFMTDecl( unsigned char )
     373InputFMTDecl( signed short int )
     374InputFMTDecl( unsigned short int )
     375InputFMTDecl( signed int )
     376InputFMTDecl( unsigned int )
     377InputFMTDecl( signed long int )
     378InputFMTDecl( unsigned long int )
     379InputFMTDecl( signed long long int )
     380InputFMTDecl( unsigned long long int )
     381
     382InputFMTDecl( float )
     383InputFMTDecl( double )
     384InputFMTDecl( long double )
     385
     386InputFMTDecl( float _Complex )
     387InputFMTDecl( double _Complex )
     388InputFMTDecl( long double _Complex )
     389
     390
     391//*********************************** Time ***********************************
    205392
    206393
Note: See TracChangeset for help on using the changeset viewer.