| 114 | cycrow | 1 | #ifndef STREAM_BASE_INCLUDED
 | 
        
           |  |  | 2 | #define STREAM_BASE_INCLUDED
 | 
        
           |  |  | 3 |   | 
        
           |  |  | 4 | namespace mystream {
 | 
        
           |  |  | 5 |   | 
        
           |  |  | 6 | class stream_base
 | 
        
           |  |  | 7 | {
 | 
        
           |  |  | 8 | 	public:
 | 
        
           |  |  | 9 | 		enum state
 | 
        
           |  |  | 10 | 		{
 | 
        
           |  |  | 11 | 			goodbit=0,
 | 
        
           |  |  | 12 | 			failbit=1,
 | 
        
           |  |  | 13 | 			eofbit=2,
 | 
        
           |  |  | 14 | 			badbit=4
 | 
        
           |  |  | 15 | 		};
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | 		enum seek_type{
 | 
        
           |  |  | 18 | 			seek_begin,
 | 
        
           |  |  | 19 | 			seek_end,
 | 
        
           |  |  | 20 | 			seek_current
 | 
        
           |  |  | 21 | 		};
 | 
        
           |  |  | 22 |   | 
        
           |  |  | 23 | 		struct pos_type;
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | 		struct off_type
 | 
        
           |  |  | 26 | 		{
 | 
        
           |  |  | 27 | 			int offset;
 | 
        
           |  |  | 28 | 			off_type() { offset=0; }
 | 
        
           |  |  | 29 | 			off_type(const off_type& other) { offset=other.offset; }
 | 
        
           |  |  | 30 | 			explicit off_type(const pos_type& other) { offset=(int)other.pos; }
 | 
        
           |  |  | 31 | 			off_type(size_t s) { offset=(int)s; }
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 | 			operator long() { return (long) offset; }
 | 
        
           |  |  | 34 | 			off_type operator -(const off_type& other) const { return (off_type)(offset - other.offset); }
 | 
        
           |  |  | 35 | 			off_type operator -(int other) const { return (off_type)(offset - other); }
 | 
        
           |  |  | 36 | 			off_type operator +(const off_type& other) const { return (off_type)(offset + other.offset); }
 | 
        
           |  |  | 37 | 			off_type operator +(int i) const { return (off_type)(offset + i); }
 | 
        
           |  |  | 38 | 			off_type operator +=(const off_type& other) { return offset+=other.offset; }
 | 
        
           |  |  | 39 | 			bool operator ==(const off_type& other) const { return offset==other.offset; }
 | 
        
           |  |  | 40 | 		};
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 | 		typedef off_type offset_type;
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 | 		struct pos_type
 | 
        
           |  |  | 45 | 		{
 | 
        
           |  |  | 46 | 			size_t pos;
 | 
        
           |  |  | 47 | 			pos_type() { pos=0; }
 | 
        
           |  |  | 48 | 			pos_type(const pos_type& other) { pos=other.pos; }
 | 
        
           |  |  | 49 | 			pos_type(size_t s) { pos=s; }
 | 
        
           |  |  | 50 | 			offset_type operator -(const pos_type& other) const { return (offset_type)(pos - other.pos); }
 | 
        
           |  |  | 51 | 			offset_type operator +(const pos_type& other) const { return (offset_type)(pos + other.pos); }
 | 
        
           |  |  | 52 | 			bool operator ==(const pos_type& other) const { return pos==other.pos; }
 | 
        
           |  |  | 53 | 			bool operator ==(int i) const { return pos == (size_t)i; }
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 | 			operator size_t () const { return (size_t)pos; }
 | 
        
           |  |  | 56 | 		};
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 | 		typedef size_t size_type;
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 | 	private:
 | 
        
           |  |  | 61 | 		int m_state;
 | 
        
           |  |  | 62 | 		int m_flags;
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 | 	public:
 | 
        
           |  |  | 65 | 		stream_base() { m_flags=0; clear(goodbit); }
 | 
        
           |  |  | 66 | 		virtual ~stream_base() { }
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 | 		void clear(state s) { m_state=s; }
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 | 		int setstate(state s)
 | 
        
           |  |  | 71 | 		{
 | 
        
           |  |  | 72 | 			int old=m_state;
 | 
        
           |  |  | 73 | 			m_state|=s;
 | 
        
           |  |  | 74 | 			return old;
 | 
        
           |  |  | 75 | 		}
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 | 		int rdstate() const { return m_state; }
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 | 		bool good() const { return rdstate() == goodbit; }
 | 
        
           |  |  | 80 | 		bool bad() const { return (rdstate() & badbit) > 0; }
 | 
        
           |  |  | 81 | 		bool fail() const { return ((rdstate() & (failbit | badbit))!=0); }
 | 
        
           |  |  | 82 | 		bool eof() const { return (rdstate() & eofbit) > 0; }
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 | 		int flags() const	{	return m_flags;	}
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 | 		int flags(int newflags)
 | 
        
           |  |  | 87 | 		{
 | 
        
           |  |  | 88 | 			int old=m_flags;
 | 
        
           |  |  | 89 | 			m_flags = newflags;
 | 
        
           |  |  | 90 | 			return old;
 | 
        
           |  |  | 91 | 		}
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 | 		int setf(int newflags)
 | 
        
           |  |  | 94 | 		{
 | 
        
           |  |  | 95 | 			int old=m_flags;
 | 
        
           |  |  | 96 | 			m_flags|=newflags;
 | 
        
           |  |  | 97 | 			return old;
 | 
        
           |  |  | 98 | 		}
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 | 		void unsetf(int mask)	{	m_flags&=~mask;	}
 | 
        
           |  |  | 101 | };
 | 
        
           |  |  | 102 |   | 
        
           |  |  | 103 | }
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 | #endif // !defined(STREAM_BASE_INCLUDED)
 |