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 4227 times.
|
4227 | IdfFile::IdfFile(IddFileType iddFileType) : m_iddFileAndFactoryWrapper(iddFileType) { |
| 29 |
1/1✓ Branch 1 taken 4227 times.
|
4227 | addVersionObject(); |
| 30 | 4227 | } | |
| 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 | 9618 | VersionString IdfFile::version() const { | |
| 43 |
2/2✓ Branch 1 taken 9618 times.
✓ Branch 4 taken 9618 times.
|
9618 | 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 | 5870 | boost::optional<IdfObject> IdfFile::versionObject() const { | |
| 55 | 5870 | OptionalIdfObject result; | |
| 56 |
2/2✓ Branch 1 taken 5689 times.
✓ Branch 2 taken 181 times.
|
5870 | if (m_versionObjectIndices.size() == 1u) { |
| 57 | 5689 | unsigned index = *(m_versionObjectIndices.begin()); | |
| 58 |
1/1✓ Branch 2 taken 5689 times.
|
5689 | result = m_objects[index]; |
| 59 | } | ||
| 60 | 5870 | 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 | 6568 | std::vector<IdfObject> IdfFile::objects() const { | |
| 86 | 6568 | IdfObjectVector result = m_objects; | |
| 87 |
2/2✓ Branch 3 taken 6568 times.
✓ Branch 4 taken 6568 times.
|
13136 | for (auto it = m_versionObjectIndices.rbegin(), itEnd = m_versionObjectIndices.rend(); it != itEnd; ++it) { |
| 88 | 6568 | auto oit = result.begin(); | |
| 89 |
2/2✓ Branch 2 taken 684 times.
✓ Branch 3 taken 6568 times.
|
7252 | for (unsigned i = 0; i < *it; ++i, ++oit) { |
| 90 | ; | ||
| 91 | } | ||
| 92 |
7/37✓ Branch 2 taken 6568 times.
✓ Branch 5 taken 6568 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6568 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 6568 times.
✗ Branch 50 not taken.
✗ Branch 52 not taken.
✓ Branch 53 taken 6568 times.
✗ Branch 55 not taken.
✓ Branch 56 taken 6568 times.
✓ Branch 58 taken 6568 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.
|
6568 | 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 6568 times.
|
6568 | result.erase(oit); |
| 96 | } | ||
| 97 | 6568 | 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 | 1376 | void IdfFile::setHeader(const std::string& header) { | |
| 134 |
1/1✓ Branch 1 taken 1376 times.
|
1376 | m_header = makeComment(header); |
| 135 | 1376 | } | |
| 136 | |||
| 137 | 1259078 | void IdfFile::addObject(const IdfObject& object) { | |
| 138 | 1259078 | m_objects.push_back(object); | |
| 139 |
4/4✓ Branch 1 taken 1259078 times.
✓ Branch 4 taken 1259078 times.
✓ Branch 7 taken 11079 times.
✓ Branch 8 taken 1247999 times.
|
1259078 | if (object.iddObject().isVersionObject()) { |
| 140 |
1/1✓ Branch 2 taken 11079 times.
|
11079 | m_versionObjectIndices.insert(m_objects.size() - 1); |
| 141 |
6/9✓ Branch 1 taken 1247999 times.
✓ Branch 4 taken 1247999 times.
✓ Branch 8 taken 182 times.
✓ Branch 10 taken 182 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 177 times.
✓ Branch 14 taken 1247822 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
2496180 | } else if ((object.iddObject().type() == IddObjectType::Catchall) && (object.numFields() > 0u) |
| 142 |
12/15✓ Branch 1 taken 1247999 times.
✓ Branch 3 taken 182 times.
✓ Branch 4 taken 1247817 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 1247817 times.
✓ Branch 22 taken 1247999 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
|
2496180 | && (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 | 1259078 | } | |
| 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 | 3337 | bool IdfFile::removeObject(const IdfObject& object) { | |
| 171 | auto it = | ||
| 172 |
2/2✓ Branch 1 taken 3337 times.
✓ Branch 6 taken 3337 times.
|
6674 | 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 3337 times.
✗ Branch 3 not taken.
|
3337 | if (it != m_objects.end()) { |
| 175 | 3337 | IdfObjectVector::size_type index(it - m_objects.begin()); | |
| 176 |
2/5✓ Branch 1 taken 3337 times.
✓ Branch 4 taken 3337 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
6674 | if (it->iddObject().isVersionObject() |
| 177 |
3/15✓ Branch 2 taken 3337 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3337 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 3337 times.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
|
10011 | || ((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 3337 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 3337 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
|
3337 | && (boost::regex_match(object.getString(0).get(), iddRegex::versionObjectName())))) { |
| 179 |
1/1✓ Branch 1 taken 3337 times.
|
3337 | m_versionObjectIndices.erase(index); |
| 180 | } | ||
| 181 |
1/1✓ Branch 2 taken 3337 times.
|
3337 | m_objects.erase(it); |
| 182 | 3337 | std::vector<IdfObjectVector::size_type> toModify; | |
| 183 |
1/2✗ Branch 5 not taken.
✓ Branch 6 taken 3337 times.
|
3337 | 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 3337 times.
|
3337 | for (auto i : toModify) { |
| 189 | ✗ | m_versionObjectIndices.erase(i); | |
| 190 | ✗ | m_versionObjectIndices.insert(i - 1); | |
| 191 | } | ||
| 192 | 3337 | return true; | |
| 193 | 3337 | } | |
| 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 | 903 | boost::optional<IdfFile> IdfFile::load(std::istream& is, const IddFileType& iddFileType, ProgressBar* progressBar) { | |
| 292 |
1/1✓ Branch 1 taken 903 times.
|
903 | IdfFile result(iddFileType); |
| 293 | // remove initial version object | ||
| 294 |
2/3✓ Branch 1 taken 903 times.
✓ Branch 3 taken 903 times.
✗ Branch 4 not taken.
|
1806 | if (OptionalIdfObject vo = result.versionObject()) { |
| 295 |
2/2✓ Branch 1 taken 903 times.
✓ Branch 4 taken 903 times.
|
903 | result.removeObject(*vo); |
| 296 | 903 | } | |
| 297 |
2/3✓ Branch 1 taken 903 times.
✓ Branch 3 taken 903 times.
✗ Branch 4 not taken.
|
903 | if (result.m_load(is, progressBar)) { |
| 298 | // check for it again here | ||
| 299 |
1/1✓ Branch 1 taken 903 times.
|
903 | result.addVersionObject(); |
| 300 |
1/1✓ Branch 1 taken 903 times.
|
903 | return result; |
| 301 | } | ||
| 302 | ✗ | return boost::none; | |
| 303 | 903 | } | |
| 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 97677 times.
✓ Branch 6 taken 499 times.
|
98176 | for (const IdfObject& object : m_objects) { |
| 435 |
1/1✓ Branch 1 taken 97677 times.
|
97677 | 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 | 2841 | bool IdfFile::m_load(std::istream& is, ProgressBar* progressBar, bool versionOnly) { | |
| 500 | |||
| 501 | 2841 | [[maybe_unused]] int lineNum = 0; // Idf line number | |
| 502 | 2841 | int objectNum = 0; // number of objects, first is #1 | |
| 503 | 2841 | std::string line; // temp string to help with reading | |
| 504 |
1/1✓ Branch 1 taken 2841 times.
|
2841 | boost::smatch matches; // matches to regular expressions |
| 505 | 2841 | std::string comment; // keep running comment | |
| 506 | 2841 | bool firstBlock = true; // to capture first comment block as the header | |
| 507 | |||
| 508 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2841 times.
|
2841 | 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 2841 times.
|
2841 | boost::iostreams::filtering_istream filt; |
| 519 | |||
| 520 | //#ifdef _POSIX_VERSION | ||
| 521 |
2/2✓ Branch 1 taken 2841 times.
✓ Branch 4 taken 2841 times.
|
2841 | 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 2841 times.
|
2841 | filt.push(is); |
| 526 | |||
| 527 | // read the file line by line using regexes | ||
| 528 |
4/4✓ Branch 1 taken 2205641 times.
✓ Branch 4 taken 2205641 times.
✓ Branch 6 taken 2202977 times.
✓ Branch 7 taken 2664 times.
|
2205641 | while (std::getline(filt, line)) { |
| 529 | |||
| 530 |
2/3✓ Branch 1 taken 2202977 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2202977 times.
|
2202977 | 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 | 2202977 | ++lineNum; | |
| 538 | |||
| 539 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2202977 times.
|
2202977 | if (progressBar) { |
| 540 | ✗ | int current = static_cast<int>(is.tellg()); | |
| 541 | ✗ | progressBar->setValue(current); | |
| 542 | } | ||
| 543 | |||
| 544 |
4/4✓ Branch 1 taken 2202977 times.
✓ Branch 4 taken 2202977 times.
✓ Branch 6 taken 34866 times.
✓ Branch 7 taken 2168111 times.
|
2202977 | if (boost::regex_match(line, idfRegex::commentOnlyLine())) { |
| 545 | // continue comment | ||
| 546 |
3/3✓ Branch 1 taken 34866 times.
✓ Branch 4 taken 34866 times.
✓ Branch 7 taken 34866 times.
|
34866 | comment += (line + idfRegex::newLinestring()); |
| 547 |
4/4✓ Branch 1 taken 2168111 times.
✓ Branch 4 taken 2168111 times.
✓ Branch 6 taken 1088435 times.
✓ Branch 7 taken 1079676 times.
|
2168111 | } else if (boost::regex_match(line, commentRegex::whitespaceOnlyLine())) { |
| 548 | // end comment | ||
| 549 |
1/1✓ Branch 2 taken 1088435 times.
|
1088435 | boost::trim(comment); |
| 550 | |||
| 551 |
2/2✓ Branch 1 taken 5091 times.
✓ Branch 2 taken 1083344 times.
|
1088435 | if (!comment.empty()) { |
| 552 |
2/2✓ Branch 0 taken 698 times.
✓ Branch 1 taken 4393 times.
|
5091 | if (firstBlock) { |
| 553 | // set this comment as the header | ||
| 554 |
1/1✓ Branch 1 taken 698 times.
|
698 | setHeader(comment); |
| 555 | 698 | 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 1088435 times.
|
1088435 | comment = ""; |
| 578 | |||
| 579 | } else { | ||
| 580 | |||
| 581 | 1079676 | bool foundEndLine(false); | |
| 582 | 1079676 | firstBlock = false; | |
| 583 | 1079676 | bool isVersion = false; | |
| 584 | |||
| 585 | // peek at the object type and name for indexing in map | ||
| 586 | 1079676 | std::string objectType; | |
| 587 | |||
| 588 |
4/4✓ Branch 1 taken 1079676 times.
✓ Branch 4 taken 1079676 times.
✓ Branch 6 taken 1079672 times.
✓ Branch 7 taken 4 times.
|
1079676 | if (boost::regex_search(line, matches, idfRegex::line())) { |
| 589 |
3/3✓ Branch 1 taken 1079672 times.
✓ Branch 4 taken 1079672 times.
✓ Branch 7 taken 1079672 times.
|
1079672 | objectType = std::string(matches[1].first, matches[1].second); |
| 590 |
1/1✓ Branch 2 taken 1079672 times.
|
1079672 | 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 1079676 times.
✓ Branch 4 taken 1079676 times.
✓ Branch 6 taken 2807 times.
✓ Branch 7 taken 1076869 times.
|
1079676 | if (boost::regex_match(objectType, iddRegex::versionObjectName())) { |
| 599 | 2807 | isVersion = true; | |
| 600 | } | ||
| 601 | |||
| 602 | // get the corresponding idd object entry | ||
| 603 | |||
| 604 |
1/1✓ Branch 1 taken 1079676 times.
|
1079676 | OptionalIddObject iddObject = m_iddFileAndFactoryWrapper.getObject(objectType); |
| 605 |
2/2✓ Branch 1 taken 186 times.
✓ Branch 2 taken 1079490 times.
|
1079676 | 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 1079490 times.
✓ Branch 4 taken 1079490 times.
✓ Branch 7 taken 1079490 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 1079490 times.
✗ Branch 13 not taken.
|
1079490 | 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 1079676 times.
✓ Branch 4 taken 1079676 times.
✓ Branch 7 taken 1079676 times.
✓ Branch 10 taken 1079676 times.
✓ Branch 13 taken 1079676 times.
|
1079676 | std::string text(comment + idfRegex::newLinestring() + line + idfRegex::newLinestring()); |
| 617 |
1/1✓ Branch 1 taken 1079676 times.
|
1079676 | comment = ""; |
| 618 | |||
| 619 | // check if this line also matches closing line object | ||
| 620 |
4/4✓ Branch 1 taken 1079676 times.
✓ Branch 4 taken 1079676 times.
✓ Branch 6 taken 11332 times.
✓ Branch 7 taken 1068344 times.
|
1079676 | if (boost::regex_match(line, idfRegex::objectEnd())) { |
| 621 | 11332 | 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 13412909 times.
✓ Branch 1 taken 1079676 times.
✓ Branch 3 taken 13412909 times.
✓ Branch 6 taken 13412909 times.
✓ Branch 8 taken 13412909 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 13412909 times.
✓ Branch 11 taken 1079676 times.
|
14492585 | while ((!foundEndLine) && (std::getline(filt, line))) { |
| 627 | 13412909 | ++lineNum; | |
| 628 | |||
| 629 | // add line to text, include newline separator | ||
| 630 |
3/3✓ Branch 1 taken 13412909 times.
✓ Branch 4 taken 13412909 times.
✓ Branch 7 taken 13412909 times.
|
13412909 | text += (line + idfRegex::newLinestring()); |
| 631 | |||
| 632 | // check if we have found the last field | ||
| 633 |
4/4✓ Branch 1 taken 13412909 times.
✓ Branch 4 taken 13412909 times.
✓ Branch 6 taken 1068344 times.
✓ Branch 7 taken 12344565 times.
|
13412909 | if (boost::regex_match(line, idfRegex::objectEnd())) { |
| 634 | 1068344 | foundEndLine = true; | |
| 635 | } | ||
| 636 | } | ||
| 637 | |||
| 638 | // construct the object | ||
| 639 |
5/6✓ Branch 0 taken 1079676 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 181 times.
✓ Branch 3 taken 1079495 times.
✓ Branch 4 taken 177 times.
✓ Branch 5 taken 4 times.
|
1079676 | if (foundEndLine && (!versionOnly || isVersion)) { |
| 640 |
2/2✓ Branch 1 taken 1079672 times.
✓ Branch 4 taken 1079672 times.
|
1079672 | OptionalIdfObject object = IdfObject::load(text, *iddObject); |
| 641 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1079672 times.
|
1079672 | 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 1079672 times.
✓ Branch 4 taken 1079672 times.
✓ Branch 7 taken 1079672 times.
✓ Branch 10 taken 1079672 times.
✓ Branch 14 taken 1079490 times.
✓ Branch 15 taken 182 times.
|
1079672 | if (object->iddObject().type() != IddObjectType::Catchall) { |
| 649 | 1079490 | ++objectNum; | |
| 650 | } | ||
| 651 | |||
| 652 | // put it in the object list | ||
| 653 |
2/2✓ Branch 1 taken 1079672 times.
✓ Branch 4 taken 1079672 times.
|
1079672 | addObject(*object); |
| 654 | } | ||
| 655 | 1079672 | } | |
| 656 | |||
| 657 |
4/4✓ Branch 0 taken 181 times.
✓ Branch 1 taken 1079495 times.
✓ Branch 2 taken 177 times.
✓ Branch 3 taken 4 times.
|
1079676 | if (versionOnly && isVersion) { |
| 658 | // Increment objectNum to avoid triggering the warning below and return false | ||
| 659 | 177 | ++objectNum; | |
| 660 | 177 | break; | |
| 661 | } | ||
| 662 | 1080030 | } | |
| 663 | } | ||
| 664 | |||
| 665 | // If we sucessfully parsed at least one object, we return true, otherwise false | ||
| 666 |
2/2✓ Branch 0 taken 2838 times.
✓ Branch 1 taken 3 times.
|
2841 | if (objectNum > 0) { |
| 667 | 2838 | 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 | 2841 | } | |
| 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 | 10583 | void IdfFile::addVersionObject() { | |
| 687 |
1/1✓ Branch 1 taken 10583 times.
|
10583 | OptionalIddObject versionIdd = m_iddFileAndFactoryWrapper.versionObject(); |
| 688 |
2/2✓ Branch 1 taken 179 times.
✓ Branch 2 taken 10404 times.
|
10583 | 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 7774 times.
✓ Branch 2 taken 2630 times.
|
10404 | if (m_versionObjectIndices.empty()) { |
| 699 |
2/2✓ Branch 1 taken 7774 times.
✓ Branch 4 taken 7774 times.
|
7774 | IdfObject versionObject(*versionIdd); |
| 700 | // look for field named "Version Identifier" | ||
| 701 |
3/3✓ Branch 1 taken 7774 times.
✓ Branch 4 taken 7774 times.
✓ Branch 7 taken 7774 times.
|
23322 | OptionalInt index = versionObject.iddObject().getFieldIndex("Version Identifier"); |
| 702 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7774 times.
|
7774 | if (!index) { |
| 703 | ✗ | LOG(Warn, "No 'Version Identifier' field found in the Version IddObject."); | |
| 704 | } else { | ||
| 705 |
4/4✓ Branch 1 taken 7774 times.
✓ Branch 4 taken 7774 times.
✓ Branch 7 taken 7774 times.
✓ Branch 10 taken 7774 times.
|
7774 | versionObject.setString(*index, version().str()); |
| 706 | } | ||
| 707 |
1/1✓ Branch 1 taken 7774 times.
|
7774 | addObject(versionObject); |
| 708 | 7774 | } | |
| 709 | 10583 | } | |
| 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 |