ostream.cpp 1.13 KB
Newer Older
1
#include "yaml-cpp/ostream.h"
2
3
4
5
#include <cstring>

namespace YAML
{
6
	ostream::ostream(): m_buffer(0), m_pos(0), m_size(0), m_row(0), m_col(0), m_comment(false)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
	{
		reserve(1024);
	}
	
	ostream::~ostream()
	{
		delete [] m_buffer;
	}
	
	void ostream::reserve(unsigned size)
	{
		if(size <= m_size)
			return;
		
		char *newBuffer = new char[size];
		std::memset(newBuffer, 0, size * sizeof(char));
		std::memcpy(newBuffer, m_buffer, m_size * sizeof(char));
		delete [] m_buffer;
		m_buffer = newBuffer;
		m_size = size;
	}
	
	void ostream::put(char ch)
	{
		if(m_pos >= m_size - 1)   // an extra space for the NULL terminator
			reserve(m_size * 2);
		
		m_buffer[m_pos] = ch;
		m_pos++;
		
		if(ch == '\n') {
			m_row++;
			m_col = 0;
40
            m_comment = false;
41
42
43
44
45
46
		} else
			m_col++;
	}

	ostream& operator << (ostream& out, const char *str)
	{
47
48
		std::size_t length = std::strlen(str);
		for(std::size_t i=0;i<length;i++)
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
			out.put(str[i]);
		return out;
	}
	
	ostream& operator << (ostream& out, const std::string& str)
	{
		out << str.c_str();
		return out;
	}
	
	ostream& operator << (ostream& out, char ch)
	{
		out.put(ch);
		return out;
	}
}