-
Notifications
You must be signed in to change notification settings - Fork 2
/
inifile.h
126 lines (106 loc) · 3.08 KB
/
inifile.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Copyright 2017, Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>
This file is part of inputmap.
inputmap is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
inputmap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with inputmap. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INICONF_H_INCLUDED
#define INICONF_H_INCLUDED
#include <string>
#include <vector>
#include <sstream>
inline bool parse_bool(const std::string &txt, bool def)
{
if (txt.empty())
return def;
return txt == "Y" || txt == "y" || txt == "1";
}
inline int parse_int(const std::string &txt, int def)
{
if (txt.empty())
return def;
std::istringstream ifs(txt);
ifs >> def;
return def;
}
inline int parse_hex_int(const std::string &txt, int def)
{
if (txt.empty())
return def;
std::istringstream ifs(txt);
ifs >> std::hex >> def;
return def;
}
std::string trim(const std::string &s);
class IniEntry
{
friend class IniFile;
friend class IniSection;
public:
template<typename F>
void preprocess_value(F filter)
{
m_value = filter(std::move(m_value));
}
const std::string name() const
{ return m_name; }
const std::string value() const
{ return m_value; }
private:
std::string m_name;
std::string m_value;
};
class IniSection
{
friend class IniFile;
public:
const std::string name() const
{ return m_name; }
template<typename F>
void preprocess_values(F filter)
{
for (auto &v : m_entries)
v.preprocess_value(filter);
}
std::string find_single_value(const std::string &name) const;
std::vector<std::string> find_multi_value(const std::string &name) const;
typedef std::vector<IniEntry>::const_iterator entry_iterator;
entry_iterator begin() const
{ return m_entries.begin(); }
entry_iterator end() const
{ return m_entries.end(); }
private:
std::string m_name;
std::vector<IniEntry> m_entries;
void add_line(const std::string &line);
};
class IniFile
{
public:
IniFile(const std::string &fileName);
template<typename F>
void preprocess_values(F filter)
{
for (auto &s : m_sections)
s.preprocess_values(filter);
}
void Dump(std::ostream &os);
const IniSection *find_single_section(const std::string &name) const;
std::vector<const IniSection*> find_multi_section(const std::string &name) const;
std::vector<IniSection>::const_iterator begin() const
{ return m_sections.begin(); }
std::vector<IniSection>::const_iterator end() const
{ return m_sections.end(); }
private:
std::vector<IniSection> m_sections;
void load(std::istream &is);
};
#endif /* INICONF_H_INCLUDED */