MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / importTableFromCsv

Method importTableFromCsv

app/src/DataModel/ProjectModel.cpp:5736–5819  ·  view source on GitHub ↗

* @brief Imports registers from a CSV file into an existing table, accepting both the * read-write/read-only permission terms and the legacy computed/constant type values. */

Source from the content-addressed store, hash-verified

5734 * read-write/read-only permission terms and the legacy computed/constant type values.
5735 */
5736void DataModel::ProjectModel::importTableFromCsv(const QString& tableName)
5737{
5738 const int idx = findTableIndexByPath(tableName);
5739 if (idx < 0)
5740 return;
5741
5742 auto it = m_tables.begin() + idx;
5743
5744 const auto path = QFileDialog::getOpenFileName(nullptr,
5745 tr("Import Table"),
5746 Misc::WorkspaceManager::instance().path("CSV"),
5747 tr("CSV files (*.csv)"));
5748
5749 if (path.isEmpty())
5750 return;
5751
5752 QFile file(path);
5753 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
5754 return;
5755
5756 QTextStream in(&file);
5757
5758 if (!in.atEnd())
5759 in.readLine();
5760
5761 constexpr int kMaxImportRows = 1'000'000;
5762
5763 int imported = 0;
5764 int rowsRead = 0;
5765 while (!in.atEnd() && rowsRead < kMaxImportRows) {
5766 ++rowsRead;
5767 const auto line = in.readLine().trimmed();
5768 if (line.isEmpty())
5769 continue;
5770
5771 const auto parts = line.split(',');
5772 if (parts.size() < 3)
5773 continue;
5774
5775 const auto name = parts[0].trimmed();
5776 const auto permsStr = parts[1].trimmed().toLower();
5777 auto valStr = parts.mid(2).join(',').trimmed();
5778 if (valStr.size() >= 2 && valStr.startsWith('"') && valStr.endsWith('"')) {
5779 valStr = valStr.mid(1, valStr.size() - 2);
5780 valStr.replace(QStringLiteral("\"\""), QStringLiteral("\""));
5781 }
5782
5783 if (name.isEmpty())
5784 continue;
5785
5786 const bool computed =
5787 (permsStr == QStringLiteral("read-write")) || (permsStr == QStringLiteral("read/write"))
5788 || (permsStr == QStringLiteral("rw")) || (permsStr == QStringLiteral("computed"));
5789
5790 bool isNumeric = false;
5791 const double dval = SerialStudio::toDouble(valStr, &isNumeric);
5792 const QVariant defaultValue = isNumeric ? QVariant(dval) : QVariant(valStr);
5793

Callers

nothing calls this directly

Calls 7

beginMethod · 0.80
isEmptyMethod · 0.80
pathMethod · 0.45
openMethod · 0.45
atEndMethod · 0.45
sizeMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected