search_path.hpp
Go to the documentation of this file.
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 
10 #pragma once
11 
12 #include <boost/process/config.hpp>
13 #include <filesystem>
14 #include <boost/tokenizer.hpp>
15 #include <boost/array.hpp>
16 #include <boost/system/error_code.hpp>
17 #include <string>
18 #include <stdexcept>
19 #include <stdlib.h>
20 #include <Shellapi.h>
21 
23 {
24 
25 #if defined(_UNICODE) || defined(UNICODE)
26  inline std::wstring search_path(const std::wstring& filename,
27  std::wstring path = L"")
28  {
29  if (path.empty())
30  {
31  path = ::_wgetenv(L"PATH");
32  if (path.empty())
33  BOOST_PROCESS_THROW(std::runtime_error(
34  "Environment variable PATH not found"));
35  }
36 
37  typedef boost::tokenizer < boost::char_separator<wchar_t>,
38  std::wstring::const_iterator, std::wstring > tokenizer;
39  boost::char_separator<wchar_t> sep(L";");
40  tokenizer tok(path, sep);
41  for (tokenizer::iterator it = tok.begin(); it != tok.end(); ++it)
42  {
43  std::filesystem::path p = *it;
44  p /= filename;
45  boost::array<std::wstring, 4> extensions =
46  { L"", L".exe", L".com", L".bat" };
47  for (boost::array<std::wstring, 4>::iterator it2 = extensions.begin();
48  it2 != extensions.end(); ++it2)
49  {
50  std::filesystem::path p2 = p;
51  p2 += *it2;
52  boost::system::error_code ec;
53  bool file = std::filesystem::is_regular_file(p2, ec);
54  if (!ec && file &&
55  SHGetFileInfoW(p2.c_str(), 0, 0, 0, SHGFI_EXETYPE))
56  {
57  return p2.wstring();
58  }
59  }
60  }
61  return L"";
62  }
63 #else
64  inline std::string search_path(const std::string& filename,
65  std::string path = "")
66  {
67  if (path.empty())
68  {
69  path = ::getenv("PATH");
70  if (path.empty())
71  BOOST_PROCESS_THROW(std::runtime_error(
72  "Environment variable PATH not found"));
73  }
74 
75  using tokenizer = boost::tokenizer<boost::char_separator<char> >;
76  boost::char_separator<char> sep(";");
77  tokenizer tok(path, sep);
78  for (tokenizer::iterator it = tok.begin(); it != tok.end(); ++it)
79  {
80  std::filesystem::path p = *it;
81  p /= filename;
82  boost::array<std::string, 4> extensions =
83  { "", ".exe", ".com", ".bat" };
84  for (boost::array<std::string, 4>::iterator it2 = extensions.begin();
85  it2 != extensions.end(); ++it2)
86  {
87  std::filesystem::path p2 = p;
88  p2 += *it2;
89  boost::system::error_code ec;
90  bool file = std::filesystem::is_regular_file(p2, ec);
91  if (!ec && file &&
92  SHGetFileInfoA(p2.string().c_str(), 0, 0, 0, SHGFI_EXETYPE))
93  {
94  return p2.string();
95  }
96  }
97  }
98  return "";
99  }
100 #endif
101 
102 }
boost::process::windows
Definition: child.hpp:15
config.hpp
BOOST_PROCESS_THROW
#define BOOST_PROCESS_THROW(EX)
Definition: config.hpp:64
boost::process::windows::search_path
std::string search_path(const std::string &filename, std::string path="")
Definition: search_path.hpp:64
filename
std::string filename
Definition: VisualizationRobot.cpp:84