Ignore:
Timestamp:
Jun 4, 2019, 6:34:15 PM (5 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
9be45a2
Parents:
0161ddf
Message:

first draft of output manipulators and start input manipulators

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.hfa

    r0161ddf r3c573e9  
    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 : Tue Jun  4 17:33:43 2019
     13// Update Count     : 286
    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#define IntegralFMTDecl( T, CODE ) \
     174static inline { \
     175        _Ostream_Manip(T) bin( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'b', { .all : 0 } }; } \
     176        _Ostream_Manip(T) oct( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'o', { .all : 0 } }; } \
     177        _Ostream_Manip(T) hex( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'x', { .all : 0 } }; } \
     178        _Ostream_Manip(T) wd( unsigned char w, T v ) { return (_Ostream_Manip(T))@{ v, w, 0, CODE, { .all : 0 } }; } \
     179        _Ostream_Manip(T) wd( unsigned char w, unsigned char p, T v ) { return (_Ostream_Manip(T))@{ v, w, p, CODE, { .flags.pc : true } }; } \
     180        _Ostream_Manip(T) & wd( unsigned char w, _Ostream_Manip(T) & fmt ) with(fmt) { wd = w; return fmt; } \
     181        _Ostream_Manip(T) & wd( unsigned char w, unsigned char p, _Ostream_Manip(T) & fmt ) with(fmt) { wd = w; pc = p; flags.pc = true; return fmt; } \
     182        _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
     183        _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; } \
     184        _Ostream_Manip(T) & nobase( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
     185        _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
     186        _Ostream_Manip(T) sign( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, CODE, { .flags.sign : true } }; } \
     187        _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
     188} \
     189forall( dtype ostype | ostream( ostype ) ) { \
     190        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
     191        void ?|?( ostype & os, _Ostream_Manip(T) f ); \
     192}
     193
     194IntegralFMTDecl( signed char, 'd' )
     195IntegralFMTDecl( unsigned char, 'u' )
     196IntegralFMTDecl( signed short int, 'd' )
     197IntegralFMTDecl( unsigned short int, 'u' )
     198IntegralFMTDecl( signed int, 'd' )
     199IntegralFMTDecl( unsigned int, 'u' )
     200IntegralFMTDecl( signed long int, 'd' )
     201IntegralFMTDecl( unsigned long int, 'u' )
     202IntegralFMTDecl( signed long long int, 'd' )
     203IntegralFMTDecl( unsigned long long int, 'u' )
     204
     205//*********************************** Floating Point ***********************************
     206
     207// Default suffix for values with no fraction is "."
     208#define FloatingPointFMTDecl( T ) \
     209static inline { \
     210        _Ostream_Manip(T) sci( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'e', { .all : 0 } }; } \
     211        _Ostream_Manip(T) hex( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'a', { .all : 0 } }; } \
     212        _Ostream_Manip(T) wd( unsigned char w, T v ) { return (_Ostream_Manip(T))@{ v, w, 0, 'f', { .all : 0 } }; } \
     213        _Ostream_Manip(T) wd( unsigned char w, unsigned char p, T v ) { return (_Ostream_Manip(T))@{ v, w, p, 'f', { .flags.pc : true } }; } \
     214        _Ostream_Manip(T) ws( unsigned char w, unsigned char p, T v ) { return (_Ostream_Manip(T))@{ v, w, p, 'g', { .flags.pc : true } }; } \
     215        _Ostream_Manip(T) & wd( unsigned char w, _Ostream_Manip(T) & fmt ) with(fmt) { wd = w; return fmt; } \
     216        _Ostream_Manip(T) & wd( unsigned char w, unsigned char p, _Ostream_Manip(T) & fmt ) with(fmt) { wd = w; pc = p; flags.pc = true; return fmt; } \
     217        _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
     218        _Ostream_Manip(T) upcase( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'G', { .all : 0 } }; } \
     219        _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { fmt.base -= 32; /* upper case */ return fmt; } \
     220        _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
     221        _Ostream_Manip(T) sign( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'g', { .flags.sign : true } }; } \
     222        _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
     223        _Ostream_Manip(T) nodp( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'g', { .flags.nobsdp : true } }; } \
     224        _Ostream_Manip(T) & nodp( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
     225} \
     226forall( dtype ostype | ostream( ostype ) ) { \
     227        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
     228        void ?|?( ostype & os, _Ostream_Manip(T) f ); \
     229}
     230
     231FloatingPointFMTDecl( double )
     232FloatingPointFMTDecl( long double )
     233
     234//*********************************** Character ***********************************
     235
     236static inline {
     237        _Ostream_Manip(char) bin( char v ) { return (_Ostream_Manip(char))@{ v, 1, 0, 'b', { .all : 0 } }; }
     238        _Ostream_Manip(char) oct( char v ) { return (_Ostream_Manip(char))@{ v, 1, 0, 'o', { .all : 0 } }; }
     239        _Ostream_Manip(char) hex( char v ) { return (_Ostream_Manip(char))@{ v, 1, 0, 'x', { .all : 0 } }; }
     240        _Ostream_Manip(char) wd( unsigned char w, char v ) { return (_Ostream_Manip(char))@{ v, w, 0, 'c', { .all : 0 } }; }
     241        _Ostream_Manip(char) & wd( unsigned char w, _Ostream_Manip(char) & fmt ) with(fmt) { wd = w; return fmt; }
     242        _Ostream_Manip(char) & left( _Ostream_Manip(char) & fmt ) { fmt.flags.left = true; return fmt; }
     243        _Ostream_Manip(char) & upcase( _Ostream_Manip(char) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; }
     244        _Ostream_Manip(char) & nobase( _Ostream_Manip(char) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
     245} // distribution
     246forall( dtype ostype | ostream( ostype ) ) {
     247        ostype & ?|?( ostype & os, _Ostream_Manip(char) f );
     248        void ?|?( ostype & os, _Ostream_Manip(char) f );
     249}
     250
     251//*********************************** C String ***********************************
     252
     253static inline {
     254        _Ostream_Manip(const char *) bin( const char * v ) { return (_Ostream_Manip(const char *))@{ v, 1, 0, 'b', { .all : 0 } }; }
     255        _Ostream_Manip(const char *) oct( const char * v ) { return (_Ostream_Manip(const char *))@{ v, 1, 0, 'o', { .all : 0 } }; }
     256        _Ostream_Manip(const char *) hex( const char * v ) { return (_Ostream_Manip(const char *))@{ v, 1, 0, 'x', { .all : 0 } }; }
     257        _Ostream_Manip(const char *) wd( unsigned char w, const char * v ) { return (_Ostream_Manip(const char *))@{ v, w, 0, 's', { .all : 0 } }; }
     258        _Ostream_Manip(const char *) wd( unsigned char w, unsigned char p, const char * v ) { return (_Ostream_Manip(const char *))@{ v, w, p, 's', { .flags.pc : true } }; }
     259        _Ostream_Manip(const char *) & wd( unsigned char w, _Ostream_Manip(const char *) & fmt ) with(fmt) { wd = w; return fmt; }
     260        _Ostream_Manip(const char *) & wd( unsigned char w, unsigned char p, _Ostream_Manip(const char *) & fmt ) with(fmt) { wd = w; pc = p; flags.pc = true; return fmt; }
     261        _Ostream_Manip(const char *) & left( _Ostream_Manip(const char *) & fmt ) { fmt.flags.left = true; return fmt; }
     262        _Ostream_Manip(const char *) & nobase( _Ostream_Manip(const char *) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
     263} // distribution
     264forall( dtype ostype | ostream( ostype ) ) {
     265        ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f );
     266        void ?|?( ostype & os, _Ostream_Manip(const char *) f );
     267}
     268
     269
     270//*********************************** Istream ***********************************
     271
    149272
    150273trait istream( dtype istype ) {
     
    204327forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, _Istream_cstrC );
    205328
     329#if 0
     330struct _Istream_Cstr {
     331        char * s, * scanset;
     332        int wd;                                                                                         // width
     333        bool ignore;                                                                            // no input argument
     334};
     335static inline _Istream_Cstr skip( char * s ) { return (_Istream_Cstr){ 0p, s, -1, false }; }
     336static inline _Istream_Cstr incl( const char * scanset, char * s ) { return (_Istream_Cstr){ s, scanset, -1, false }; }
     337static inline _Istream_Cstr excl( const char * scanset, char * s ) { return (_Istream_Cstr){ s, scanset, -1, false }; }
     338static inline _Istream_Cstr cstr( char * s ) { return (_Istream_Cstr){ s, 0p, -1, false }; }
     339static inline _Istream_Cstr ignore( char * s ) { return (_Istream_Cstr)@{ s, 0p, -1, true }; }
     340static inline _Istream_Cstr ignore( _Istream_Cstr & fmt ) { fmt.ignore = true; return fmt; }
     341static inline _Istream_Cstr wd( unsigned int w, char * s ) { return (_Istream_Cstr)@{ s, 0p, w, false }; }
     342forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, _Istream_Cstr );
     343
     344//*********************************** Manipulators ***********************************
     345
     346forall( otype T )
     347struct _Istream_Manip {
     348        T & val;                                                                                        // polymorphic base-type
     349        int wd;                                                                                         // width
     350        bool ignore;                                                                            // no input argument
     351}; // _Istream_Manip
     352
     353#define InputFMTDecl( T ) \
     354static inline _Istream_Manip(T) ignore( T & v ) { return (_Istream_Manip(T))@{ v, -1, true }; } \
     355static inline _Istream_Manip(T) ignore( _Istream_Manip(T) & fmt ) { fmt.ignore = true; return fmt; } \
     356static inline _Istream_Manip(T) wd( unsigned int w, T & v ) { return (_Istream_Manip(T))@{ v, w, false }; } \
     357forall( dtype istype | istream( istype ) ) { \
     358        istype & ?|?( istype & is, _Istream_Manip(T) f ); \
     359}
     360
     361InputFMTDecl( char )
     362InputFMTDecl( signed char )
     363InputFMTDecl( unsigned char )
     364InputFMTDecl( signed short int )
     365InputFMTDecl( unsigned short int )
     366InputFMTDecl( signed int )
     367InputFMTDecl( unsigned int )
     368InputFMTDecl( signed long int )
     369InputFMTDecl( unsigned long int )
     370InputFMTDecl( signed long long int )
     371InputFMTDecl( unsigned long long int )
     372
     373InputFMTDecl( float )
     374InputFMTDecl( double )
     375InputFMTDecl( long double )
     376
     377InputFMTDecl( float _Complex )
     378InputFMTDecl( double _Complex )
     379InputFMTDecl( long double _Complex )
     380#endif // 0
     381
     382
     383//*********************************** Time ***********************************
     384
    206385
    207386#include <time_t.hfa>                                                                   // Duration (constructors) / Time (constructors)
Note: See TracChangeset for help on using the changeset viewer.