Mercurial > hg > gpsynth
comparison third_party/boost/process/detail/posix_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/posix_helpers.hpp | |
16 * | |
17 * Includes the declaration of helper functions for POSIX systems. | |
18 */ | |
19 | |
20 #ifndef BOOST_PROCESS_POSIX_HELPERS_HPP | |
21 #define BOOST_PROCESS_POSIX_HELPERS_HPP | |
22 | |
23 #include <boost/process/config.hpp> | |
24 #include <boost/process/environment.hpp> | |
25 #include <string> | |
26 #include <utility> | |
27 #include <cstring> | |
28 #include <cstddef> | |
29 | |
30 namespace boost { | |
31 namespace process { | |
32 namespace detail { | |
33 | |
34 /** | |
35 * Converts an environment to a char** table as used by execve(). | |
36 * | |
37 * Converts the environment's contents to the format used by the | |
38 * execve() system call. The returned char** array is allocated | |
39 * in dynamic memory; the caller must free it when not used any | |
40 * more. Each entry is also allocated in dynamic memory and is a | |
41 * NULL-terminated string of the form var=value; these must also be | |
42 * released by the caller. | |
43 * | |
44 * This operation is only available on POSIX systems. | |
45 * | |
46 * \return The first argument of the pair is an integer that indicates | |
47 * how many strings are stored in the second argument. The | |
48 * second argument is a NULL-terminated, dynamically allocated | |
49 * array of dynamically allocated strings representing the | |
50 * enviroment's content. Each array entry is a NULL-terminated | |
51 * string of the form var=value. The caller is responsible for | |
52 * freeing them. | |
53 */ | |
54 inline std::pair<std::size_t, char**> environment_to_envp(const environment | |
55 &env) | |
56 { | |
57 std::size_t nargs = env.size(); | |
58 char **envp = new char*[nargs + 1]; | |
59 environment::size_type i = 0; | |
60 for (environment::const_iterator it = env.begin(); it != env.end(); ++it) | |
61 { | |
62 std::string s = it->first + "=" + it->second; | |
63 envp[i] = new char[s.size() + 1]; | |
64 std::strncpy(envp[i], s.c_str(), s.size() + 1); | |
65 ++i; | |
66 } | |
67 envp[i] = 0; | |
68 return std::pair<std::size_t, char**>(nargs, envp); | |
69 } | |
70 | |
71 /** | |
72 * Converts the command line to an array of C strings. | |
73 * | |
74 * Converts the command line's list of arguments to the format expected | |
75 * by the \a argv parameter in the POSIX execve() system call. | |
76 * | |
77 * This operation is only available on POSIX systems. | |
78 * | |
79 * \return The first argument of the pair is an integer that indicates | |
80 * how many strings are stored in the second argument. The | |
81 * second argument is a NULL-terminated, dynamically allocated | |
82 * array of dynamically allocated strings holding the arguments | |
83 * to the executable. The caller is responsible for freeing them. | |
84 */ | |
85 template <class Arguments> | |
86 inline std::pair<std::size_t, char**> collection_to_argv(const Arguments &args) | |
87 { | |
88 std::size_t nargs = args.size(); | |
89 char **argv = new char*[nargs + 1]; | |
90 typename Arguments::size_type i = 0; | |
91 for (typename Arguments::const_iterator it = args.begin(); it != args.end(); | |
92 ++it) | |
93 { | |
94 argv[i] = new char[it->size() + 1]; | |
95 std::strncpy(argv[i], it->c_str(), it->size() + 1); | |
96 ++i; | |
97 } | |
98 argv[nargs] = 0; | |
99 return std::pair<std::size_t, char**>(nargs, argv); | |
100 } | |
101 | |
102 } | |
103 } | |
104 } | |
105 | |
106 #endif |