comparison third_party/boost/process/detail/windows_helpers.hpp @ 0:add35537fdbb tip

Initial import
author irh <ian.r.hobson@gmail.com>
date Thu, 25 Aug 2011 11:05:55 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:add35537fdbb
1 //
2 // Boost.Process
3 // ~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
6 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
7 // Copyright (c) 2009 Boris Schaeling
8 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
9 //
10 // Distributed under the Boost Software License, Version 1.0. (See accompanying
11 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
12 //
13
14 /**
15 * \file boost/process/detail/windows_helpers.hpp
16 *
17 * Includes the declaration of helper functions for Windows systems.
18 */
19
20 #ifndef BOOST_PROCESS_WINDOWS_HELPERS_HPP
21 #define BOOST_PROCESS_WINDOWS_HELPERS_HPP
22
23 #include <boost/process/config.hpp>
24 #include <boost/process/environment.hpp>
25 #include <boost/shared_array.hpp>
26 #include <string>
27 #include <vector>
28 #include <cstddef>
29 #include <string.h>
30 #include <windows.h>
31
32 namespace boost {
33 namespace process {
34 namespace detail {
35
36 /**
37 * Converts an environment to a string used by CreateProcess().
38 *
39 * Converts the environment's contents to the format used by the
40 * CreateProcess() system call. The returned char* string is
41 * allocated in dynamic memory; the caller must free it when not
42 * used any more. This is enforced by the use of a shared pointer.
43 *
44 * This operation is only available on Windows systems.
45 *
46 * \return A dynamically allocated char* string that represents
47 * the environment's content. This string is of the form
48 * var1=value1\\0var2=value2\\0\\0.
49 */
50 inline boost::shared_array<char> environment_to_windows_strings(environment
51 &env)
52 {
53 boost::shared_array<char> envp;
54
55 if (env.empty())
56 {
57 envp.reset(new char[2]);
58 ZeroMemory(envp.get(), 2);
59 }
60 else
61 {
62 std::string s;
63 for (environment::const_iterator it = env.begin(); it != env.end();
64 ++it)
65 {
66 s += it->first + "=" + it->second;
67 s.push_back(0);
68 }
69 envp.reset(new char[s.size() + 1]);
70 #if (BOOST_MSVC >= 1400)
71 memcpy_s(envp.get(), s.size() + 1, s.c_str(), s.size() + 1);
72 #else
73 memcpy(envp.get(), s.c_str(), s.size() + 1);
74 #endif
75 }
76
77 return envp;
78 }
79
80 /**
81 * Converts the command line to a plain string.
82 *
83 * Converts the command line's list of arguments to the format expected by the
84 * \a lpCommandLine parameter in the CreateProcess() system call.
85 *
86 * This operation is only available on Windows systems.
87 *
88 * \return A dynamically allocated string holding the command line
89 * to be passed to the executable. It is returned in a
90 * shared_array object to ensure its release at some point.
91 */
92 template <class Arguments>
93 inline boost::shared_array<char> collection_to_windows_cmdline(const Arguments
94 &args)
95 {
96 typedef std::vector<std::string> arguments_t;
97 arguments_t args2;
98 typename Arguments::size_type i = 0;
99 std::size_t size = 0;
100 for (typename Arguments::const_iterator it = args.begin(); it != args.end();
101 ++it)
102 {
103 std::string arg = *it;
104
105 std::string::size_type pos = 0;
106 while ( (pos = arg.find('"', pos)) != std::string::npos)
107 {
108 arg.replace(pos, 1, "\\\"");
109 pos += 2;
110 }
111
112 if (arg.find(' ') != std::string::npos)
113 arg = '\"' + arg + '\"';
114
115 if (i++ != args.size() - 1)
116 arg += ' ';
117
118 args2.push_back(arg);
119 size += arg.size() + 1;
120 }
121
122 boost::shared_array<char> cmdline(new char[size]);
123 cmdline.get()[0] = '\0';
124 for (arguments_t::size_type i = 0; i < args.size(); ++i)
125 #if (BOOST_MSVC >= 1400)
126 strcat_s(cmdline.get(), size, args2[i].c_str());
127 #else
128 strncat(cmdline.get(), args2[i].c_str(), args2[i].size());
129 #endif
130
131 return cmdline;
132 }
133
134 }
135 }
136 }
137
138 #endif