Home | History | Annotate | Download | only in ustl-1.0
      1 // This file is part of the ustl library, an STL implementation.
      2 //
      3 // Copyright (C) 2005 by Mike Sharov <msharov (at) users.sourceforge.net>
      4 // This file is free software, distributed under the MIT License.
      5 //
      6 // uios.h
      7 //
      8 // Types used by the streams for option setting.
      9 //
     10 
     11 #ifndef UIOS_H_630C16E316F7650E3A02E1C6611B789A
     12 #define UIOS_H_630C16E316F7650E3A02E1C6611B789A
     13 
     14 #include "utypes.h"
     15 
     16 namespace ustl {
     17 
     18 class file_exception;
     19 
     20 const char endl = '\n';		///< End of line character.
     21 const char ends = '\0';		///< End of string character.
     22 
     23 /// Defines types and constants used by all stream classes.
     24 class ios_base {
     25 public:
     26     /// Used to set parameters for stringstreams
     27     enum fmtflags {
     28 	boolalpha	= (1 << 0),	///< Boolean values printed as text.
     29 	dec		= (1 << 1),	///< Decimal number output.
     30 	fixed		= (1 << 2),	///< Fixed-point float output.
     31 	hex		= (1 << 3),	///< Hexadecimal number output.
     32 	internal	= (1 << 4),
     33 	left		= (1 << 5),	///< Left alignment.
     34 	oct		= (1 << 6),	///< Octal number output.
     35 	right		= (1 << 7),	///< Right alignment.
     36 	scientific	= (1 << 8),	///< Scientific float format.
     37 	showbase	= (1 << 9),	///< Add 0x or 0 prefixes on hex and octal numbers.
     38 	showpoint	= (1 << 10),	///< Print decimal point.
     39 	showpos		= (1 << 11),
     40 	skipws		= (1 << 12),	///< Skip whitespace when reading.
     41 	unitbuf		= (1 << 13),
     42 	uppercase	= (1 << 14),
     43 	adjustfield	= (1 << 15),
     44 	basefield	= (1 << 16),
     45 	floatfield	= (1 << 17)
     46     };
     47     /// For file-based streams, specifies fd mode.
     48     enum openmode_bits {
     49 	in	= (1 << 0),
     50 	out	= (1 << 1),
     51 	app	= (1 << 2),
     52 	ate	= (1 << 3),
     53 	binary	= (1 << 4),
     54 	trunc	= (1 << 5),
     55 	#ifndef DOXYGEN_SHOULD_SKIP_THIS
     56 	nonblock= (1 << 6),
     57 	nocreate= (1 << 7),
     58 	noctty	= (1 << 8),
     59 	nombits	= 9
     60 	#endif
     61     };
     62     /// Seek directions, equivalent to SEEK_SET, SEEK_CUR, and SEEK_END.
     63     enum seekdir {
     64 	beg,
     65 	cur,
     66 	end
     67     };
     68     /// I/O state bitmasks.
     69     enum iostate_bits {
     70 	goodbit	= 0,
     71 	badbit	= (1 << 0),
     72 	eofbit	= (1 << 1),
     73 	failbit	= (1 << 2),
     74 	#ifndef DOXYGEN_SHOULD_SKIP_THIS
     75 	nbadbits = 3,
     76 	allbadbits = 0x7
     77 	#endif
     78     };
     79 
     80     typedef uint32_t		openmode;	///< Holds openmode_bits.
     81     typedef uint32_t		iostate;	///< Holds iostate_bits for a file stream.
     82     typedef file_exception	failure;	///< Thrown by fstream on errors.
     83 
     84     static const char c_DefaultDelimiters [16];	///< Default word delimiters for stringstreams.
     85 public:
     86     inline		ios_base (void)			: m_State (goodbit), m_Exceptions (goodbit) {}
     87     inline iostate	rdstate (void) const		{ return (m_State); }
     88     inline bool		bad (void) const		{ return (rdstate() & badbit); }
     89     inline bool		good (void) const		{ return (rdstate() == goodbit); }
     90     inline bool		fail (void) const		{ return (rdstate() & (badbit | failbit)); }
     91     inline bool		eof (void) const		{ return (rdstate() & eofbit); }
     92     inline bool		operator! (void) const		{ return (fail()); }
     93     inline void		clear (iostate v = goodbit)	{ m_State = v; }
     94     inline void		setstate (iostate v)		{ m_State |= v; }
     95     inline iostate	exceptions (void) const		{ return (m_Exceptions); }
     96     inline iostate	exceptions (iostate v)		{ return (m_Exceptions = v); }
     97 protected:
     98     inline bool		set_and_throw (iostate v)	{ setstate(v); return (exceptions() & v); }
     99 private:
    100     uint16_t		m_State;	///< Open state, using ios::iostate_bits.
    101     uint16_t		m_Exceptions;	///< Exception flags, using ios::iostate_bits.
    102 };
    103 
    104 } // namespace ustl
    105 
    106 #endif
    107 
    108