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

3
#include "noncopyable.h"
4
#include <deque>
5
6
#include <ios>
#include <string>
7
8
#include <iostream>
#include <set>
9
10
11

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

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

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

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

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

		int pos, line, column;
33
34
	
	private:
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
		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;
54
	};
55
56
57
58
59
60
61
62
63
64
65
66

	// 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);
	}	
67
}