]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/windows/process.cpp
Use C++11 features with containers
[libs/core.git] / source / core / windows / process.cpp
index 7b1efd693aff45fce41594914b4d16dc2264de70..da76fe38dd3551f82dbfd3910d995a0058191108 100644 (file)
@@ -1,6 +1,7 @@
 #include <windows.h>
 #include <msp/core/systemerror.h>
 #include <msp/io/handle_private.h>
+#include <msp/strings/utils.h>
 #include "process.h"
 #include "process_private.h"
 
@@ -13,19 +14,19 @@ string quote_argument(const string &arg)
        string result;
        bool need_quotes = false;
        bool backslash = false;
-       for(string::const_iterator i=arg.begin(); i!=arg.end(); ++i)
+       for(char c: arg)
        {
-               if(*i=='\\')
+               if(c=='\\')
                        backslash = true;
-               else if(*i=='"')
+               else if(c=='"')
                {
                        if(backslash)
                                result += '\\';
                        result += '\\';
                }
-               else if(*i==' ')
+               else if(c==' ')
                        need_quotes = true;
-               result += *i;
+               result += c;
        }
 
        if(need_quotes)
@@ -55,11 +56,8 @@ void Process::platform_get_self_info(Private &priv)
 void Process::execute(const string &command, bool path_search, const Arguments &args)
 {
        string cmdline = quote_argument(command);
-       for(Arguments::const_iterator i=args.begin(); i!=args.end(); ++i)
-       {
-               cmdline += ' ';
-               cmdline += quote_argument(*i);
-       }
+       for(const string &a: args)
+               append(cmdline, " ", quote_argument(a));
 
        STARTUPINFO startup;
        startup.cb = sizeof(STARTUPINFO);
@@ -82,9 +80,15 @@ void Process::execute(const string &command, bool path_search, const Arguments &
        }
        const char *cmdptr = (path_search ? 0 : command.c_str());
        const char *wd = (work_dir.empty() ? 0 : work_dir.c_str());
-       if(!CreateProcess(cmdptr, const_cast<char *>(cmdline.c_str()), 0, 0, false, 0, 0, wd, &startup, &priv->info))
+       if(!CreateProcess(cmdptr, const_cast<char *>(cmdline.c_str()), 0, 0, true, 0, 0, wd, &startup, &priv->info))
                throw system_error("CreateProcess");
-       // XXX Should we close the duplicated handles?  What if CreateProcess fails?
+
+       if(redirect)
+       {
+               CloseHandle(startup.hStdInput);
+               CloseHandle(startup.hStdOutput);
+               CloseHandle(startup.hStdError);
+       }
 
        running = true;