File indexing completed on 2025-01-30 09:58:03
0001
0002
0003
0004
0005
0006 #ifndef BOOST_PROCESS_DETAIL_ENV_HPP_
0007 #define BOOST_PROCESS_DETAIL_ENV_HPP_
0008
0009 #include <boost/process/detail/traits/wchar_t.hpp>
0010 #include <boost/process/environment.hpp>
0011 #include <boost/none.hpp>
0012
0013 #if defined(BOOST_POSIX_API)
0014 #include <boost/process/detail/posix/env_init.hpp>
0015 #elif defined(BOOST_WINDOWS_API)
0016 #include <boost/process/detail/windows/env_init.hpp>
0017 #endif
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 namespace boost {
0043
0044 namespace process { namespace detail {
0045
0046
0047 template<typename Char>
0048 std::size_t make_env_string_size(const std::basic_string<Char> & ch)
0049 {
0050 return ch.size() + 1;
0051 }
0052
0053 template<typename Char>
0054 std::size_t make_env_string_size(const Char * ch)
0055 {
0056 std::size_t sz = 0;
0057 while (ch[sz] != null_char<Char>())
0058 sz++;
0059
0060 sz++;
0061 return sz;
0062 }
0063
0064 template<typename Char, typename Container>
0065 inline std::basic_string<Char> make_env_string(const Container & value)
0066 {
0067 std::size_t sz = 0;
0068 for (auto & v : value)
0069 sz += make_env_string_size(v);
0070
0071 std::basic_string<Char> s;
0072 s.reserve(sz);
0073
0074 for (auto & val : value)
0075 (s += val) += api::env_seperator<Char>();
0076
0077 s.resize(s.size() -1);
0078 return s;
0079 }
0080
0081
0082 template<typename Char>
0083 struct env_set
0084 {
0085 using string_type = std::basic_string<Char>;
0086 string_type key;
0087 string_type value;
0088 };
0089
0090 template<typename Char>
0091 struct env_append
0092 {
0093 using string_type = std::basic_string<Char>;
0094 string_type key;
0095 string_type value;
0096 };
0097
0098
0099
0100 template<typename Char>
0101 struct env_reset
0102 {
0103 using string_type = std::basic_string<Char>;
0104 string_type key;
0105 };
0106
0107
0108 template<> struct is_wchar_t<env_set<wchar_t>> : std::true_type {};
0109 template<> struct is_wchar_t<env_append<wchar_t>> : std::true_type {};
0110 template<> struct is_wchar_t<env_reset<wchar_t>> : std::true_type {};
0111 template<> struct is_wchar_t<basic_environment<wchar_t>> : std::true_type {};
0112
0113
0114 template<>
0115 struct char_converter<char, env_set<wchar_t>>
0116 {
0117 static env_set<char> conv(const env_set<wchar_t> & in)
0118 {
0119 return {::boost::process::detail::convert(in.key),
0120 ::boost::process::detail::convert(in.value)};
0121 }
0122 };
0123
0124 template<>
0125 struct char_converter<wchar_t, env_set<char>>
0126 {
0127 static env_set<wchar_t> conv(const env_set<char> & in)
0128 {
0129 return {::boost::process::detail::convert(in.key),
0130 ::boost::process::detail::convert(in.value)};
0131 }
0132 };
0133
0134 template<>
0135 struct char_converter<char, env_append<wchar_t>>
0136 {
0137 static env_append<char> conv(const env_append<wchar_t> & in)
0138 {
0139 return {::boost::process::detail::convert(in.key),
0140 ::boost::process::detail::convert(in.value)};
0141 }
0142 };
0143
0144 template<>
0145 struct char_converter<wchar_t, env_append<char>>
0146 {
0147 static env_append<wchar_t> conv(const env_append<char> & in)
0148 {
0149 return {::boost::process::detail::convert(in.key),
0150 ::boost::process::detail::convert(in.value)};
0151 }
0152 };
0153
0154 template<>
0155 struct char_converter<char, env_reset<wchar_t>>
0156 {
0157 static env_reset<char> conv(const env_reset<wchar_t> & in)
0158 {
0159 return {::boost::process::detail::convert(in.key)};
0160 }
0161 };
0162
0163 template<>
0164 struct char_converter<wchar_t, env_reset<char>>
0165 {
0166 static env_reset<wchar_t> conv(const env_reset<char> & in)
0167 {
0168 return {::boost::process::detail::convert(in.key)};
0169 }
0170 };
0171
0172
0173 template<typename Char>
0174 struct env_init
0175 {
0176 basic_environment<Char> env;
0177 };
0178
0179 template<>
0180 struct char_converter<char, env_init<wchar_t>>
0181 {
0182 static env_init<char> conv(const env_init<wchar_t> & in)
0183 {
0184 return {basic_environment<char>(in.env)};
0185 }
0186 };
0187
0188 template<>
0189 struct char_converter<wchar_t, env_init<char>>
0190 {
0191 static env_init<wchar_t> conv(const env_init<char> & in)
0192 {
0193 return {basic_environment<wchar_t>(in.env)};
0194 }
0195 };
0196
0197 template<>
0198 struct char_converter<char, basic_environment<wchar_t>>
0199 {
0200 static basic_environment<char> conv(const basic_environment<wchar_t> & in)
0201 {
0202 return { basic_environment<char>(in) };
0203 }
0204 };
0205
0206 template<>
0207 struct char_converter<wchar_t, basic_environment<char>>
0208 {
0209 static basic_environment<wchar_t> conv(const basic_environment<char> & in)
0210 {
0211 return { basic_environment<wchar_t>(in) };
0212 }
0213 };
0214
0215 template<typename Char>
0216 struct env_proxy
0217 {
0218 using string_type = std::basic_string<Char>;
0219 string_type key;
0220
0221
0222 env_set<Char> operator=(const string_type & value)
0223 {
0224 return {std::move(key), value};
0225 }
0226 env_set<Char> operator=(const std::vector<string_type> & value)
0227 {
0228 return {std::move(key), make_env_string<Char>(value)};
0229 }
0230 env_set<Char> operator=(const std::initializer_list<const Char*> & value)
0231 {
0232 return {std::move(key), make_env_string<Char>(value)};
0233 }
0234
0235 env_append<Char> operator+=(const string_type & value)
0236 {
0237 return {std::move(key), value};
0238 }
0239 env_append<Char> operator+=(const std::vector<string_type> & value)
0240 {
0241 return {std::move(key), make_env_string<Char>(value)};
0242 }
0243 env_append<Char> operator+=(const std::initializer_list<const Char*> & value)
0244 {
0245 return {std::move(key), make_env_string<Char>(value)};
0246 }
0247 env_reset<Char> operator=(boost::none_t)
0248 {
0249 return {std::move(key)};
0250 }
0251 };
0252
0253 struct env_
0254 {
0255 constexpr env_() {};
0256
0257 template<typename Char>
0258 env_set<Char> operator()(const std::basic_string<Char> & key,
0259 const std::basic_string<Char> & value) const
0260 {
0261 return {key, value};
0262 }
0263 template<typename Char>
0264 env_set<Char> operator()(const std::basic_string<Char> & key,
0265 const std::vector<std::basic_string<Char>> & value) const
0266 {
0267 return {key, make_env_string<Char>(value)};
0268 }
0269 template<typename Char>
0270 env_set<Char> operator()(const std::basic_string<Char> & key,
0271 const std::initializer_list<Char*> & value) const
0272 {
0273 return {key, make_env_string<Char>(value)};
0274 }
0275 template<typename Char>
0276 env_reset<Char> operator()(const std::basic_string<Char> & key, boost::none_t)
0277 {
0278 return {key};
0279 }
0280 template<typename Char>
0281 env_proxy<Char> operator[](const std::basic_string<Char> & key) const
0282 {
0283 return {key};
0284 }
0285 template<typename Char>
0286 env_proxy<Char> operator[](const Char* key) const
0287 {
0288 return {key};
0289 }
0290 template<typename Char>
0291 env_init<Char> operator()(const basic_environment<Char> & env) const
0292 {
0293 return {env};
0294 }
0295 template<typename Char>
0296 env_init<Char> operator= (const basic_environment<Char> & env) const
0297 {
0298 return {env};
0299 }
0300 };
0301
0302 template<typename Char>
0303 struct env_builder
0304 {
0305 basic_environment<Char> env;
0306 env_builder() : env{basic_native_environment<Char>()} {}
0307
0308 void operator()(const basic_environment<Char> & e)
0309 {
0310 env = e;
0311 }
0312
0313 void operator()(env_init<Char> & ei)
0314 {
0315 env = std::move(ei.env);
0316 }
0317 void operator()(env_set<Char> & es)
0318 {
0319 env[es.key] = es.value;
0320 }
0321 void operator()(env_reset<Char> & es)
0322 {
0323 env.erase(es.key);
0324 }
0325 template<typename T>
0326 void operator()(env_append<T> & es)
0327 {
0328 env[es.key] += es.value;
0329 }
0330
0331 typedef api::env_init<Char> result_type;
0332 api::env_init<Char> get_initializer()
0333 {
0334 return api::env_init<Char>(std::move(env));
0335 }
0336 };
0337
0338 template<>
0339 struct initializer_builder<env_tag<char>>
0340 {
0341 typedef env_builder<char> type;
0342 };
0343
0344 template<>
0345 struct initializer_builder<env_tag<wchar_t>>
0346 {
0347 typedef env_builder<wchar_t> type;
0348 };
0349
0350 }
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498 constexpr boost::process::detail::env_ env{};
0499
0500
0501 }}
0502
0503 #endif