summaryrefslogtreecommitdiffstats
path: root/yaml-cpp/src/ostream.cpp
blob: a7f1e14bb8c4412c1c994a786fb0dcaf55953bc3 (plain)
1
2
3
4
5
6
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "yaml-cpp/ostream.h"
#include <cstring>

namespace YAML
{
	ostream::ostream(): m_buffer(0), m_pos(0), m_size(0), m_row(0), m_col(0)
	{
		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;
		} else
			m_col++;
	}

	ostream& operator << (ostream& out, const char *str)
	{
		std::size_t length = std::strlen(str);
		for(std::size_t i=0;i<length;i++)
			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;
	}
}