GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 56.6% 94 / 0 / 166
Functions: 50.0% 7 / 0 / 14
Branches: 49.6% 124 / 0 / 250

src/utilities/idf/ImfFile.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 "ImfFile.hpp"
7 #include "IdfRegex.hpp"
8 #include "../idd/CommentRegex.hpp"
9 #include <utilities/idd/IddFactory.hxx>
10 #include <utilities/idd/IddEnums.hxx>
11
12 #include "../core/Assert.hpp"
13 #include "../core/PathHelpers.hpp"
14
15 #include <boost/iostreams/filter/newline.hpp>
16 #include <boost/iostreams/filtering_stream.hpp>
17
18 namespace openstudio {
19
20 // CONSTRUCTORS
21
22
1/1
✓ Branch 2 taken 827 times.
827 ImfFile::ImfFile(IddFileType iddFileType) : m_iddFileAndFactoryWrapper(iddFileType) {}
23
24 // GETTERS
25
26 ✗ IddFile ImfFile::iddFile() const {
27 ✗ return m_iddFileAndFactoryWrapper.iddFile();
28 }
29
30 ✗ IddFileType ImfFile::iddFileType() const {
31 ✗ return m_iddFileAndFactoryWrapper.iddFileType();
32 }
33
34 1 StringVector ImfFile::sectionNames() const {
35 1 StringVector result;
36
2/2
✓ Branch 5 taken 30 times.
✓ Branch 6 taken 1 time.
31 for (const SectionMapType::value_type& pair : m_sectionMap) {
37
1/1
✓ Branch 1 taken 30 times.
30 result.push_back(pair.first);
38 }
39 1 return result;
40 ✗ }
41
42 30 IdfObjectVector ImfFile::section(const std::string& section) const {
43 30 IdfObjectVector result;
44
1/1
✓ Branch 1 taken 30 times.
30 auto it = m_sectionMap.find(section);
45
1/2
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 if (it != m_sectionMap.end()) {
46
1/1
✓ Branch 2 taken 30 times.
30 result = it->second;
47 }
48 60 return result;
49 ✗ }
50
51 // QUERIES
52
53 116 unsigned ImfFile::numSections() const {
54 116 return m_sectionMap.size();
55 }
56
57 // SERIALIZATION
58
59 115 OptionalImfFile ImfFile::load(std::istream& is, IddFileType iddFileType) {
60 115 OptionalImfFile result;
61
2/2
✓ Branch 1 taken 115 times.
✓ Branch 4 taken 115 times.
115 ImfFile temp{IddFileType::EnergyPlus};
62
1/1
✓ Branch 1 taken 115 times.
115 temp.m_iddFileAndFactoryWrapper.setIddFile(iddFileType);
63
1/1
✓ Branch 1 taken 115 times.
115 bool success = temp.m_load(is);
64 // cppcheck-suppress knownConditionTrueFalse
65
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 if (success) {
66
1/1
✓ Branch 1 taken 115 times.
115 result = temp;
67 }
68 230 return result;
69 115 }
70
71 ✗ OptionalImfFile ImfFile::load(std::istream& is, const IddFile& iddFile) {
72 ✗ OptionalImfFile result;
73 ✗ ImfFile temp{IddFileType::EnergyPlus};
74 ✗ temp.m_iddFileAndFactoryWrapper.setIddFile(iddFile);
75 ✗ bool success = temp.m_load(is);
76 // cppcheck-suppress knownConditionTrueFalse
77 ✗ if (success) {
78 ✗ result = temp;
79 }
80 ✗ return result;
81 ✗ }
82
83 115 OptionalImfFile ImfFile::load(const openstudio::path& p, IddFileType iddFileType) {
84 // complete path
85
2/2
✓ Branch 1 taken 115 times.
✓ Branch 5 taken 115 times.
115 path wp = completePathToFile(p, path(), "imf", true);
86
87 // try to open file and parse
88
1/1
✓ Branch 1 taken 115 times.
115 openstudio::filesystem::ifstream inFile(wp);
89
2/3
✓ Branch 1 taken 115 times.
✓ Branch 3 taken 115 times.
✗ Branch 4 not taken.
115 if (inFile) {
90 try {
91
1/1
✓ Branch 1 taken 115 times.
115 return load(inFile, iddFileType);
92 ✗ } catch (...) {
93 ✗ return boost::none;
94 ✗ }
95 }
96
97 ✗ return boost::none;
98 115 }
99
100 ✗ OptionalImfFile ImfFile::load(const openstudio::path& p, const IddFile& iddFile) {
101 // complete path
102 ✗ path wp = completePathToFile(p, path(), "imf", true);
103
104 // try to open file and parse
105 ✗ openstudio::filesystem::ifstream inFile(wp);
106 ✗ if (inFile) {
107 try {
108 ✗ return load(inFile, iddFile);
109 ✗ } catch (...) {
110 ✗ return boost::none;
111 ✗ }
112 }
113
114 ✗ return boost::none;
115 ✗ }
116
117 ✗ std::ostream& ImfFile::print(std::ostream& os) const {
118 ✗ for (const SectionMapType::value_type& pair : m_sectionMap) {
119 ✗ os << "##def " << pair.first << "[]" << '\n';
120 ✗ for (const IdfObject& object : pair.second) {
121 ✗ os << object;
122 }
123 ✗ os << "##enddef " << pair.first << '\n' << '\n';
124 }
125 ✗ return os;
126 }
127
128 ✗ bool ImfFile::save(const openstudio::path& p, bool overwrite) {
129
130 ✗ path wp = completePathToFile(p, path(), "imf", true);
131 ✗ if (!wp.empty() && (overwrite == false)) {
132 ✗ LOG(Info, "ImfFile save method failed because instructed not to overwrite path '" << toString(p) << "'.");
133 ✗ return false;
134 }
135 ✗ if (makeParentFolder(p)) {
136 ✗ openstudio::filesystem::ofstream outFile(p);
137 ✗ if (outFile) {
138 try {
139 ✗ print(outFile);
140 ✗ return true;
141 ✗ } catch (...) {
142 ✗ LOG(Error, "Unable to write ImfFile to path '" << toString(p) << "'.");
143 ✗ return false;
144 ✗ }
145 }
146 ✗ }
147
148 ✗ LOG(Error, "Unable to write ImfFile to path '" << toString(p) << "', because parent directory could not be created.");
149 ✗ return false;
150 ✗ }
151
152 // PRIVATE
153
154 115 bool ImfFile::m_load(std::istream& is) {
155
156 // keep track of line number in the Idf
157 115 [[maybe_unused]] int lineNum = 0;
158
159 // number of object in the Idf, 1 is first object
160 115 [[maybe_unused]] int objectNum = 0;
161
162 // temp string to read file
163 115 std::string line;
164
165 // this will contain matches to regular expressions
166
1/1
✓ Branch 1 taken 115 times.
115 boost::smatch matches;
167
168 // keep running comment
169 115 std::string comment;
170
171 // keep track of the current section
172 115 std::string section;
173
174 // get CommentOnly IddObject
175 115 OptionalIddObject commentOnlyIddObject;
176
4/4
✓ Branch 1 taken 115 times.
✓ Branch 4 taken 115 times.
✓ Branch 7 taken 115 times.
✓ Branch 10 taken 115 times.
115 commentOnlyIddObject = IddFactory::instance().getObject(IddObjectType::CommentOnly);
177
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✗ Branch 4 not taken.
115 OS_ASSERT(commentOnlyIddObject);
178
179 // Use a boost filter to make sure that no matter what line endings come in,
180 // they are converted to what is expected by the current os
181
1/1
✓ Branch 1 taken 115 times.
115 boost::iostreams::filtering_istream filt;
182
183 //#ifdef _POSIX_VERSION
184
2/2
✓ Branch 1 taken 115 times.
✓ Branch 4 taken 115 times.
115 filt.push(boost::iostreams::newline_filter(boost::iostreams::newline::posix));
185 //#else
186 // filt.push(boost::iostreams::newline_filter(boost::iostreams::newline::dos));
187 //#endif
188
1/1
✓ Branch 1 taken 115 times.
115 filt.push(is);
189
190 // read the rest of the file line by line
191 // todo, do this by regex
192
4/4
✓ Branch 1 taken 362365 times.
✓ Branch 4 taken 362365 times.
✓ Branch 6 taken 362250 times.
✓ Branch 7 taken 115 times.
362365 while (getline(filt, line)) {
193 362250 ++lineNum;
194
195
4/4
✓ Branch 1 taken 362250 times.
✓ Branch 4 taken 362250 times.
✓ Branch 6 taken 47150 times.
✓ Branch 7 taken 315100 times.
362250 if (boost::regex_match(line, idfRegex::commentOnlyLine())) {
196 // continue comment
197
3/3
✓ Branch 1 taken 47150 times.
✓ Branch 4 taken 47150 times.
✓ Branch 7 taken 47150 times.
47150 comment += (line + idfRegex::newLinestring());
198 47150 continue;
199
200
4/4
✓ Branch 1 taken 315100 times.
✓ Branch 4 taken 315100 times.
✓ Branch 6 taken 169510 times.
✓ Branch 7 taken 145590 times.
315100 } else if (boost::regex_match(line, commentRegex::whitespaceOnlyLine())) {
201
202 // end comment
203
1/1
✓ Branch 2 taken 169510 times.
169510 boost::trim(comment);
204
205
2/2
✓ Branch 1 taken 26450 times.
✓ Branch 2 taken 143060 times.
169510 if (!comment.empty()) {
206 // make a comment only object to hold the comment
207 26450 OptionalIdfObject commentOnlyObject;
208
6/6
✓ Branch 1 taken 26450 times.
✓ Branch 4 taken 26450 times.
✓ Branch 7 taken 26450 times.
✓ Branch 10 taken 26450 times.
✓ Branch 13 taken 26450 times.
✓ Branch 16 taken 26450 times.
26450 commentOnlyObject = IdfObject::load(commentOnlyIddObject->name() + ";" + comment, *commentOnlyIddObject);
209
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 26450 times.
✗ Branch 4 not taken.
26450 OS_ASSERT(commentOnlyObject);
210
211 // put it in the object list
212
3/3
✓ Branch 1 taken 26450 times.
✓ Branch 4 taken 26450 times.
✓ Branch 7 taken 26450 times.
26450 m_sectionMap[section].push_back(*commentOnlyObject);
213 26450 }
214
215 //clear out comment
216
1/1
✓ Branch 1 taken 169510 times.
169510 comment = "";
217
218 169510 continue;
219
220
4/4
✓ Branch 1 taken 145590 times.
✓ Branch 4 taken 145590 times.
✓ Branch 6 taken 3450 times.
✓ Branch 7 taken 142140 times.
315100 } else if (boost::regex_search(line, matches, idfRegex::imfSection())) {
221
222 // end comment
223
1/1
✓ Branch 2 taken 3450 times.
3450 boost::trim(comment);
224
225
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3450 times.
3450 if (!comment.empty()) {
226 // make a comment only object to hold the comment
227 ✗ OptionalIdfObject commentOnlyObject;
228 ✗ commentOnlyObject = IdfObject::load(commentOnlyIddObject->name() + ";" + comment, *commentOnlyIddObject);
229 ✗ OS_ASSERT(commentOnlyObject);
230
231 // put it in the object list
232 ✗ m_sectionMap[section].push_back(*commentOnlyObject);
233 ✗ }
234
235 //clear out comment
236
1/1
✓ Branch 1 taken 3450 times.
3450 comment = "";
237
238 // get the new imf section
239
3/3
✓ Branch 1 taken 3450 times.
✓ Branch 4 taken 3450 times.
✓ Branch 7 taken 3450 times.
3450 section = std::string(matches[1].first, matches[1].second);
240
1/1
✓ Branch 2 taken 3450 times.
3450 boost::trim(section);
241
242
4/4
✓ Branch 1 taken 142140 times.
✓ Branch 4 taken 142140 times.
✓ Branch 6 taken 3450 times.
✓ Branch 7 taken 138690 times.
142140 } else if (boost::regex_search(line, matches, idfRegex::imfSectionEnd())) {
243 // end comment
244
1/1
✓ Branch 2 taken 3450 times.
3450 boost::trim(comment);
245
246
2/2
✓ Branch 1 taken 345 times.
✓ Branch 2 taken 3105 times.
3450 if (!comment.empty()) {
247 // make a comment only object to hold the comment
248 345 OptionalIdfObject commentOnlyObject;
249
6/6
✓ Branch 1 taken 345 times.
✓ Branch 4 taken 345 times.
✓ Branch 7 taken 345 times.
✓ Branch 10 taken 345 times.
✓ Branch 13 taken 345 times.
✓ Branch 16 taken 345 times.
345 commentOnlyObject = IdfObject::load(commentOnlyIddObject->name() + ";" + comment, *commentOnlyIddObject);
250
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 345 times.
✗ Branch 4 not taken.
345 OS_ASSERT(commentOnlyObject);
251
252 // put it in the object list
253
3/3
✓ Branch 1 taken 345 times.
✓ Branch 4 taken 345 times.
✓ Branch 7 taken 345 times.
345 m_sectionMap[section].push_back(*commentOnlyObject);
254 345 }
255
256 //clear out comment
257
1/1
✓ Branch 1 taken 3450 times.
3450 comment = "";
258 } else { // IdfObject
259
260 138690 bool foundEndLine(false);
261
262 // a valid Idf object to parse
263 138690 ++objectNum;
264
265 // peek at the object type and name for indexing in map
266 138690 std::string objectType;
267 // std::string objName;
268 // bool objectHasName(false);
269
270
3/4
✓ Branch 1 taken 138690 times.
✓ Branch 4 taken 138690 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 138690 times.
138690 if (boost::regex_search(line, matches, idfRegex::objectTypeAndName())) {
271 ✗ objectType = std::string(matches[1].first, matches[1].second);
272 ✗ boost::trim(objectType);
273 // objName = std::string(matches[2].first, matches[2].second);
274 // boost::trim(objName);
275
3/4
✓ Branch 1 taken 138690 times.
✓ Branch 4 taken 138690 times.
✓ Branch 6 taken 138690 times.
✗ Branch 7 not taken.
138690 } else if (boost::regex_search(line, matches, idfRegex::line())) {
276 // doesn't match name, just type
277
3/3
✓ Branch 1 taken 138690 times.
✓ Branch 4 taken 138690 times.
✓ Branch 7 taken 138690 times.
138690 objectType = std::string(matches[1].first, matches[1].second);
278
1/1
✓ Branch 2 taken 138690 times.
138690 boost::trim(objectType);
279 } else {
280 // can't figure out the object's type
281 ✗ LOG(Warn, "Unrecognizable object type '" + line + "'. Defaulting to 'Catchall'.");
282 ✗ objectType = "Catchall";
283 }
284
285 // get the corresponding idd object entry
286
1/1
✓ Branch 1 taken 138690 times.
138690 OptionalIddObject iddObject = m_iddFileAndFactoryWrapper.getObject(objectType);
287
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 138690 times.
138690 if (!iddObject) {
288 ✗ LOG(Warn, "Cannot find object type '" + objectType + "' in Idd. Placing data in Catchall object.");
289 ✗ iddObject = IddObject();
290 ✗ objectType = "Catchall";
291 }
292
293 // put the text for this object in a new string with a newline
294
2/2
✓ Branch 1 taken 138690 times.
✓ Branch 4 taken 138690 times.
138690 std::string text(line + idfRegex::newLinestring());
295
296 // check if this line also matches closing line object
297
3/4
✓ Branch 1 taken 138690 times.
✓ Branch 4 taken 138690 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 138690 times.
138690 if (boost::regex_match(line, idfRegex::objectEnd())) {
298 ✗ foundEndLine = true;
299 }
300
301 // continue reading until we have seen the entire object
302 // last line will be thrown away, requires empty line between objects in Idf
303
7/8
✓ Branch 0 taken 2605210 times.
✓ Branch 1 taken 138690 times.
✓ Branch 3 taken 2605210 times.
✓ Branch 6 taken 2605210 times.
✓ Branch 8 taken 2605210 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2605210 times.
✓ Branch 11 taken 138690 times.
2743900 while ((!foundEndLine) && (std::getline(filt, line))) {
304 2605210 ++lineNum;
305
306 // add line to text, include newline separator
307
3/3
✓ Branch 1 taken 2605210 times.
✓ Branch 4 taken 2605210 times.
✓ Branch 7 taken 2605210 times.
2605210 text += (line + idfRegex::newLinestring());
308
309 // check if we have found the last field
310
4/4
✓ Branch 1 taken 2605210 times.
✓ Branch 4 taken 2605210 times.
✓ Branch 6 taken 138690 times.
✓ Branch 7 taken 2466520 times.
2605210 if (boost::regex_match(line, idfRegex::objectEnd())) {
311 138690 foundEndLine = true;
312 }
313 }
314
315 // construct the object
316
2/2
✓ Branch 1 taken 138690 times.
✓ Branch 4 taken 138690 times.
138690 OptionalIdfObject object = IdfObject::load(text, *iddObject);
317
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 138690 times.
138690 if (!object) {
318 ✗ LOG(Error, "Unable to construct IdfObject from text: " << '\n'
319 << text << '\n'
320 << "Throwing this object out and parsing the remainder of the file.");
321 ✗ continue;
322 ✗ }
323
324 // put it in the object list
325
3/3
✓ Branch 1 taken 138690 times.
✓ Branch 4 taken 138690 times.
✓ Branch 7 taken 138690 times.
138690 m_sectionMap[section].push_back(*object);
326 138690 }
327 }
328
329 115 return true;
330 115 }
331
332 ✗ std::ostream& operator<<(std::ostream& os, const ImfFile& imfFile) {
333 ✗ return imfFile.print(os);
334 }
335
336 } // namespace openstudio
337