-
Notifications
You must be signed in to change notification settings - Fork 48
/
conf_file.hh
89 lines (71 loc) · 3.53 KB
/
conf_file.hh
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
//
// INI file IO library
// Derived from Gary McNickle's ConfFile code
//
// adapted by Brian Kowolowski, 20060126
//
#ifndef CONF_FILE_HH
#define CONF_FILE_HH
#include <string>
#include <vector>
#include <utility> // std::pair
// MAX_BUFFER_LEN
// Used simply as a max size of some internal buffers. Determines the maximum
// length of a line that will be read from or written to the file or the
// report output.
constexpr size_t MAX_BUFFER_LEN = 1024;
const std::string CommentIndicators = ";#";
const std::string EqualIndicators = "=:";
const std::string WhiteSpace = " \t\n\r";
class ConfFile
{
public:
// a section is a list of key-value-pairs
typedef std::pair<std::string, std::string> SectionEntry;
typedef std::vector<SectionEntry> SectionEntries;
// create config-file, if load is true, try to load config-file into memory
// throws std::runtime_error if Load fails
ConfFile(const std::string &fileName, const bool load = false);
~ConfFile();
// set dirty flag, when dirty is set the config file will be written
void set_dirty(const bool value=true);
bool Load();
bool Save();
// get a key's value; overloaded for type conversion convenience
bool GetValue(std::string &value, const std::string &key, const std::string §ion = "") const;
bool GetValue(double &value, const std::string &key, const std::string §ion = "") const;
bool GetValue(int &value, const std::string &key, const std::string §ion = "") const;
// set a key's value; created if doesn't exist
bool SetValue(const std::string &value, const std::string &key, const std::string §ion = "");
bool SetValue(const double value, const std::string &key, const std::string §ion = "");
bool SetValue(const int value, const std::string &key, const std::string §ion = "");
// remove a key from a section
bool DeleteKey(const std::string &key, const std::string §ion = "");
// remove a section from the file
bool DeleteSection(const std::string §ion);
// CreateSection: Creates the new section if it does not allready
// exist. Section is created with no keys.
bool CreateSection(const std::string §ion);
// returns number of sections in file
size_t SectionCount() const;
// returns number of keys across all sections
size_t KeyCount() const;
// get a list of section names
const std::vector<std::string> &getSectionNames() const;
// get a list of keys of the specified section
// throws std::out_of_range if section does not exist
std::vector<std::string> keys(const std::string §ion = "") const;
// GetSection: Returns the requested section (if found), otherwise std::out_of_range
const SectionEntries &at(const std::string §ion) const;
// check if section is contained
bool contains(const std::string §ion) const;
protected:
// GetSection: Returns the requested section (if found), otherwise std::out_of_range
SectionEntries &at(const std::string §ion);
private:
const std::string fileName; // The filename to write to
std::vector<std::string> section_names = {""};
std::vector<SectionEntries> data = {{}};
bool dirty; // Tracks whether or not data has changed.
};
#endif // CONF_FILE_HH