search_path.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) 2006, 2007 Julio M. Merino Vidal
  2. // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
  3. // Copyright (c) 2009 Boris Schaeling
  4. // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
  5. // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_PROCESS_WINDOWS_SEARCH_PATH_HPP
  10. #define BOOST_PROCESS_WINDOWS_SEARCH_PATH_HPP
  11. #include <boost/process/v1/detail/config.hpp>
  12. #include <boost/process/v1/filesystem.hpp>
  13. #include <boost/system/error_code.hpp>
  14. #include <string>
  15. #include <stdexcept>
  16. #include <array>
  17. #include <atomic>
  18. #include <cstdlib>
  19. #include <boost/winapi/shell.hpp>
  20. #include <boost/process/v1/environment.hpp>
  21. namespace boost { namespace process { BOOST_PROCESS_V1_INLINE namespace v1 { namespace detail { namespace windows {
  22. inline boost::process::v1::filesystem::path search_path(
  23. const boost::process::v1::filesystem::path &filename,
  24. const std::vector<boost::process::v1::filesystem::path> &path)
  25. {
  26. const ::boost::process::v1::wnative_environment ne{};
  27. typedef typename ::boost::process::v1::wnative_environment::const_entry_type value_type;
  28. const auto id = L"PATHEXT";
  29. auto itr = std::find_if(ne.cbegin(), ne.cend(),
  30. [&](const value_type & e)
  31. {return id == ::boost::to_upper_copy(e.get_name(), ::boost::process::v1::detail::process_locale());});
  32. std::vector<std::wstring> extensions_in;
  33. if (itr != ne.cend())
  34. extensions_in = itr->to_vector();
  35. std::vector<std::wstring> extensions((extensions_in.size() * 2) + 1);
  36. auto it_ex = extensions.begin();
  37. it_ex++;
  38. it_ex = std::transform(extensions_in.begin(), extensions_in.end(), it_ex,
  39. [](const std::wstring & ws){return boost::to_lower_copy(ws, ::boost::process::v1::detail::process_locale());});
  40. std::transform(extensions_in.begin(), extensions_in.end(), it_ex,
  41. [](const std::wstring & ws){return boost::to_upper_copy(ws, ::boost::process::v1::detail::process_locale());});
  42. std::copy(std::make_move_iterator(extensions_in.begin()), std::make_move_iterator(extensions_in.end()), extensions.begin() + 1);
  43. for (auto & ext : extensions)
  44. boost::to_lower(ext);
  45. for (const boost::process::v1::filesystem::path & pp_ : path)
  46. {
  47. auto p = pp_ / filename;
  48. for (boost::process::v1::filesystem::path ext : extensions)
  49. {
  50. boost::process::v1::filesystem::path pp_ext = p;
  51. pp_ext += ext;
  52. #if defined(BOOST_PROCESS_USE_STD_FS)
  53. std::error_code ec;
  54. #else
  55. boost::system::error_code ec;
  56. #endif
  57. bool file = boost::process::v1::filesystem::is_regular_file(pp_ext, ec);
  58. if (!ec && file &&
  59. ::boost::winapi::sh_get_file_info(pp_ext.native().c_str(), 0, 0, 0, ::boost::winapi::SHGFI_EXETYPE_))
  60. {
  61. return pp_ext;
  62. }
  63. }
  64. }
  65. return "";
  66. }
  67. }}}}}
  68. #endif