From a662af88de75f2fb386a96b156be92223cf49fe9 Mon Sep 17 00:00:00 2001 From: Sebastien Barre Date: Fri, 27 May 2005 22:11:26 -0400 Subject: ENH: add convenience function to convert Windows command line args into Unix argc/argv. Pulled and cleaned from PV/VV/VJ init code --- Source/kwsys/SystemTools.cxx | 103 ++++++++++++++++++++++++++++++++++++++++ Source/kwsys/SystemTools.hxx.in | 17 +++++-- 2 files changed, 116 insertions(+), 4 deletions(-) diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx index 0383661..9d6499e 100644 --- a/Source/kwsys/SystemTools.cxx +++ b/Source/kwsys/SystemTools.cxx @@ -3138,6 +3138,109 @@ void SystemTools::Delay(unsigned int msec) #endif } +void SystemTools::ConvertWindowsCommandLineToUnixArguments( + const char *cmd_line, int *argc, char ***argv) +{ + if (!cmd_line || !argc || !argv) + { + return; + } + + // A space delimites an argument except when it is inside a quote + + (*argc) = 1; + + size_t cmd_line_len = strlen(cmd_line); + + size_t i; + for (i = 0; i < cmd_line_len; i++) + { + while (isspace(cmd_line[i]) && i < cmd_line_len) + { + i++; + } + if (i < cmd_line_len) + { + if (cmd_line[i] == '\"') + { + i++; + while (cmd_line[i] != '\"' && i < cmd_line_len) + { + i++; + } + (*argc)++; + } + else + { + while (!isspace(cmd_line[i]) && i < cmd_line_len) + { + i++; + } + (*argc)++; + } + } + } + + (*argv) = new char* [(*argc) + 1]; + (*argv)[(*argc)] = NULL; + + // Set the first arg to be the exec name + + (*argv)[0] = new char [1024]; +#ifdef _WIN32 + ::GetModuleFileName(0, (*argv)[0], 1024); +#else + (*argv)[0][0] = '\0'; +#endif + + // Allocate the others + + int j; + for (j = 1; j < (*argc); j++) + { + (*argv)[j] = new char [cmd_line_len + 10]; + } + + // Grab the args + + size_t pos = 0; + int argc_idx = 1; + + for (i = 0; i < cmd_line_len; i++) + { + while (isspace(cmd_line[i]) && i < cmd_line_len) + { + i++; + } + if (i < cmd_line_len) + { + if (cmd_line[i] == '\"') + { + i++; + pos = i; + while (cmd_line[i] != '\"' && i < cmd_line_len) + { + i++; + } + memcpy((*argv)[argc_idx], &cmd_line[pos], i - pos); + (*argv)[argc_idx][i - pos] = '\0'; + argc_idx++; + } + else + { + pos = i; + while (!isspace(cmd_line[i]) && i < cmd_line_len) + { + i++; + } + memcpy((*argv)[argc_idx], &cmd_line[pos], i - pos); + (*argv)[argc_idx][i - pos] = '\0'; + argc_idx++; + } + } + } + } + kwsys_stl::string SystemTools::GetOperatingSystemNameAndVersion() { kwsys_stl::string res; diff --git a/Source/kwsys/SystemTools.hxx.in b/Source/kwsys/SystemTools.hxx.in index ca24616..ba57618 100644 --- a/Source/kwsys/SystemTools.hxx.in +++ b/Source/kwsys/SystemTools.hxx.in @@ -410,6 +410,12 @@ public: */ static bool IsSubDirectory(const char* fileOrDir, const char* dir); + /** + * Convert the path to a string that can be used in a unix makefile. + * double slashes are removed, and spaces are escaped. + */ + static kwsys_stl::string ConvertToUnixOutputPath(const char*); + /** ----------------------------------------------------------------- * File Manipulation Routines * ----------------------------------------------------------------- @@ -655,11 +661,14 @@ public: static kwsys_stl::string GetOperatingSystemNameAndVersion(); /** - * Convert the path to a string that can be used in a unix makefile. - * double slashes are removed, and spaces are escaped. + * Convert windows-style arguments given as a command-line string + * into more traditional argc/argv arguments. + * Note that argv[0] will be assigned the executable name using + * the ::GetModuleFileName function. */ - static kwsys_stl::string ConvertToUnixOutputPath(const char*); - + static void ConvertWindowsCommandLineToUnixArguments( + const char *cmd_line, int *argc, char ***argv); + protected: // these two functions can be called from ConvertToOutputPath -- cgit v0.12