forked from sourcesimian/uICAL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vobjectstream.cpp
140 lines (133 loc) · 4.36 KB
/
vobjectstream.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
/*############################################################################
# Copyright (c) 2020 Source Simian : https://github.com/sourcesimian/uICAL #
############################################################################*/
#include "uICAL/cppstl.h"
#include "uICAL/types.h"
#include "uICAL/error.h"
#include "uICAL/util.h"
#include "uICAL/logging.h"
#include "uICAL/vevent.h"
#include "uICAL/vline.h"
#include "uICAL/vlinestream.h"
#include "uICAL/vobject.h"
#include "uICAL/vobjectstream.h"
namespace uICAL
{
VObjectStream::VObjectStream(VLineStream &stm)
: stm(stm), useLine([](const string parent, const string line)
{
std::ignore = parent;
std::ignore = line;
return true; })
{
}
VObjectStream::VObjectStream(VLineStream &stm, lineP_t useLine)
: stm(stm), useLine(useLine)
{
}
string VObjectStream::nextObjectName()
{
VLine_ptr line = this->stm.next();
if (!line)
{
log_error("Parse error: %s", "Empty stream");
throw ParseError(string("Parse error, empty stream"));
}
if (line->name == "END")
{
log_trace("Final component: %s", line->as_str().c_str());
return string::none();
}
if (line->name != "BEGIN")
{
log_error("Parse error, expected BEGIN: %s", line->as_str().c_str());
throw ParseError(string("Parse error, expected BEGIN: ") + line->as_str());
}
log_trace("Component BEGIN: %s", line->value.c_str());
return line->value;
}
void VObjectStream::loadObject(string objName, VObject_ptr &obj, bool recurse)
{
while (true)
{
VLine_ptr line = this->stm.next();
if (!line)
{
log_error("%s", "Parse error, unexpected end of ICAL");
throw ParseError("Parse error, unexpected end of ICAL");
}
if (line->name == "BEGIN")
{
stm.repeatLine();
if (recurse || obj == nullptr)
{
string childName = this->nextObjectName();
VObject_ptr child = nullptr;
if (this->useLine(childName, string::none()))
{
child = new_ptr<VObject>();
child->name = childName;
log_trace("Use component: %s", childName.c_str());
}
else
{
log_trace("Ignore component: %s", childName.c_str());
}
loadObject(childName, child, true);
if (obj && child)
{
obj->children.push_back(child);
}
}
else
{
return;
}
}
else if (line->name == "END")
{
if (objName == line->value)
{
log_trace("Component END: %s", line->value.c_str());
return;
}
log_error("Mismatch \"%s\": %s", objName.c_str(), line->as_str().c_str());
throw ParseError(string("Mismatch for: \"") + objName.c_str() + string("\": ") + line->as_str());
}
else
{
if (obj && this->useLine(obj->getName(), line->name))
{
obj->lines.push_back(line);
log_trace("Use line: %s", line->as_str().c_str());
}
else
{
log_trace("Ignore line: %s", line->as_str().c_str());
}
}
}
}
VObject_ptr VObjectStream::nextObject(bool recurse)
{
while (true)
{
string objName = this->nextObjectName();
if (objName.empty())
{
return nullptr;
}
VObject_ptr obj = nullptr;
if (this->useLine(objName, string()))
{
obj = new_ptr<VObject>();
obj->name = objName;
}
loadObject(objName, obj, recurse);
if (obj)
{
return obj;
}
}
}
}