map.cpp 4.04 KB
Newer Older
1
2
3
4
5
#include "map.h"
#include "node.h"
#include "scanner.h"
#include "token.h"
#include "exceptions.h"
6
#include "emitter.h"
7
#include <memory>
8
9
10
11
12
13

namespace YAML
{
	Map::Map()
	{
	}
Jesse Beder's avatar
Jesse Beder committed
14
15
16
17
18
19
20
21
22
	
	Map::Map(const node_map& data)
	{
		for(node_map::const_iterator it=data.begin();it!=data.end();++it) {
			std::auto_ptr<Node> pKey = it->first->Clone();
			std::auto_ptr<Node> pValue = it->second->Clone();
			m_data[pKey.release()] = pValue.release();
		}
	}
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

	Map::~Map()
	{
		Clear();
	}

	void Map::Clear()
	{
		for(node_map::const_iterator it=m_data.begin();it!=m_data.end();++it) {
			delete it->first;
			delete it->second;
		}
		m_data.clear();
	}

Jesse Beder's avatar
Jesse Beder committed
38
39
40
41
42
	Content *Map::Clone() const
	{
		return new Map(m_data);
	}
	
43
44
45
46
47
48
49
50
51
52
53
54
	bool Map::GetBegin(std::map <Node *, Node *, ltnode>::const_iterator& it) const
	{
		it = m_data.begin();
		return true;
	}

	bool Map::GetEnd(std::map <Node *, Node *, ltnode>::const_iterator& it) const
	{
		it = m_data.end();
		return true;
	}

55
56
57
58
59
	std::size_t Map::GetSize() const
	{
		return m_data.size();
	}

60
61
62
63
64
65
	void Map::Parse(Scanner *pScanner, const ParserState& state)
	{
		Clear();

		// split based on start token
		switch(pScanner->peek().type) {
66
67
			case Token::BLOCK_MAP_START: ParseBlock(pScanner, state); break;
			case Token::FLOW_MAP_START: ParseFlow(pScanner, state); break;
Jesse Beder's avatar
Jesse Beder committed
68
			default: break;
69
70
71
72
73
74
75
76
77
78
		}
	}

	void Map::ParseBlock(Scanner *pScanner, const ParserState& state)
	{
		// eat start token
		pScanner->pop();

		while(1) {
			if(pScanner->empty())
79
				throw ParserException(Mark::null(), ErrorMsg::END_OF_MAP);
80
81

			Token token = pScanner->peek();
82
			if(token.type != Token::KEY && token.type != Token::VALUE && token.type != Token::BLOCK_MAP_END)
83
				throw ParserException(token.mark, ErrorMsg::END_OF_MAP);
84

85
			if(token.type == Token::BLOCK_MAP_END) {
86
				pScanner->pop();
87
				break;
88
			}
89

90
			std::auto_ptr <Node> pKey(new Node), pValue(new Node);
91
92
			
			// grab key (if non-null)
93
			if(token.type == Token::KEY) {
94
95
96
				pScanner->pop();
				pKey->Parse(pScanner, state);
			}
97

98
			// now grab value (optional)
99
			if(!pScanner->empty() && pScanner->peek().type == Token::VALUE) {
100
101
				pScanner->pop();
				pValue->Parse(pScanner, state);
102
			}
103
104
105

			// assign the map with the actual pointers
			m_data[pKey.release()] = pValue.release();
106
107
108
109
110
111
112
113
114
115
		}
	}

	void Map::ParseFlow(Scanner *pScanner, const ParserState& state)
	{
		// eat start token
		pScanner->pop();

		while(1) {
			if(pScanner->empty())
116
				throw ParserException(Mark::null(), ErrorMsg::END_OF_MAP_FLOW);
117
118
119

			Token& token = pScanner->peek();
			// first check for end
120
			if(token.type == Token::FLOW_MAP_END) {
121
122
123
				pScanner->pop();
				break;
			}
124
			
125
126
			std::auto_ptr <Node> pKey(new Node), pValue(new Node);

127
128
129
130
131
132
			// grab key (if non-null)
			if(token.type == Token::KEY) {
				pScanner->pop();
				pKey->Parse(pScanner, state);
			}
			
133
			// now grab value (optional)
134
			if(!pScanner->empty() && pScanner->peek().type == Token::VALUE) {
135
136
				pScanner->pop();
				pValue->Parse(pScanner, state);
137
			}
138
			
139
140
			// now eat the separator (or could be a map end, which we ignore - but if it's neither, then it's a bad node)
			Token& nextToken = pScanner->peek();
141
			if(nextToken.type == Token::FLOW_ENTRY)
142
				pScanner->pop();
143
			else if(nextToken.type != Token::FLOW_MAP_END)
144
				throw ParserException(nextToken.mark, ErrorMsg::END_OF_MAP_FLOW);
145
146
147

			// assign the map with the actual pointers
			m_data[pKey.release()] = pValue.release();
148
149
150
		}
	}

151
	void Map::Write(Emitter& out) const
152
	{
153
154
155
156
		out << BeginMap;
		for(node_map::const_iterator it=m_data.begin();it!=m_data.end();++it)
			out << Key << *it->first << Value << *it->second;
		out << EndMap;
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
	}

	int Map::Compare(Content *pContent)
	{
		return -pContent->Compare(this);
	}

	int Map::Compare(Map *pMap)
	{
		node_map::const_iterator it = m_data.begin(), jt = pMap->m_data.begin();
		while(1) {
			if(it == m_data.end()) {
				if(jt == pMap->m_data.end())
					return 0;
				else
					return -1;
			}
			if(jt == pMap->m_data.end())
				return 1;

			int cmp = it->first->Compare(*jt->first);
			if(cmp != 0)
				return cmp;

			cmp = it->second->Compare(*jt->second);
			if(cmp != 0)
				return cmp;
		}

		return 0;
	}
}