You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.9 KiB
67 lines
1.9 KiB
// config_updater.cpp — Universal INI config file updater |
|
#include "config_updater.h" |
|
#include <QFile> |
|
#include <QTextStream> |
|
|
|
bool config_update_option(const QString& filePath, const QString& section, |
|
const QString& param, const QString& value, int flags) |
|
{ |
|
QStringList lines; |
|
|
|
QFile f(filePath); |
|
if (f.exists()) { |
|
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
return false; |
|
QTextStream in(&f); |
|
while (!in.atEnd()) |
|
lines.append(in.readLine()); |
|
f.close(); |
|
} |
|
|
|
const QString sectionHdr = "[" + section + "]"; |
|
const QString newLine = param + "=" + value; |
|
|
|
int secIdx = -1; |
|
for (int i = 0; i < lines.size(); i++) { |
|
if (lines[i].trimmed() == sectionHdr) { secIdx = i; break; } |
|
} |
|
|
|
int secEnd = lines.size(); |
|
if (secIdx >= 0) { |
|
for (int i = secIdx + 1; i < lines.size(); i++) { |
|
if (lines[i].trimmed().startsWith('[')) { secEnd = i; break; } |
|
} |
|
} |
|
|
|
if (secIdx >= 0) { |
|
int ins = secEnd; |
|
while (ins > secIdx + 1 && lines[ins - 1].trimmed().isEmpty()) |
|
ins--; |
|
|
|
if (flags == CONFIG_UPDATE_REPLACE) { |
|
int paramIdx = -1; |
|
for (int i = secIdx + 1; i < secEnd; i++) { |
|
if (lines[i].trimmed().startsWith(param + "=")) { paramIdx = i; break; } |
|
} |
|
if (paramIdx >= 0) |
|
lines[paramIdx] = newLine; |
|
else |
|
lines.insert(ins, newLine); |
|
} else { |
|
lines.insert(ins, newLine); |
|
} |
|
} else { |
|
if (!lines.isEmpty() && !lines.last().trimmed().isEmpty()) |
|
lines.append(""); |
|
lines.append(sectionHdr); |
|
lines.append(newLine); |
|
} |
|
|
|
if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) |
|
return false; |
|
QTextStream out(&f); |
|
for (const QString& line : lines) |
|
out << line << "\n"; |
|
f.close(); |
|
return true; |
|
}
|
|
|