src/utilities/idd/IddFile.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 "IddFile.hpp" | ||
| 7 | #include "IddFile_Impl.hpp" | ||
| 8 | |||
| 9 | #include "IddRegex.hpp" | ||
| 10 | #include "IddEnums.hpp" | ||
| 11 | #include <utilities/idd/IddEnums.hxx> | ||
| 12 | |||
| 13 | #include "../core/ASCIIStrings.hpp" | ||
| 14 | #include "../core/PathHelpers.hpp" | ||
| 15 | #include "../core/Assert.hpp" | ||
| 16 | |||
| 17 | #include "../core/Containers.hpp" | ||
| 18 | |||
| 19 | namespace openstudio { | ||
| 20 | |||
| 21 | namespace detail { | ||
| 22 | |||
| 23 | // CONSTRUCTORS | ||
| 24 | |||
| 25 | // GETTERS | ||
| 26 | |||
| 27 | 5126 | std::string IddFile_Impl::version() const { | |
| 28 | 5126 | return m_version; | |
| 29 | } | ||
| 30 | |||
| 31 | ✗ | std::string IddFile_Impl::build() const { | |
| 32 | ✗ | return m_build; | |
| 33 | } | ||
| 34 | |||
| 35 | ✗ | std::string IddFile_Impl::header() const { | |
| 36 | ✗ | return m_header; | |
| 37 | } | ||
| 38 | |||
| 39 | 378 | std::vector<IddObject> IddFile_Impl::objects() const { | |
| 40 | 378 | return m_objects; | |
| 41 | } | ||
| 42 | |||
| 43 | 1 | std::vector<std::string> IddFile_Impl::groups() const { | |
| 44 | 1 | StringSet result; | |
| 45 |
3/3✓ Branch 1 taken 1 time.
✓ Branch 7 taken 885 times.
✓ Branch 8 taken 1 time.
|
886 | for (const IddObject& object : objects()) { |
| 46 |
2/2✓ Branch 1 taken 885 times.
✓ Branch 4 taken 885 times.
|
885 | result.insert(object.group()); |
| 47 | 1 | } | |
| 48 |
1/1✓ Branch 3 taken 1 time.
|
3 | return {result.begin(), result.end()}; |
| 49 | 1 | } | |
| 50 | |||
| 51 | ✗ | std::vector<IddObject> IddFile_Impl::getObjectsInGroup(const std::string& group) const { | |
| 52 | ✗ | IddObjectVector result; | |
| 53 | ✗ | for (const IddObject& object : m_objects) { | |
| 54 | ✗ | if (istringEqual(object.group(), group)) { | |
| 55 | ✗ | result.push_back(object); | |
| 56 | } | ||
| 57 | } | ||
| 58 | ✗ | return result; | |
| 59 | ✗ | } | |
| 60 | |||
| 61 | 2065 | std::vector<IddObject> IddFile_Impl::getObjects(const boost::regex& objectRegex) const { | |
| 62 | 2065 | IddObjectVector result; | |
| 63 | |||
| 64 |
2/2✓ Branch 5 taken 964602 times.
✓ Branch 6 taken 2065 times.
|
966667 | for (const IddObject& object : m_objects) { |
| 65 |
4/4✓ Branch 1 taken 964602 times.
✓ Branch 4 taken 964602 times.
✓ Branch 7 taken 1886 times.
✓ Branch 8 taken 962716 times.
|
964602 | if (boost::regex_match(object.name(), objectRegex)) { |
| 66 |
1/1✓ Branch 1 taken 1886 times.
|
1886 | result.push_back(object); |
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | 2065 | return result; | |
| 71 | ✗ | } | |
| 72 | |||
| 73 | 5460 | boost::optional<IddObject> IddFile_Impl::versionObject() const { | |
| 74 |
2/2✓ Branch 1 taken 3395 times.
✓ Branch 2 taken 2065 times.
|
10920 | if (m_versionObject) { |
| 75 |
1/1✓ Branch 1 taken 3395 times.
|
3395 | return m_versionObject; |
| 76 | } | ||
| 77 | |||
| 78 | 2065 | OptionalIddObject result; | |
| 79 |
2/2✓ Branch 1 taken 2065 times.
✓ Branch 4 taken 2065 times.
|
2065 | IddObjectVector candidates = getObjects(iddRegex::versionObjectName()); |
| 80 |
2/2✓ Branch 1 taken 1886 times.
✓ Branch 2 taken 179 times.
|
2065 | if (candidates.size() == 1u) { |
| 81 |
1/1✓ Branch 2 taken 1886 times.
|
1886 | result = candidates[0]; |
| 82 | } | ||
| 83 |
1/1✓ Branch 1 taken 2065 times.
|
2065 | m_versionObject = result; |
| 84 |
1/1✓ Branch 1 taken 2065 times.
|
2065 | return result; |
| 85 | 2065 | } | |
| 86 | |||
| 87 | 873767 | boost::optional<IddObject> IddFile_Impl::getObject(const std::string& objectName) const { | |
| 88 | 873767 | OptionalIddObject result; | |
| 89 |
2/2✓ Branch 5 taken 129417236 times.
✓ Branch 6 taken 181 times.
|
129417417 | for (const IddObject& object : m_objects) { |
| 90 |
4/4✓ Branch 1 taken 129417236 times.
✓ Branch 4 taken 129417236 times.
✓ Branch 7 taken 873586 times.
✓ Branch 8 taken 128543650 times.
|
129417236 | if (istringEqual(object.name(), objectName)) { |
| 91 |
1/1✓ Branch 1 taken 873586 times.
|
873586 | result = object; |
| 92 | 873586 | break; | |
| 93 | } | ||
| 94 | } | ||
| 95 | 873767 | return result; | |
| 96 | ✗ | } | |
| 97 | |||
| 98 | 6 | boost::optional<IddObject> IddFile_Impl::getObject(IddObjectType objectType) const { | |
| 99 | 6 | OptionalIddObject result; | |
| 100 | |||
| 101 |
2/3✓ Branch 1 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
|
6 | if (objectType == IddObjectType::UserCustom) { |
| 102 | ✗ | LOG(Info, "Asked to return IddObject of type IddObjectType::UserCustom. Since " | |
| 103 | << "UserCustom object types are generally not unique, returning false rather than " | ||
| 104 | << "an IddObject. Please specify a different IddObjectType, or use " << "getObject(const std::string&)."); | ||
| 105 | ✗ | return result; | |
| 106 | } | ||
| 107 | |||
| 108 |
2/2✓ Branch 5 taken 3097 times.
✓ Branch 6 taken 2 times.
|
3099 | for (const IddObject& object : m_objects) { |
| 109 |
3/3✓ Branch 1 taken 3097 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 3093 times.
|
3097 | if (object.type() == objectType) { |
| 110 |
1/1✓ Branch 1 taken 4 times.
|
4 | result = object; |
| 111 | 4 | break; | |
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | 6 | return result; | |
| 116 | ✗ | } | |
| 117 | |||
| 118 | 6 | std::vector<IddObject> IddFile_Impl::requiredObjects() const { | |
| 119 | 6 | IddObjectVector result; | |
| 120 |
2/2✓ Branch 5 taken 3402 times.
✓ Branch 6 taken 6 times.
|
3408 | for (const IddObject& object : m_objects) { |
| 121 |
3/3✓ Branch 1 taken 3402 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 3384 times.
|
3402 | if (object.properties().required) { |
| 122 |
1/1✓ Branch 1 taken 18 times.
|
18 | result.push_back(object); |
| 123 | } | ||
| 124 | } | ||
| 125 | 6 | return result; | |
| 126 | ✗ | } | |
| 127 | |||
| 128 | 6 | std::vector<IddObject> IddFile_Impl::uniqueObjects() const { | |
| 129 | 6 | IddObjectVector result; | |
| 130 |
2/2✓ Branch 5 taken 3402 times.
✓ Branch 6 taken 6 times.
|
3408 | for (const IddObject& object : m_objects) { |
| 131 |
3/3✓ Branch 1 taken 3402 times.
✓ Branch 3 taken 312 times.
✓ Branch 4 taken 3090 times.
|
3402 | if (object.properties().unique) { |
| 132 |
1/1✓ Branch 1 taken 312 times.
|
312 | result.push_back(object); |
| 133 | } | ||
| 134 | } | ||
| 135 | 6 | return result; | |
| 136 | ✗ | } | |
| 137 | |||
| 138 | // SETTERS | ||
| 139 | |||
| 140 | 335 | void IddFile_Impl::setVersion(const std::string& version) { | |
| 141 | 335 | m_version = version; | |
| 142 | 335 | } | |
| 143 | |||
| 144 | 335 | void IddFile_Impl::setHeader(const std::string& header) { | |
| 145 | 335 | m_header = header; | |
| 146 | 335 | } | |
| 147 | |||
| 148 | 204295 | void IddFile_Impl::addObject(const IddObject& object) { | |
| 149 | 204295 | m_objects.push_back(object); | |
| 150 | 204295 | } | |
| 151 | |||
| 152 | // SERIALIZATION | ||
| 153 | |||
| 154 | 1741 | std::shared_ptr<IddFile_Impl> IddFile_Impl::load(std::istream& is) { | |
| 155 | 1741 | std::shared_ptr<IddFile_Impl> result; | |
| 156 | 1741 | IddFile_Impl iddFileImpl; | |
| 157 | |||
| 158 | try { | ||
| 159 |
1/1✓ Branch 1 taken 1741 times.
|
1741 | iddFileImpl.parse(is); |
| 160 | ✗ | } catch (...) { | |
| 161 | ✗ | return result; | |
| 162 | ✗ | } | |
| 163 | |||
| 164 |
3/5✓ Branch 1 taken 1741 times.
✓ Branch 4 taken 1741 times.
✓ Branch 7 taken 1741 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
|
1741 | result = std::shared_ptr<IddFile_Impl>(new IddFile_Impl(iddFileImpl)); |
| 165 | 1741 | return result; | |
| 166 | 1741 | } | |
| 167 | |||
| 168 | 101 | std::ostream& IddFile_Impl::print(std::ostream& os) const { | |
| 169 |
2/2✓ Branch 1 taken 101 times.
✓ Branch 4 taken 101 times.
|
101 | os << m_header << '\n'; |
| 170 | 101 | std::string groupName; | |
| 171 |
2/2✓ Branch 5 taken 57585 times.
✓ Branch 6 taken 101 times.
|
57686 | for (const IddObject& object : m_objects) { |
| 172 |
3/3✓ Branch 1 taken 57585 times.
✓ Branch 5 taken 2660 times.
✓ Branch 6 taken 54925 times.
|
57585 | if (object.group() != groupName) { |
| 173 |
1/1✓ Branch 1 taken 2660 times.
|
2660 | groupName = object.group(); |
| 174 |
4/4✓ Branch 1 taken 2660 times.
✓ Branch 4 taken 2660 times.
✓ Branch 7 taken 2660 times.
✓ Branch 10 taken 2660 times.
|
2660 | os << "\\group " << groupName << '\n' << '\n'; |
| 175 | } | ||
| 176 |
1/1✓ Branch 1 taken 57585 times.
|
57585 | object.print(os); |
| 177 | } | ||
| 178 | 101 | return os; | |
| 179 | 101 | } | |
| 180 | |||
| 181 | // PRIVATE | ||
| 182 | |||
| 183 | 1741 | void IddFile_Impl::parse(std::istream& is) { | |
| 184 | |||
| 185 | // keep track of line number in the idd | ||
| 186 | 1741 | int lineNum = 0; | |
| 187 | |||
| 188 | // number of object in the idd, 1 is first object | ||
| 189 | 1741 | [[maybe_unused]] int objectNum = 0; | |
| 190 | |||
| 191 | // stream for header | ||
| 192 |
1/1✓ Branch 1 taken 1741 times.
|
1741 | std::stringstream header; |
| 193 | |||
| 194 | // have we read the entire header yet | ||
| 195 | 1741 | bool headerClosed = false; | |
| 196 | |||
| 197 | 1741 | std::string currentGroup; | |
| 198 | |||
| 199 | // fake a comment only object and put it in the object list and object map | ||
| 200 | OptionalIddObject commentOnlyObject = | ||
| 201 |
4/4✓ Branch 1 taken 1741 times.
✓ Branch 4 taken 1741 times.
✓ Branch 7 taken 1741 times.
✓ Branch 10 taken 1741 times.
|
1741 | IddObject::load(iddRegex::commentOnlyObjectName(), currentGroup, iddRegex::commentOnlyObjectText(), IddObjectType::CommentOnly); |
| 202 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 1741 times.
✗ Branch 4 not taken.
|
1741 | OS_ASSERT(commentOnlyObject); |
| 203 |
2/2✓ Branch 1 taken 1741 times.
✓ Branch 4 taken 1741 times.
|
1741 | m_objects.push_back(*commentOnlyObject); |
| 204 | |||
| 205 | // temp string to read file | ||
| 206 | 1741 | std::string line; | |
| 207 | |||
| 208 | // this will contain matches to regular expressions | ||
| 209 |
1/1✓ Branch 1 taken 1741 times.
|
1741 | boost::smatch matches; |
| 210 | |||
| 211 | // read in the version from the first line | ||
| 212 |
1/1✓ Branch 1 taken 1741 times.
|
1741 | getline(is, line); |
| 213 |
3/4✓ Branch 1 taken 1741 times.
✓ Branch 4 taken 1741 times.
✓ Branch 6 taken 1741 times.
✗ Branch 7 not taken.
|
1741 | if (boost::regex_search(line, matches, iddRegex::version())) { |
| 214 | |||
| 215 |
3/3✓ Branch 1 taken 1741 times.
✓ Branch 4 taken 1741 times.
✓ Branch 7 taken 1741 times.
|
1741 | m_version = std::string(matches[1].first, matches[1].second); |
| 216 | |||
| 217 | // this line belongs to the header | ||
| 218 |
2/2✓ Branch 1 taken 1741 times.
✓ Branch 4 taken 1741 times.
|
1741 | header << line << '\n'; |
| 219 | |||
| 220 | } else { | ||
| 221 | // idd file must have a version on the first line of input | ||
| 222 | ✗ | LOG_AND_THROW("Idd file does not contain version on first line: '" << line << "'"); | |
| 223 | } | ||
| 224 | |||
| 225 | // read the rest of the file line by line | ||
| 226 | // todo, do this by regex | ||
| 227 |
4/4✓ Branch 1 taken 1027843 times.
✓ Branch 4 taken 1027843 times.
✓ Branch 6 taken 1026102 times.
✓ Branch 7 taken 1741 times.
|
1027843 | while (getline(is, line)) { |
| 228 | 1026102 | ++lineNum; | |
| 229 | |||
| 230 | // remove whitespace | ||
| 231 |
1/1✓ Branch 1 taken 1026102 times.
|
1026102 | openstudio::ascii_trim(line); |
| 232 | |||
| 233 |
2/2✓ Branch 1 taken 53464 times.
✓ Branch 2 taken 972638 times.
|
1026102 | if (line.empty()) { |
| 234 | |||
| 235 | 53464 | headerClosed = true; | |
| 236 | |||
| 237 | // empty line | ||
| 238 | 53464 | continue; | |
| 239 |
4/4✓ Branch 1 taken 972638 times.
✓ Branch 4 taken 972638 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 972636 times.
|
972638 | } else if (boost::regex_search(line, matches, iddRegex::build())) { |
| 240 |
3/3✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
|
2 | m_build = std::string(matches[1].first, matches[1].second); |
| 241 | // this line belongs to the header | ||
| 242 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
2 | header << line << '\n'; |
| 243 | |||
| 244 |
4/4✓ Branch 1 taken 972636 times.
✓ Branch 4 taken 972636 times.
✓ Branch 6 taken 49826 times.
✓ Branch 7 taken 922810 times.
|
972636 | } else if (boost::regex_match(line, iddRegex::commentOnlyLine())) { |
| 245 | |||
| 246 |
2/2✓ Branch 0 taken 49452 times.
✓ Branch 1 taken 374 times.
|
49826 | if (!headerClosed) { |
| 247 |
2/2✓ Branch 1 taken 49452 times.
✓ Branch 4 taken 49452 times.
|
49452 | header << line << '\n'; |
| 248 | } | ||
| 249 | |||
| 250 | // comment only line | ||
| 251 | 49826 | continue; | |
| 252 |
4/4✓ Branch 1 taken 922810 times.
✓ Branch 4 taken 922810 times.
✓ Branch 6 taken 41707 times.
✓ Branch 7 taken 881103 times.
|
922810 | } else if (boost::regex_search(line, matches, iddRegex::group())) { |
| 253 | |||
| 254 | 41707 | headerClosed = true; | |
| 255 | |||
| 256 | // get the group name | ||
| 257 |
3/3✓ Branch 1 taken 41707 times.
✓ Branch 4 taken 41707 times.
✓ Branch 7 taken 41707 times.
|
41707 | std::string groupName(matches[1].first, matches[1].second); |
| 258 |
1/1✓ Branch 1 taken 41707 times.
|
41707 | openstudio::ascii_trim(groupName); |
| 259 | |||
| 260 | // set the current group | ||
| 261 |
1/1✓ Branch 1 taken 41707 times.
|
41707 | currentGroup = groupName; |
| 262 | |||
| 263 | 41707 | continue; | |
| 264 | 41707 | } else { | |
| 265 | |||
| 266 | 881103 | headerClosed = true; | |
| 267 | |||
| 268 | //int beginLineNum(lineNum); | ||
| 269 | 881103 | bool foundClosingLine(false); | |
| 270 | |||
| 271 | // a valid idd object to parse | ||
| 272 | 881103 | ++objectNum; | |
| 273 | |||
| 274 | // peek at the object name for indexing in map | ||
| 275 | 881103 | std::string objectName; | |
| 276 |
3/4✓ Branch 1 taken 881103 times.
✓ Branch 4 taken 881103 times.
✓ Branch 6 taken 881103 times.
✗ Branch 7 not taken.
|
881103 | if (boost::regex_search(line, matches, iddRegex::line())) { |
| 277 |
3/3✓ Branch 1 taken 881103 times.
✓ Branch 4 taken 881103 times.
✓ Branch 7 taken 881103 times.
|
881103 | objectName = std::string(matches[1].first, matches[1].second); |
| 278 |
1/1✓ Branch 1 taken 881103 times.
|
881103 | openstudio::ascii_trim(objectName); |
| 279 | } else { | ||
| 280 | // can't figure out the object's name | ||
| 281 | ✗ | LOG_AND_THROW("Cannot determine object name on line " << lineNum << ": '" << line << "'"); | |
| 282 | } | ||
| 283 | |||
| 284 | // put the text for this object in a new string | ||
| 285 |
1/1✓ Branch 1 taken 881103 times.
|
881103 | std::string text(line); |
| 286 | |||
| 287 | // check if the object has no fields | ||
| 288 |
4/4✓ Branch 1 taken 881103 times.
✓ Branch 4 taken 881103 times.
✓ Branch 6 taken 200 times.
✓ Branch 7 taken 880903 times.
|
881103 | if (boost::regex_match(line, iddRegex::objectNoFields())) { |
| 289 | 200 | foundClosingLine = true; | |
| 290 | } | ||
| 291 | |||
| 292 | // check if the object has fields, and last field on this line | ||
| 293 |
3/4✓ Branch 1 taken 881103 times.
✓ Branch 4 taken 881103 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 881103 times.
|
881103 | if (boost::regex_match(line, iddRegex::closingField())) { |
| 294 | ✗ | foundClosingLine = true; | |
| 295 | } | ||
| 296 | |||
| 297 | // continue reading until we have seen the entire object | ||
| 298 | // last line will be thrown away, requires empty line between objects in idd | ||
| 299 |
3/4✓ Branch 1 taken 61038980 times.
✓ Branch 4 taken 61038980 times.
✓ Branch 6 taken 61038980 times.
✗ Branch 7 not taken.
|
61038980 | while (getline(is, line)) { |
| 300 | 61038980 | ++lineNum; | |
| 301 | |||
| 302 | // remove whitespace | ||
| 303 |
1/1✓ Branch 1 taken 61038980 times.
|
61038980 | openstudio::ascii_trim(line); |
| 304 | |||
| 305 | // found last field and this is not a field comment | ||
| 306 |
8/8✓ Branch 0 taken 4985295 times.
✓ Branch 1 taken 56053685 times.
✓ Branch 3 taken 4985295 times.
✓ Branch 6 taken 4985295 times.
✓ Branch 8 taken 881103 times.
✓ Branch 9 taken 4104192 times.
✓ Branch 10 taken 881103 times.
✓ Branch 11 taken 60157877 times.
|
61038980 | if (foundClosingLine && (!boost::regex_match(line, iddRegex::metaDataComment()))) { |
| 307 | 881103 | break; | |
| 308 | } | ||
| 309 | |||
| 310 |
1/2✓ Branch 1 taken 60157877 times.
✗ Branch 2 not taken.
|
60157877 | if (!line.empty()) { |
| 311 | // if the line is not empty add it to the text | ||
| 312 | // note, text does not include newlines | ||
| 313 |
1/1✓ Branch 1 taken 60157877 times.
|
60157877 | text += line; |
| 314 | |||
| 315 | // check if we have found the last field | ||
| 316 |
4/4✓ Branch 1 taken 60157877 times.
✓ Branch 4 taken 60157877 times.
✓ Branch 6 taken 880903 times.
✓ Branch 7 taken 59276974 times.
|
60157877 | if (boost::regex_match(line, iddRegex::closingField())) { |
| 317 | 880903 | foundClosingLine = true; | |
| 318 | } | ||
| 319 | } | ||
| 320 | } | ||
| 321 | |||
| 322 | // construct the IddObject using default UserCustom type | ||
| 323 |
1/1✓ Branch 1 taken 881103 times.
|
881103 | OptionalIddObject object = IddObject::load(objectName, currentGroup, text); |
| 324 | |||
| 325 | // construct a new object and put it in the object vector | ||
| 326 |
1/2✓ Branch 0 taken 881103 times.
✗ Branch 1 not taken.
|
881103 | if (object) { |
| 327 |
2/2✓ Branch 1 taken 881103 times.
✓ Branch 4 taken 881103 times.
|
881103 | m_objects.push_back(*object); |
| 328 | } else { | ||
| 329 | ✗ | LOG_AND_THROW("Unable to construct IddObject from text: " << '\n' << text); | |
| 330 | } | ||
| 331 | 881103 | } | |
| 332 | } | ||
| 333 | |||
| 334 | // set header | ||
| 335 |
1/1✓ Branch 1 taken 1741 times.
|
1741 | m_header = header.str(); |
| 336 | 1741 | } | |
| 337 | |||
| 338 | } // namespace detail | ||
| 339 | |||
| 340 | // CONSTRUCTORS | ||
| 341 | |||
| 342 | 3678 | IddFile::IddFile() : m_impl(std::make_shared<detail::IddFile_Impl>()) {} | |
| 343 | |||
| 344 | // | ||
| 345 | // IddFile::IddFile(const IddFile& other) : m_impl(other.m_impl) {} | ||
| 346 | // IddFile& IddFile::operator=(const IddFile& other) { | ||
| 347 | // m_impl = other.m_impl; | ||
| 348 | // return *this; | ||
| 349 | // } | ||
| 350 | // | ||
| 351 | // IddFile::IddFile(IddFile&& other) noexcept : m_impl(std::move(other.m_impl)) {} | ||
| 352 | // | ||
| 353 | // IddFile& IddFile::operator=(IddFile&& other) noexcept { | ||
| 354 | // m_impl = std::move(other.m_impl); | ||
| 355 | // return *this; | ||
| 356 | // } | ||
| 357 | |||
| 358 | // PRIVATE CONSTRUCTOR | ||
| 359 | |||
| 360 | 1741 | IddFile::IddFile(const std::shared_ptr<detail::IddFile_Impl>& impl) noexcept : m_impl(impl) {} | |
| 361 | ✗ | IddFile::IddFile(std::shared_ptr<detail::IddFile_Impl>&& impl) noexcept : m_impl(std::move(impl)) {} | |
| 362 | |||
| 363 | 179 | IddFile IddFile::catchallIddFile() { | |
| 364 | 179 | IddFile result; | |
| 365 |
2/2✓ Branch 1 taken 179 times.
✓ Branch 4 taken 179 times.
|
179 | result.addObject(IddObject()); |
| 366 | 179 | return result; | |
| 367 | ✗ | } | |
| 368 | |||
| 369 | // GETTERS | ||
| 370 | |||
| 371 | 5126 | std::string IddFile::version() const { | |
| 372 | 5126 | return m_impl->version(); | |
| 373 | } | ||
| 374 | |||
| 375 | ✗ | std::string IddFile::build() const { | |
| 376 | ✗ | return m_impl->build(); | |
| 377 | } | ||
| 378 | ✗ | std::string IddFile::header() const { | |
| 379 | ✗ | return m_impl->header(); | |
| 380 | } | ||
| 381 | |||
| 382 | 377 | std::vector<IddObject> IddFile::objects() const { | |
| 383 | 377 | return m_impl->objects(); | |
| 384 | } | ||
| 385 | |||
| 386 | 1 | std::vector<std::string> IddFile::groups() const { | |
| 387 | 1 | return m_impl->groups(); | |
| 388 | } | ||
| 389 | |||
| 390 | ✗ | std::vector<IddObject> IddFile::getObjectsInGroup(const std::string& group) const { | |
| 391 | ✗ | return m_impl->getObjectsInGroup(group); | |
| 392 | } | ||
| 393 | |||
| 394 | ✗ | std::vector<IddObject> IddFile::getObjects(const boost::regex& objectRegex) const { | |
| 395 | ✗ | return m_impl->getObjects(objectRegex); | |
| 396 | } | ||
| 397 | |||
| 398 | 5460 | boost::optional<IddObject> IddFile::versionObject() const { | |
| 399 | 5460 | return m_impl->versionObject(); | |
| 400 | } | ||
| 401 | |||
| 402 | 873767 | boost::optional<IddObject> IddFile::getObject(const std::string& objectName) const { | |
| 403 | 873767 | return m_impl->getObject(objectName); | |
| 404 | } | ||
| 405 | |||
| 406 | 6 | boost::optional<IddObject> IddFile::getObject(IddObjectType objectType) const { | |
| 407 | 6 | return m_impl->getObject(objectType); | |
| 408 | } | ||
| 409 | |||
| 410 | 6 | std::vector<IddObject> IddFile::requiredObjects() const { | |
| 411 | 6 | return m_impl->requiredObjects(); | |
| 412 | } | ||
| 413 | |||
| 414 | 6 | std::vector<IddObject> IddFile::uniqueObjects() const { | |
| 415 | 6 | return m_impl->uniqueObjects(); | |
| 416 | } | ||
| 417 | |||
| 418 | // SERIALIZATION | ||
| 419 | |||
| 420 | 1741 | OptionalIddFile IddFile::load(std::istream& is) { | |
| 421 |
1/1✓ Branch 1 taken 1741 times.
|
1741 | std::shared_ptr<detail::IddFile_Impl> p = detail::IddFile_Impl::load(is); |
| 422 |
1/2✓ Branch 1 taken 1741 times.
✗ Branch 2 not taken.
|
1741 | if (p) { |
| 423 |
1/1✓ Branch 2 taken 1741 times.
|
1741 | return IddFile(p); |
| 424 | } | ||
| 425 | ✗ | return boost::none; | |
| 426 | 1741 | } | |
| 427 | |||
| 428 | 2 | OptionalIddFile IddFile::load(const openstudio::path& p) { | |
| 429 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 5 taken 2 times.
|
2 | openstudio::path wp = completePathToFile(p, path(), "idd", true); |
| 430 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (wp.empty()) { |
| 431 | ✗ | return boost::none; | |
| 432 | } | ||
| 433 |
1/1✓ Branch 1 taken 2 times.
|
2 | openstudio::filesystem::ifstream inFile(wp); |
| 434 |
2/3✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
|
2 | if (!inFile) { |
| 435 | ✗ | return boost::none; | |
| 436 | } | ||
| 437 |
1/1✓ Branch 1 taken 2 times.
|
2 | return load(inFile); |
| 438 | 2 | } | |
| 439 | |||
| 440 | 101 | std::ostream& IddFile::print(std::ostream& os) const { | |
| 441 | 101 | return m_impl->print(os); | |
| 442 | } | ||
| 443 | |||
| 444 | ✗ | std::pair<VersionString, std::string> IddFile::parseVersionBuild(const openstudio::path& p) { | |
| 445 | ✗ | std::ifstream ifs(openstudio::toSystemFilename(p)); | |
| 446 | |||
| 447 | ✗ | if (!ifs.good()) { | |
| 448 | ✗ | throw std::runtime_error("Unable to open file for reading: " + openstudio::toString(p)); | |
| 449 | } | ||
| 450 | |||
| 451 | ✗ | ifs.seekg(0, std::ios_base::end); | |
| 452 | ✗ | const auto end = ifs.tellg(); | |
| 453 | ✗ | ifs.seekg(0, std::ios_base::beg); | |
| 454 | |||
| 455 | ✗ | const auto length_to_read = std::min(std::streampos(10000), end); | |
| 456 | |||
| 457 | ✗ | std::vector<char> data(length_to_read); | |
| 458 | ✗ | ifs.read(data.data(), length_to_read); | |
| 459 | ✗ | const std::string strdata(data.cbegin(), data.cend()); | |
| 460 | |||
| 461 | ✗ | std::string build; | |
| 462 | ✗ | boost::smatch matches; | |
| 463 | ✗ | if (boost::regex_search(strdata, matches, iddRegex::build())) { | |
| 464 | ✗ | build = std::string(matches[1].first, matches[1].second); | |
| 465 | } | ||
| 466 | |||
| 467 | ✗ | if (boost::regex_search(strdata, matches, iddRegex::version())) { | |
| 468 | ✗ | return std::make_pair(VersionString(std::string(matches[1].first, matches[1].second)), build); | |
| 469 | } | ||
| 470 | |||
| 471 | ✗ | throw std::runtime_error("Unable to parse version from IDD: " + openstudio::toString(p)); | |
| 472 | ✗ | } | |
| 473 | |||
| 474 | 100 | bool IddFile::save(const openstudio::path& p, bool overwrite) { | |
| 475 |
2/2✓ Branch 1 taken 100 times.
✓ Branch 5 taken 100 times.
|
100 | path wp = completePathToFile(p, path(), "idd", true); |
| 476 |
3/6✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 100 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 100 times.
|
100 | if (!wp.empty() && (overwrite == false)) { |
| 477 | ✗ | LOG(Info, "IddFile save method failed because instructed not to overwrite path '" << toString(p) << "'."); | |
| 478 | ✗ | return false; | |
| 479 | } | ||
| 480 |
2/3✓ Branch 2 taken 100 times.
✓ Branch 5 taken 100 times.
✗ Branch 6 not taken.
|
100 | if (makeParentFolder(p)) { |
| 481 |
1/1✓ Branch 1 taken 100 times.
|
100 | openstudio::filesystem::ofstream outFile(p); |
| 482 |
2/3✓ Branch 1 taken 100 times.
✓ Branch 3 taken 100 times.
✗ Branch 4 not taken.
|
100 | if (outFile) { |
| 483 | try { | ||
| 484 |
1/1✓ Branch 1 taken 100 times.
|
100 | print(outFile); |
| 485 |
1/1✓ Branch 1 taken 100 times.
|
100 | outFile.close(); |
| 486 | 100 | return true; | |
| 487 | ✗ | } catch (...) { | |
| 488 | ✗ | LOG(Error, "Unable to write IddFile to path '" << toString(p) << "'."); | |
| 489 | ✗ | return false; | |
| 490 | ✗ | } | |
| 491 | } | ||
| 492 | 100 | } | |
| 493 | |||
| 494 | ✗ | LOG(Error, "Unable to write IddFile to path '" << toString(p) << "', because parent directory could not be created."); | |
| 495 | ✗ | return false; | |
| 496 | 100 | } | |
| 497 | |||
| 498 | // PROTECTED | ||
| 499 | |||
| 500 | 335 | void IddFile::setVersion(const std::string& version) { | |
| 501 | 335 | m_impl->setVersion(version); | |
| 502 | 335 | } | |
| 503 | |||
| 504 | 335 | void IddFile::setHeader(const std::string& header) { | |
| 505 | 335 | m_impl->setHeader(header); | |
| 506 | 335 | } | |
| 507 | |||
| 508 | 204295 | void IddFile::addObject(const IddObject& object) { | |
| 509 | 204295 | m_impl->addObject(object); | |
| 510 | 204295 | } | |
| 511 | |||
| 512 | // NON-MEMBER FUNCTIONS | ||
| 513 | |||
| 514 | ✗ | std::ostream& operator<<(std::ostream& os, const IddFile& iddFile) { | |
| 515 | ✗ | return iddFile.print(os); | |
| 516 | } | ||
| 517 | |||
| 518 | } // namespace openstudio | ||
| 519 |