Fix issues with badly encoded escape sequences in config files.
This commit is contained in:
		| @@ -25,17 +25,49 @@ INIFile::INIFile() | |||||||
|  |  | ||||||
| QString INIFile::unescape(QString orig) | QString INIFile::unescape(QString orig) | ||||||
| { | { | ||||||
| 	orig.replace("\\n", "\n"); | 	QString out; | ||||||
| 	orig.replace("\\t", "\t"); | 	QChar prev = 0; | ||||||
| 	orig.replace("\\\\", "\\"); | 	for(auto c: orig) | ||||||
| 	return 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) | QString INIFile::escape(QString orig) | ||||||
| { | { | ||||||
| 	orig.replace("\\", "\\\\"); | 	QString out; | ||||||
| 	orig.replace("\n", "\\n"); | 	for(auto c: orig) | ||||||
| 	orig.replace("\t", "\\t"); | 	{ | ||||||
| 	return 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) | bool INIFile::saveFile(QString fileName) | ||||||
|   | |||||||
| @@ -33,6 +33,6 @@ public: | |||||||
|  |  | ||||||
| 	QVariant get(QString key, QVariant def) const; | 	QVariant get(QString key, QVariant def) const; | ||||||
| 	void set(QString key, QVariant val); | 	void set(QString key, QVariant val); | ||||||
| 	QString unescape(QString orig); | 	static QString unescape(QString orig); | ||||||
| 	QString escape(QString orig); | 	static QString escape(QString orig); | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -38,5 +38,6 @@ EditNotesDialog::~EditNotesDialog() | |||||||
|  |  | ||||||
| QString EditNotesDialog::getText() | QString EditNotesDialog::getText() | ||||||
| { | { | ||||||
| 	return ui->noteEditor->toPlainText(); | 	QString test = ui->noteEditor->toPlainText(); | ||||||
|  | 	return test; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -23,6 +23,7 @@ endmacro() | |||||||
|  |  | ||||||
| add_unit_test(pathutils tst_pathutils.cpp) | add_unit_test(pathutils tst_pathutils.cpp) | ||||||
| add_unit_test(userutils tst_userutils.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(UpdateChecker tst_UpdateChecker.cpp) | ||||||
| add_unit_test(DownloadUpdateTask tst_DownloadUpdateTask.cpp) | add_unit_test(DownloadUpdateTask tst_DownloadUpdateTask.cpp) | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										43
									
								
								tests/tst_inifile.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								tests/tst_inifile.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | |||||||
|  | #include <QTest> | ||||||
|  | #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<QString>("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" | ||||||
		Reference in New Issue
	
	Block a user