src/utilities/filetypes/CSVFile.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 "CSVFile.hpp" | ||
| 7 | #include "CSVFile_Impl.hpp" | ||
| 8 | |||
| 9 | #include "../core/Assert.hpp" | ||
| 10 | #include "../core/PathHelpers.hpp" | ||
| 11 | #include "../core/Checksum.hpp" | ||
| 12 | #include "../data/Variant.hpp" | ||
| 13 | #include "../data/Vector.hpp" | ||
| 14 | #include "../time/DateTime.hpp" | ||
| 15 | |||
| 16 | #include <iostream> | ||
| 17 | |||
| 18 | #include <boost/regex.hpp> | ||
| 19 | |||
| 20 | namespace openstudio { | ||
| 21 | namespace detail { | ||
| 22 | |||
| 23 | 3 | CSVFile_Impl::CSVFile_Impl() : m_numColumns(0) {} | |
| 24 | |||
| 25 | 1 | CSVFile_Impl::CSVFile_Impl(const std::string& s) { | |
| 26 |
1/1✓ Branch 1 taken 1 time.
|
1 | std::istringstream ss(s); |
| 27 | |||
| 28 | // will throw on error | ||
| 29 |
1/1✓ Branch 1 taken 1 time.
|
1 | m_rows = parseRows(ss); |
| 30 | |||
| 31 |
1/1✓ Branch 1 taken 1 time.
|
1 | assignNumColumns(); |
| 32 |
1/1✓ Branch 1 taken 1 time.
|
1 | padRows(); |
| 33 | 1 | } | |
| 34 | |||
| 35 | 3 | CSVFile_Impl::CSVFile_Impl(const openstudio::path& p) { | |
| 36 |
5/8✓ Branch 1 taken 3 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 3 times.
|
3 | if (!boost::filesystem::exists(p) || !boost::filesystem::is_regular_file(p)) { |
| 37 | ✗ | LOG_AND_THROW("Path '" << p << "' is not a CSVFile file"); | |
| 38 | } | ||
| 39 | |||
| 40 | // open file | ||
| 41 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::ifstream ifs(openstudio::toSystemFilename(p)); |
| 42 | |||
| 43 | // will throw on error | ||
| 44 |
1/1✓ Branch 1 taken 3 times.
|
3 | m_rows = parseRows(ifs); |
| 45 | |||
| 46 |
1/1✓ Branch 1 taken 3 times.
|
3 | m_path = p; |
| 47 |
1/1✓ Branch 1 taken 3 times.
|
3 | assignNumColumns(); |
| 48 |
1/1✓ Branch 1 taken 3 times.
|
3 | padRows(); |
| 49 | 3 | } | |
| 50 | |||
| 51 | ✗ | CSVFile CSVFile_Impl::clone() const { | |
| 52 | ✗ | CSVFile result(this->string()); | |
| 53 | ✗ | if (m_path) { | |
| 54 | ✗ | result.setPath(*m_path); | |
| 55 | } | ||
| 56 | |||
| 57 | ✗ | return result; | |
| 58 | ✗ | } | |
| 59 | |||
| 60 | 4 | std::string CSVFile_Impl::string() const { | |
| 61 |
4/7✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
4 | static const boost::regex escapeItRegex(","); |
| 62 | |||
| 63 | 4 | std::string s; | |
| 64 |
1/1✓ Branch 1 taken 4 times.
|
4 | std::stringstream result; |
| 65 |
2/2✓ Branch 5 taken 8769 times.
✓ Branch 6 taken 4 times.
|
8773 | for (const auto& row : m_rows) { |
| 66 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 8769 times.
✗ Branch 4 not taken.
|
8769 | OS_ASSERT(row.size() == m_numColumns); |
| 67 |
2/2✓ Branch 0 taken 17547 times.
✓ Branch 1 taken 8769 times.
|
26316 | for (size_t i = 0; i < m_numColumns; ++i) { |
| 68 | |||
| 69 |
5/6✓ Branch 2 taken 17547 times.
✓ Branch 5 taken 17547 times.
✓ Branch 7 taken 9 times.
✓ Branch 8 taken 8769 times.
✓ Branch 9 taken 8769 times.
✗ Branch 10 not taken.
|
17547 | switch (row[i].variantType().value()) { |
| 70 | 9 | case VariantType::Integer: | |
| 71 |
2/2✓ Branch 2 taken 9 times.
✓ Branch 5 taken 9 times.
|
9 | result << row[i].valueAsInteger(); |
| 72 | 9 | break; | |
| 73 | 8769 | case VariantType::Double: | |
| 74 |
2/2✓ Branch 2 taken 8769 times.
✓ Branch 5 taken 8769 times.
|
8769 | result << row[i].valueAsDouble(); |
| 75 | 8769 | break; | |
| 76 | 8769 | case VariantType::String: | |
| 77 |
1/1✓ Branch 2 taken 8769 times.
|
8769 | s = row[i].valueAsString(); |
| 78 |
2/3✓ Branch 1 taken 8769 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8769 times.
|
8769 | if (boost::regex_match(s, escapeItRegex)) { |
| 79 | ✗ | result << "\"" << s << "\""; | |
| 80 | } else { | ||
| 81 |
1/1✓ Branch 1 taken 8769 times.
|
8769 | result << s; |
| 82 | } | ||
| 83 | 8769 | break; | |
| 84 | ✗ | default: | |
| 85 | ✗ | break; | |
| 86 | } | ||
| 87 | |||
| 88 |
2/2✓ Branch 0 taken 8778 times.
✓ Branch 1 taken 8769 times.
|
17547 | if (i < m_numColumns - 1) { |
| 89 |
1/1✓ Branch 1 taken 8778 times.
|
8778 | result << ","; |
| 90 | } | ||
| 91 | } | ||
| 92 |
1/1✓ Branch 1 taken 8769 times.
|
8769 | result << "\n"; |
| 93 | } | ||
| 94 |
1/1✓ Branch 1 taken 4 times.
|
8 | return result.str(); |
| 95 | 4 | } | |
| 96 | |||
| 97 | 1 | bool CSVFile_Impl::save() const { | |
| 98 |
1/1✓ Branch 1 taken 1 time.
|
1 | boost::optional<openstudio::path> p = path(); |
| 99 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
|
1 | if (!p) { |
| 100 | ✗ | return false; | |
| 101 | } | ||
| 102 | |||
| 103 |
3/4✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
1 | if (makeParentFolder(*p)) { |
| 104 |
3/3✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
|
1 | std::ofstream outFile(openstudio::toSystemFilename(*p)); |
| 105 | |||
| 106 |
2/3✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
|
1 | if (outFile) { |
| 107 | try { | ||
| 108 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | outFile << string(); |
| 109 |
1/1✓ Branch 1 taken 1 time.
|
1 | outFile.close(); |
| 110 | 1 | return true; | |
| 111 | ✗ | } catch (...) { | |
| 112 | ✗ | LOG(Error, "Unable to write file to path '" << toString(*p) << "'."); | |
| 113 | ✗ | return false; | |
| 114 | ✗ | } | |
| 115 | } | ||
| 116 | 1 | } | |
| 117 | |||
| 118 | ✗ | LOG(Error, "Unable to write file to path '" << toString(*p) << "', because parent directory " << "could not be created."); | |
| 119 | |||
| 120 | ✗ | return false; | |
| 121 | 1 | } | |
| 122 | |||
| 123 | 1 | bool CSVFile_Impl::saveAs(const openstudio::path& p) { | |
| 124 | 1 | setPath(p); | |
| 125 | 1 | return save(); | |
| 126 | } | ||
| 127 | |||
| 128 | 1 | boost::optional<openstudio::path> CSVFile_Impl::path() const { | |
| 129 | 1 | return m_path; | |
| 130 | } | ||
| 131 | |||
| 132 | 1 | bool CSVFile_Impl::setPath(const openstudio::path& path) { | |
| 133 | 1 | m_path = path; | |
| 134 | 1 | return true; | |
| 135 | } | ||
| 136 | |||
| 137 | ✗ | void CSVFile_Impl::resetPath() { | |
| 138 | ✗ | m_path.reset(); | |
| 139 | ✗ | } | |
| 140 | |||
| 141 | 21 | unsigned CSVFile_Impl::numColumns() const { | |
| 142 | 21 | return m_numColumns; | |
| 143 | } | ||
| 144 | |||
| 145 | 8 | unsigned CSVFile_Impl::numRows() const { | |
| 146 | 8 | return m_rows.size(); | |
| 147 | } | ||
| 148 | |||
| 149 | 4 | std::vector<std::vector<Variant>> CSVFile_Impl::rows() const { | |
| 150 | 4 | return m_rows; | |
| 151 | } | ||
| 152 | |||
| 153 | 6 | void CSVFile_Impl::addRow(const std::vector<Variant>& row) { | |
| 154 | 6 | m_rows.push_back(row); | |
| 155 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
|
6 | if (row.size() > m_numColumns) { |
| 156 | 4 | m_numColumns = row.size(); | |
| 157 | 4 | padRows(); | |
| 158 | } | ||
| 159 | |||
| 160 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 4 taken 6 times.
|
9 | while (m_rows[m_rows.size() - 1].size() < m_numColumns) { |
| 161 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 6 taken 3 times.
|
3 | m_rows[m_rows.size() - 1].push_back(Variant("")); |
| 162 | } | ||
| 163 | 6 | } | |
| 164 | |||
| 165 | ✗ | void CSVFile_Impl::setRows(const std::vector<std::vector<Variant>>& rows) { | |
| 166 | ✗ | m_rows = rows; | |
| 167 | ✗ | assignNumColumns(); | |
| 168 | ✗ | } | |
| 169 | |||
| 170 | 1 | void CSVFile_Impl::clear() { | |
| 171 | 1 | m_rows.clear(); | |
| 172 | 1 | m_path.reset(); | |
| 173 | 1 | m_numColumns = 0; | |
| 174 | 1 | } | |
| 175 | |||
| 176 | 2 | unsigned CSVFile_Impl::addColumn(const std::vector<DateTime>& dateTimes) { | |
| 177 | 2 | unsigned n = dateTimes.size(); | |
| 178 | 2 | ensureNumRows(n); | |
| 179 | |||
| 180 | 2 | unsigned numRows = m_rows.size(); | |
| 181 |
2/2✓ Branch 0 taken 8763 times.
✓ Branch 1 taken 2 times.
|
8765 | for (unsigned i = 0; i < numRows; ++i) { |
| 182 |
1/2✓ Branch 0 taken 8763 times.
✗ Branch 1 not taken.
|
8763 | if (i < n) { |
| 183 |
3/3✓ Branch 3 taken 8763 times.
✓ Branch 6 taken 8763 times.
✓ Branch 9 taken 8763 times.
|
8763 | m_rows[i].push_back(Variant(dateTimes[i].toISO8601())); |
| 184 | } else { | ||
| 185 | ✗ | m_rows[i].push_back(Variant("")); | |
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | 2 | ++m_numColumns; | |
| 190 | 2 | return m_numColumns; | |
| 191 | } | ||
| 192 | |||
| 193 | 2 | unsigned CSVFile_Impl::addColumn(const Vector& values) { | |
| 194 | 2 | unsigned n = values.size(); | |
| 195 | 2 | ensureNumRows(n); | |
| 196 | |||
| 197 | 2 | unsigned numRows = m_rows.size(); | |
| 198 |
2/2✓ Branch 0 taken 8763 times.
✓ Branch 1 taken 2 times.
|
8765 | for (unsigned i = 0; i < numRows; ++i) { |
| 199 |
1/2✓ Branch 0 taken 8763 times.
✗ Branch 1 not taken.
|
8763 | if (i < n) { |
| 200 |
3/3✓ Branch 2 taken 8763 times.
✓ Branch 5 taken 8763 times.
✓ Branch 8 taken 8763 times.
|
8763 | m_rows[i].push_back(Variant(values[i])); |
| 201 | } else { | ||
| 202 | ✗ | m_rows[i].push_back(Variant("")); | |
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | 2 | ++m_numColumns; | |
| 207 | 2 | return m_numColumns; | |
| 208 | } | ||
| 209 | |||
| 210 | 1 | unsigned CSVFile_Impl::addColumn(const std::vector<double>& values) { | |
| 211 | 1 | unsigned n = values.size(); | |
| 212 | 1 | ensureNumRows(n); | |
| 213 | |||
| 214 | 1 | unsigned numRows = m_rows.size(); | |
| 215 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
|
4 | for (unsigned i = 0; i < numRows; ++i) { |
| 216 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (i < n) { |
| 217 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 6 taken 3 times.
|
3 | m_rows[i].push_back(Variant(values[i])); |
| 218 | } else { | ||
| 219 | ✗ | m_rows[i].push_back(Variant("")); | |
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | 1 | ++m_numColumns; | |
| 224 | 1 | return m_numColumns; | |
| 225 | } | ||
| 226 | |||
| 227 | 1 | unsigned CSVFile_Impl::addColumn(const std::vector<std::string>& values) { | |
| 228 | 1 | unsigned n = values.size(); | |
| 229 | 1 | ensureNumRows(n); | |
| 230 | |||
| 231 | 1 | unsigned numRows = m_rows.size(); | |
| 232 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
|
4 | for (unsigned i = 0; i < numRows; ++i) { |
| 233 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (i < n) { |
| 234 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 6 taken 3 times.
|
3 | m_rows[i].push_back(Variant(values[i])); |
| 235 | } else { | ||
| 236 | ✗ | m_rows[i].push_back(Variant("")); | |
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | 1 | ++m_numColumns; | |
| 241 | 1 | return m_numColumns; | |
| 242 | } | ||
| 243 | |||
| 244 | 1 | std::vector<DateTime> CSVFile_Impl::getColumnAsDateTimes(unsigned columnIndex) const { | |
| 245 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | if (columnIndex >= m_numColumns) { |
| 246 | ✗ | LOG(Warn, "Column index " << columnIndex << " invalid for number of columns " << m_numColumns); | |
| 247 | ✗ | return {}; | |
| 248 | } | ||
| 249 | |||
| 250 | 1 | std::vector<DateTime> result; | |
| 251 | |||
| 252 | 1 | unsigned numRows = m_rows.size(); | |
| 253 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
|
4 | for (unsigned i = 0; i < numRows; ++i) { |
| 254 |
3/4✓ Branch 1 taken 3 times.
✓ Branch 6 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
|
3 | if (m_rows[i][columnIndex].variantType() != VariantType::String) { |
| 255 | ✗ | LOG(Warn, "Value at row " << i << " and column " << columnIndex << " is not a DateTime string"); | |
| 256 | ✗ | return {}; | |
| 257 | } | ||
| 258 | |||
| 259 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 6 taken 3 times.
|
3 | boost::optional<DateTime> dateTime = DateTime::fromISO8601(m_rows[i][columnIndex].valueAsString()); |
| 260 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (!dateTime) { |
| 261 | ✗ | LOG(Warn, "Value at row " << i << " and column " << columnIndex << " is not a DateTime string"); | |
| 262 | ✗ | return {}; | |
| 263 | } | ||
| 264 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | result.push_back(*dateTime); |
| 265 | 3 | } | |
| 266 | |||
| 267 | 1 | return result; | |
| 268 | 1 | } | |
| 269 | |||
| 270 | 2 | std::vector<double> CSVFile_Impl::getColumnAsDoubleVector(unsigned columnIndex) const { | |
| 271 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (columnIndex >= m_numColumns) { |
| 272 | ✗ | LOG(Warn, "Column index " << columnIndex << " invalid for number of columns " << m_numColumns); | |
| 273 | ✗ | return {}; | |
| 274 | } | ||
| 275 | |||
| 276 | 2 | std::vector<double> result; | |
| 277 | |||
| 278 | 2 | unsigned numRows = m_rows.size(); | |
| 279 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
|
8 | for (unsigned i = 0; i < numRows; ++i) { |
| 280 | |||
| 281 | 6 | boost::optional<double> value; | |
| 282 |
3/4✓ Branch 1 taken 6 times.
✓ Branch 6 taken 6 times.
✓ Branch 9 taken 6 times.
✗ Branch 10 not taken.
|
6 | if (m_rows[i][columnIndex].variantType() == VariantType::Double) { |
| 283 |
2/2✓ Branch 3 taken 6 times.
✓ Branch 6 taken 6 times.
|
6 | value = m_rows[i][columnIndex].valueAsDouble(); |
| 284 | ✗ | } else if (m_rows[i][columnIndex].variantType() == VariantType::Integer) { | |
| 285 | ✗ | value = m_rows[i][columnIndex].valueAsInteger(); | |
| 286 | } | ||
| 287 | |||
| 288 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if (!value) { |
| 289 | ✗ | LOG(Warn, "Value at row " << i << " and column " << columnIndex << " is not a numeric value"); | |
| 290 | ✗ | return {}; | |
| 291 | } | ||
| 292 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
|
6 | result.push_back(*value); |
| 293 | } | ||
| 294 | |||
| 295 | 2 | return result; | |
| 296 | 2 | } | |
| 297 | |||
| 298 | 1 | std::vector<std::string> CSVFile_Impl::getColumnAsStringVector(unsigned columnIndex) const { | |
| 299 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | if (columnIndex >= m_numColumns) { |
| 300 | ✗ | LOG(Warn, "Column index " << columnIndex << " invalid for number of columns " << m_numColumns); | |
| 301 | ✗ | return {}; | |
| 302 | } | ||
| 303 | |||
| 304 | 1 | std::vector<std::string> result; | |
| 305 | |||
| 306 | 1 | unsigned numRows = m_rows.size(); | |
| 307 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
|
4 | for (unsigned i = 0; i < numRows; ++i) { |
| 308 | |||
| 309 |
3/4✓ Branch 1 taken 3 times.
✓ Branch 6 taken 3 times.
✓ Branch 9 taken 3 times.
✗ Branch 10 not taken.
|
3 | if (m_rows[i][columnIndex].variantType() == VariantType::String) { |
| 310 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 6 taken 3 times.
|
3 | result.push_back(m_rows[i][columnIndex].valueAsString()); |
| 311 | ✗ | } else if (m_rows[i][columnIndex].variantType() == VariantType::Double) { | |
| 312 | ✗ | std::stringstream ss; | |
| 313 | ✗ | ss << m_rows[i][columnIndex].valueAsDouble(); | |
| 314 | ✗ | result.push_back(ss.str()); | |
| 315 | ✗ | } else if (m_rows[i][columnIndex].variantType() == VariantType::Integer) { | |
| 316 | ✗ | std::stringstream ss; | |
| 317 | ✗ | ss << m_rows[i][columnIndex].valueAsInteger(); | |
| 318 | ✗ | result.push_back(ss.str()); | |
| 319 | ✗ | } | |
| 320 | } | ||
| 321 | |||
| 322 | 1 | return result; | |
| 323 | 1 | } | |
| 324 | |||
| 325 | // throws on error | ||
| 326 | 4 | std::vector<std::vector<Variant>> CSVFile_Impl::parseRows(std::istream& input) { | |
| 327 | 4 | std::vector<std::vector<Variant>> result; | |
| 328 | |||
| 329 | // DLM: what conditions should make this throw? | ||
| 330 | |||
| 331 | // Excel formated CSV regex, \A[^,"]*(?=,)|(?:[^",]*"[^"]*"[^",]*)+|[^",]*"[^"]*\Z|(?<=,)[^,]*(?=,)|(?<=,)[^,]*\Z|\A[^,]*\Z | ||
| 332 | static const boost::regex csvRegex( | ||
| 333 |
4/7✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
4 | "\\A[^,\"]*(?=,)|(?:[^\",]*\"[^\"]*\"[^\",]*)+|[^\",]*\"[^\"]*\\Z|(?<=,)[^,]*(?=,)|(?<=,)[^,]*\\Z|\\A[^,]*\\Z"); |
| 334 |
4/7✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
4 | static const boost::regex intRegex("^[-0-9]+$"); |
| 335 |
4/7✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
4 | static const boost::regex doubleRegex("^[+-]?\\d+\\.?(\\d+)?$"); |
| 336 |
4/7✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
4 | static const boost::regex quoteRegex("^\"(.*)\"$"); |
| 337 | |||
| 338 | 4 | std::string line; | |
| 339 |
4/4✓ Branch 1 taken 17546 times.
✓ Branch 4 taken 17546 times.
✓ Branch 6 taken 17542 times.
✓ Branch 7 taken 4 times.
|
17546 | while (std::getline(input, line)) { |
| 340 | 17542 | std::vector<Variant> row; | |
| 341 |
1/1✓ Branch 3 taken 17542 times.
|
17542 | boost::regex_token_iterator<std::string::iterator> it{line.begin(), line.end(), csvRegex, 0}; |
| 342 | 17542 | boost::regex_token_iterator<std::string::iterator> end; | |
| 343 |
3/3✓ Branch 1 taken 376925 times.
✓ Branch 3 taken 359383 times.
✓ Branch 4 taken 17542 times.
|
376925 | while (it != end) { |
| 344 |
2/2✓ Branch 1 taken 359383 times.
✓ Branch 4 taken 359383 times.
|
359383 | std::string value = *it; |
| 345 |
3/3✓ Branch 1 taken 359383 times.
✓ Branch 3 taken 227786 times.
✓ Branch 4 taken 131597 times.
|
359383 | if (boost::regex_match(value, intRegex)) { |
| 346 |
3/3✓ Branch 1 taken 227786 times.
✓ Branch 4 taken 227786 times.
✓ Branch 7 taken 227786 times.
|
227786 | row.push_back(Variant(std::stoi(value))); |
| 347 |
3/3✓ Branch 1 taken 131597 times.
✓ Branch 3 taken 78933 times.
✓ Branch 4 taken 52664 times.
|
131597 | } else if (boost::regex_match(value, doubleRegex)) { |
| 348 |
3/3✓ Branch 1 taken 78933 times.
✓ Branch 4 taken 78933 times.
✓ Branch 7 taken 78933 times.
|
78933 | row.push_back(Variant(std::stod(value))); |
| 349 | } else { | ||
| 350 |
1/1✓ Branch 1 taken 52664 times.
|
52664 | boost::smatch valueMatch; |
| 351 |
3/3✓ Branch 1 taken 52664 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 52662 times.
|
52664 | if (boost::regex_search(value, valueMatch, quoteRegex)) { |
| 352 |
4/4✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
|
2 | row.push_back(Variant(valueMatch[1])); |
| 353 | } else { | ||
| 354 |
2/2✓ Branch 1 taken 52662 times.
✓ Branch 4 taken 52662 times.
|
52662 | row.push_back(Variant(value)); |
| 355 | } | ||
| 356 | 52664 | } | |
| 357 |
1/1✓ Branch 1 taken 359383 times.
|
359383 | ++it; |
| 358 | 359383 | } | |
| 359 |
1/1✓ Branch 1 taken 17542 times.
|
17542 | result.push_back(row); |
| 360 | 17542 | } | |
| 361 | |||
| 362 | 8 | return result; | |
| 363 | 4 | } | |
| 364 | |||
| 365 | 4 | void CSVFile_Impl::assignNumColumns() { | |
| 366 | 4 | m_numColumns = 0; | |
| 367 |
2/2✓ Branch 4 taken 17542 times.
✓ Branch 5 taken 4 times.
|
17546 | for (const auto& row : m_rows) { |
| 368 | 17542 | m_numColumns = std::max<unsigned>(m_numColumns, row.size()); | |
| 369 | } | ||
| 370 | 4 | } | |
| 371 | |||
| 372 | 6 | void CSVFile_Impl::ensureNumRows(unsigned numRows) { | |
| 373 | // add empty cells to existing columns if needed | ||
| 374 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
|
6 | if (numRows > m_rows.size()) { |
| 375 | 2 | unsigned numRowsToAdd = numRows - m_rows.size(); | |
| 376 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
2 | std::vector<Variant> blankRow(m_numColumns, Variant("")); |
| 377 |
2/2✓ Branch 0 taken 8763 times.
✓ Branch 1 taken 2 times.
|
8765 | for (unsigned i = 0; i < numRowsToAdd; ++i) { |
| 378 |
1/1✓ Branch 1 taken 8763 times.
|
8763 | m_rows.push_back(blankRow); |
| 379 | } | ||
| 380 | 2 | } | |
| 381 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | OS_ASSERT(m_rows.size() >= numRows); |
| 382 | 6 | } | |
| 383 | |||
| 384 | 8 | void CSVFile_Impl::padRows() { | |
| 385 |
2/2✓ Branch 5 taken 17549 times.
✓ Branch 6 taken 8 times.
|
17557 | for (auto& row : m_rows) { |
| 386 |
2/2✓ Branch 1 taken 306994 times.
✓ Branch 2 taken 17549 times.
|
324543 | while (row.size() < m_numColumns) { |
| 387 |
2/2✓ Branch 1 taken 306994 times.
✓ Branch 4 taken 306994 times.
|
306994 | row.push_back(Variant("")); |
| 388 | } | ||
| 389 | } | ||
| 390 | 8 | } | |
| 391 | |||
| 392 | } // namespace detail | ||
| 393 | |||
| 394 |
2/4✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
3 | CSVFile::CSVFile() : m_impl(std::shared_ptr<detail::CSVFile_Impl>(new detail::CSVFile_Impl())) {} |
| 395 | |||
| 396 |
2/4✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
1 | CSVFile::CSVFile(const std::string& s) : m_impl(std::shared_ptr<detail::CSVFile_Impl>(new detail::CSVFile_Impl(s))) {} |
| 397 | |||
| 398 |
2/4✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
3 | CSVFile::CSVFile(const openstudio::path& p) : m_impl(std::shared_ptr<detail::CSVFile_Impl>(new detail::CSVFile_Impl(p))) {} |
| 399 | |||
| 400 | ✗ | CSVFile::CSVFile(std::shared_ptr<detail::CSVFile_Impl> impl) : m_impl(impl) {} | |
| 401 | |||
| 402 | ✗ | CSVFile CSVFile::clone() const { | |
| 403 | ✗ | return getImpl<detail::CSVFile_Impl>()->clone(); | |
| 404 | } | ||
| 405 | |||
| 406 | ✗ | boost::optional<CSVFile> CSVFile::load(const std::string& s) { | |
| 407 | ✗ | boost::optional<CSVFile> result; | |
| 408 | try { | ||
| 409 | ✗ | result = CSVFile(s); | |
| 410 | ✗ | } catch (const std::exception&) { | |
| 411 | ✗ | } | |
| 412 | ✗ | return result; | |
| 413 | ✗ | } | |
| 414 | |||
| 415 | 3 | boost::optional<CSVFile> CSVFile::load(const openstudio::path& p) { | |
| 416 | 3 | boost::optional<CSVFile> result; | |
| 417 | try { | ||
| 418 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | result = CSVFile(p); |
| 419 | ✗ | } catch (const std::exception&) { | |
| 420 | ✗ | } | |
| 421 | 3 | return result; | |
| 422 | ✗ | } | |
| 423 | |||
| 424 | 3 | std::string CSVFile::string() const { | |
| 425 |
1/1✓ Branch 3 taken 3 times.
|
3 | return getImpl<detail::CSVFile_Impl>()->string(); |
| 426 | } | ||
| 427 | |||
| 428 | ✗ | bool CSVFile::save() const { | |
| 429 | ✗ | return getImpl<detail::CSVFile_Impl>()->save(); | |
| 430 | } | ||
| 431 | |||
| 432 | 1 | bool CSVFile::saveAs(const openstudio::path& p) { | |
| 433 |
1/1✓ Branch 3 taken 1 time.
|
1 | return getImpl<detail::CSVFile_Impl>()->saveAs(p); |
| 434 | } | ||
| 435 | |||
| 436 | ✗ | boost::optional<openstudio::path> CSVFile::path() const { | |
| 437 | ✗ | return getImpl<detail::CSVFile_Impl>()->path(); | |
| 438 | } | ||
| 439 | |||
| 440 | ✗ | bool CSVFile::setPath(const openstudio::path& path) { | |
| 441 | ✗ | return getImpl<detail::CSVFile_Impl>()->setPath(path); | |
| 442 | } | ||
| 443 | |||
| 444 | ✗ | void CSVFile::resetPath() { | |
| 445 | ✗ | getImpl<detail::CSVFile_Impl>()->resetPath(); | |
| 446 | ✗ | } | |
| 447 | |||
| 448 | 21 | unsigned CSVFile::numColumns() const { | |
| 449 |
1/1✓ Branch 3 taken 21 times.
|
21 | return getImpl<detail::CSVFile_Impl>()->numColumns(); |
| 450 | } | ||
| 451 | |||
| 452 | 8 | unsigned CSVFile::numRows() const { | |
| 453 |
1/1✓ Branch 3 taken 8 times.
|
8 | return getImpl<detail::CSVFile_Impl>()->numRows(); |
| 454 | } | ||
| 455 | |||
| 456 | 4 | std::vector<std::vector<Variant>> CSVFile::rows() const { | |
| 457 |
1/1✓ Branch 3 taken 4 times.
|
4 | return getImpl<detail::CSVFile_Impl>()->rows(); |
| 458 | } | ||
| 459 | |||
| 460 | 6 | void CSVFile::addRow(const std::vector<Variant>& row) { | |
| 461 |
1/1✓ Branch 3 taken 6 times.
|
6 | getImpl<detail::CSVFile_Impl>()->addRow(row); |
| 462 | 6 | } | |
| 463 | |||
| 464 | ✗ | void CSVFile::setRows(const std::vector<std::vector<Variant>>& rows) { | |
| 465 | ✗ | getImpl<detail::CSVFile_Impl>()->setRows(rows); | |
| 466 | ✗ | } | |
| 467 | |||
| 468 | 1 | void CSVFile::clear() { | |
| 469 |
1/1✓ Branch 3 taken 1 time.
|
1 | getImpl<detail::CSVFile_Impl>()->clear(); |
| 470 | 1 | } | |
| 471 | |||
| 472 | 2 | unsigned CSVFile::addColumn(const std::vector<DateTime>& dateTimes) { | |
| 473 |
1/1✓ Branch 3 taken 2 times.
|
2 | return getImpl<detail::CSVFile_Impl>()->addColumn(dateTimes); |
| 474 | } | ||
| 475 | |||
| 476 | 2 | unsigned CSVFile::addColumn(const Vector& values) { | |
| 477 |
1/1✓ Branch 3 taken 2 times.
|
2 | return getImpl<detail::CSVFile_Impl>()->addColumn(values); |
| 478 | } | ||
| 479 | |||
| 480 | 1 | unsigned CSVFile::addColumn(const std::vector<double>& values) { | |
| 481 |
1/1✓ Branch 3 taken 1 time.
|
1 | return getImpl<detail::CSVFile_Impl>()->addColumn(values); |
| 482 | } | ||
| 483 | |||
| 484 | 1 | unsigned CSVFile::addColumn(const std::vector<std::string>& values) { | |
| 485 |
1/1✓ Branch 3 taken 1 time.
|
1 | return getImpl<detail::CSVFile_Impl>()->addColumn(values); |
| 486 | } | ||
| 487 | |||
| 488 | 1 | std::vector<DateTime> CSVFile::getColumnAsDateTimes(unsigned columnIndex) const { | |
| 489 |
1/1✓ Branch 3 taken 1 time.
|
1 | return getImpl<detail::CSVFile_Impl>()->getColumnAsDateTimes(columnIndex); |
| 490 | } | ||
| 491 | |||
| 492 | 2 | std::vector<double> CSVFile::getColumnAsDoubleVector(unsigned columnIndex) const { | |
| 493 |
1/1✓ Branch 3 taken 2 times.
|
2 | return getImpl<detail::CSVFile_Impl>()->getColumnAsDoubleVector(columnIndex); |
| 494 | } | ||
| 495 | |||
| 496 | 1 | std::vector<std::string> CSVFile::getColumnAsStringVector(unsigned columnIndex) const { | |
| 497 |
1/1✓ Branch 3 taken 1 time.
|
1 | return getImpl<detail::CSVFile_Impl>()->getColumnAsStringVector(columnIndex); |
| 498 | } | ||
| 499 | |||
| 500 | ✗ | std::ostream& operator<<(std::ostream& os, const CSVFile& csvFile) { | |
| 501 | ✗ | os << csvFile.string(); | |
| 502 | ✗ | return os; | |
| 503 | } | ||
| 504 | |||
| 505 | } // namespace openstudio | ||
| 506 |