File: TdfParser.h

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (215 lines) | stat: -rwxr-xr-x 5,650 bytes parent folder | download
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef TDF_PARSER_H
#define TDF_PARSER_H

#include <string>
#include <vector>
#include <map>
#include <sstream>

#include "System/Sync/SyncedPrimitiveIO.h"
#include "System/Exceptions.h"
#include "System/float3.h"

/**
 * Used to parse TDF Config files.
 * An example of such a file is script.txt.
 */
class TdfParser
{
public:
	struct TdfSection;
	typedef std::map<std::string, std::string> valueMap_t;
	typedef std::map<std::string, TdfSection*> sectionsMap_t;

	struct parse_error : public content_error
	{
	public:
		parse_error(std::string const& line_of_error, size_t line, size_t column, std::string const& filename) throw();
		parse_error(size_t line, size_t column, std::string const& filename) throw();
		parse_error(std::string const& message, std::string const& line_of_error, size_t line, size_t column, std::string const& filename) throw();
		~parse_error() throw();

		size_t get_line() const;
		size_t get_column() const;
		std::string const& get_filename() const;

	private:
		size_t line;
		size_t column;
		std::string filename;
	};

	struct TdfSection
	{
		~TdfSection();

		TdfSection* construct_subsection(const std::string& name);
		void print(std::ostream& out) const;
		bool remove(const std::string& key);
		void add_name_value(const std::string& name, const std::string& value);

		template<typename T>
		void AddPair(const std::string& key, const T& value);

		sectionsMap_t sections;
		valueMap_t values;
	};

	TdfParser() {};
	TdfParser(std::string const& filename);
	TdfParser(const char* buffer, size_t size);
	virtual ~TdfParser();

	void print(std::ostream& out) const;

	void LoadFile(std::string const& file);
	void LoadBuffer(const char* buffer, size_t size);

	/**
	 * Retreive a specific value from the file and returns it, returns the specified default value if not found.
	 * @param defaultValue
	 * @param location location of value.
	 * @return returns the value on success, default otherwise.
	 */
	std::string SGetValueDef(std::string const& defaultValue, std::string const& location) const;

	/**
	 * Retreive a specific value from the file and returns it.
	 * @param value string to store value or error-message in.
	 * @param location location of value in the form "section\\section\\ ... \\name".
	 * @return returns true on success, false otherwise and error message in value.
	 */
	bool SGetValue(std::string &value, std::string const& location) const;

	template <typename T>
	bool GetValue(T& val, const std::string& location) const;

	bool GetValue(bool& val, const std::string& location) const;

	/**
	 * Treat the value as a vector and fill out vec with the items.
	 * @param location location of value in the form "section\\section\\ ... \\name".
	 * @param vec reference to a vector to store items in.
	 * @return returns number of items found.
	 */
	template<typename T>
	int GetVector(std::vector<T> &vec, std::string const& location) const;

	/// Returns a map with all values in section
	const valueMap_t& GetAllValues(std::string const& location) const;
	/// Returns a vector containing all section names
	std::vector<std::string> GetSectionList(std::string const& location) const;
	bool SectionExist(std::string const& location) const;

	template<typename T>
	void ParseArray(std::string const& value, T *array, int length) const;

	template<typename T>
	void GetDef(T& value, const std::string& defvalue, const std::string& key) const;

	void GetDef(std::string& value, const std::string& defvalue, const std::string& key) const
	{
		value = SGetValueDef(defvalue, key);
	}

	/**
	 * Retreive a value into value, or use defvalue if it does not exist
	 * (templeted defvalue version of GetDef)
	 */
	template<typename T>
	void GetTDef(T& value, const T& defvalue, const std::string& key) const;

	TdfSection* GetRootSection() { return &root_section; }

private:
	TdfSection root_section;
	std::string filename;

	std::vector<std::string> GetLocationVector(std::string const& location) const;

	void parse_buffer(char const* buf, size_t size);

public:
	float3 GetFloat3(float3 def, std::string const& location) const;
};


template<typename T>
void TdfParser::TdfSection::AddPair(const std::string& key, const T& value)
{
	std::ostringstream buf;
	buf << value;
	add_name_value(key, buf.str());
}

template <typename T>
bool TdfParser::GetValue(T& val, const std::string& location) const
{
	std::string buf;
	if (SGetValue(buf, location)) {
		std::istringstream stream(buf);
		stream >> val;
		return true;
	} else {
		return false;
	}
}

template<typename T>
int TdfParser::GetVector(std::vector<T> &vec, std::string const& location) const
{
	std::string vecstring;
	std::stringstream stream;
	SGetValue(vecstring, location);
	stream << vecstring;

	int i = 0;
	T value;
	while (stream >> value) {
		vec.push_back(value);
		i++;
	}

	return i;
}

template<typename T>
void TdfParser::ParseArray(std::string const& value, T *array, int length) const
{
	std::stringstream stream;
	stream << value;

	for (size_t i = 0; i < length; i++) {
		stream >> array[i];
		//char slask;
		//stream >> slask;
	}
}

template<typename T>
void TdfParser::GetTDef(T& value, const T& defvalue, const std::string& key) const
{
	std::string str;
	if (!SGetValue(str, key)) {
		value = defvalue;
		return;
	}

	std::stringstream stream;
	stream << str;
	stream >> value;
}

template<typename T>
void TdfParser::GetDef(T& value, const std::string& defvalue, const std::string& key) const
{
	std::string str;
	str = SGetValueDef(defvalue, key);
	std::istringstream stream(str);
	stream >> value;
}


#endif /* TDF_PARSER_H */