-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathIddFile.cpp
More file actions
518 lines (402 loc) · 13.8 KB
/
IddFile.cpp
File metadata and controls
518 lines (402 loc) · 13.8 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/***********************************************************************************************************************
* OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
* See also https://openstudio.net/license
***********************************************************************************************************************/
#include "IddFile.hpp"
#include "IddFile_Impl.hpp"
#include "IddRegex.hpp"
#include "IddEnums.hpp"
#include <utilities/idd/IddEnums.hxx>
#include "../core/ASCIIStrings.hpp"
#include "../core/PathHelpers.hpp"
#include "../core/Assert.hpp"
#include "../core/Containers.hpp"
namespace openstudio {
namespace detail {
// CONSTRUCTORS
// GETTERS
std::string IddFile_Impl::version() const {
return m_version;
}
std::string IddFile_Impl::build() const {
return m_build;
}
std::string IddFile_Impl::header() const {
return m_header;
}
std::vector<IddObject> IddFile_Impl::objects() const {
return m_objects;
}
std::vector<std::string> IddFile_Impl::groups() const {
StringSet result;
for (const IddObject& object : objects()) {
result.insert(object.group());
}
return {result.begin(), result.end()};
}
std::vector<IddObject> IddFile_Impl::getObjectsInGroup(const std::string& group) const {
IddObjectVector result;
for (const IddObject& object : m_objects) {
if (istringEqual(object.group(), group)) {
result.push_back(object);
}
}
return result;
}
std::vector<IddObject> IddFile_Impl::getObjects(const boost::regex& objectRegex) const {
IddObjectVector result;
for (const IddObject& object : m_objects) {
if (boost::regex_match(object.name(), objectRegex)) {
result.push_back(object);
}
}
return result;
}
boost::optional<IddObject> IddFile_Impl::versionObject() const {
if (m_versionObject) {
return m_versionObject;
}
OptionalIddObject result;
IddObjectVector candidates = getObjects(iddRegex::versionObjectName());
if (candidates.size() == 1u) {
result = candidates[0];
}
m_versionObject = result;
return result;
}
boost::optional<IddObject> IddFile_Impl::getObject(const std::string& objectName) const {
OptionalIddObject result;
for (const IddObject& object : m_objects) {
if (istringEqual(object.name(), objectName)) {
result = object;
break;
}
}
return result;
}
boost::optional<IddObject> IddFile_Impl::getObject(IddObjectType objectType) const {
OptionalIddObject result;
if (objectType == IddObjectType::UserCustom) {
LOG(Info, "Asked to return IddObject of type IddObjectType::UserCustom. Since "
<< "UserCustom object types are generally not unique, returning false rather than "
<< "an IddObject. Please specify a different IddObjectType, or use " << "getObject(const std::string&).");
return result;
}
for (const IddObject& object : m_objects) {
if (object.type() == objectType) {
result = object;
break;
}
}
return result;
}
std::vector<IddObject> IddFile_Impl::requiredObjects() const {
IddObjectVector result;
for (const IddObject& object : m_objects) {
if (object.properties().required) {
result.push_back(object);
}
}
return result;
}
std::vector<IddObject> IddFile_Impl::uniqueObjects() const {
IddObjectVector result;
for (const IddObject& object : m_objects) {
if (object.properties().unique) {
result.push_back(object);
}
}
return result;
}
// SETTERS
void IddFile_Impl::setVersion(const std::string& version) {
m_version = version;
}
void IddFile_Impl::setHeader(const std::string& header) {
m_header = header;
}
void IddFile_Impl::addObject(const IddObject& object) {
m_objects.push_back(object);
}
// SERIALIZATION
std::shared_ptr<IddFile_Impl> IddFile_Impl::load(std::istream& is) {
std::shared_ptr<IddFile_Impl> result;
IddFile_Impl iddFileImpl;
try {
iddFileImpl.parse(is);
} catch (...) {
return result;
}
result = std::shared_ptr<IddFile_Impl>(new IddFile_Impl(iddFileImpl));
return result;
}
std::ostream& IddFile_Impl::print(std::ostream& os) const {
os << m_header << '\n';
std::string groupName;
for (const IddObject& object : m_objects) {
if (object.group() != groupName) {
groupName = object.group();
os << "\\group " << groupName << '\n' << '\n';
}
object.print(os);
}
return os;
}
// PRIVATE
void IddFile_Impl::parse(std::istream& is) {
// keep track of line number in the idd
int lineNum = 0;
// number of object in the idd, 1 is first object
[[maybe_unused]] int objectNum = 0;
// stream for header
std::stringstream header;
// have we read the entire header yet
bool headerClosed = false;
std::string currentGroup;
// fake a comment only object and put it in the object list and object map
OptionalIddObject commentOnlyObject =
IddObject::load(iddRegex::commentOnlyObjectName(), currentGroup, iddRegex::commentOnlyObjectText(), IddObjectType::CommentOnly);
OS_ASSERT(commentOnlyObject);
m_objects.push_back(*commentOnlyObject);
// temp string to read file
std::string line;
// this will contain matches to regular expressions
boost::smatch matches;
// read in the version from the first line
getline(is, line);
if (boost::regex_search(line, matches, iddRegex::version())) {
m_version = std::string(matches[1].first, matches[1].second);
// this line belongs to the header
header << line << '\n';
} else {
// idd file must have a version on the first line of input
LOG_AND_THROW("Idd file does not contain version on first line: '" << line << "'");
}
// read the rest of the file line by line
// todo, do this by regex
while (getline(is, line)) {
++lineNum;
// remove whitespace
openstudio::ascii_trim(line);
if (line.empty()) {
headerClosed = true;
// empty line
continue;
} else if (boost::regex_search(line, matches, iddRegex::build())) {
m_build = std::string(matches[1].first, matches[1].second);
// this line belongs to the header
header << line << '\n';
} else if (boost::regex_match(line, iddRegex::commentOnlyLine())) {
if (!headerClosed) {
header << line << '\n';
}
// comment only line
continue;
} else if (boost::regex_search(line, matches, iddRegex::group())) {
headerClosed = true;
// get the group name
std::string groupName(matches[1].first, matches[1].second);
openstudio::ascii_trim(groupName);
// set the current group
currentGroup = groupName;
continue;
} else {
headerClosed = true;
//int beginLineNum(lineNum);
bool foundClosingLine(false);
// a valid idd object to parse
++objectNum;
// peek at the object name for indexing in map
std::string objectName;
if (boost::regex_search(line, matches, iddRegex::line())) {
objectName = std::string(matches[1].first, matches[1].second);
openstudio::ascii_trim(objectName);
} else {
// can't figure out the object's name
LOG_AND_THROW("Cannot determine object name on line " << lineNum << ": '" << line << "'");
}
// put the text for this object in a new string
std::string text(line);
// check if the object has no fields
if (boost::regex_match(line, iddRegex::objectNoFields())) {
foundClosingLine = true;
}
// check if the object has fields, and last field on this line
if (boost::regex_match(line, iddRegex::closingField())) {
foundClosingLine = true;
}
// continue reading until we have seen the entire object
// last line will be thrown away, requires empty line between objects in idd
while (getline(is, line)) {
++lineNum;
// remove whitespace
openstudio::ascii_trim(line);
// found last field and this is not a field comment
if (foundClosingLine && (!boost::regex_match(line, iddRegex::metaDataComment()))) {
break;
}
if (!line.empty()) {
// if the line is not empty add it to the text
// note, text does not include newlines
text += line;
// check if we have found the last field
if (boost::regex_match(line, iddRegex::closingField())) {
foundClosingLine = true;
}
}
}
// construct the IddObject using default UserCustom type
OptionalIddObject object = IddObject::load(objectName, currentGroup, text);
// construct a new object and put it in the object vector
if (object) {
m_objects.push_back(*object);
} else {
LOG_AND_THROW("Unable to construct IddObject from text: " << '\n' << text);
}
}
}
// set header
m_header = header.str();
}
} // namespace detail
// CONSTRUCTORS
IddFile::IddFile() : m_impl(std::make_shared<detail::IddFile_Impl>()) {}
//
// IddFile::IddFile(const IddFile& other) : m_impl(other.m_impl) {}
// IddFile& IddFile::operator=(const IddFile& other) {
// m_impl = other.m_impl;
// return *this;
// }
//
// IddFile::IddFile(IddFile&& other) noexcept : m_impl(std::move(other.m_impl)) {}
//
// IddFile& IddFile::operator=(IddFile&& other) noexcept {
// m_impl = std::move(other.m_impl);
// return *this;
// }
// PRIVATE CONSTRUCTOR
IddFile::IddFile(const std::shared_ptr<detail::IddFile_Impl>& impl) noexcept : m_impl(impl) {}
IddFile::IddFile(std::shared_ptr<detail::IddFile_Impl>&& impl) noexcept : m_impl(std::move(impl)) {}
IddFile IddFile::catchallIddFile() {
IddFile result;
result.addObject(IddObject());
return result;
}
// GETTERS
std::string IddFile::version() const {
return m_impl->version();
}
std::string IddFile::build() const {
return m_impl->build();
}
std::string IddFile::header() const {
return m_impl->header();
}
std::vector<IddObject> IddFile::objects() const {
return m_impl->objects();
}
std::vector<std::string> IddFile::groups() const {
return m_impl->groups();
}
std::vector<IddObject> IddFile::getObjectsInGroup(const std::string& group) const {
return m_impl->getObjectsInGroup(group);
}
std::vector<IddObject> IddFile::getObjects(const boost::regex& objectRegex) const {
return m_impl->getObjects(objectRegex);
}
boost::optional<IddObject> IddFile::versionObject() const {
return m_impl->versionObject();
}
boost::optional<IddObject> IddFile::getObject(const std::string& objectName) const {
return m_impl->getObject(objectName);
}
boost::optional<IddObject> IddFile::getObject(IddObjectType objectType) const {
return m_impl->getObject(objectType);
}
std::vector<IddObject> IddFile::requiredObjects() const {
return m_impl->requiredObjects();
}
std::vector<IddObject> IddFile::uniqueObjects() const {
return m_impl->uniqueObjects();
}
// SERIALIZATION
OptionalIddFile IddFile::load(std::istream& is) {
std::shared_ptr<detail::IddFile_Impl> p = detail::IddFile_Impl::load(is);
if (p) {
return IddFile(p);
}
return boost::none;
}
OptionalIddFile IddFile::load(const openstudio::path& p) {
openstudio::path wp = completePathToFile(p, path(), "idd", true);
if (wp.empty()) {
return boost::none;
}
openstudio::filesystem::ifstream inFile(wp);
if (!inFile) {
return boost::none;
}
return load(inFile);
}
std::ostream& IddFile::print(std::ostream& os) const {
return m_impl->print(os);
}
std::pair<VersionString, std::string> IddFile::parseVersionBuild(const openstudio::path& p) {
std::ifstream ifs(openstudio::toSystemFilename(p));
if (!ifs.good()) {
throw std::runtime_error("Unable to open file for reading: " + openstudio::toString(p));
}
ifs.seekg(0, std::ios_base::end);
const auto end = ifs.tellg();
ifs.seekg(0, std::ios_base::beg);
const auto length_to_read = std::min(std::streampos(10000), end);
std::vector<char> data(length_to_read);
ifs.read(data.data(), length_to_read);
const std::string strdata(data.cbegin(), data.cend());
std::string build;
boost::smatch matches;
if (boost::regex_search(strdata, matches, iddRegex::build())) {
build = std::string(matches[1].first, matches[1].second);
}
if (boost::regex_search(strdata, matches, iddRegex::version())) {
return std::make_pair(VersionString(std::string(matches[1].first, matches[1].second)), build);
}
throw std::runtime_error("Unable to parse version from IDD: " + openstudio::toString(p));
}
bool IddFile::save(const openstudio::path& p, bool overwrite) {
path wp = completePathToFile(p, path(), "idd", true);
if (!wp.empty() && (overwrite == false)) {
LOG(Info, "IddFile save method failed because instructed not to overwrite path '" << toString(p) << "'.");
return false;
}
if (makeParentFolder(p)) {
openstudio::filesystem::ofstream outFile(p);
if (outFile) {
try {
print(outFile);
outFile.close();
return true;
} catch (...) {
LOG(Error, "Unable to write IddFile to path '" << toString(p) << "'.");
return false;
}
}
}
LOG(Error, "Unable to write IddFile to path '" << toString(p) << "', because parent directory could not be created.");
return false;
}
// PROTECTED
void IddFile::setVersion(const std::string& version) {
m_impl->setVersion(version);
}
void IddFile::setHeader(const std::string& header) {
m_impl->setHeader(header);
}
void IddFile::addObject(const IddObject& object) {
m_impl->addObject(object);
}
// NON-MEMBER FUNCTIONS
std::ostream& operator<<(std::ostream& os, const IddFile& iddFile) {
return iddFile.print(os);
}
} // namespace openstudio