Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // boost/process/v2/windows/impl/default_launcher.ipp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2022 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 #ifndef BOOST_PROCESS_V2_WINDOWS_IMPL_DEFAULT_LAUNCHER_IPP
0011 #define BOOST_PROCESS_V2_WINDOWS_IMPL_DEFAULT_LAUNCHER_IPP
0012 
0013 #include <boost/process/v2/detail/config.hpp>
0014 #include <boost/process/v2/windows/default_launcher.hpp>
0015 
0016 BOOST_PROCESS_V2_BEGIN_NAMESPACE
0017 namespace windows
0018 {
0019 
0020   std::size_t default_launcher::escaped_argv_length(basic_string_view<wchar_t> ws)
0021   {
0022     if (ws.empty())
0023       return 2u; // just quotes
0024 
0025     constexpr static auto space = L' ';
0026     constexpr static auto quote = L'"';
0027 
0028     const auto has_space = ws.find(space) != basic_string_view<wchar_t>::npos;
0029     const auto quoted = (ws.front() == quote) && (ws.back() == quote);
0030     const auto needs_escape = has_space && !quoted ;
0031 
0032     if (!needs_escape)
0033       return ws.size();
0034     else
0035       return ws.size() + std::count(ws.begin(), ws.end(), quote) + 2u;
0036   }
0037 
0038 
0039   std::size_t default_launcher::escape_argv_string(wchar_t * itr, std::size_t max_size, 
0040                                         basic_string_view<wchar_t> ws)
0041   { 
0042     const auto sz = escaped_argv_length(ws);
0043     if (sz > max_size)
0044       return 0u;
0045     if (ws.empty())      
0046     {
0047       itr[0] = L'"';
0048       itr[1] = L'"';
0049       return 2u;
0050     }
0051 
0052     const auto has_space = ws.find(L' ') != basic_string_view<wchar_t>::npos;
0053     const auto quoted = (ws.front() == L'"') && (ws.back() ==  L'"');
0054     const auto needs_escape = has_space && !quoted;
0055 
0056     if (!needs_escape)
0057       return std::copy(ws.begin(), ws.end(), itr) - itr;
0058 
0059     if (sz < (2u + ws.size()))
0060       return 0u;
0061       
0062     const auto end = itr + sz; 
0063     const auto begin = itr;
0064     *(itr ++) = L'"';
0065     for (auto wc : ws)
0066     {
0067       if (wc == L'"')
0068         *(itr++) = L'\\';
0069       *(itr++) = wc;
0070     }
0071 
0072     *(itr ++) = L'"';
0073     return itr - begin;
0074   }
0075 
0076 }
0077 BOOST_PROCESS_V2_END_NAMESPACE
0078 
0079 
0080 #endif //BOOST_PROCESS_V2_WINDOWS_IMPL_DEFAULT_LAUNCHER_IPP