Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:05

0001 // Copyright (c) 2016 Klemens D. Morgenstern
0002 //
0003 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 
0007 #ifndef BOOST_PROCESS_DETAIL_POSIX_CMD_HPP_
0008 #define BOOST_PROCESS_DETAIL_POSIX_CMD_HPP_
0009 
0010 #include <boost/process/detail/config.hpp>
0011 #include <boost/process/detail/posix/handler.hpp>
0012 #include <string>
0013 #include <vector>
0014 
0015 namespace boost
0016 {
0017 namespace process
0018 {
0019 namespace detail
0020 {
0021 namespace posix
0022 {
0023 
0024 
0025 template<typename Char>
0026 inline std::vector<std::basic_string<Char>> build_cmd(const std::basic_string<Char> & value)
0027 {
0028     std::vector<std::basic_string<Char>>  ret;
0029 
0030     bool in_quotes = false;
0031     auto beg = value.begin();
0032     for (auto itr = value.begin(); itr != value.end(); itr++)
0033     {
0034         if (*itr == quote_sign<Char>())
0035             in_quotes = !in_quotes;
0036 
0037         if (!in_quotes && (*itr == space_sign<Char>()))
0038         {
0039             if (itr != beg)
0040             {
0041                 ret.emplace_back(beg, itr);
0042                 beg = itr + 1;
0043             }
0044         }
0045     }
0046     if (beg != value.end())
0047         ret.emplace_back(beg, value.end());
0048 
0049     return ret;
0050 }
0051 
0052 template<typename Char>
0053 struct cmd_setter_ : handler_base_ext
0054 {
0055     typedef Char value_type;
0056     typedef std::basic_string<value_type> string_type;
0057 
0058     cmd_setter_(string_type && cmd_line)      : _cmd_line(api::build_cmd(std::move(cmd_line))) {}
0059     cmd_setter_(const string_type & cmd_line) : _cmd_line(api::build_cmd(cmd_line)) {}
0060     template <class Executor>
0061     void on_setup(Executor& exec) 
0062     {
0063         exec.exe = _cmd_impl.front();
0064         exec.cmd_line = &_cmd_impl.front();
0065         exec.cmd_style = true;
0066     }
0067     string_type str() const
0068     {
0069         string_type ret;
0070         std::size_t size = 0;
0071         for (auto & cmd : _cmd_line)
0072             size += cmd.size() + 1;
0073         ret.reserve(size -1);
0074 
0075         for (auto & cmd : _cmd_line)
0076         {
0077             if (!ret.empty())
0078                 ret += equal_sign<Char>();
0079             ret += cmd;
0080         }
0081         return ret;
0082     }
0083 private:
0084     static inline std::vector<Char*> make_cmd(std::vector<string_type> & args);
0085     std::vector<string_type> _cmd_line;
0086     std::vector<Char*> _cmd_impl  = make_cmd(_cmd_line);
0087 };
0088 
0089 template<typename Char>
0090 std::vector<Char*> cmd_setter_<Char>::make_cmd(std::vector<std::basic_string<Char>> & args)
0091 {
0092     std::vector<Char*> vec;
0093 
0094     for (auto & v : args)
0095         vec.push_back(&v.front());
0096 
0097     vec.push_back(nullptr);
0098 
0099     return vec;
0100 }
0101 
0102 }}}}
0103 
0104 #endif