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 <stdlib.h>
13
14#include <filesystem>
15#include <stdexcept>
16#include <string>
17
18#include <unistd.h>
19
21#include <boost/tokenizer.hpp>
22
24{
25
26 inline std::string
27 search_path(const std::string& filename, std::string path = "")
28 {
29 if (path.empty())
30 {
31 path = ::getenv("PATH");
32 if (path.empty())
33 BOOST_PROCESS_THROW(std::runtime_error("Environment variable PATH not found"));
34 }
35
36 std::string result;
37 using tokenizer = boost::tokenizer<boost::char_separator<char>>;
38 boost::char_separator<char> sep(":");
39 tokenizer tok(path, sep);
40 for (tokenizer::iterator it = tok.begin(); it != tok.end(); ++it)
41 {
42 std::filesystem::path p = *it;
43 p /= filename;
44 if (!::access(p.c_str(), X_OK))
45 {
46 result = p.string();
47 break;
48 }
49 }
50 return result;
51 }
52
53} // namespace boost::process::posix
Defines various macros.
#define BOOST_PROCESS_THROW(EX)
Defines how exceptions are thrown.
Definition config.hpp:69
std::string search_path(const std::string &filename, std::string path="")