From 5f7a48a35ef2b3951fcc6317802a2cdd63c47b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 29 Mar 2014 21:16:54 +0100 Subject: [PATCH] Fix issues with badly encoded escape sequences in config files. --- depends/settings/inifile.cpp | 48 +++++++++++++++++++++++++++------ depends/settings/inifile.h | 4 +-- gui/dialogs/EditNotesDialog.cpp | 3 ++- tests/CMakeLists.txt | 1 + tests/tst_inifile.cpp | 43 +++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 tests/tst_inifile.cpp diff --git a/depends/settings/inifile.cpp b/depends/settings/inifile.cpp index 1170f0b1..b2098913 100644 --- a/depends/settings/inifile.cpp +++ b/depends/settings/inifile.cpp @@ -25,17 +25,49 @@ INIFile::INIFile() QString INIFile::unescape(QString orig) { - orig.replace("\\n", "\n"); - orig.replace("\\t", "\t"); - orig.replace("\\\\", "\\"); - return orig; + QString out; + QChar prev = 0; + for(auto c: orig) + { + if(prev == '\\') + { + if(c == 'n') + out += '\n'; + else if (c == 't') + out += '\t'; + else + out += c; + prev = 0; + } + else + { + if(c == '\\') + { + prev = c; + continue; + } + out += c; + prev = 0; + } + } + return out; } + QString INIFile::escape(QString orig) { - orig.replace("\\", "\\\\"); - orig.replace("\n", "\\n"); - orig.replace("\t", "\\t"); - return orig; + QString out; + for(auto c: orig) + { + if(c == '\n') + out += "\\n"; + else if (c == '\t') + out += "\\t"; + else if(c == '\\') + out += "\\\\"; + else + out += c; + } + return out; } bool INIFile::saveFile(QString fileName) diff --git a/depends/settings/inifile.h b/depends/settings/inifile.h index 27da7bf0..4fb0054d 100644 --- a/depends/settings/inifile.h +++ b/depends/settings/inifile.h @@ -33,6 +33,6 @@ public: QVariant get(QString key, QVariant def) const; void set(QString key, QVariant val); - QString unescape(QString orig); - QString escape(QString orig); + static QString unescape(QString orig); + static QString escape(QString orig); }; diff --git a/gui/dialogs/EditNotesDialog.cpp b/gui/dialogs/EditNotesDialog.cpp index a265a4d0..f2aa029f 100644 --- a/gui/dialogs/EditNotesDialog.cpp +++ b/gui/dialogs/EditNotesDialog.cpp @@ -38,5 +38,6 @@ EditNotesDialog::~EditNotesDialog() QString EditNotesDialog::getText() { - return ui->noteEditor->toPlainText(); + QString test = ui->noteEditor->toPlainText(); + return test; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7aeae59f..2236e8fc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -23,6 +23,7 @@ endmacro() add_unit_test(pathutils tst_pathutils.cpp) add_unit_test(userutils tst_userutils.cpp) +add_unit_test(inifile tst_inifile.cpp) add_unit_test(UpdateChecker tst_UpdateChecker.cpp) add_unit_test(DownloadUpdateTask tst_DownloadUpdateTask.cpp) diff --git a/tests/tst_inifile.cpp b/tests/tst_inifile.cpp new file mode 100644 index 00000000..c0f57c12 --- /dev/null +++ b/tests/tst_inifile.cpp @@ -0,0 +1,43 @@ +#include +#include "TestUtil.h" + +#include "depends/settings/inifile.h" + +class IniFileTest : public QObject +{ + Q_OBJECT +private +slots: + void initTestCase() + { + + } + void cleanupTestCase() + { + + } + + void test_PathCombine1_data() + { + QTest::addColumn("through"); + + QTest::newRow("unix path") << "/abc/def/ghi/jkl"; + QTest::newRow("windows path") << "C:\\Program files\\terrible\\name\\of something\\"; + QTest::newRow("Plain text") << "Lorem ipsum dolor sit amet."; + QTest::newRow("Escape sequences") << "Lorem\n\t\n\\n\\tAAZ\nipsum dolor\n\nsit amet."; + QTest::newRow("Escape sequences 2") << "\"\n\n\""; + } + void test_PathCombine1() + { + QFETCH(QString, through); + + QString there = INIFile::escape(through); + QString back = INIFile::unescape(there); + + QCOMPARE(back, through); + } +}; + +QTEST_GUILESS_MAIN_MULTIMC(IniFileTest) + +#include "tst_inifile.moc"