summaryrefslogtreecommitdiff
path: root/src/profile.cpp
diff options
context:
space:
mode:
authorJonathan Feenstra <26406078+JonathanFeenstra@users.noreply.github.com>2026-05-17 09:28:30 +0200
committerGitHub <noreply@github.com>2026-05-17 02:28:30 -0500
commitab38cf0e059c15af82da7e48045823a5cbbb5d52 (patch)
tree314a78b4e8bfe3ceee91474403b153c55cbf5bd6 /src/profile.cpp
parent0c95bf22e6be661fbed83cbc7ac0a43596bc80f4 (diff)
Improve code for reading text files line-by-line
Diffstat (limited to 'src/profile.cpp')
-rw-r--r--src/profile.cpp28
1 files changed, 13 insertions, 15 deletions
diff --git a/src/profile.cpp b/src/profile.cpp
index 2d8247a7..001c9816 100644
--- a/src/profile.cpp
+++ b/src/profile.cpp
@@ -333,7 +333,7 @@ void Profile::renameModInAllProfiles(const QString& oldName, const QString& newN
void Profile::renameModInList(QFile& modList, const QString& oldName,
const QString& newName)
{
- if (!modList.open(QIODevice::ReadOnly)) {
+ if (!modList.open(QIODevice::ReadOnly | QIODevice::Text)) {
reportError(tr("failed to open %1").arg(modList.fileName()));
return;
}
@@ -342,10 +342,12 @@ void Profile::renameModInList(QFile& modList, const QString& oldName,
outBuffer.open(QIODevice::WriteOnly);
int renamed = 0;
- while (!modList.atEnd()) {
- QByteArray line = modList.readLine();
-
- if (line.length() == 0) {
+ // trim empty line at the end which should not log a warning about invalid data
+ const QByteArray contents = modList.readAll().trimmed();
+ modList.close();
+ // modList is CRLF, but the QIODevice::Text flag translates all line breaks to LF
+ for (const QByteArray& line : contents.split('\n')) {
+ if (line.isEmpty()) {
// ignore empty lines
log::warn("mod list contained invalid data: empty line");
continue;
@@ -374,7 +376,6 @@ void Profile::renameModInList(QFile& modList, const QString& oldName,
outBuffer.write(qUtf8Printable(modName));
outBuffer.write("\r\n");
}
- modList.close();
if (renamed) {
modList.open(QIODevice::WriteOnly);
@@ -438,14 +439,14 @@ void Profile::refreshModStatus()
bool warnAboutOverwrite = false;
// load mods from file and update enabled state and priority for them
- int index = 0;
- while (!file.atEnd()) {
- QByteArray line = file.readLine().trimmed();
-
+ int index = 0;
+ const QByteArray contents = file.readAll();
+ file.close();
+ for (QByteArray& line : contents.split('\n')) {
// find the mod name and the enabled status
bool enabled = true;
QString modName;
- if (line.length() == 0) {
+ if (line.isEmpty()) {
// empty line
continue;
} else if (line.at(0) == '#') {
@@ -498,10 +499,7 @@ void Profile::refreshModStatus()
// need to rewrite the modlist to fix this
modStatusModified = true;
}
-
- } // while (!file.atEnd())
-
- file.close();
+ }
const int numKnownMods = index;
int topInsert = 0;