GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 74.3% 333 / 0 / 448
Functions: 85.7% 36 / 0 / 42
Branches: 57.9% 468 / 0 / 808

src/utilities/idf/IdfFile.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "IdfFile.hpp"
7 #include <utilities/idf/IdfObject_Impl.hpp> // needed for serialization
8 #include "IdfRegex.hpp"
9 #include "ValidityReport.hpp"
10
11 #include "../idd/IddRegex.hpp"
12 #include <utilities/idd/IddFactory.hxx>
13 #include <utilities/idd/IddEnums.hxx>
14 #include "../idd/CommentRegex.hpp"
15 #include "../idd/Comments.hpp"
16
17 #include "../plot/ProgressBar.hpp"
18 #include "../core/PathHelpers.hpp"
19 #include "../core/Assert.hpp"
20
21 #include <boost/iostreams/filter/newline.hpp>
22 #include <boost/iostreams/filtering_stream.hpp>
23
24 namespace openstudio {
25
26 // CONSTRUCTORS
27
28
1/1
✓ Branch 4 taken 4239 times.
4239 IdfFile::IdfFile(IddFileType iddFileType) : m_iddFileAndFactoryWrapper(iddFileType) {
29
1/1
✓ Branch 1 taken 4239 times.
4239 addVersionObject();
30 4239 }
31
32
1/1
✓ Branch 4 taken 3695 times.
3695 IdfFile::IdfFile(const IddFile& iddFile) : m_iddFileAndFactoryWrapper(iddFile) {
33
1/1
✓ Branch 1 taken 3695 times.
3695 addVersionObject();
34 3695 }
35
36 // GETTERS
37
38 2174 std::string IdfFile::header() const {
39 2174 return m_header;
40 }
41
42 9630 VersionString IdfFile::version() const {
43
2/2
✓ Branch 1 taken 9630 times.
✓ Branch 4 taken 9630 times.
9630 return VersionString(m_iddFileAndFactoryWrapper.version());
44 }
45
46 717 IddFile IdfFile::iddFile() const {
47 717 return m_iddFileAndFactoryWrapper.iddFile();
48 }
49
50 45 IddFileType IdfFile::iddFileType() const {
51 45 return m_iddFileAndFactoryWrapper.iddFileType();
52 }
53
54 5881 boost::optional<IdfObject> IdfFile::versionObject() const {
55 5881 OptionalIdfObject result;
56
2/2
✓ Branch 1 taken 5700 times.
✓ Branch 2 taken 181 times.
5881 if (m_versionObjectIndices.size() == 1u) {
57 5700 unsigned index = *(m_versionObjectIndices.begin());
58
1/1
✓ Branch 2 taken 5700 times.
5700 result = m_objects[index];
59 }
60 5881 return result;
61 ✗ }
62
63 ✗ boost::optional<IdfObject> IdfFile::getObject(unsigned index) const {
64 ✗ OptionalIdfObject result;
65 ✗ if (index < m_objects.size()) {
66 ✗ result = m_objects[index];
67 }
68 ✗ return result;
69 ✗ }
70
71 216 boost::optional<IdfObject> IdfFile::getObject(const Handle& handle) const {
72 216 OptionalIdfObject result;
73
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 199 times.
216 if (handle.isNull()) {
74 17 return result;
75 }
76
1/2
✓ Branch 5 taken 47386 times.
✗ Branch 6 not taken.
47386 for (const IdfObject& object : m_objects) {
77
4/4
✓ Branch 1 taken 47386 times.
✓ Branch 4 taken 47386 times.
✓ Branch 6 taken 199 times.
✓ Branch 7 taken 47187 times.
47386 if (object.handle() == handle) {
78
1/1
✓ Branch 1 taken 199 times.
199 result = object;
79 199 break;
80 }
81 }
82 199 return result;
83 ✗ }
84
85 6582 std::vector<IdfObject> IdfFile::objects() const {
86 6582 IdfObjectVector result = m_objects;
87
2/2
✓ Branch 3 taken 6582 times.
✓ Branch 4 taken 6582 times.
13164 for (auto it = m_versionObjectIndices.rbegin(), itEnd = m_versionObjectIndices.rend(); it != itEnd; ++it) {
88 6582 auto oit = result.begin();
89
2/2
✓ Branch 2 taken 684 times.
✓ Branch 3 taken 6582 times.
7266 for (unsigned i = 0; i < *it; ++i, ++oit) {
90 ;
91 }
92
7/37
✓ Branch 2 taken 6582 times.
✓ Branch 5 taken 6582 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6582 times.
✗ Branch 10 not taken.
✗ Branch 14 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 24 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 31 not taken.
✗ Branch 35 not taken.
✗ Branch 38 not taken.
✗ Branch 41 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✓ Branch 48 taken 6582 times.
✗ Branch 50 not taken.
✗ Branch 52 not taken.
✓ Branch 53 taken 6582 times.
✗ Branch 55 not taken.
✓ Branch 56 taken 6582 times.
✓ Branch 58 taken 6582 times.
✗ Branch 59 not taken.
✗ Branch 61 not taken.
✗ Branch 62 not taken.
✗ Branch 64 not taken.
✗ Branch 65 not taken.
✗ Branch 67 not taken.
✗ Branch 68 not taken.
6582 OS_ASSERT(oit->iddObject().isVersionObject()
93 || ((oit->iddObject().type() == IddObjectType::Catchall) && (oit->numFields() > 0u)
94 && (boost::regex_match(oit->getString(0).get(), iddRegex::versionObjectName()))));
95
1/1
✓ Branch 2 taken 6582 times.
6582 result.erase(oit);
96 }
97 6582 return result;
98 ✗ }
99
100 126 std::vector<IdfObject> IdfFile::getObjectsByType(IddObjectType objectType) const {
101 126 IdfObjectVector result;
102
2/2
✓ Branch 5 taken 23118 times.
✓ Branch 6 taken 126 times.
23244 for (const auto& idfObject : m_objects) {
103
4/4
✓ Branch 1 taken 23118 times.
✓ Branch 4 taken 23118 times.
✓ Branch 8 taken 25 times.
✓ Branch 9 taken 23093 times.
23118 if (idfObject.iddObject().type() == objectType) {
104
1/1
✓ Branch 1 taken 25 times.
25 result.push_back(idfObject);
105 }
106 }
107 126 return result;
108 ✗ }
109
110 747 std::vector<IdfObject> IdfFile::getObjectsByType(const IddObject& objectType) const {
111 747 IdfObjectVector result;
112
3/3
✓ Branch 1 taken 747 times.
✓ Branch 8 taken 349576 times.
✓ Branch 9 taken 747 times.
350323 for (const IdfObject& object : objects()) {
113
4/4
✓ Branch 1 taken 349576 times.
✓ Branch 4 taken 349576 times.
✓ Branch 7 taken 318 times.
✓ Branch 8 taken 349258 times.
349576 if (object.iddObject() == objectType) {
114
1/1
✓ Branch 1 taken 318 times.
318 result.push_back(object);
115 }
116 747 }
117 747 return result;
118 ✗ }
119
120 1 std::vector<IdfObject> IdfFile::getObjectsByName(const std::string& name) const {
121 1 IdfObjectVector result;
122
2/2
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 1 time.
6 for (const IdfObject& object : m_objects) {
123
1/1
✓ Branch 1 taken 5 times.
5 OptionalString objectName = object.name();
124
8/8
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 8 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 11 taken 4 times.
5 if (objectName && openstudio::istringEqual(name, *objectName)) {
125
1/1
✓ Branch 1 taken 1 time.
1 result.push_back(object);
126 }
127 5 }
128 1 return result;
129 ✗ }
130
131 // SETTERS
132
133 1387 void IdfFile::setHeader(const std::string& header) {
134
1/1
✓ Branch 1 taken 1387 times.
1387 m_header = makeComment(header);
135 1387 }
136
137 1260269 void IdfFile::addObject(const IdfObject& object) {
138 1260269 m_objects.push_back(object);
139
4/4
✓ Branch 1 taken 1260269 times.
✓ Branch 4 taken 1260269 times.
✓ Branch 7 taken 11102 times.
✓ Branch 8 taken 1249167 times.
1260269 if (object.iddObject().isVersionObject()) {
140
1/1
✓ Branch 2 taken 11102 times.
11102 m_versionObjectIndices.insert(m_objects.size() - 1);
141
6/9
✓ Branch 1 taken 1249167 times.
✓ Branch 4 taken 1249167 times.
✓ Branch 8 taken 182 times.
✓ Branch 10 taken 182 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 177 times.
✓ Branch 14 taken 1248990 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
2498516 } else if ((object.iddObject().type() == IddObjectType::Catchall) && (object.numFields() > 0u)
142
12/15
✓ Branch 1 taken 1249167 times.
✓ Branch 3 taken 182 times.
✓ Branch 4 taken 1248985 times.
✓ Branch 6 taken 182 times.
✓ Branch 9 taken 182 times.
✓ Branch 12 taken 182 times.
✓ Branch 15 taken 182 times.
✓ Branch 17 taken 177 times.
✓ Branch 18 taken 5 times.
✓ Branch 19 taken 182 times.
✓ Branch 20 taken 1248985 times.
✓ Branch 22 taken 1249167 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
2498516 && (boost::regex_match(object.getString(0).get(), iddRegex::versionObjectName()))) {
143
1/1
✓ Branch 2 taken 177 times.
177 m_versionObjectIndices.insert(m_objects.size() - 1);
144 }
145 1260269 }
146
147 ✗ void IdfFile::addObjects(const std::vector<IdfObject>& objects) {
148 ✗ for (const IdfObject& object : objects) {
149 ✗ addObject(object);
150 }
151 ✗ }
152
153 1 void IdfFile::insertObjectByIddObjectType(const IdfObject& object) {
154
1/2
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 for (auto it = m_objects.begin(), itEnd = m_objects.end(); it != itEnd; ++it) {
155
11/18
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 11 taken 3 times.
✓ Branch 14 taken 3 times.
✓ Branch 17 taken 1 time.
✓ Branch 18 taken 2 times.
✓ Branch 19 taken 3 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 3 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 1 time.
✓ Branch 26 taken 2 times.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
3 if (it == itEnd || object.iddObject().type() < it->iddObject().type()) {
156 // insert object immediately before it
157
1/1
✓ Branch 2 taken 1 time.
1 it = m_objects.insert(it, object);
158
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 if (object.iddObject().isVersionObject()
159
8/16
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 9 taken 1 time.
✓ Branch 12 taken 1 time.
✓ Branch 15 taken 1 time.
✗ Branch 18 not taken.
✓ Branch 19 taken 1 time.
✗ Branch 21 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✓ Branch 26 taken 1 time.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
2 || ((object.iddObject().type() == IddObjectType::Catchall) && (object.numFields() > 0u)
160
2/12
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 1 time.
✓ Branch 17 taken 1 time.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
1 && (boost::regex_match(object.getString(0).get(), iddRegex::versionObjectName())))) {
161 ✗ unsigned index = unsigned(it - m_objects.begin());
162 ✗ m_versionObjectIndices.insert(index);
163 ✗ OS_ASSERT(m_objects[index] == object);
164 }
165 1 return;
166 }
167 }
168 }
169
170 3348 bool IdfFile::removeObject(const IdfObject& object) {
171 auto it =
172
2/2
✓ Branch 1 taken 3348 times.
✓ Branch 6 taken 3348 times.
6696 std::find_if(m_objects.begin(), m_objects.end(), [h = object.handle()](const IdfObject& obj) { return handleEquals<IdfObject, Handle>(obj, h); });
173
174
1/2
✓ Branch 2 taken 3348 times.
✗ Branch 3 not taken.
3348 if (it != m_objects.end()) {
175 3348 IdfObjectVector::size_type index(it - m_objects.begin());
176
2/5
✓ Branch 1 taken 3348 times.
✓ Branch 4 taken 3348 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
6696 if (it->iddObject().isVersionObject()
177
3/15
✓ Branch 2 taken 3348 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3348 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 19 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 24 taken 3348 times.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
10044 || ((object.iddObject().type() == IddObjectType::Catchall) && (object.numFields() > 0u)
178
2/12
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 3348 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 3348 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
3348 && (boost::regex_match(object.getString(0).get(), iddRegex::versionObjectName())))) {
179
1/1
✓ Branch 1 taken 3348 times.
3348 m_versionObjectIndices.erase(index);
180 }
181
1/1
✓ Branch 2 taken 3348 times.
3348 m_objects.erase(it);
182 3348 std::vector<IdfObjectVector::size_type> toModify;
183
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 3348 times.
3348 for (const auto i : m_versionObjectIndices) {
184 ✗ if (i > index) {
185 ✗ toModify.push_back(i);
186 }
187 }
188
1/2
✗ Branch 4 not taken.
✓ Branch 5 taken 3348 times.
3348 for (auto i : toModify) {
189 ✗ m_versionObjectIndices.erase(i);
190 ✗ m_versionObjectIndices.insert(i - 1);
191 }
192 3348 return true;
193 3348 }
194 ✗ return false;
195 }
196
197 ✗ int IdfFile::removeObjects(const std::vector<IdfObject>& objects) {
198 ✗ int result(0);
199 ✗ for (const IdfObject& object : objects) {
200 ✗ bool ok = removeObject(object);
201 ✗ if (ok) {
202 ✗ ++result;
203 }
204 }
205 ✗ return result;
206 }
207
208 // QUERIES
209
210 2 bool IdfFile::empty() const {
211
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!m_header.empty()) {
212 ✗ return false;
213 }
214
3/3
✓ Branch 1 taken 2 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
2 if (!objects().empty()) {
215 1 return false;
216 }
217 1 return true;
218 }
219
220 2434 unsigned IdfFile::numObjects() const {
221
1/1
✓ Branch 1 taken 2434 times.
2434 return objects().size();
222 }
223
224 120 unsigned IdfFile::numObjectsOfType(IddObjectType objectType) const {
225
1/1
✓ Branch 1 taken 120 times.
120 return getObjectsByType(objectType).size();
226 }
227
228 ✗ unsigned IdfFile::numObjectsOfType(const IddObject& objectType) const {
229 ✗ return getObjectsByType(objectType).size();
230 }
231
232 12 bool IdfFile::isValid(StrictnessLevel level) const {
233
1/1
✓ Branch 1 taken 12 times.
12 ValidityReport report = validityReport(level);
234
1/1
✓ Branch 1 taken 12 times.
24 return (report.numErrors() == 0);
235 12 }
236
237 14 ValidityReport IdfFile::validityReport(StrictnessLevel level) const {
238 14 ValidityReport report(level);
239
240 // StrictnessLevel::Minimal
241 // DataErrorType::NoIdd
242 // \todo Only way there can be no IddFile is if IddFileType is set to UserCustom
243
244 // by-object items
245
2/2
✓ Branch 5 taken 1357 times.
✓ Branch 6 taken 14 times.
1371 for (const IdfObject& object : m_objects) {
246 // object-level report
247
1/1
✓ Branch 1 taken 1357 times.
1357 ValidityReport objectReport = object.validityReport(level);
248
1/1
✓ Branch 1 taken 1357 times.
1357 OptionalDataError oError = objectReport.nextError();
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1357 times.
2714 while (oError) {
250 ✗ report.insertError(*oError);
251 ✗ oError = objectReport.nextError();
252 }
253
254 // StrictnessLevel::Draft
255
3/3
✓ Branch 1 taken 1357 times.
✓ Branch 4 taken 1349 times.
✓ Branch 5 taken 8 times.
1357 if (level > StrictnessLevel::Minimal) {
256 // DataErrorType::NoIdd
257 // object-level
258
5/5
✓ Branch 1 taken 1349 times.
✓ Branch 4 taken 1349 times.
✓ Branch 7 taken 1349 times.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 1346 times.
1349 if (!m_iddFileAndFactoryWrapper.isInFile(object.iddObject().type())) {
259
3/3
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
3 report.insertError(DataError(object, DataErrorType(DataErrorType::NoIdd)));
260 }
261 } // StrictnessLevel::Draft
262 1357 }
263
264 // StrictnessLevel::Final
265
3/3
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 12 times.
14 if (level > StrictnessLevel::Draft) {
266 // DataErrorType::NullAndRequired
267 // collection-level: required object missing
268
1/1
✓ Branch 1 taken 2 times.
2 IddObjectVector requiredObjects = m_iddFileAndFactoryWrapper.requiredObjects();
269
2/2
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 2 times.
6 for (const IddObject& iddObject : requiredObjects) {
270
4/4
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 2 times.
4 if (numObjectsOfType(iddObject.type()) < 1) {
271
4/4
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
2 report.insertError(DataError(DataErrorType(DataErrorType::NullAndRequired), iddObject.type()));
272 }
273 }
274
275 // DataErrorType::Duplicate
276 // collection-level: unique object duplicated
277
1/1
✓ Branch 1 taken 2 times.
2 IddObjectVector uniqueObjects = m_iddFileAndFactoryWrapper.uniqueObjects();
278
2/2
✓ Branch 5 taken 116 times.
✓ Branch 6 taken 2 times.
118 for (const IddObject& iddObject : uniqueObjects) {
279
3/4
✓ Branch 1 taken 116 times.
✓ Branch 4 taken 116 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 116 times.
116 if (numObjectsOfType(iddObject.type()) > 1) {
280 ✗ report.insertError(DataError(DataErrorType(DataErrorType::Duplicate), iddObject.type()));
281 }
282 }
283
284 2 } // StrictnessLevel::Final
285
286 14 return report;
287 ✗ }
288
289 // SERIALIZATON
290
291 914 boost::optional<IdfFile> IdfFile::load(std::istream& is, const IddFileType& iddFileType, ProgressBar* progressBar) {
292
1/1
✓ Branch 1 taken 914 times.
914 IdfFile result(iddFileType);
293 // remove initial version object
294
2/3
✓ Branch 1 taken 914 times.
✓ Branch 3 taken 914 times.
✗ Branch 4 not taken.
1828 if (OptionalIdfObject vo = result.versionObject()) {
295
2/2
✓ Branch 1 taken 914 times.
✓ Branch 4 taken 914 times.
914 result.removeObject(*vo);
296 914 }
297
2/3
✓ Branch 1 taken 914 times.
✓ Branch 3 taken 914 times.
✗ Branch 4 not taken.
914 if (result.m_load(is, progressBar)) {
298 // check for it again here
299
1/1
✓ Branch 1 taken 914 times.
914 result.addVersionObject();
300
1/1
✓ Branch 1 taken 914 times.
914 return result;
301 }
302 ✗ return boost::none;
303 914 }
304
305 1759 OptionalIdfFile IdfFile::load(std::istream& is, const IddFile& iddFile, ProgressBar* progressBar) {
306
1/1
✓ Branch 1 taken 1759 times.
1759 IdfFile result(iddFile);
307 // remove initial version object
308
2/3
✓ Branch 1 taken 1759 times.
✓ Branch 3 taken 1759 times.
✗ Branch 4 not taken.
3518 if (OptionalIdfObject vo = result.versionObject()) {
309
2/2
✓ Branch 1 taken 1759 times.
✓ Branch 4 taken 1759 times.
1759 result.removeObject(*vo);
310 1759 }
311
3/3
✓ Branch 1 taken 1759 times.
✓ Branch 3 taken 1758 times.
✓ Branch 4 taken 1 time.
1759 if (result.m_load(is, progressBar)) {
312 // check for it again here
313
1/1
✓ Branch 1 taken 1758 times.
1758 result.addVersionObject();
314
1/1
✓ Branch 1 taken 1758 times.
1758 return result;
315 }
316 1 return boost::none;
317 1759 }
318
319 168 OptionalIdfFile IdfFile::load(const path& p, ProgressBar* progressBar) {
320 // determine IddFileType
321
1/1
✓ Branch 1 taken 168 times.
168 IddFileType iddType(IddFileType::EnergyPlus); // default
322
323 // switch if file extension equal to modelFileExtension() or componentFileExtension()
324
1/1
✓ Branch 2 taken 168 times.
168 std::string pext = p.extension().string();
325
1/2
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
168 if (!pext.empty()) {
326 // remove '.'
327
1/1
✓ Branch 4 taken 168 times.
168 pext = std::string(++pext.begin(), pext.end());
328 }
329
10/16
✓ Branch 1 taken 168 times.
✓ Branch 4 taken 167 times.
✓ Branch 5 taken 1 time.
✓ Branch 7 taken 167 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 167 times.
✓ Branch 12 taken 167 times.
✓ Branch 13 taken 1 time.
✓ Branch 15 taken 168 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 1 time.
✓ Branch 19 taken 167 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
168 if ((pext == modelFileExtension()) || (pext == componentFileExtension())) {
330
1/1
✓ Branch 1 taken 1 time.
1 iddType = IddFileType(IddFileType::OpenStudio);
331 }
332
333
1/1
✓ Branch 1 taken 168 times.
336 return load(p, iddType, progressBar);
334 168 }
335
336 189 OptionalIdfFile IdfFile::load(const path& p, const IddFileType& iddFileType, ProgressBar* progressBar) {
337 // complete path
338
1/1
✓ Branch 1 taken 189 times.
189 path wp(p);
339
340
1/1
✓ Branch 1 taken 189 times.
189 std::string ext = getFileExtension(p);
341
342
3/3
✓ Branch 1 taken 189 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 187 times.
189 if (iddFileType == IddFileType::OpenStudio) {
343 // can be Model or Component
344
8/26
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 2 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 2 times.
✓ Branch 21 taken 2 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 2 times.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✓ Branch 27 taken 2 times.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
6 if (!(openstudio::istringEqual(ext, "osm") || openstudio::istringEqual(ext, "osc"))) {
345 ✗ LOG_FREE(Warn, "openstudio.setFileExtension",
346 "Path p, '" << toString(p) << "', has an unexpected file extension. Was expecting 'osm' or 'osc'.");
347 }
348 } else {
349
1/1
✓ Branch 1 taken 187 times.
187 std::string ext = getFileExtension(p);
350
10/38
✓ Branch 1 taken 187 times.
✓ Branch 4 taken 187 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 187 times.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✓ Branch 25 taken 187 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 187 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 187 times.
✗ Branch 32 not taken.
✓ Branch 33 taken 187 times.
✓ Branch 34 taken 187 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 187 times.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✓ Branch 40 taken 187 times.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 54 not taken.
✗ Branch 55 not taken.
561 if (!(openstudio::istringEqual(ext, "idf") || openstudio::istringEqual(ext, "imf") || openstudio::istringEqual(ext, "ddy"))) {
351 ✗ LOG_FREE(Warn, "openstudio.setFileExtension",
352 "Path p, '" << toString(p) << "', has an unexpected file extension. Was expecting 'idf', 'ddy', or 'imf'.");
353 }
354 187 }
355
356 // pass warnOnMisMatch as false since we warn above anyways
357 // In fact, don't pass the ext param, skip the entire call to setFileExtension which is pointless since it won't force replace it
358
2/2
✓ Branch 1 taken 189 times.
✓ Branch 5 taken 189 times.
189 wp = completePathToFile(wp, path(), "", false);
359
360 // try to open file and parse
361
1/1
✓ Branch 1 taken 189 times.
189 openstudio::filesystem::ifstream inFile(wp);
362
3/3
✓ Branch 1 taken 189 times.
✓ Branch 3 taken 187 times.
✓ Branch 4 taken 2 times.
189 if (inFile) {
363 try {
364
1/1
✓ Branch 1 taken 187 times.
187 return load(inFile, iddFileType, progressBar);
365 ✗ } catch (...) {
366 ✗ return boost::none;
367 ✗ }
368 }
369
370 2 return boost::none;
371 189 }
372
373 2 OptionalIdfFile IdfFile::load(const path& p, const IddFile& iddFile, ProgressBar* progressBar) {
374 // complete path
375
2/2
✓ Branch 1 taken 2 times.
✓ Branch 5 taken 2 times.
2 path wp = completePathToFile(p, path(), "idf", false);
376
377 // try to open file and parse
378
1/1
✓ Branch 1 taken 2 times.
2 openstudio::filesystem::ifstream inFile(wp);
379
2/3
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
2 if (inFile) {
380 try {
381
1/1
✓ Branch 1 taken 2 times.
2 return load(inFile, iddFile, progressBar);
382 ✗ } catch (...) {
383 ✗ return boost::none;
384 ✗ }
385 }
386
387 ✗ return boost::none;
388 2 }
389
390 179 boost::optional<VersionString> IdfFile::loadVersionOnly(std::istream& is) {
391 179 boost::optional<VersionString> result;
392
1/1
✓ Branch 1 taken 179 times.
179 IddFile catchallIdd = IddFile::catchallIddFile();
393
1/1
✓ Branch 1 taken 179 times.
179 IdfFile idf(catchallIdd);
394
2/4
✓ Branch 1 taken 179 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 179 times.
✗ Branch 7 not taken.
179 OS_ASSERT(!idf.versionObject());
395
1/1
✓ Branch 1 taken 179 times.
179 idf.m_load(is, nullptr, true);
396
3/3
✓ Branch 1 taken 179 times.
✓ Branch 3 taken 177 times.
✓ Branch 4 taken 2 times.
358 if (OptionalIdfObject oVersionObject = idf.versionObject()) {
397
2/2
✓ Branch 1 taken 177 times.
✓ Branch 4 taken 177 times.
177 unsigned n = oVersionObject->numFields();
398 // Note: oVersionObject->iddObject().type() == Catchall, so m_fields[0] is either "OS:Version" or "Version"
399 // Added a prerelease field for OS:Version only... I could explicitly test if OS:Version, but it has more fields than the E+ one,
400 // so testing if n==4 is enough, and will be faster
401 // if (openstudio::istringEqual("OS:VERSION", oVersionObject->getString(0, false).get())) {
402
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 150 times.
177 if (n == 4) { // This is m_fields.size, which includes "OS:Version" itself. So if n == 4, you do have a prerelease tag
403 27 --n; // penultimate, as ultimate = Prerelease tag
404 }
405
406
4/4
✓ Branch 1 taken 177 times.
✓ Branch 4 taken 177 times.
✓ Branch 7 taken 177 times.
✓ Branch 10 taken 177 times.
177 std::string versionString = oVersionObject->getString(n - 1, true).get();
407
408
1/2
✓ Branch 1 taken 177 times.
✗ Branch 2 not taken.
177 if (!versionString.empty()) {
409
2/2
✓ Branch 1 taken 177 times.
✓ Branch 4 taken 177 times.
177 result = VersionString(versionString);
410 }
411 356 }
412 358 return result;
413 179 }
414
415 1 boost::optional<VersionString> IdfFile::loadVersionOnly(const path& p) {
416 1 boost::optional<VersionString> result;
417
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
1 path wp = completePathToFile(p, path(), "idf", false);
418
1/1
✓ Branch 1 taken 1 time.
1 openstudio::filesystem::ifstream inFile(wp);
419
2/3
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
1 if (inFile) {
420 try {
421
1/1
✓ Branch 1 taken 1 time.
1 return loadVersionOnly(inFile);
422 ✗ } catch (...) {
423 ✗ return result;
424 ✗ }
425 }
426 ✗ return result;
427 1 }
428
429 499 std::ostream& IdfFile::print(std::ostream& os) const {
430
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 497 times.
499 if (!m_header.empty()) {
431 2 os << m_header << '\n';
432 }
433 499 os << '\n';
434
2/2
✓ Branch 5 taken 98069 times.
✓ Branch 6 taken 499 times.
98568 for (const IdfObject& object : m_objects) {
435
1/1
✓ Branch 1 taken 98069 times.
98069 object.print(os);
436 }
437 499 return os;
438 }
439
440 459 bool IdfFile::save(const openstudio::path& p, bool overwrite) {
441
442 // default extension
443 459 std::string expectedExtension;
444 459 bool enforceExtension = false;
445
2/2
✓ Branch 1 taken 459 times.
✓ Branch 4 taken 459 times.
459 OptionalIddFileType iddType = m_iddFileAndFactoryWrapper.iddFileType();
446
1/2
✓ Branch 0 taken 459 times.
✗ Branch 1 not taken.
459 if (iddType) {
447
4/4
✓ Branch 1 taken 459 times.
✓ Branch 4 taken 459 times.
✓ Branch 7 taken 46 times.
✓ Branch 8 taken 413 times.
459 if (*iddType == IddFileType::EnergyPlus) {
448
1/1
✓ Branch 1 taken 46 times.
46 expectedExtension = "idf";
449 46 enforceExtension = true;
450
3/4
✓ Branch 1 taken 413 times.
✓ Branch 4 taken 413 times.
✓ Branch 7 taken 413 times.
✗ Branch 8 not taken.
413 } else if (*iddType == IddFileType::OpenStudio) {
451
1/1
✓ Branch 1 taken 413 times.
413 std::string ext = getFileExtension(p);
452
3/3
✓ Branch 1 taken 413 times.
✓ Branch 5 taken 113 times.
✓ Branch 6 taken 300 times.
413 if (ext == componentFileExtension()) {
453
1/1
✓ Branch 1 taken 113 times.
113 expectedExtension = componentFileExtension();
454 // no need to enforce b/c already checked
455 } else {
456
1/1
✓ Branch 1 taken 300 times.
300 expectedExtension = modelFileExtension();
457 300 enforceExtension = true;
458 }
459 413 }
460 }
461
462 // set extension if appropriate
463
1/1
✓ Branch 1 taken 459 times.
459 path wp(p);
464
2/2
✓ Branch 0 taken 346 times.
✓ Branch 1 taken 113 times.
459 if (enforceExtension) {
465
1/1
✓ Branch 1 taken 346 times.
346 wp = setFileExtension(p, expectedExtension, false, true);
466 }
467
468 // do not overwrite if not allowed
469
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 446 times.
459 if (!overwrite) {
470
1/1
✓ Branch 3 taken 13 times.
13 path temp = completePathToFile(wp, path());
471
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 11 times.
13 if (!temp.empty()) {
472
8/8
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 17 taken 2 times.
✓ Branch 20 taken 2 times.
✓ Branch 23 taken 2 times.
2 LOG(Info, "Save method failed because instructed not to overwrite path '" << toString(wp) << "'.");
473 2 return false;
474 }
475 13 }
476
477
2/3
✓ Branch 2 taken 457 times.
✓ Branch 5 taken 457 times.
✗ Branch 6 not taken.
457 if (makeParentFolder(wp)) {
478
1/1
✓ Branch 1 taken 457 times.
457 openstudio::filesystem::ofstream outFile(wp);
479
2/3
✓ Branch 1 taken 457 times.
✓ Branch 3 taken 457 times.
✗ Branch 4 not taken.
457 if (outFile) {
480 try {
481
1/1
✓ Branch 1 taken 457 times.
457 print(outFile);
482
1/1
✓ Branch 1 taken 457 times.
457 outFile.close();
483 457 return true;
484 ✗ } catch (...) {
485 ✗ LOG(Error, "Unable to write file to path '" << toString(wp) << "'.");
486 ✗ return false;
487 ✗ }
488 }
489 457 }
490
491 ✗ LOG(Error, "Unable to write file to path '" << toString(wp) << "', because parent directory " << "could not be created.");
492 ✗ return false;
493 459 }
494
495 // PRIVATE
496
497 // SERIALIZATION
498
499 2852 bool IdfFile::m_load(std::istream& is, ProgressBar* progressBar, bool versionOnly) {
500
501 2852 [[maybe_unused]] int lineNum = 0; // Idf line number
502 2852 int objectNum = 0; // number of objects, first is #1
503 2852 std::string line; // temp string to help with reading
504
1/1
✓ Branch 1 taken 2852 times.
2852 boost::smatch matches; // matches to regular expressions
505 2852 std::string comment; // keep running comment
506 2852 bool firstBlock = true; // to capture first comment block as the header
507
508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2852 times.
2852 if (progressBar) {
509 ✗ is.seekg(0, std::ios_base::end);
510 ✗ int streamsize = static_cast<int>(is.tellg());
511 ✗ progressBar->setMinimum(0);
512 ✗ progressBar->setMaximum(streamsize);
513 ✗ is.seekg(0, std::ios_base::beg);
514 }
515
516 // Use a boost filter to make sure that no matter what line endings come in,
517 // they are converted to what is expected by the current os
518
1/1
✓ Branch 1 taken 2852 times.
2852 boost::iostreams::filtering_istream filt;
519
520 //#ifdef _POSIX_VERSION
521
2/2
✓ Branch 1 taken 2852 times.
✓ Branch 4 taken 2852 times.
2852 filt.push(boost::iostreams::newline_filter(boost::iostreams::newline::posix));
522 //#else
523 // filt.push(boost::iostreams::newline_filter(boost::iostreams::newline::dos));
524 //#endif
525
1/1
✓ Branch 1 taken 2852 times.
2852 filt.push(is);
526
527 // read the file line by line using regexes
528
4/4
✓ Branch 1 taken 2207489 times.
✓ Branch 4 taken 2207489 times.
✓ Branch 6 taken 2204814 times.
✓ Branch 7 taken 2675 times.
2207489 while (std::getline(filt, line)) {
529
530
2/3
✓ Branch 1 taken 2204814 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2204814 times.
2204814 if (line == "\r") {
531 // This is not a real line at all, just the vestiges from the
532 // previous windows formatted line being parsed on unix, skip it,
533 // don't even count it
534 ✗ continue;
535 }
536
537 2204814 ++lineNum;
538
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2204814 times.
2204814 if (progressBar) {
540 ✗ int current = static_cast<int>(is.tellg());
541 ✗ progressBar->setValue(current);
542 }
543
544
4/4
✓ Branch 1 taken 2204814 times.
✓ Branch 4 taken 2204814 times.
✓ Branch 6 taken 35130 times.
✓ Branch 7 taken 2169684 times.
2204814 if (boost::regex_match(line, idfRegex::commentOnlyLine())) {
545 // continue comment
546
3/3
✓ Branch 1 taken 35130 times.
✓ Branch 4 taken 35130 times.
✓ Branch 7 taken 35130 times.
35130 comment += (line + idfRegex::newLinestring());
547
4/4
✓ Branch 1 taken 2169684 times.
✓ Branch 4 taken 2169684 times.
✓ Branch 6 taken 1089227 times.
✓ Branch 7 taken 1080457 times.
2169684 } else if (boost::regex_match(line, commentRegex::whitespaceOnlyLine())) {
548 // end comment
549
1/1
✓ Branch 2 taken 1089227 times.
1089227 boost::trim(comment);
550
551
2/2
✓ Branch 1 taken 5102 times.
✓ Branch 2 taken 1084125 times.
1089227 if (!comment.empty()) {
552
2/2
✓ Branch 0 taken 709 times.
✓ Branch 1 taken 4393 times.
5102 if (firstBlock) {
553 // set this comment as the header
554
1/1
✓ Branch 1 taken 709 times.
709 setHeader(comment);
555 709 firstBlock = false;
556 } else {
557
1/2
✓ Branch 0 taken 4393 times.
✗ Branch 1 not taken.
4393 if (!versionOnly) {
558
559 // make a comment only object to hold the comment
560
2/2
✓ Branch 1 taken 4393 times.
✓ Branch 4 taken 4393 times.
4393 OptionalIddObject commentOnlyIddObject = m_iddFileAndFactoryWrapper.getObject(IddObjectType::CommentOnly);
561
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4393 times.
4393 if (!commentOnlyIddObject) {
562 ✗ LOG(Error, "IddFile does not contain a CommentOnly object. Will not be able to save comment objects.");
563 ✗ continue;
564 ✗ }
565
566 4393 OptionalIdfObject commentOnlyObject;
567
6/6
✓ Branch 1 taken 4393 times.
✓ Branch 4 taken 4393 times.
✓ Branch 7 taken 4393 times.
✓ Branch 10 taken 4393 times.
✓ Branch 13 taken 4393 times.
✓ Branch 16 taken 4393 times.
4393 commentOnlyObject = IdfObject::load(commentOnlyIddObject->name() + ";" + comment, *commentOnlyIddObject);
568
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 4393 times.
✗ Branch 4 not taken.
4393 OS_ASSERT(commentOnlyObject);
569
570 // put it in the object list
571
2/2
✓ Branch 1 taken 4393 times.
✓ Branch 4 taken 4393 times.
4393 addObject(*commentOnlyObject);
572 4393 }
573 }
574 }
575
576 //clear out comment
577
1/1
✓ Branch 1 taken 1089227 times.
1089227 comment = "";
578
579 } else {
580
581 1080457 bool foundEndLine(false);
582 1080457 firstBlock = false;
583 1080457 bool isVersion = false;
584
585 // peek at the object type and name for indexing in map
586 1080457 std::string objectType;
587
588
4/4
✓ Branch 1 taken 1080457 times.
✓ Branch 4 taken 1080457 times.
✓ Branch 6 taken 1080453 times.
✓ Branch 7 taken 4 times.
1080457 if (boost::regex_search(line, matches, idfRegex::line())) {
589
3/3
✓ Branch 1 taken 1080453 times.
✓ Branch 4 taken 1080453 times.
✓ Branch 7 taken 1080453 times.
1080453 objectType = std::string(matches[1].first, matches[1].second);
590
1/1
✓ Branch 2 taken 1080453 times.
1080453 boost::trim(objectType);
591 } else {
592 // can't figure out the object's type
593
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!versionOnly) {
594
7/7
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
✓ Branch 10 taken 4 times.
✓ Branch 15 taken 4 times.
✓ Branch 18 taken 4 times.
✓ Branch 21 taken 4 times.
4 LOG(Warn, "Unrecognizable object type '" + line + "'. Defaulting to 'Catchall'.");
595 }
596
1/1
✓ Branch 1 taken 4 times.
4 objectType = "Catchall";
597 }
598
4/4
✓ Branch 1 taken 1080457 times.
✓ Branch 4 taken 1080457 times.
✓ Branch 6 taken 2818 times.
✓ Branch 7 taken 1077639 times.
1080457 if (boost::regex_match(objectType, iddRegex::versionObjectName())) {
599 2818 isVersion = true;
600 }
601
602 // get the corresponding idd object entry
603
604
1/1
✓ Branch 1 taken 1080457 times.
1080457 OptionalIddObject iddObject = m_iddFileAndFactoryWrapper.getObject(objectType);
605
2/2
✓ Branch 1 taken 186 times.
✓ Branch 2 taken 1080271 times.
1080457 if (!iddObject) {
606
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 181 times.
186 if (!versionOnly) {
607
7/7
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
✓ Branch 10 taken 5 times.
✓ Branch 15 taken 5 times.
✓ Branch 18 taken 5 times.
✓ Branch 21 taken 5 times.
5 LOG(Warn, "Cannot find object type '" + objectType + "' in Idd. Placing data in Catchall object.");
608 }
609
2/2
✓ Branch 1 taken 186 times.
✓ Branch 4 taken 186 times.
186 iddObject = IddObject();
610
1/1
✓ Branch 1 taken 186 times.
186 objectType = "Catchall";
611 } else {
612
4/6
✓ Branch 1 taken 1080271 times.
✓ Branch 4 taken 1080271 times.
✓ Branch 7 taken 1080271 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 1080271 times.
✗ Branch 13 not taken.
1080271 OS_ASSERT(iddObject->type() != IddObjectType::Catchall);
613 }
614
615 // put the text for this object in a new string with a newline
616
5/5
✓ Branch 1 taken 1080457 times.
✓ Branch 4 taken 1080457 times.
✓ Branch 7 taken 1080457 times.
✓ Branch 10 taken 1080457 times.
✓ Branch 13 taken 1080457 times.
1080457 std::string text(comment + idfRegex::newLinestring() + line + idfRegex::newLinestring());
617
1/1
✓ Branch 1 taken 1080457 times.
1080457 comment = "";
618
619 // check if this line also matches closing line object
620
4/4
✓ Branch 1 taken 1080457 times.
✓ Branch 4 taken 1080457 times.
✓ Branch 6 taken 11343 times.
✓ Branch 7 taken 1069114 times.
1080457 if (boost::regex_match(line, idfRegex::objectEnd())) {
621 11343 foundEndLine = true;
622 }
623
624 // continue reading until we have seen the entire object
625 // last line will be thrown away, requires empty line between objects in Idf
626
7/8
✓ Branch 0 taken 13439089 times.
✓ Branch 1 taken 1080457 times.
✓ Branch 3 taken 13439089 times.
✓ Branch 6 taken 13439089 times.
✓ Branch 8 taken 13439089 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 13439089 times.
✓ Branch 11 taken 1080457 times.
14519546 while ((!foundEndLine) && (std::getline(filt, line))) {
627 13439089 ++lineNum;
628
629 // add line to text, include newline separator
630
3/3
✓ Branch 1 taken 13439089 times.
✓ Branch 4 taken 13439089 times.
✓ Branch 7 taken 13439089 times.
13439089 text += (line + idfRegex::newLinestring());
631
632 // check if we have found the last field
633
4/4
✓ Branch 1 taken 13439089 times.
✓ Branch 4 taken 13439089 times.
✓ Branch 6 taken 1069114 times.
✓ Branch 7 taken 12369975 times.
13439089 if (boost::regex_match(line, idfRegex::objectEnd())) {
634 1069114 foundEndLine = true;
635 }
636 }
637
638 // construct the object
639
5/6
✓ Branch 0 taken 1080457 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 181 times.
✓ Branch 3 taken 1080276 times.
✓ Branch 4 taken 177 times.
✓ Branch 5 taken 4 times.
1080457 if (foundEndLine && (!versionOnly || isVersion)) {
640
2/2
✓ Branch 1 taken 1080453 times.
✓ Branch 4 taken 1080453 times.
1080453 OptionalIdfObject object = IdfObject::load(text, *iddObject);
641
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1080453 times.
1080453 if (!object) {
642 ✗ LOG(Error, "Unable to construct IdfObject from text: " << '\n'
643 << text << '\n'
644 << "Throwing this object out and parsing the remainder of the file.");
645 ✗ continue;
646 ✗ } else {
647 // a valid Idf object to parse
648
6/6
✓ Branch 1 taken 1080453 times.
✓ Branch 4 taken 1080453 times.
✓ Branch 7 taken 1080453 times.
✓ Branch 10 taken 1080453 times.
✓ Branch 14 taken 1080271 times.
✓ Branch 15 taken 182 times.
1080453 if (object->iddObject().type() != IddObjectType::Catchall) {
649 1080271 ++objectNum;
650 }
651
652 // put it in the object list
653
2/2
✓ Branch 1 taken 1080453 times.
✓ Branch 4 taken 1080453 times.
1080453 addObject(*object);
654 }
655 1080453 }
656
657
4/4
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 1080276 times.
✓ Branch 2 taken 177 times.
✓ Branch 3 taken 4 times.
1080457 if (versionOnly && isVersion) {
658 // Increment objectNum to avoid triggering the warning below and return false
659 177 ++objectNum;
660 177 break;
661 }
662 1080811 }
663 }
664
665 // If we sucessfully parsed at least one object, we return true, otherwise false
666
2/2
✓ Branch 0 taken 2849 times.
✓ Branch 1 taken 3 times.
2852 if (objectNum > 0) {
667 2849 return true;
668 } else {
669
5/5
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
3 LOG(Error, "Could not parse a single valid object in file.");
670 3 return false;
671 }
672 2852 }
673
674 411 IddFileAndFactoryWrapper IdfFile::iddFileAndFactoryWrapper() const {
675 411 return m_iddFileAndFactoryWrapper;
676 }
677
678 675 void IdfFile::setIddFileAndFactoryWrapper(const IddFileAndFactoryWrapper& iddFileAndFactoryWrapper) {
679 675 m_iddFileAndFactoryWrapper = iddFileAndFactoryWrapper;
680 675 }
681
682 // PRIVATE
683
684 // SETTERS
685
686 10606 void IdfFile::addVersionObject() {
687
1/1
✓ Branch 1 taken 10606 times.
10606 OptionalIddObject versionIdd = m_iddFileAndFactoryWrapper.versionObject();
688
2/2
✓ Branch 1 taken 179 times.
✓ Branch 2 taken 10427 times.
10606 if (!versionIdd) {
689 179 bool printWarn(true);
690
10/22
✓ Branch 1 taken 179 times.
✓ Branch 4 taken 179 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 179 times.
✓ Branch 10 taken 179 times.
✓ Branch 14 taken 179 times.
✓ Branch 16 taken 179 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 179 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 179 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 179 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 179 times.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
179 if ((m_iddFileAndFactoryWrapper.objects().size() == 1u) && (m_iddFileAndFactoryWrapper.objects()[0] == IddObject())) {
691 179 printWarn = false;
692 }
693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 179 times.
179 if (printWarn) {
694 ✗ LOG(Warn, "Unable to add Version Idf object, because could not identify a Version " << "Idd object.");
695 }
696 179 return;
697 }
698
2/2
✓ Branch 1 taken 7786 times.
✓ Branch 2 taken 2641 times.
10427 if (m_versionObjectIndices.empty()) {
699
2/2
✓ Branch 1 taken 7786 times.
✓ Branch 4 taken 7786 times.
7786 IdfObject versionObject(*versionIdd);
700 // look for field named "Version Identifier"
701
3/3
✓ Branch 1 taken 7786 times.
✓ Branch 4 taken 7786 times.
✓ Branch 7 taken 7786 times.
23358 OptionalInt index = versionObject.iddObject().getFieldIndex("Version Identifier");
702
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7786 times.
7786 if (!index) {
703 ✗ LOG(Warn, "No 'Version Identifier' field found in the Version IddObject.");
704 } else {
705
4/4
✓ Branch 1 taken 7786 times.
✓ Branch 4 taken 7786 times.
✓ Branch 7 taken 7786 times.
✓ Branch 10 taken 7786 times.
7786 versionObject.setString(*index, version().str());
706 }
707
1/1
✓ Branch 1 taken 7786 times.
7786 addObject(versionObject);
708 7786 }
709 10606 }
710
711 // QUERIES
712
713 ✗ IdfObjectVector IdfFile::m_objectsWithConflictingNames(const std::string& name, bool getAll) const {
714
715 ✗ IdfObjectVector result;
716 ✗ IdfObjectVector candidates = getObjectsByName(name); // uses iequals
717
718 ✗ unsigned n = candidates.size();
719 ✗ if (n < 2) {
720 ✗ return result;
721 } // no potential conflicts
722
723 ✗ BoolVector hasConflict(candidates.size(), false);
724
725 // main purpose of iteration is to determine if candidates[i] has conflict
726 ✗ for (unsigned i = 0; i < n; ++i) {
727 ✗ if (hasConflict[i]) {
728 ✗ continue;
729 }
730
731 // IddObjectTypes must be unique. find all conflicts with type since inexpensive.
732 ✗ IddObjectType type = candidates[i].iddObject().type();
733 ✗ for (unsigned j = 0; j < n; ++j) {
734 ✗ if (j == i) {
735 ✗ continue;
736 }
737 ✗ if (candidates[j].iddObject().type() == type) {
738 ✗ hasConflict[i] = true;
739 ✗ hasConflict[j] = true;
740 }
741 }
742
743 ✗ if (hasConflict[i]) {
744 ✗ if (getAll) {
745 ✗ continue;
746 } else {
747 ✗ break;
748 }
749 }
750
751 // Reference List Names cannot overlap
752 ✗ StringVector refs = candidates[i].iddObject().references();
753 ✗ for (unsigned j = 0; j < n; ++j) {
754 ✗ if (j == i) {
755 ✗ continue;
756 }
757 ✗ for (const std::string& ref : candidates[j].iddObject().references()) {
758 ✗ if (std::find_if(refs.begin(), refs.end(), [&ref](const auto& r) { return openstudio::istringEqual(r, ref); }) != refs.end()) {
759 ✗ hasConflict[i] = true;
760 ✗ hasConflict[j] = true;
761 ✗ break;
762 }
763 ✗ }
764 ✗ if (hasConflict[i]) {
765 ✗ break;
766 }
767 }
768
769 ✗ if (hasConflict[i] && !getAll) {
770 ✗ break;
771 }
772 ✗ }
773
774 ✗ for (unsigned i = 0; i < n; ++i) {
775 ✗ if (hasConflict[i]) {
776 ✗ result.push_back(candidates[i]);
777 }
778 }
779
780 ✗ return result;
781 ✗ }
782
783 // NON-MEMBER FUNCTIONS
784
785 39 std::ostream& operator<<(std::ostream& os, const IdfFile& idfFile) {
786 39 return idfFile.print(os);
787 }
788
789 } // namespace openstudio
790