stream.cpp 1.05 KB
Newer Older
1
2
3
4
5
6
#include "crt.h"
#include "stream.h"
#include <iostream>

namespace YAML
{
7
	Stream::Stream(std::istream& input): buffer(0), pos(0), line(0), column(0), size(0)
8
	{
9
10
11
12
13
14
15
		std::streambuf *pBuf = input.rdbuf();

		// store entire file in buffer
		size = pBuf->pubseekoff(0, std::ios::end, std::ios::in);
		pBuf->pubseekpos(0, std::ios::in);
		buffer = new char[size];
		pBuf->sgetn(buffer, size);
16
	}
17
18
19
20
21
22
23

	Stream::~Stream()
	{
		delete [] buffer;
	}


24
25
	char Stream::peek()
	{
26
		return buffer[pos];
27
28
	}
	
29
	Stream::operator bool() const
30
	{
31
		return pos < size;
32
33
34
35
36
37
	}

	// get
	// . Extracts a character from the stream and updates our position
	char Stream::get()
	{
38
39
		char ch = buffer[pos];
		pos++;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
		column++;
		if(ch == '\n') {
			column = 0;
			line++;
		}
		return ch;
	}

	// get
	// . Extracts 'n' characters from the stream and updates our position
	std::string Stream::get(int n)
	{
		std::string ret;
		for(int i=0;i<n;i++)
			ret += get();
		return ret;
	}

	// eat
	// . Eats 'n' characters and updates our position.
	void Stream::eat(int n)
	{
		for(int i=0;i<n;i++)
			get();
	}

}