-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertyList.h
111 lines (85 loc) · 3.66 KB
/
PropertyList.h
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
#ifndef _PROPERTYLIST_H
#define _PROPERTYLIST_H
#include <map>
#include <string>
class PropertyList
{
public:
// A group type, which is a map of string->string associations
typedef std::map<std::string, std::string> Group;
private:
// List of groups associated by name, string->Group association
std::map<std::string, Group> m_GroupList;
// Remove unacceptable characters from s as defined by `badchars` in .cpp
static void cutCmdString(std::string& s);
// Remove quotes surrounding given string
static void cutQuotedString(std::string& s);
// cut all characters contained in `chars` surrounding data in string `s`
static void cutAdjacentChars(std::string& s, std::string chars);
// Used for getting parsable string representation of strings with spaces using quotes
// Otherwise returns the given string unaffected.
static std::string getStringRep(const std::string& s);
// The main line parsing method
// Line is given as `s`, and current group as `group`
void parseLine(const std::string& s, std::string& group);
// Returns the integer representation of the string `s`
// This assumes the string `s` is parsable as an integer
int stringToInt(const std::string& s) const;
float stringToFloat(const std::string& s) const;
public:
// Accepts a filename as `fn` and reads datafile into memory
// Returns true on success, otherwise false
bool open(const char* fn);
// Saves the file under filename `fn`
// Returns true on success, otherwise false
bool save(const char* fn);
bool getStringValue(const char* _group, const char* _prop, std::string& value);
bool getIntValue(const char* _group, const char* _prop, int& value);
bool getFloatValue(const char* _group, const char* _prop, float& value);
std::string getString(const char* _group, const char* _prop, std::string def = "") {
std::string v;
return getStringValue(_group,_prop,v) ? v : def;
}
int getInt(const char* _group, const char* _prop, int def = 0) {
int v;
return getIntValue(_group,_prop,v) ? v : def;
}
float getFloat(const char* _group, const char* _prop, float def = 0.0f) {
float v;
return getFloatValue(_group,_prop,v) ? v : def;
}
void setStringValue(const char* _group, const char* _prop, const std::string& value);
void setIntValue(const char* _group, const char* _prop, const int value);
void setFloatValue(const char* _group, const char* _prop, const float value);
// Removes properties and gruops from the PropertyList
void removeProperty(const char* _group, const char* _prop);
void removeGroup(const char* _group);
Group* getGroup(const char* _name)
{
if(!isGroup(_name))
return NULL;
return &m_GroupList[_name];
}
// Returns if property list is empty
bool isEmpty() const
{
return m_GroupList.empty();
//return (m_GroupList.size()==0);
}
// returns number of groups contained in PropertyList
unsigned int groupCount() const
{
return m_GroupList.size();
}
// Clears Property List
void clear()
{
m_GroupList.clear();
}
// Checks if a group exists with the given name `_group`
bool isGroup(const char* _group) const
{
return (m_GroupList.find(_group)!=m_GroupList.end());
}
};
#endif