summaryrefslogtreecommitdiff
path: root/plugins/New_GPG/src/include/boost/process/windows/search_path.hpp
blob: ceb9c6f7c2eeea8b75742b62c4d09ae7656aea4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
// Copyright (c) 2009 Boris Schaeling
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
// Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_PROCESS_WINDOWS_SEARCH_PATH_HPP
#define BOOST_PROCESS_WINDOWS_SEARCH_PATH_HPP

#include <boost/process/config.hpp>
#include <boost/filesystem.hpp>
#include <boost/tokenizer.hpp>
#include <boost/array.hpp>
#include <boost/system/error_code.hpp>
#include <string>
#include <stdexcept>
#include <stdlib.h>
#include <Shellapi.h>

namespace boost { namespace process { namespace windows {

#if defined(_UNICODE) || defined(UNICODE)
inline std::wstring search_path(const std::wstring &filename,
    std::wstring path = L"")
{
    if (path.empty())
    {
        path = ::_wgetenv(L"PATH");
        if (path.empty())
            BOOST_PROCESS_THROW(std::runtime_error(
                "Environment variable PATH not found"));
    }

    typedef boost::tokenizer<boost::char_separator<wchar_t>,
        std::wstring::const_iterator, std::wstring> tokenizer;
    boost::char_separator<wchar_t> sep(L";");
    tokenizer tok(path, sep);
    for (tokenizer::iterator it = tok.begin(); it != tok.end(); ++it)
    {
        boost::filesystem::path p = *it;
        p /= filename;
        boost::array<std::string, 4> extensions =
            { "", ".exe", ".com", ".bat" };
        for (boost::array<std::string, 4>::iterator it2 = extensions.begin();
            it2 != extensions.end(); ++it2)
        {
            boost::filesystem::path p2 = p;
            p2 /= (std::string)*it2;
            boost::system::error_code ec;
            bool file = boost::filesystem::is_regular_file(p2, ec);
            if (!ec && file &&
                SHGetFileInfoW(p2.c_str(), 0, 0, 0, SHGFI_EXETYPE))
            {
                return p2.wstring();
            }
        }
    }
    return L"";
}
#else
inline std::string search_path(const std::string &filename,
    std::string path = "")
{
    if (path.empty())
    {
        path = ::getenv("PATH");
        if (path.empty())
            BOOST_PROCESS_THROW(std::runtime_error(
                "Environment variable PATH not found"));
    }

    typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
    boost::char_separator<char> sep(";");
    tokenizer tok(path, sep);
    for (tokenizer::iterator it = tok.begin(); it != tok.end(); ++it)
    {
        boost::filesystem::path p = *it;
        p /= filename;
        boost::array<std::string, 4> extensions =
            { "", ".exe", ".com", ".bat" };
        for (boost::array<std::string, 4>::iterator it2 = extensions.begin();
            it2 != extensions.end(); ++it2)
        {
            boost::filesystem::path p2 = p;
            p2 += *it2;
            boost::system::error_code ec;
            bool file = boost::filesystem::is_regular_file(p2, ec);
            if (!ec && file &&
                SHGetFileInfoA(p2.string().c_str(), 0, 0, 0, SHGFI_EXETYPE))
            {
                return p2.string();
            }
        }
    }
    return "";
}
#endif

}}}

#endif