forked from fmihpc/vlsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
muxml.cpp
241 lines (203 loc) · 7.06 KB
/
muxml.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/** This file is part of VLSV file format.
*
* Copyright 2011, 2012 Finnish Meteorological Institute
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <iostream>
#include "muxml.h"
using namespace std;
using namespace muxml;
XMLNode::XMLNode(XMLNode* parent): parent(parent) { }
XMLNode::~XMLNode() {
// Delete all children:
for (map<string,XMLNode*>::iterator it=children.begin(); it!=children.end(); ++it) {
delete it->second;
it->second = NULL;
}
}
MuXML::MuXML() {
root = new XMLNode(NULL);
}
MuXML::~MuXML() {
delete root;
root = NULL;
}
void MuXML::clear() {
delete root;
root = new XMLNode(NULL);
}
XMLNode* MuXML::find(const std::string& nodeName,const XMLNode* node) const {
if (node == NULL) node = root;
// Recursive depth-first find. First check if child's name matches the searched name. If it does, return
// pointer to child. Otherwise search through child's children to see if it has the searched node.
for (multimap<string,XMLNode*>::const_iterator it=node->children.begin(); it!=node->children.end(); ++it) {
if (it->first == nodeName) return it->second;
XMLNode* tmp = find(nodeName,it->second);
if (tmp != NULL) return tmp;
}
return NULL;
}
XMLNode* MuXML::find(const std::string& nodeName,const std::list<std::pair<std::string,std::string> >& attribs,const XMLNode* node) const {
if (node == NULL) node = root;
list<pair<string,string> >::const_iterator jt;
for (multimap<string,XMLNode*>::const_iterator it=node->children.begin(); it!=node->children.end(); ++it) {
bool matchFound = true;
if (it->first != nodeName) {
matchFound = false;
}
// Tag name matches, check that attributes match:
if (matchFound == true) {
for (jt = attribs.begin(); jt!=attribs.end(); ++jt) {
map<string,string>::const_iterator tmp = it->second->attributes.find((*jt).first);
if (tmp == it->second->attributes.end()) {matchFound = false; break;} // attribute name was not found
if (tmp->second != (*jt).second) {matchFound = false; break;} // attribute value did not match
}
if (matchFound == true) {
return it->second;
}
}
// Recursively check children's nodes:
XMLNode* tmp = find(nodeName,attribs,it->second);
if (tmp != NULL) return tmp;
}
return NULL;
}
string MuXML::getAttributeValue(const XMLNode* node,const std::string& attribName) const {
map<string,string>::const_iterator it=node->attributes.find(attribName);
if (it == node->attributes.end()) return "";
return it->second;
}
void MuXML::getAttributes(const XMLNode* node,std::map<std::string,std::string>& attribs) const {
attribs = node->attributes;
}
string MuXML::getNodeValue(const XMLNode* node) const {
return node->value;
}
XMLNode* MuXML::getRoot() const {return root;}
void MuXML::print(std::ostream& out,const int& level,const XMLNode* node) const {
const int tab = 3;
if (node == NULL) {
node = root;
//out << "XML TREE CONTENTS:" << endl;
}
for (map<string,XMLNode*>::const_iterator it=node->children.begin(); it!=node->children.end(); ++it) {
// Indent
for (int i=0; i<level; ++i) out << ' ';
// Write child's name and its attributes:
out << '<' << it->first;
for (map<string,string>::const_iterator jt=it->second->attributes.begin(); jt!=it->second->attributes.end(); ++jt) {
out << ' ' << jt->first << "=\"" << jt->second << "\"";
}
// Write child's value:
out << ">" << it->second->value;
// Call print for the child:
if (it->second->children.size() > 0) {
out << endl;
print(out,level+tab,it->second);
for (int i=0; i<level; ++i) out << ' ';
}
// Write child's end tag:
out << "</" << it->first << '>' << endl;
}
}
bool MuXML::read(std::istream& in,XMLNode* parent,const int& level,const char& currentChar) {
in >> noskipws;
if (parent == NULL) parent = root;
int index = 0;
bool success = true;
char c = currentChar;
char buffer[1024];
do {
// Skip empty chars:
while ((c == ' ' || c == '\t' || c == '\n') && in.good() == true) in >> c;
// Start to read tag name and its attributes:
if (c == '<') {
in >> c;
while ((c == ' ' || c == '\t' || c == '\n') && in.good() == true) in >> c;
// Read end tag and return:
if (c == '/') {
while (in.eof() == false && c != '>') in >> c;
in >> c;
if (in.good() == false) success = false;
return success;
}
index = 0;
while (c != ' ' && c != '>') {
buffer[index] = c;
++index;
in >> c;
if (in.good() == false) {success = false; break;}
}
buffer[index] = '\0';
XMLNode* node = addNode(parent,buffer,"");
// Remove empty spaces
while ((c == ' ' || c == '\t' || c == '\n') && in.good() == true) in >> c;
if (in.eof() == true) {success = false; break;}
// If next char is '>' the tag ends. Otherwise read attribute values:
if (c != '>') {
while (c != '>') {
while ((c == ' ' || c == '\t' || c == '\n') && in.good() == true) in >> c;
index = 0;
while (c != '=' && in.good() == true) {
buffer[index] = c;
++index;
in >> c;
}
buffer[index] = '\0';
string attribName = buffer;
in >> c; // Forward from '='
in >> c; // Forward from '"'
index = 0;
while (c != '"' && in.good() == true) {
buffer[index] = c;
++index;
in >> c;
}
in >> c;
if (in.good() == false) {success = false; break;}
buffer[index] = '\0';
string attribValue = buffer;
addAttribute(node,attribName,attribValue);
}
in >> c;
} else {
in >> c;
}
while ((c == ' ' || c == '\t' || c == '\n') && in.good() == true) in >> c;
// Read tag's value:
index = 0;
while ((c != ' ' && c != '\t' && c != '\n' && c != '<') && in.good() == true) {
buffer[index] = c;
++index;
in >> c;
}
if (in.good() == false) {success = false; break;}
while ((c == ' ' || c == '\t' || c == '\n') && in.good() == true) in >> c;
buffer[index] = '\0';
changeValue(node,buffer);
if (c == '<') {
read(in,node,level+1,c);
in >> c;
}
while ((c == ' ' || c == '\t' || c == '\n') && in.good() == true) in >> c;
}
if (in.good() == false && in.eof() == false) return false;
if (in.eof() == true) {
return true;
}
} while (success == true);
return true;
}