diff options
| author | Jonathan Feenstra <26406078+JonathanFeenstra@users.noreply.github.com> | 2026-05-17 09:28:30 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-05-17 02:28:30 -0500 |
| commit | ab38cf0e059c15af82da7e48045823a5cbbb5d52 (patch) | |
| tree | 314a78b4e8bfe3ceee91474403b153c55cbf5bd6 /src/categories.cpp | |
| parent | 0c95bf22e6be661fbed83cbc7ac0a43596bc80f4 (diff) | |
Improve code for reading text files line-by-line
Diffstat (limited to 'src/categories.cpp')
| -rw-r--r-- | src/categories.cpp | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/src/categories.cpp b/src/categories.cpp index 48ee4bc8..b5e25538 100644 --- a/src/categories.cpp +++ b/src/categories.cpp @@ -57,13 +57,17 @@ void CategoryFactory::loadCategories() QFile categoryFile(categoriesFilePath()); bool needLoad = false; - if (!categoryFile.open(QIODevice::ReadOnly)) { + if (!categoryFile.open(QIODevice::ReadOnly | QIODevice::Text)) { needLoad = true; } else { - int lineNum = 0; - while (!categoryFile.atEnd()) { - QByteArray line = categoryFile.readLine(); - ++lineNum; + const auto lines = categoryFile.readAll().split('\n'); + categoryFile.close(); + for (int lineNum = 0; lineNum < lines.size(); ++lineNum) { + const auto& line = lines[lineNum]; + if (line.isEmpty()) { + continue; + } + QList<QByteArray> cells = line.split('|'); if (cells.count() == 4) { std::vector<NexusCategory> nexusCats; @@ -76,7 +80,7 @@ void CategoryFactory::loadCategories() if (!ok) { log::error(tr("invalid category id {0}"), iter->constData()); } - nexusCats.push_back(NexusCategory("Unknown", temp)); + nexusCats.emplace_back("Unknown", temp); } } bool cell0Ok = true; @@ -103,16 +107,19 @@ void CategoryFactory::loadCategories() line.constData(), cells.count()); } } - categoryFile.close(); QFile nexusMapFile(nexusMappingFilePath()); - if (!nexusMapFile.open(QIODevice::ReadOnly)) { + if (!nexusMapFile.open(QIODevice::ReadOnly | QIODevice::Text)) { needLoad = true; } else { - int nexLineNum = 0; - while (!nexusMapFile.atEnd()) { - QByteArray nexLine = nexusMapFile.readLine(); - ++nexLineNum; + const auto nexLines = nexusMapFile.readAll().split('\n'); + nexusMapFile.close(); + for (int nexLineNum = 0; nexLineNum < nexLines.size(); ++nexLineNum) { + const auto& nexLine = nexLines[nexLineNum]; + if (nexLine.isEmpty()) { + continue; + } + QList<QByteArray> nexCells = nexLine.split('|'); if (nexCells.count() == 3) { std::vector<NexusCategory> nexusCats; @@ -129,12 +136,11 @@ void CategoryFactory::loadCategories() m_NexusMap.insert_or_assign(nexID, NexusCategory(nexName, nexID)); m_NexusMap.at(nexID).setCategoryID(catID); } else { - log::error(tr("invalid nexus category line {0}: {1} ({2} cells)"), lineNum, + log::error(tr("invalid nexus category line {0}: {1} ({2} cells)"), nexLineNum, nexLine.constData(), nexCells.count()); } } } - nexusMapFile.close(); } std::sort(m_Categories.begin(), m_Categories.end()); setParents(); |
