stream.h 1.35 KB
Newer Older
1
2
#pragma once

3
#include <deque>
4
5
#include <ios>
#include <string>
6
7
#include <iostream>
#include <set>
8
9
10

namespace YAML
{
11
	static const size_t MAX_PARSER_PUSHBACK = 8;
12

13
	class Stream
14
	{
15
	public:
16
17
		friend class StreamCharSource;
		
18
19
		Stream(std::istream& input);
		~Stream();
20

21
22
		operator bool() const;
		bool operator !() const { return !static_cast <bool>(*this); }
23

24
		char peek() const;
25
26
27
28
		char get();
		std::string get(int n);
		void eat(int n = 1);

29
30
31
		static char eof() { return 0x04; }

		int pos, line, column;
32
33
	
	private:
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
		enum CharacterSet {utf8, utf16le, utf16be, utf32le, utf32be};

		std::istream& m_input;
		CharacterSet m_charSet;
		unsigned char m_bufPushback[MAX_PARSER_PUSHBACK];
		mutable size_t m_nPushedBack;
		mutable std::deque<char> m_readahead;
		unsigned char* const m_pPrefetched;
		mutable size_t m_nPrefetchedAvailable;
		mutable size_t m_nPrefetchedUsed;
		
		void AdvanceCurrent();
		char CharAt(size_t i) const;
		bool ReadAheadTo(size_t i) const;
		bool _ReadAheadTo(size_t i) const;
		void StreamInUtf8() const;
		void StreamInUtf16() const;
		void StreamInUtf32() const;
		unsigned char GetNextByte() const;
53
	};
54
55
56
57
58
59
60
61
62
63
64
65

	// CharAt
	// . Unchecked access
	inline char Stream::CharAt(size_t i) const {
		return m_readahead[i];
	}
	
	inline bool Stream::ReadAheadTo(size_t i) const {
		if(m_readahead.size() > i)
			return true;
		return _ReadAheadTo(i);
	}	
66
}