summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Noonan <steven@uplinklabs.net>2009-09-30 00:14:29 -0700
committerSteven Noonan <steven@uplinklabs.net>2009-09-30 00:16:44 -0700
commit55c53405603d7b6342cd33573130b4511311fec9 (patch)
tree8da87136ad62b41ed926478b1757addfc4d8c06b
parent5c67594c4d192cdd9c5490969d261da1e5244a75 (diff)
downloadcrawl-ref-55c53405603d7b6342cd33573130b4511311fec9.tar.gz
crawl-ref-55c53405603d7b6342cd33573130b4511311fec9.zip
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 <steven@uplinklabs.net>
-rw-r--r--crawl-ref/source/libutil.cc30
-rw-r--r--crawl-ref/source/libutil.h2
2 files changed, 32 insertions, 0 deletions
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);