-
-
Notifications
You must be signed in to change notification settings - Fork 897
Expand file tree
/
Copy pathIfcSpfHeader.cpp
More file actions
290 lines (257 loc) · 12.4 KB
/
IfcSpfHeader.cpp
File metadata and controls
290 lines (257 loc) · 12.4 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell 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 *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#include "IfcSpfHeader.h"
#include "IfcFile.h"
#include "IfcLogger.h"
static const char* const ISO_10303_21 = "ISO-10303-21";
static const char* const HEADER = "HEADER";
static const char* const ENDSEC = "ENDSEC";
static const char* const DATA = "DATA";
using namespace IfcParse;
namespace {
IfcEntityInstanceData read_from_spf_file(IfcParse::impl::in_memory_file_storage* storage, const IfcParse::entity* decl) {
if (storage != nullptr) {
parse_context pc;
storage->tokens->Next();
storage->load(-1, nullptr, pc, -1);
return pc.construct(boost::none, *storage->references_to_resolve, decl, decl->as_entity()->attribute_count(), -1);
} else {
// std::unreachable();
return IfcEntityInstanceData(in_memory_attribute_storage(10));
}
}
} // namespace
void IfcSpfHeader::readSemicolon() {
if (storage_ != nullptr) {
if (!TokenFunc::isOperator(storage_->tokens->Next(), ';')) {
throw IfcException(std::string("Expected ;"));
}
} else {
// std::unreachable();
}
}
void IfcSpfHeader::readTerminal(const std::string& term, Trail trail) {
if (storage_ != nullptr) {
if (TokenFunc::asStringRef(storage_->tokens->Next()) != term) {
throw IfcException(std::string("Expected " + term));
}
if (trail == TRAILING_SEMICOLON) {
readSemicolon();
}
} else {
// std::unreachable();
}
}
IfcParse::IfcSpfHeader::IfcSpfHeader(IfcParse::IfcFile* file)
: file_(file),
file_description_(nullptr),
file_name_(nullptr),
file_schema_(nullptr)
{
Header_section_schema::get_schema();
if (file == nullptr) {
// overwritten later in IfcFile::setDefaultHeaderValues() when we know the schema identifier
file_description_ = new Header_section_schema::file_description({}, "");
file_description_->file_ = file_;
file_name_ = new Header_section_schema::file_name("", "", {}, {}, "", "", "");
file_name_->file_ = file_;
file_schema_ = new Header_section_schema::file_schema({});
file_schema_->file_ = file_;
} else {
storage_ = std::visit([this](auto& m) -> decltype(storage_) {
if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::in_memory_file_storage>) {
return &m;
}
return nullptr;
}, file_->storage_);
if (storage_ == nullptr) {
file_description_ = Header_section_schema::get_schema().instantiate(&Header_section_schema::file_description::Class(), IfcEntityInstanceData(rocks_db_attribute_storage{}))->as<Header_section_schema::file_description>();
file_description_->file_ = file_;
file_name_ = Header_section_schema::get_schema().instantiate(&Header_section_schema::file_name::Class(), IfcEntityInstanceData(rocks_db_attribute_storage{}))->as<Header_section_schema::file_name>();
file_name_->file_ = file_;
file_schema_ = Header_section_schema::get_schema().instantiate(&Header_section_schema::file_schema::Class(), IfcEntityInstanceData(rocks_db_attribute_storage{}))->as<Header_section_schema::file_schema>();
file_schema_->file_ = file_;
}
}
}
IfcParse::IfcSpfHeader::IfcSpfHeader(IfcParse::IfcSpfLexer* lexer)
{
Header_section_schema::get_schema();
storage_ = new impl::in_memory_file_storage;
storage_->tokens = lexer;
file_ = nullptr;
// overwritten later in IfcFile::setDefaultHeaderValues() when we know the schema identifier
file_description_ = new Header_section_schema::file_description({}, "");
file_description_->file_ = file_;
file_name_ = new Header_section_schema::file_name("", "", {}, {}, "", "", "");
file_name_->file_ = file_;
file_schema_ = new Header_section_schema::file_schema({});
file_schema_->file_ = file_;
}
IfcParse::IfcSpfHeader::~IfcSpfHeader() {
delete file_schema_;
delete file_name_;
delete file_description_;
}
void IfcParse::IfcSpfHeader::file(IfcParse::IfcFile* file)
{
this->file_ = file;
if (file != nullptr) {
storage_ = std::visit([this](auto& m) -> decltype(storage_) {
if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::in_memory_file_storage>) {
return &m;
}
return nullptr;
}, file_->storage_);
}
}
void IfcSpfHeader::read() {
readTerminal(ISO_10303_21, TRAILING_SEMICOLON);
readTerminal(HEADER, TRAILING_SEMICOLON);
// | The header section of every exchange structure shall contain one
// | instance of each of the following entities: file_description, file_name,
// | and file_schema, and they shall appear in that order. Instances of
// | file_population, section_language and section_context may appear after
// | file_schema. If instances of user-defined header section entities are
// | present, they shall appear after the header section entity instances
// | defined in this section.
//
// ISO 10303-21 Second edition 2002-01-15 p. 16
readTerminal(Header_section_schema::file_description::Class().name_uc(), NONE);
delete file_description_;
file_description_ = new Header_section_schema::file_description(read_from_spf_file(storage_, &Header_section_schema::file_description::Class()));
file_description_->file_ = file_;
readSemicolon();
readTerminal(Header_section_schema::file_name::Class().name_uc(), NONE);
delete file_name_;
file_name_ = new Header_section_schema::file_name(read_from_spf_file(storage_, &Header_section_schema::file_name::Class()));
file_name_->file_ = file_;
readSemicolon();
readTerminal(Header_section_schema::file_schema::Class().name_uc(), NONE);
delete file_schema_;
file_schema_ = new Header_section_schema::file_schema(read_from_spf_file(storage_, &Header_section_schema::file_schema::Class()));
file_schema_->file_ = file_;
readSemicolon();
}
bool IfcSpfHeader::tryRead() {
try {
read();
return true;
} catch (const std::exception& e) {
Logger::Error(e);
return false;
}
}
void IfcSpfHeader::write(std::ostream& out) const {
out << ISO_10303_21 << ";"
<< "\n";
out << HEADER << ";"
<< "\n";
file_description()->toString(out, true);
out << ";"
<< "\n";
file_name()->toString(out, true);
out << ";"
<< "\n";
file_schema()->toString(out, true);
out << ";"
<< "\n";
out << ENDSEC << ";"
<< "\n";
out << DATA << ";"
<< "\n";
}
const Header_section_schema::file_description* IfcParse::IfcSpfHeader::file_description() const {
if (file_description_ == nullptr) {
std::visit([this](auto& m) {
if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::rocks_db_file_storage>) {
file_description_ = new Header_section_schema::file_description(rocks_db_attribute_storage{});
} else if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::in_memory_file_storage>) {
file_description_ = new Header_section_schema::file_description(in_memory_attribute_storage(Header_section_schema::file_description::Class().attribute_count()));
}
}, file_->storage_);
file_description_->file_ = file_;
}
return file_description_;
}
const Header_section_schema::file_name* IfcParse::IfcSpfHeader::file_name() const {
if (file_name_ == nullptr) {
std::visit([this](auto& m) {
if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::rocks_db_file_storage>) {
file_name_ = new Header_section_schema::file_name(rocks_db_attribute_storage{});
} else if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::in_memory_file_storage>) {
file_name_ = new Header_section_schema::file_name(in_memory_attribute_storage(Header_section_schema::file_name::Class().attribute_count()));
}
}, file_->storage_);
file_name_->file_ = file_;
}
return file_name_;
}
const Header_section_schema::file_schema* IfcParse::IfcSpfHeader::file_schema() const {
if (file_schema_ == nullptr) {
std::visit([this](auto& m) {
if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::rocks_db_file_storage>) {
file_schema_ = new Header_section_schema::file_schema(rocks_db_attribute_storage{});
} else if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::in_memory_file_storage>) {
file_schema_ = new Header_section_schema::file_schema(in_memory_attribute_storage(Header_section_schema::file_schema::Class().attribute_count()));
}
}, file_->storage_);
file_schema_->file_ = file_;
}
return file_schema_;
}
Header_section_schema::file_description* IfcParse::IfcSpfHeader::file_description() {
if (file_description_ == nullptr) {
std::visit([this](auto& m) {
if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::rocks_db_file_storage>) {
file_description_ = new Header_section_schema::file_description(rocks_db_attribute_storage{});
} else if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::in_memory_file_storage>) {
file_description_ = new Header_section_schema::file_description(in_memory_attribute_storage(Header_section_schema::file_description::Class().attribute_count()));
}
}, file_->storage_);
file_description_->file_ = file_;
}
return file_description_;
}
Header_section_schema::file_name* IfcParse::IfcSpfHeader::file_name() {
if (file_name_ == nullptr) {
std::visit([this](auto& m) {
if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::rocks_db_file_storage>) {
file_name_ = new Header_section_schema::file_name(rocks_db_attribute_storage{});
} else if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::in_memory_file_storage>) {
file_name_ = new Header_section_schema::file_name(in_memory_attribute_storage(Header_section_schema::file_name::Class().attribute_count()));
}
}, file_->storage_);
file_name_->file_ = file_;
}
return file_name_;
}
Header_section_schema::file_schema* IfcParse::IfcSpfHeader::file_schema() {
if (file_schema_ == nullptr) {
std::visit([this](auto& m) {
if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::rocks_db_file_storage>) {
file_schema_ = new Header_section_schema::file_schema(rocks_db_attribute_storage{});
} else if constexpr (std::is_same_v<std::decay_t<decltype(m)>, impl::in_memory_file_storage>) {
file_schema_ = new Header_section_schema::file_schema(in_memory_attribute_storage(Header_section_schema::file_schema::Class().attribute_count()));
}
}, file_->storage_);
file_schema_->file_ = file_;
}
return file_schema_;
}