From eb5128aa6ce4a33aa9021b51231d0934294c7caa Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 27 Feb 2017 23:42:54 +0300 Subject: Add string function for sanitization strings. --- src/utils/stringutils.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/utils/stringutils.cpp') diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index bb7f7efb5..28b2c8e1e 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -385,6 +385,20 @@ std::string& replaceAll(std::string& context, return context; } +void replaceRecursiveAll(std::string& context, + const std::string &restrict from, + const char to) +{ + size_t lookHere = 0; + size_t foundHere; + const size_t fromSize = from.size(); + while ((foundHere = context.find(from, lookHere)) != std::string::npos) + { + context.replace(foundHere, fromSize, 1, to); + lookHere = foundHere; + } +} + bool getBoolFromString(const std::string &text) { std::string txt = text; @@ -1015,6 +1029,21 @@ std::string escapeString(std::string str) return "\"" + str + "\""; } +void sanitizePath(std::string &path) +{ +#ifdef WIN32 + const char sepStr = '\\'; + const std::string sep2Str = "\\\\"; + const std::string sepWrongStr = "/"; +#else + const char sepStr = '/'; + const std::string sep2Str = "//"; + const std::string sepWrongStr = "\\"; +#endif + replaceRecursiveAll(path, sepWrongStr, sepStr); + replaceRecursiveAll(path, sep2Str, sepStr); +} + #ifndef DYECMD void replaceItemLinks(std::string &msg) { -- cgit v1.2.3-70-g09d2