-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTAGParser.cpp
86 lines (78 loc) · 1.42 KB
/
TAGParser.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
#include <string>
#include <unordered_map>
#include "TAGParser.hpp"
size_t TAGParser::parseTagAttributes(std::string &data, std::unordered_map<std::string, std::string> &attributes, size_t pos)
{
int state = 0;
std::string name = "";
std::string value = "";
for (; pos < data.size(); ++pos)
{
if (state == 0 && data[pos] == '/')
{
//cout << "returning" << endl;
return pos;
}
switch (state)
{
case 0:
if (data[pos] == '=')
{
++state;
continue;
}
name += data[pos];
continue;
case 1:
if (data[pos] == '"')
{
++state;
continue;
}
else
{
//TODO: parse error, missing started "
}
continue;
case 2:
if (data[pos] == '"')
{
attributes.insert(std::pair<std::string, std::string>(name, value));
name = "";
value = "";
++state;
continue;
}
value += data[pos];
continue;
case 3:
//cout << "name:" << name << " value:" << value << endl;
state = 0;
continue;
}
}
return pos;
}
std::string TAGParser::getTagByName(std::string name, std::string &data)
{
size_t pos = data.find("<" + name + " ");
if (pos == std::string::npos)
{
return "";
}
pos += name.size() + 2;
size_t end_pos = data.find(">", pos);
if (end_pos == std::string::npos)
{
return "";
}
if (data.at(end_pos - 1) == '/')
{
end_pos -= 1;
}
while (data.at(end_pos - 1) == ' ')
{
end_pos -= 1;
}
return data.substr(pos, end_pos - pos);
}