summaryrefslogtreecommitdiff
path: root/src/mongo/util/text.cpp
diff options
context:
space:
mode:
authorAndy Schwerin <schwerin@10gen.com>2012-12-04 15:15:40 -0500
committerAndy Schwerin <schwerin@10gen.com>2012-12-04 18:29:00 -0500
commit0753b2b2a5ed8e70763d50e6945aea72f0e9f372 (patch)
treeb79f0e16309af25735d7506068ed1f066cd59ca2 /src/mongo/util/text.cpp
parented810cab19d462ae99c07c40ee976fd680435245 (diff)
downloadmongo-0753b2b2a5ed8e70763d50e6945aea72f0e9f372.tar.gz
SERVER-7252 Introduce and test function for building a Windows command line from an argument vector.
Diffstat (limited to 'src/mongo/util/text.cpp')
-rw-r--r--src/mongo/util/text.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/mongo/util/text.cpp b/src/mongo/util/text.cpp
index 1c587b6d303..1c075773177 100644
--- a/src/mongo/util/text.cpp
+++ b/src/mongo/util/text.cpp
@@ -18,6 +18,8 @@
#include "pch.h"
#include <boost/smart_ptr/scoped_array.hpp>
+#include <sstream>
+
#ifdef _WIN32
#include <io.h>
#endif
@@ -299,5 +301,51 @@ namespace mongo {
#endif // #if defined(_WIN32)
+ // See "Parsing C++ Command-Line Arguments (C++)"
+ // http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx
+ static void quoteForWindowsCommandLine(const std::string& arg, std::ostream& os) {
+ if (arg.find_first_of(" \t\"") == std::string::npos) {
+ os << arg;
+ }
+ else {
+ os << '"';
+ std::string backslashes = "";
+ for (std::string::const_iterator iter = arg.begin(), end = arg.end();
+ iter != end; ++iter) {
+
+ switch (*iter) {
+ case '\\':
+ backslashes.push_back(*iter);
+ if (iter + 1 == end)
+ os << backslashes << backslashes;
+ break;
+ case '"':
+ os << backslashes << backslashes << "\\\"";
+ break;
+ default:
+ os << backslashes << *iter;
+ backslashes.clear();
+ break;
+ }
+ }
+ os << '"';
+ }
+ }
+
+ std::string constructUtf8WindowsCommandLine(const std::vector<std::string>& argv) {
+ if (argv.empty())
+ return "";
+
+ std::ostringstream commandLine;
+ std::vector<std::string>::const_iterator iter = argv.begin();
+ std::vector<std::string>::const_iterator end = argv.end();
+ quoteForWindowsCommandLine(*iter, commandLine);
+ ++iter;
+ for (; iter != end; ++iter) {
+ commandLine << ' ';
+ quoteForWindowsCommandLine(*iter, commandLine);
+ }
+ return commandLine.str();
+ }
}