QSettings:如何从 INI 文件中读取数组
QSettings: How to read array from INI file
我想从 INI 文件中读取逗号分隔的数据。我已经在这里阅读过:
- QSettings::IniFormat values with "," returned as QStringList
- How to read a value using QSetting if the value contains comma character
...逗号被视为分隔符,QSettings 值函数将 return QStringList.
但是,我在 INI 文件中的数据如下所示:
norm-factor=<<eof
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
eof
我不需要整个矩阵。所有连接在一起的行对我来说都足够公平。但是QSettings可以处理这样的结构吗?
我应该使用以下方式阅读:
QStringList norms = ini->value("norm-factor", QStringList()).toStringList();
还是我必须用另一种方式解析它?
换行符是个问题,因为 INI 文件使用换行符作为它们自己的语法。
Qt 似乎不支持您的续行类型 (<<eol ... eol
)。
QSettings s("./inifile", QSettings::IniFormat);
qDebug() << s.value("norm-factor");
产量
QVariant(QString, "<<eof")
<<eol
表达式本身可能是无效的 INI。 (Wikipedia on INI files)
我建议您手动解析文件。
Ronny Brendel 的回答是正确的...我只是添加了解决上述问题的代码...它创建了包含更正数组的临时 INI 文件:
/**
* @param src source INI file
* @param dst destination (fixed) INI file
*/
void fixINI(const QString &src, const QString &dst) const {
// Opens source and destination files
QFile fsrc(src);
QFile fdst(dst);
if (!fsrc.open(QIODevice::ReadOnly)) {
QString msg("Cannot open '" + src + "'' file.");
throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
}
if (!fdst.open(QIODevice::WriteOnly)) {
QString msg("Cannot open '" + dst + "'' file.");
throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
}
// Stream
QTextStream in(&fsrc);
QTextStream out(&fdst);
bool arrayMode = false;
QString cache;
while (!in.atEnd()) {
// Read current line
QString line = in.readLine();
// Enables array mode
// NOTE: Clear cache and store 'key=' to it, without '<<eof' text
if (arrayMode == false && line.contains("<<eof")) {
arrayMode = true;
cache = line.remove("<<eof").trimmed();
continue;
}
// Disables array mode
// NOTE: Flush cache into output and remove last ',' separator
if (arrayMode == true && line.trimmed().compare("eof") == 0) {
arrayMode = false;
out << cache.left(cache.length() - 1) << "\n";
continue;
}
// Store line into cache or copy it to output
if (arrayMode) {
cache += line.trimmed() + ",";
} else {
out << line << "\n";
}
}
fsrc.close();
fdst.close();
}
我想从 INI 文件中读取逗号分隔的数据。我已经在这里阅读过:
- QSettings::IniFormat values with "," returned as QStringList
- How to read a value using QSetting if the value contains comma character
...逗号被视为分隔符,QSettings 值函数将 return QStringList.
但是,我在 INI 文件中的数据如下所示:
norm-factor=<<eof
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
eof
我不需要整个矩阵。所有连接在一起的行对我来说都足够公平。但是QSettings可以处理这样的结构吗?
我应该使用以下方式阅读:
QStringList norms = ini->value("norm-factor", QStringList()).toStringList();
还是我必须用另一种方式解析它?
换行符是个问题,因为 INI 文件使用换行符作为它们自己的语法。
Qt 似乎不支持您的续行类型 (<<eol ... eol
)。
QSettings s("./inifile", QSettings::IniFormat);
qDebug() << s.value("norm-factor");
产量
QVariant(QString, "<<eof")
<<eol
表达式本身可能是无效的 INI。 (Wikipedia on INI files)
我建议您手动解析文件。
Ronny Brendel 的回答是正确的...我只是添加了解决上述问题的代码...它创建了包含更正数组的临时 INI 文件:
/**
* @param src source INI file
* @param dst destination (fixed) INI file
*/
void fixINI(const QString &src, const QString &dst) const {
// Opens source and destination files
QFile fsrc(src);
QFile fdst(dst);
if (!fsrc.open(QIODevice::ReadOnly)) {
QString msg("Cannot open '" + src + "'' file.");
throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
}
if (!fdst.open(QIODevice::WriteOnly)) {
QString msg("Cannot open '" + dst + "'' file.");
throw new Exception(NULL, msg, this, __FUNCTION__, __LINE__);
}
// Stream
QTextStream in(&fsrc);
QTextStream out(&fdst);
bool arrayMode = false;
QString cache;
while (!in.atEnd()) {
// Read current line
QString line = in.readLine();
// Enables array mode
// NOTE: Clear cache and store 'key=' to it, without '<<eof' text
if (arrayMode == false && line.contains("<<eof")) {
arrayMode = true;
cache = line.remove("<<eof").trimmed();
continue;
}
// Disables array mode
// NOTE: Flush cache into output and remove last ',' separator
if (arrayMode == true && line.trimmed().compare("eof") == 0) {
arrayMode = false;
out << cache.left(cache.length() - 1) << "\n";
continue;
}
// Store line into cache or copy it to output
if (arrayMode) {
cache += line.trimmed() + ",";
} else {
out << line << "\n";
}
}
fsrc.close();
fdst.close();
}