-
Notifications
You must be signed in to change notification settings - Fork 1
/
NTreeReader.cpp
194 lines (163 loc) · 4.45 KB
/
NTreeReader.cpp
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
// @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@
#include "NTreeReader.h"
#include <iostream>
using std::cout;
using std::endl;
NTreeReader::NTreeReader(int lev, const string& n, const size_t kl) : level(lev), keylevel(kl), name(n), path("")
{
}
NTreeReader::NTreeReader(string filename, size_t kl) : level(0), keylevel(kl), name(""), path(filename)
{
if (filename == "") return; // Don't think this is necessary anymore, but it doesn't hurt
ifstream in(filename.c_str(), ios::in|ios::binary);
if (in.fail())
{
std::cerr << "Failed to open file " << filename << endl;
return;
}
string contents = "";
string buffer;
while (getline(in, buffer))
{
contents += buffer + '\n';
}
istringstream instr(contents);
Parse(instr);
}
void NTreeReader::Parse(istringstream& in)
{
string currline = "";
size_t linelevel = level;
string valname = "";
string nextname = "";
istringstream line;
size_t strpos = 0;
while(getline(in, currline))
{
linelevel = currline.find_first_not_of(" \t");
if (linelevel == string::npos) // Skip whitespace
{
continue;
}
// When using keylevel, everything is assumed to be on the same level or problems result
if (linelevel > level && !keylevel)
{
NTreeReaderPtr newreader(new NTreeReader(linelevel, nextname, keylevel));
in.seekg(strpos);
newreader->Parse(in);
children.push_back(newreader);
}
else if (linelevel < level && !keylevel)
{
in.seekg(strpos);
return;
}
else // We need to read the value
{
line.clear();
line.str(currline);
size_t count = 0;
do
{
line >> valname;
} while (count++ < keylevel);
values[valname] = currline;
}
strpos = in.tellg();
nextname = currline.substr(linelevel);
}
// No need for a return statement here anymore, but the comment is useful.
return; // Means we reached the end of the file
}
const NTreeReader& NTreeReader::GetItem(const int num) const
{
return *children.at(num);
}
const NTreeReader& NTreeReader::operator()(const int num) const
{
return GetItem(num);
}
const NTreeReader& NTreeReader::GetItemByName(const string name) const
{
return GetItem(GetItemIndex(name));
}
int NTreeReader::GetItemIndex(const string name) const
{
for (size_t i = 0; i < children.size(); ++i)
{
if (children[i]->name == name)
return i;
}
return -1;
}
string NTreeReader::ReadLine(string& ret, const string name) const
{
if (HaveValue(name, 0))
{
string tempret = "";
istringstream readval(values[name]);
for (ssize_t i = -1; i < (ssize_t) keylevel; ++i)
{
readval >> tempret;
}
readval.ignore();
getline(readval, tempret);
if (tempret != "")
ret = tempret;
}
return ret;
}
string NTreeReader::ReadVal(const string& line, const int num) const
{
istringstream readval(line);
int i = -1 - keylevel; // Always need to read at least two values even if they pass in 0
string retval;
while (i <= num)
{
if (!(readval >> retval))
return "";
++i;
}
return retval;
}
bool NTreeReader::HaveValue(const string& name, const int num) const
{
bool retval = values.find(name) != values.end();
if (retval)
{
istringstream readval(values[name]);
int i = -1 - keylevel;
string dummy;
while (readval >> dummy && i < num)
{
++i;
}
if (i != num) retval = false; // This may not work as intended, testing is needed
}
return retval;
}
size_t NTreeReader::NumChildren() const
{
return children.size();
}
string NTreeReader::GetPath() const
{
return path;
}