File indexing completed on 2025-01-18 09:50:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_PROCESS_V2_DETAIL_IMPL_ENVIRONMENT_WIN_IPP
0012 #define BOOST_PROCESS_V2_DETAIL_IMPL_ENVIRONMENT_WIN_IPP
0013
0014
0015 #include <boost/process/v2/detail/config.hpp>
0016 #include <boost/process/v2/detail/environment_win.hpp>
0017 #include <boost/process/v2/detail/impl/environment.ipp>
0018 #include <boost/process/v2/detail/last_error.hpp>
0019
0020 #include <algorithm>
0021 #include <cwctype>
0022 #include <cstring>
0023
0024 #include <shellapi.h>
0025
0026 #include <boost/process/v2/cstring_ref.hpp>
0027 #include <boost/process/v2/error.hpp>
0028
0029 BOOST_PROCESS_V2_BEGIN_NAMESPACE
0030
0031 namespace environment
0032 {
0033 namespace detail
0034 {
0035
0036 std::basic_string<char_type, value_char_traits<char_type>> get(
0037 basic_cstring_ref<char_type, key_char_traits<char_type>> key,
0038 error_code & ec)
0039 {
0040 std::basic_string<char_type, value_char_traits<char_type>> buf;
0041
0042 std::size_t size = 0u;
0043 do
0044 {
0045 buf.resize(buf.size() + 4096);
0046 size = ::GetEnvironmentVariableW(key.c_str(), &buf.front(), static_cast<DWORD>(buf.size()));
0047 }
0048 while (size == buf.size());
0049
0050 buf.resize(size);
0051
0052 if (buf.size() == 0)
0053 BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
0054
0055 return buf;
0056 }
0057
0058 void set(basic_cstring_ref<char_type, key_char_traits<char_type>> key,
0059 basic_cstring_ref<char_type, value_char_traits<char_type>> value,
0060 error_code & ec)
0061 {
0062 if (!::SetEnvironmentVariableW(key.c_str(), value.c_str()))
0063 BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
0064 }
0065
0066 void unset(basic_cstring_ref<char_type, key_char_traits<char_type>> key,
0067 error_code & ec)
0068 {
0069 if (!::SetEnvironmentVariableW(key.c_str(), nullptr))
0070 BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
0071 }
0072
0073
0074 std::basic_string<char, value_char_traits<char>> get(
0075 basic_cstring_ref<char, key_char_traits<char>> key,
0076 error_code & ec)
0077 {
0078 std::basic_string<char, value_char_traits<char>> buf;
0079
0080 std::size_t size = 0u;
0081 do
0082 {
0083 buf.resize(buf.size() + 4096);
0084 size = ::GetEnvironmentVariableA(key.c_str(), &buf.front(), static_cast<DWORD>(buf.size()));
0085 }
0086 while (size == buf.size());
0087
0088 buf.resize(size);
0089
0090 if (buf.size() == 0)
0091 BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
0092
0093 return buf;
0094 }
0095
0096 void set(basic_cstring_ref<char, key_char_traits<char>> key,
0097 basic_cstring_ref<char, value_char_traits<char>> value,
0098 error_code & ec)
0099 {
0100 if (!::SetEnvironmentVariableA(key.c_str(), value.c_str()))
0101 BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
0102 }
0103
0104 void unset(basic_cstring_ref<char, key_char_traits<char>> key,
0105 error_code & ec)
0106 {
0107 if (!::SetEnvironmentVariableA(key.c_str(), nullptr))
0108 BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
0109 }
0110
0111
0112 native_handle_type load_native_handle() { return ::GetEnvironmentStringsW(); }
0113 void native_handle_deleter::operator()(native_handle_type nh) const
0114 {
0115 ::FreeEnvironmentStringsW(nh);
0116 }
0117
0118 native_iterator next(native_iterator nh)
0119 {
0120 while (*nh != L'\0')
0121 nh++;
0122 return ++nh;
0123 }
0124
0125
0126 native_iterator find_end(native_handle_type nh)
0127 {
0128 while ((*nh != L'\0') || (*std::next(nh) != L'\0'))
0129 nh++;
0130 return ++nh;
0131 }
0132
0133 bool is_executable(const filesystem::path & pth, error_code & ec)
0134 {
0135 return filesystem::is_regular_file(pth, ec) && SHGetFileInfoW(pth.native().c_str(), 0,0,0, SHGFI_EXETYPE);
0136 }
0137
0138 }
0139 }
0140 BOOST_PROCESS_V2_END_NAMESPACE
0141
0142 #endif