From 55c53405603d7b6342cd33573130b4511311fec9 Mon Sep 17 00:00:00 2001 From: Steven Noonan Date: Wed, 30 Sep 2009 00:14:29 -0700 Subject: libutil.cc: add escape_path_spaces() Spaces need to be escaped in any system() calls, so we have this simple function which escapes spaces with either doublequotes (Windows style) or backslashes (UNIX style). Signed-off-by: Steven Noonan --- crawl-ref/source/libutil.cc | 30 ++++++++++++++++++++++++++++++ crawl-ref/source/libutil.h | 2 ++ 2 files changed, 32 insertions(+) diff --git a/crawl-ref/source/libutil.cc b/crawl-ref/source/libutil.cc index 58f6661805..d6064e7bbc 100644 --- a/crawl-ref/source/libutil.cc +++ b/crawl-ref/source/libutil.cc @@ -153,6 +153,36 @@ std::string make_stringf(const char *s, ...) return (buf); } +std::string &escape_path_spaces(std::string &s) +{ + std::string result; + result.clear(); +#ifdef UNIX + for (const char* ch = s.c_str(); *ch != '\0'; ++ch) + { + if (*ch == ' ') + { + result += '\\'; + } + result += *ch; + } +#elif defined(WIN32CONSOLE) || defined(WIN32TILES) + if (s.find(" ") != std::string::npos && + s.find("\"") == std::string::npos) + { + result = "\"" + s + "\""; + } else { + return s; + } +#else + // Not implemented for this platform. Assume that + // escaping isn't necessary. + return s; +#endif + s = result; + return s; +} + std::string &uppercase(std::string &s) { for (unsigned i = 0, sz = s.size(); i < sz; ++i) diff --git a/crawl-ref/source/libutil.h b/crawl-ref/source/libutil.h index f64e8ae07c..d3e02628e4 100644 --- a/crawl-ref/source/libutil.h +++ b/crawl-ref/source/libutil.h @@ -28,6 +28,8 @@ std::string apply_description(description_level_type desc, description_level_type description_type_by_name(const char *desc); +std::string &escape_path_spaces(std::string &s); + std::string lowercase_string(std::string s); std::string &lowercase(std::string &s); std::string &uppercase(std::string &s); -- cgit v1.2.3-54-g00ecf