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
|
#pragma once
#include "generator/osm_element.hpp"
#include "base/assert.hpp"
#include "base/string_utils.hpp"
#include <functional>
#include <string>
class XMLSource
{
public:
using Emitter = std::function<void(OsmElement *)>;
XMLSource(Emitter fn) : m_emitter(fn) {}
void CharData(std::string const &) {}
void AddAttr(std::string const & key, std::string const & value)
{
if (!m_current)
return;
if (key == "id")
CHECK(strings::to_uint64(value, m_current->m_id), ("Unknown element with invalid id:", value));
else if (key == "lon")
CHECK(strings::to_double(value, m_current->m_lon), ("Bad node lon:", value));
else if (key == "lat")
CHECK(strings::to_double(value, m_current->m_lat), ("Bad node lat:", value));
else if (key == "ref")
CHECK(strings::to_uint64(value, m_current->m_ref), ("Bad node ref in way:", value));
else if (key == "k")
m_current->m_k = value;
else if (key == "v")
m_current->m_v = value;
else if (key == "type")
m_current->m_memberType = OsmElement::StringToEntityType(value);
else if (key == "role")
m_current->m_role = value;
}
bool Push(std::string const & tagName)
{
ASSERT_GREATER_OR_EQUAL(tagName.size(), 2, ());
// As tagKey we use first two char of tag name.
OsmElement::EntityType tagKey =
OsmElement::EntityType(*reinterpret_cast<uint16_t const *>(tagName.data()));
switch (++m_depth)
{
case 1:
m_current = nullptr;
break;
case 2:
m_current = &m_parent;
m_current->m_type = tagKey;
break;
default:
m_current = &m_child;
m_current->m_type = tagKey;
break;
}
return true;
}
void Pop(std::string const & v)
{
switch (--m_depth)
{
case 0:
break;
case 1:
m_emitter(m_current);
m_parent.Clear();
break;
default:
switch (m_child.m_type)
{
case OsmElement::EntityType::Member:
m_parent.AddMember(m_child.m_ref, m_child.m_memberType, m_child.m_role);
break;
case OsmElement::EntityType::Tag:
m_parent.AddTag(m_child.m_k, m_child.m_v);
break;
case OsmElement::EntityType::Nd:
m_parent.AddNd(m_child.m_ref);
break;
default:
break;
}
m_current = &m_parent;
m_child.Clear();
}
}
private:
OsmElement m_parent;
OsmElement m_child;
size_t m_depth = 0;
OsmElement * m_current = nullptr;
Emitter m_emitter;
};
|