From a2117475da46895963d6c504eed5e1c2c5f96038 Mon Sep 17 00:00:00 2001 From: scorpion-26 Date: Wed, 1 Nov 2023 00:22:32 +0100 Subject: [PATCH] Allow escaping strings in config \n \\ and \t now will be translated to their escaped counterpart --- data/config | 4 ++++ src/Common.h | 12 ++++++++++++ src/Config.cpp | 6 ++++++ 3 files changed, 22 insertions(+) diff --git a/data/config b/data/config index 55ab8c0..222e6a1 100644 --- a/data/config +++ b/data/config @@ -6,6 +6,10 @@ # - Before the variable # - After the ':' # - After the value +# +# String variables can be escaped ([Notation in config] -> "Result"): +# - foo\\bar -> "foobar" +# - foo\nbar -> "foobar" # The following three options control the ordering of the widgets. # Reordering can cause slight margin inconsistencies, diff --git a/src/Common.h b/src/Common.h index 9dc41ab..e6a3531 100644 --- a/src/Common.h +++ b/src/Common.h @@ -150,6 +150,18 @@ namespace Utils } return ""; } + + inline void Replace(std::string& str, const std::string& string, const std::string& replacement) + { + size_t curPos = 0; + curPos = str.find(string); + while (curPos != std::string::npos) + { + str.replace(curPos, string.length(), replacement); + curPos += string.length(); + curPos = str.find(string, curPos); + } + } } struct Process diff --git a/src/Config.cpp b/src/Config.cpp index 541c0cd..fa523d4 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -17,6 +17,12 @@ template<> void ApplyProperty(std::string& propertyToSet, const std::string_view& value) { propertyToSet = value; + + // Escape characters. This is a very hacky way to this, I know. + // TODO: Do something more efficient + Utils::Replace(propertyToSet, "\\n", "\n"); + Utils::Replace(propertyToSet, "\\\\", "\\"); + Utils::Replace(propertyToSet, "\\t", "\t"); } template<>