-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSaxIterators.cpp
More file actions
145 lines (121 loc) · 2.66 KB
/
SaxIterators.cpp
File metadata and controls
145 lines (121 loc) · 2.66 KB
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
#include "SaxIterators.h"
namespace xml {
namespace sax {
IteratorHelper::IteratorHelper(std::string & buffer, EventState const & event_state, ParserAutomata::Consumer & consumer, std::istream & is):
buffer(buffer),
event_state(event_state),
consumer(consumer),
is(is)
{}
void IteratorHelper::setEndStates(EventState e1, EventState e2)
{
endEvt1 = e1;
endEvt2 = e2;
}
bool IteratorHelper::isDone() const
{
return event_state == endEvt1 || event_state == endEvt2 || is.fail();
}
char IteratorHelper::consume()
{
int ch = is.get();
if( ch == std::char_traits<char>::eof() ) return 0;
consumer.consume(ch);
return ch;
}
void IteratorHelper::drain()
{
while( ! (isDone() || consumer.fail() ) ) {
consume();
}
}
// --
AttributeIterator::AttributeIterator(IteratorHelper & ih):
ih(ih)
{
ih.setEndStates(start_tag,empty_tag);
}
AttributeIterator::~AttributeIterator()
{
ih.drain();
}
auto AttributeIterator::getNext() -> OptionalAttribute
{
Attribute result;
if( ! pull(attr_name) ) return OptionalAttribute();
result.name = ih.buffer;
if( ! pull(attr_value) ) return OptionalAttribute();
result.value = ih.buffer;
return result;
}
bool AttributeIterator::pull(EventState evt)
{
while( ! ih.isDone() && ih.event_state != evt ) {
ih.consume();
}
return ih.event_state == evt;
}
std::vector<Attribute> AttributeIterator::getAttributes()
{
std::vector<Attribute> result;
for(;;) {
Attribute attr;
if( ! pull(attr_name) ) break;
attr.name = ih.buffer;
if( ! pull(attr_value) ) break;
attr.value = ih.buffer;
result.push_back(attr);
}
return result;
}
// --
CharIterator::CharIterator(IteratorHelper & ih):
ih(ih),
ptrGetChar(ih.buffer.empty() ? &CharIterator::getCharFromStream : &CharIterator::getCharFromBuffer)
{
ih.setEndStates(end_chars,special_element_end);
}
CharIterator::~CharIterator()
{
ih.drain();
}
char CharIterator::getCharFromBuffer()
{
char result = ih.buffer[0];
ih.buffer.erase(ih.buffer.begin());
if( ih.buffer.empty() ) {
ptrGetChar = &CharIterator::getCharFromStream;
}
return result;
}
char CharIterator::getCharFromStream()
{
if( ih.isDone() ) return 0;
char ch = ih.consume();
if( ih.event_state == special_element_ending ) return processEndCData();
return ih.isDone() ? 0 : ch;
}
char CharIterator::processEndCData()
{
do {
ih.consume();
} while( ih.event_state == special_element_ending );
if( ih.buffer.empty() ) return 0;
ptrGetChar = &CharIterator::getCharFromBuffer;
return getCharFromBuffer();
}
char CharIterator::getChar()
{
return (this->*ptrGetChar)();
}
std::string CharIterator::getText()
{
std::string result;
char ch;
while( (ch = getChar()) != 0 ) {
result.push_back(ch);
}
return result;
}
}
}