search_path.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/detail/config.hpp>
  12. #include <boost/filesystem/path.hpp>
  13. #include <boost/filesystem/operations.hpp>
  14. #include <boost/system/error_code.hpp>
  15. #include <string>
  16. #include <stdexcept>
  17. #include <array>
  18. #include <atomic>
  19. #include <cstdlib>
  20. #include <boost/winapi/shell.hpp>
  21. #include <boost/process/environment.hpp>
  22. namespace boost { namespace process { namespace detail { namespace windows {
  23. inline boost::filesystem::path search_path(
  24. const boost::filesystem::path &filename,
  25. const std::vector<boost::filesystem::path> &path)
  26. {
  27. const ::boost::process::wnative_environment ne{};
  28. typedef typename ::boost::process::wnative_environment::const_entry_type value_type;
  29. const auto id = L"PATHEXT";
  30. auto itr = std::find_if(ne.cbegin(), ne.cend(),
  31. [&](const value_type & e)
  32. {return id == ::boost::to_upper_copy(e.get_name(), ::boost::process::detail::process_locale());});
  33. auto extensions_in = itr->to_vector();
  34. std::vector<std::wstring> extensions((extensions_in.size() * 2) + 1);
  35. auto it_ex = extensions.begin();
  36. it_ex++;
  37. it_ex = std::transform(extensions_in.begin(), extensions_in.end(), it_ex,
  38. [](const std::wstring & ws){return boost::to_lower_copy(ws, ::boost::process::detail::process_locale());});
  39. std::transform(extensions_in.begin(), extensions_in.end(), it_ex,
  40. [](const std::wstring & ws){return boost::to_upper_copy(ws, ::boost::process::detail::process_locale());});
  41. std::copy(std::make_move_iterator(extensions_in.begin()), std::make_move_iterator(extensions_in.end()), extensions.begin() + 1);
  42. for (auto & ext : extensions)
  43. boost::to_lower(ext);
  44. for (const boost::filesystem::path & pp_ : path)
  45. {
  46. auto p = pp_ / filename;
  47. for (boost::filesystem::path ext : extensions)
  48. {
  49. boost::filesystem::path pp_ext = p;
  50. pp_ext += ext;
  51. boost::system::error_code ec;
  52. bool file = boost::filesystem::is_regular_file(pp_ext, ec);
  53. if (!ec && file &&
  54. ::boost::winapi::sh_get_file_info(pp_ext.native().c_str(), 0, 0, 0, ::boost::winapi::SHGFI_EXETYPE_))
  55. {
  56. return pp_ext;
  57. }
  58. }
  59. }
  60. return "";
  61. }
  62. }}}}
  63. #endif