Properly detect UTF-8 BOM markers in ini files
If we detect a utf8 BOM mark at the beginning of the .ini file, skip the marker and set the iniCodec to utf8. Task-number: QTBUG-23381 Change-Id: I1b37fc4f1638a48e4f3ee71ab165e2989bc592f1 Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>bb10
parent
ad1ec8b57d
commit
38c2e3d104
|
|
@ -1637,6 +1637,15 @@ bool QConfFileSettingsPrivate::readIniFile(const QByteArray &data,
|
|||
int sectionPosition = 0;
|
||||
bool ok = true;
|
||||
|
||||
#ifndef QT_NO_TEXTCODEC
|
||||
// detect utf8 BOM
|
||||
const uchar *dd = (const uchar *)data.constData();
|
||||
if (data.size() >= 3 && dd[0] == 0xef && dd[1] == 0xbb && dd[2] == 0xbf) {
|
||||
iniCodec = QTextCodec::codecForName("UTF-8");
|
||||
dataPos = 3;
|
||||
}
|
||||
#endif
|
||||
|
||||
while (readIniLine(data, dataPos, lineStart, lineLen, equalsPos)) {
|
||||
char ch = data.at(lineStart);
|
||||
if (ch == '[') {
|
||||
|
|
|
|||
|
|
@ -282,7 +282,7 @@ public:
|
|||
bool isWritable() const;
|
||||
QString fileName() const;
|
||||
|
||||
static bool readIniFile(const QByteArray &data, UnparsedSettingsMap *unparsedIniSections);
|
||||
bool readIniFile(const QByteArray &data, UnparsedSettingsMap *unparsedIniSections);
|
||||
static bool readIniSection(const QSettingsKey §ion, const QByteArray &data,
|
||||
ParsedSettingsMap *settingsMap, QTextCodec *codec);
|
||||
static bool readIniLine(const QByteArray &data, int &dataPos, int &lineStart, int &lineLen,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
[section1]
|
||||
foo1=bar1
|
||||
[section2]
|
||||
foo2=bar2
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
<!DOCTYPE RCC><RCC version="1.0">
|
||||
<qresource>
|
||||
<file>resourcefile.ini</file>
|
||||
<file>resourcefile2.ini</file>
|
||||
<file>resourcefile3.ini</file>
|
||||
<file>resourcefile4.ini</file>
|
||||
<file>resourcefile5.ini</file>
|
||||
</qresource>
|
||||
<RCC>
|
||||
<qresource prefix="/">
|
||||
<file>resourcefile.ini</file>
|
||||
<file>resourcefile2.ini</file>
|
||||
<file>resourcefile3.ini</file>
|
||||
<file>resourcefile4.ini</file>
|
||||
<file>resourcefile5.ini</file>
|
||||
<file>bom.ini</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
|
|
|||
|
|
@ -161,6 +161,7 @@ private slots:
|
|||
void testByteArray_data();
|
||||
void testByteArray();
|
||||
void iniCodec();
|
||||
void bom();
|
||||
|
||||
private:
|
||||
const bool m_canWriteNativeSystemSettings;
|
||||
|
|
@ -730,6 +731,15 @@ void tst_QSettings::iniCodec()
|
|||
|
||||
}
|
||||
|
||||
void tst_QSettings::bom()
|
||||
{
|
||||
QSettings s(":/bom.ini", QSettings::IniFormat);
|
||||
QStringList allkeys = s.allKeys();
|
||||
QCOMPARE(allkeys.size(), 2);
|
||||
QVERIFY(allkeys.contains("section1/foo1"));
|
||||
QVERIFY(allkeys.contains("section2/foo2"));
|
||||
}
|
||||
|
||||
void tst_QSettings::testErrorHandling_data()
|
||||
{
|
||||
QTest::addColumn<int>("filePerms"); // -1 means file should not exist
|
||||
|
|
|
|||
Loading…
Reference in New Issue