diff --git a/src/timew.h b/src/timew.h index 376f4bf9..46494477 100644 --- a/src/timew.h +++ b/src/timew.h @@ -41,5 +41,6 @@ int dispatchCommand (const std::vector &, Database&, Rules&, Extens // utiƀ.cpp std::string osName (); std::string escape (const std::string&, int); +std::string quoteIfNeeded (const std::string&); #endif diff --git a/src/util.cpp b/src/util.cpp index f07760eb..095fdbb7 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -25,6 +25,7 @@ //////////////////////////////////////////////////////////////////////////////// #include +#include #include //////////////////////////////////////////////////////////////////////////////// @@ -79,3 +80,25 @@ std::string escape (const std::string& input, int c) } //////////////////////////////////////////////////////////////////////////////// +std::string quoteIfNeeded (const std::string& input) +{ + auto quote = input.find ('"'); + auto space = input.find (' '); + + if (quote == std::string::npos && + space == std::string::npos) + return input; + + std::string output; + if (quote != std::string::npos) + output = escape (input, '"'); + else + output = input; + + if (space != std::string::npos) + output = std::string ("\"") + output + "\""; + + return output; +} + +////////////////////////////////////////////////////////////////////////////////