set_args.hpp
Go to the documentation of this file.
1// Copyright (c) 2006, 2007 Julio M. Merino Vidal
2// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
3// Copyright (c) 2009 Boris Schaeling
4// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
5// Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
6//
7// Distributed under the Boost Software License, Version 1.0. (See accompanying
8// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9
10#pragma once
11
12#include <sstream>
13
14#include <boost/algorithm/string/predicate.hpp>
16#include <boost/range/algorithm/copy.hpp>
17#include <boost/range/begin.hpp>
18#include <boost/range/end.hpp>
19#include <boost/shared_array.hpp>
20
22{
23
24 template <class Range>
26 {
27 private:
28 using ConstIterator = typename Range::const_iterator;
29 using String = typename Range::value_type;
30 using Char = typename String::value_type;
31 using OStringStream = std::basic_ostringstream<Char>;
32
33 public:
34 explicit set_args_(const Range& args)
35 {
36 ConstIterator it = boost::const_begin(args);
37 ConstIterator end = boost::const_end(args);
38 if (it != end)
39 {
40 exe_ = *it;
41 OStringStream os;
42 for (; it != end; ++it)
43 {
44 if (boost::algorithm::contains(*it, String(1, static_cast<Char>(' '))))
45 {
46 os << static_cast<Char>('"') << *it << static_cast<Char>('"');
47 }
48 else
49 {
50 os << *it;
51 }
52 os << static_cast<Char>(' ');
53 }
54 String s = os.str();
55 cmd_line_.reset(new Char[s.size() + 1]);
56 boost::copy(s, cmd_line_.get());
57 cmd_line_[s.size()] = 0;
58 }
59 else
60 {
61 cmd_line_.reset(new Char[1]());
62 }
63 }
64
65 template <class WindowsExecutor>
66 void
67 on_CreateProcess_setup(WindowsExecutor& e) const
68 {
69 e.cmd_line = cmd_line_.get();
70 if (!e.exe && !exe_.empty())
71 {
72 e.exe = exe_.c_str();
73 }
74 }
75
76 private:
77 boost::shared_array<Char> cmd_line_;
78 String exe_;
79 };
80
81 template <class Range>
82 set_args_<Range>
83 set_args(const Range& range)
84 {
85 return set_args_<Range>(range);
86 }
87
88} // namespace boost::process::windows::initializers
void on_CreateProcess_setup(WindowsExecutor &e) const
Definition set_args.hpp:67
set_args_< Range > set_args(const Range &range)
Definition set_args.hpp:83