-
Notifications
You must be signed in to change notification settings - Fork 1
/
NTreeReader.h
101 lines (88 loc) · 3.56 KB
/
NTreeReader.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
// @Begin License@
// This file is part of Coldest.
//
// Coldest 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.
//
// Coldest 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 Coldest. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2008, 2011 Ben Nemec
// @End License@
#ifndef __NTREEREADER_H
#define __NTREEREADER_H
#include <string>
#include <map>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
#include <boost/shared_ptr.hpp>
using std::string;
using std::map;
using std::vector;
using std::ifstream;
using std::istringstream;
using std::ios;
using std::endl;
using boost::shared_ptr;
/*
Reads NTree (Node Tree) files. These are files in a custom format somewhat like a simplified YAML, though the NTree
variation happens to also match the format used by some Unix-like tools such as df, or the format of the file /etc/mtab on Linux.
Essentially any text file where each entry has its own line and uses whitespace as a delimiter can be read by this class.
At this time the format is only documented by this code, but that may change in the future.
Historical Note: This class used to be named IniReader, because the original intent was only to read ini-style files. Since it
grew to be more than that, a name change was in order. Also, there is a different inireader library unrelated to this class.
*/
class NTreeReader
{
public:
explicit NTreeReader(int lev = 0, const string& n = "", size_t kl = 0);
NTreeReader(string, size_t kl = 0); // Allow implicit conversion from string though
const NTreeReader& GetItem(const int) const;
const NTreeReader& operator()(const int) const;
const NTreeReader& GetItemByName(const string) const;
int GetItemIndex(const string) const;
template <typename T>
T Read(T&, const string&, const int num = 0) const;
string ReadLine(string&, const string) const;
size_t NumChildren() const;
string GetPath() const;
string GetName() const {return name;}
private:
void Parse(istringstream&);
string ReadVal(const string&, const int) const;
bool HaveValue(const string&, const int) const;
vector<shared_ptr<NTreeReader> > children;
/* This mutable is a workaround for the fact that operator[] on a map is non-const,
yet we want to be able to use it in const functions (after ensuring that the
key actually exists in the map, so we know that it will in fact not make any
changes to the map). This is probably somewhat dangerous, as we might forget
to make this check or just plain change the map in a const function, but I don't
think that would be a serious problem so this stays (for now).
*/
mutable map<string, string, std::less<string> > values;
size_t level;
size_t keylevel;
string name;
string path;
};
typedef shared_ptr<NTreeReader> NTreeReaderPtr;
template <typename T>
T NTreeReader::Read(T& ret, const string& name, const int num) const
{
if (HaveValue(name, num))
{
istringstream convert(ReadVal(values[name], num));
convert >> ret;
}
return ret;
}
#endif