Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 08:42:37

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_WINDOWS_BASIC_CMD_HPP_
0008 #define BOOST_PROCESS_DETAIL_WINDOWS_BASIC_CMD_HPP_
0009 
0010 #include <boost/algorithm/string/trim.hpp>
0011 #include <boost/algorithm/string/replace.hpp>
0012 #include <boost/process/v1/shell.hpp>
0013 #include <boost/process/v1/detail/windows/handler.hpp>
0014 
0015 #include <vector>
0016 #include <string>
0017 #include <iterator>
0018 
0019 
0020 namespace boost
0021 {
0022 namespace process
0023 {
0024 BOOST_PROCESS_V1_INLINE namespace v1
0025 {
0026 namespace detail
0027 {
0028 namespace windows
0029 {
0030 
0031 inline std::string build_args(const std::string & exe, std::vector<std::string> && data)
0032 {
0033     std::string st = exe;
0034 
0035     //put in quotes if it has spaces or double quotes
0036     if(!exe.empty())
0037     {
0038         auto it = st.find_first_of(" \"");
0039 
0040         if(it != st.npos)//contains spaces.
0041         {
0042             // double existing quotes
0043             boost::replace_all(st, "\"", "\"\"");
0044 
0045             // surround with quotes
0046             st.insert(st.begin(), '"');
0047             st += '"';
0048         }
0049     }
0050 
0051     for (auto & arg : data)
0052     {
0053         if(!arg.empty())
0054         {
0055             auto it = arg.find_first_of(" \"");//contains space or double quotes?
0056             if(it != arg.npos)//yes
0057             {
0058                 // double existing quotes
0059                 boost::replace_all(arg, "\"", "\"\"");
0060 
0061                 // surround with quotes
0062                 arg.insert(arg.begin(), '"');
0063                 arg += '"';
0064             }
0065         }
0066         else
0067         {
0068             arg = "\"\"";
0069         }
0070 
0071         if (!st.empty())//first one does not need a preceding space
0072             st += ' ';
0073 
0074         st += arg;
0075     }
0076     return st;
0077 }
0078 
0079 inline std::wstring build_args(const std::wstring & exe, std::vector<std::wstring> && data)
0080 {
0081     std::wstring st = exe;
0082 
0083     //put in quotes if it has spaces or double quotes
0084     if(!exe.empty())
0085     {
0086         auto it = st.find_first_of(L" \"");
0087 
0088         if(it != st.npos)//contains spaces or double quotes.
0089         {
0090             // double existing quotes
0091             boost::replace_all(st, L"\"", L"\"\"");
0092 
0093             // surround with quotes
0094             st.insert(st.begin(), L'"');
0095             st += L'"';
0096         }
0097     }
0098 
0099     for(auto & arg : data)
0100     {
0101         if(!arg.empty())
0102         {
0103             auto it = arg.find_first_of(L" \"");//contains space or double quotes?
0104             if(it != arg.npos)//yes
0105             {
0106                 // double existing quotes
0107                 boost::replace_all(arg, L"\"", L"\"\"");
0108 
0109                 // surround with quotes
0110                 arg.insert(arg.begin(), L'"');
0111                 arg += '"';
0112             }
0113         }
0114         else
0115         {
0116             arg = L"\"\"";
0117         }
0118 
0119         if (!st.empty())//first one does not need a preceding space
0120             st += L' ';
0121 
0122         st += arg;
0123     }
0124     return st;
0125 }
0126 
0127 template<typename Char>
0128 struct exe_cmd_init : handler_base_ext
0129 {
0130     using value_type  = Char;
0131     using string_type = std::basic_string<value_type>;
0132 
0133     static const char*    c_arg(char)    { return "/c";}
0134     static const wchar_t* c_arg(wchar_t) { return L"/c";}
0135 
0136     exe_cmd_init(const string_type & exe, bool cmd_only = false)
0137                 : exe(exe), args({}), cmd_only(cmd_only) {};
0138     exe_cmd_init(string_type && exe, bool cmd_only = false)
0139                 : exe(std::move(exe)), args({}), cmd_only(cmd_only) {};
0140 
0141     exe_cmd_init(string_type && exe, std::vector<string_type> && args)
0142             : exe(std::move(exe)), args(build_args(this->exe, std::move(args))), cmd_only(false) {};
0143     template <class Executor>
0144     void on_setup(Executor& exec) const
0145     {
0146 
0147         if (cmd_only && args.empty())
0148             exec.cmd_line = exe.c_str();
0149         else
0150         {
0151             exec.exe = exe.c_str();
0152             exec.cmd_line = args.c_str();
0153         }
0154     }
0155     static exe_cmd_init<Char> exe_args(string_type && exe, std::vector<string_type> && args)
0156     {
0157         return exe_cmd_init<Char>(std::move(exe), std::move(args));
0158     }
0159     static exe_cmd_init<Char> cmd(string_type&& cmd)
0160     {
0161         return exe_cmd_init<Char>(std::move(cmd), true);
0162     }
0163     static exe_cmd_init<Char> exe_args_shell(string_type && exe, std::vector<string_type> && args)
0164     {
0165         std::vector<string_type> args_ = {c_arg(Char()), std::move(exe)};
0166         args_.insert(args_.end(), std::make_move_iterator(args.begin()), std::make_move_iterator(args.end()));
0167         string_type sh = get_shell(Char());
0168 
0169         return exe_cmd_init<Char>(std::move(sh), std::move(args_));
0170     }
0171 
0172 #ifdef BOOST_PROCESS_USE_STD_FS
0173     static std:: string get_shell(char)    {return shell(). string(); }
0174     static std::wstring get_shell(wchar_t) {return shell().wstring(); }
0175 #else
0176     static std:: string get_shell(char)    {return shell(). string(codecvt()); }
0177     static std::wstring get_shell(wchar_t) {return shell().wstring(codecvt());}
0178 #endif
0179 
0180     static exe_cmd_init<Char> cmd_shell(string_type&& cmd)
0181     {
0182         std::vector<string_type> args = {c_arg(Char()), std::move(cmd)};
0183         string_type sh = get_shell(Char());
0184 
0185         return exe_cmd_init<Char>(
0186                 std::move(sh),
0187                 std::move(args));
0188     }
0189 private:
0190     string_type exe;
0191     string_type args;
0192     bool cmd_only;
0193 };
0194 
0195 }
0196 
0197 
0198 
0199 }
0200 }
0201 }
0202 }
0203 
0204 
0205 #endif /* INCLUDE_BOOST_PROCESS_WINDOWS_ARGS_HPP_ */