src/airflow/contam/PrjReader.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 "PrjReader.hpp" | ||
| 7 | #include <iostream> | ||
| 8 | #include <cstdlib> | ||
| 9 | |||
| 10 | #include "../../utilities/core/Logger.hpp" | ||
| 11 | #include "../../utilities/core/FilesystemHelpers.hpp" | ||
| 12 | #include "../../utilities/core/StringHelpers.hpp" | ||
| 13 | |||
| 14 | namespace openstudio { | ||
| 15 | namespace contam { | ||
| 16 | |||
| 17 | ✗ | Reader::Reader(openstudio::filesystem::ifstream& file) : m_stream(openstudio::filesystem::read_as_string(file)), m_lineNumber(0) {} | |
| 18 | |||
| 19 | 6 | Reader::Reader(const std::string& string, int starting) : m_lineNumber(starting) { | |
| 20 |
1/1✓ Branch 1 taken 6 times.
|
6 | m_stream.str(string); |
| 21 | 6 | } | |
| 22 | |||
| 23 | ✗ | double Reader::readDouble() { | |
| 24 | ✗ | const auto string = readString(); | |
| 25 | try { | ||
| 26 | ✗ | return openstudio::string_conversions::to<double>(string); | |
| 27 | ✗ | } catch (const std::bad_cast&) { | |
| 28 | ✗ | LOG_AND_THROW("Floating point (double) conversion error at line " << m_lineNumber << " for \"" << string << "\""); | |
| 29 | ✗ | } | |
| 30 | ✗ | } | |
| 31 | |||
| 32 | 7014 | std::string Reader::readString() { | |
| 33 | while (true) { | ||
| 34 |
2/2✓ Branch 1 taken 1494 times.
✓ Branch 2 taken 7092 times.
|
8586 | while (m_entries.empty()) { |
| 35 | 1494 | std::string input; | |
| 36 |
1/1✓ Branch 1 taken 1494 times.
|
1494 | std::getline(m_stream, input); |
| 37 |
6/6✓ Branch 1 taken 1494 times.
✓ Branch 4 taken 1494 times.
✓ Branch 7 taken 1494 times.
✓ Branch 10 taken 1494 times.
✓ Branch 13 taken 1494 times.
✓ Branch 16 taken 1494 times.
|
1494 | LOG(Debug, "Line read: " << input); |
| 38 |
2/3✓ Branch 1 taken 1494 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1494 times.
|
1494 | if (!m_stream) { |
| 39 | ✗ | LOG_AND_THROW("Failed to read input at line " << m_lineNumber); | |
| 40 | } | ||
| 41 | 1494 | m_lineNumber++; | |
| 42 |
2/2✓ Branch 1 taken 144 times.
✓ Branch 2 taken 1494 times.
|
1638 | while (input[0] == '!') { |
| 43 |
1/1✓ Branch 1 taken 144 times.
|
144 | std::getline(m_stream, input); |
| 44 |
6/6✓ Branch 1 taken 144 times.
✓ Branch 4 taken 144 times.
✓ Branch 7 taken 144 times.
✓ Branch 10 taken 144 times.
✓ Branch 13 taken 144 times.
✓ Branch 16 taken 144 times.
|
144 | LOG(Debug, "Line read: " << input); |
| 45 |
2/3✓ Branch 1 taken 144 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 144 times.
|
144 | if (!m_stream) { |
| 46 | ✗ | LOG_AND_THROW("Failed to read input at line " << m_lineNumber); | |
| 47 | } | ||
| 48 | 144 | m_lineNumber++; | |
| 49 | } | ||
| 50 | |||
| 51 | 1494 | std::vector<std::string> strs; | |
| 52 |
2/2✓ Branch 1 taken 1494 times.
✓ Branch 4 taken 1494 times.
|
1494 | boost::split(strs, input, boost::is_any_of(" ")); |
| 53 | |||
| 54 | 1494 | m_entries.clear(); | |
| 55 |
2/2✓ Branch 5 taken 13248 times.
✓ Branch 6 taken 1494 times.
|
14742 | for (const auto& s : strs) { |
| 56 |
1/1✓ Branch 1 taken 13248 times.
|
13248 | std::string s2 = boost::replace_all_copy(s, " ", ""); |
| 57 |
1/1✓ Branch 1 taken 13248 times.
|
13248 | boost::replace_all(s2, "\r", ""); |
| 58 |
1/1✓ Branch 1 taken 13248 times.
|
13248 | boost::replace_all(s2, "\n", ""); |
| 59 |
1/1✓ Branch 1 taken 13248 times.
|
13248 | boost::replace_all(s2, "\t", ""); |
| 60 |
2/2✓ Branch 1 taken 7326 times.
✓ Branch 2 taken 5922 times.
|
13248 | if (!s2.empty()) { |
| 61 |
1/1✓ Branch 1 taken 7326 times.
|
7326 | m_entries.push_back(s2); |
| 62 | } | ||
| 63 | 13248 | } | |
| 64 | 1494 | } | |
| 65 |
1/1✓ Branch 2 taken 7092 times.
|
7092 | std::string out = m_entries.front(); |
| 66 | 7092 | m_entries.pop_front(); | |
| 67 |
2/2✓ Branch 1 taken 78 times.
✓ Branch 2 taken 7014 times.
|
7092 | if (out[0] == '!') { |
| 68 | 78 | m_entries.clear(); | |
| 69 | } else { | ||
| 70 |
6/6✓ Branch 1 taken 7014 times.
✓ Branch 4 taken 7014 times.
✓ Branch 7 taken 7014 times.
✓ Branch 10 taken 7014 times.
✓ Branch 13 taken 7014 times.
✓ Branch 16 taken 7014 times.
|
7014 | LOG(Debug, "String return: " << out); |
| 71 | 14028 | return out; | |
| 72 | } | ||
| 73 | 7170 | } | |
| 74 | } | ||
| 75 | |||
| 76 | 4044 | int Reader::readInt() { | |
| 77 |
1/1✓ Branch 1 taken 4044 times.
|
4044 | std::string string = readString(); |
| 78 | 4044 | int value = 0; | |
| 79 | try { | ||
| 80 |
1/1✓ Branch 1 taken 4044 times.
|
4044 | value = std::stoi(string); |
| 81 | ✗ | } catch (const std::exception&) { | |
| 82 | ✗ | LOG_AND_THROW("Integer conversion error at line " << m_lineNumber << " for \"" << string << "\""); | |
| 83 | ✗ | } | |
| 84 | 4044 | return value; | |
| 85 | 4044 | } | |
| 86 | |||
| 87 | 6 | unsigned int Reader::readUInt() { | |
| 88 |
1/1✓ Branch 1 taken 6 times.
|
6 | std::string string = readString(); |
| 89 | 6 | int value = 0; | |
| 90 | try { | ||
| 91 |
1/1✓ Branch 1 taken 6 times.
|
6 | value = std::stoul(string); |
| 92 | ✗ | } catch (const std::exception&) { | |
| 93 | ✗ | LOG_AND_THROW("Unsigned Integer conversion error at line " << m_lineNumber << " for \"" << string << "\""); | |
| 94 | ✗ | } | |
| 95 | 6 | return value; | |
| 96 | 6 | } | |
| 97 | |||
| 98 | 546 | std::string Reader::readLine() { | |
| 99 | /* Dump any other input */ | ||
| 100 |
2/2✓ Branch 1 taken 36 times.
✓ Branch 2 taken 510 times.
|
546 | if (!m_entries.empty()) { |
| 101 | 36 | m_entries.clear(); | |
| 102 | } | ||
| 103 | 546 | std::string input; | |
| 104 |
1/1✓ Branch 1 taken 546 times.
|
546 | std::getline(m_stream, input); |
| 105 |
6/6✓ Branch 1 taken 546 times.
✓ Branch 4 taken 546 times.
✓ Branch 7 taken 546 times.
✓ Branch 10 taken 546 times.
✓ Branch 13 taken 546 times.
✓ Branch 16 taken 546 times.
|
546 | LOG(Debug, "Line read: " << input); |
| 106 |
2/3✓ Branch 1 taken 546 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 546 times.
|
546 | if (!m_stream) { |
| 107 | ✗ | LOG_AND_THROW("Failed to read input at line " << m_lineNumber); | |
| 108 | } | ||
| 109 | 546 | m_lineNumber++; | |
| 110 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 546 times.
|
552 | while (input[0] == '!') { |
| 111 |
1/1✓ Branch 1 taken 6 times.
|
6 | std::getline(m_stream, input); |
| 112 |
6/6✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
✓ Branch 16 taken 6 times.
|
6 | LOG(Debug, "Line read: " << input); |
| 113 |
2/3✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
|
6 | if (!m_stream) { |
| 114 | ✗ | LOG_AND_THROW("Failed to read input at line " << m_lineNumber); | |
| 115 | } | ||
| 116 | 6 | m_lineNumber++; | |
| 117 | } | ||
| 118 | 546 | return input; | |
| 119 | ✗ | } | |
| 120 | |||
| 121 | 6 | void Reader::read999() { | |
| 122 |
1/1✓ Branch 1 taken 6 times.
|
6 | std::string input = readLine(); |
| 123 |
2/3✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
|
6 | if (!boost::starts_with(input, "-999")) { |
| 124 | ✗ | LOG_AND_THROW("Failed to read -999 at line " << m_lineNumber); | |
| 125 | } | ||
| 126 | 6 | } | |
| 127 | |||
| 128 | 66 | void Reader::read999(std::string mesg) { | |
| 129 |
1/1✓ Branch 1 taken 66 times.
|
66 | std::string input = readLine(); |
| 130 |
2/3✓ Branch 1 taken 66 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 66 times.
|
66 | if (!boost::starts_with(input, "-999")) { |
| 131 | ✗ | LOG_AND_THROW(mesg << " at line " << m_lineNumber); | |
| 132 | } | ||
| 133 | 66 | } | |
| 134 | |||
| 135 | 6 | void Reader::readEnd() { | |
| 136 |
1/1✓ Branch 1 taken 6 times.
|
6 | std::string input = readLine(); |
| 137 |
2/3✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
|
6 | if (!boost::starts_with(input, "* end project file.")) { |
| 138 | ✗ | LOG_AND_THROW("Failed to read file end at line " << m_lineNumber); | |
| 139 | } | ||
| 140 | 6 | } | |
| 141 | |||
| 142 | ✗ | void Reader::skipSection() { | |
| 143 | ✗ | readSection(); | |
| 144 | ✗ | } | |
| 145 | |||
| 146 | 78 | std::string Reader::readSection() { | |
| 147 | 78 | std::string section; | |
| 148 | while (true) { | ||
| 149 | 510 | std::string input; | |
| 150 |
1/1✓ Branch 1 taken 510 times.
|
510 | std::getline(m_stream, input); |
| 151 |
2/3✓ Branch 1 taken 510 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 510 times.
|
510 | if (!m_stream) { |
| 152 | ✗ | LOG_AND_THROW("Failed to read input at line " << m_lineNumber); | |
| 153 | } | ||
| 154 | 510 | m_lineNumber++; | |
| 155 |
2/2✓ Branch 1 taken 510 times.
✓ Branch 4 taken 510 times.
|
510 | section += std::string(input + '\n'); |
| 156 |
2/2✓ Branch 1 taken 78 times.
✓ Branch 2 taken 432 times.
|
510 | if (input.find("-999") == 0) { |
| 157 | 78 | break; | |
| 158 | } | ||
| 159 | 942 | } | |
| 160 | 78 | return section; | |
| 161 | ✗ | } | |
| 162 | |||
| 163 | 6 | std::vector<int> Reader::readIntVector(bool terminated) { | |
| 164 | 6 | int n = readInt(); | |
| 165 | 6 | std::vector<int> vector; | |
| 166 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | for (int i = 0; i < n; i++) { |
| 167 | ✗ | vector.push_back(readInt()); | |
| 168 | } | ||
| 169 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (terminated) { |
| 170 | ✗ | read999("Failed to find section termination"); | |
| 171 | } | ||
| 172 | 6 | return vector; | |
| 173 | ✗ | } | |
| 174 | |||
| 175 | template <> | ||
| 176 | 3978 | int Reader::read<int>() { | |
| 177 | 3978 | return readInt(); | |
| 178 | } | ||
| 179 | |||
| 180 | template <> | ||
| 181 | ✗ | unsigned int Reader::read<unsigned int>() { | |
| 182 | ✗ | return readUInt(); | |
| 183 | } | ||
| 184 | |||
| 185 | template <> | ||
| 186 | ✗ | double Reader::read<double>() { | |
| 187 | ✗ | return readDouble(); | |
| 188 | } | ||
| 189 | |||
| 190 | template <> | ||
| 191 | 96 | std::string Reader::read<std::string>() { | |
| 192 | 96 | return readString(); | |
| 193 | } | ||
| 194 | |||
| 195 | template <> | ||
| 196 | ✗ | double Reader::readNumber<double>() { | |
| 197 | ✗ | return readInt(); | |
| 198 | } | ||
| 199 | |||
| 200 | template <> | ||
| 201 | 1968 | std::string Reader::readNumber<std::string>() { | |
| 202 | 1968 | std::string string = readString(); | |
| 203 |
2/3✓ Branch 1 taken 1968 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1968 times.
|
1968 | if (!openstudio::contam::is_valid<double>(string)) { |
| 204 | ✗ | LOG_AND_THROW("Invalid number \"" << string << "\" on line " << m_lineNumber); | |
| 205 | } | ||
| 206 | 1968 | return string; | |
| 207 | ✗ | } | |
| 208 | |||
| 209 | } // namespace contam | ||
| 210 | } // namespace openstudio | ||
| 211 |