-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPkgConfigHelper.cpp
More file actions
83 lines (66 loc) · 2.3 KB
/
Copy pathPkgConfigHelper.cpp
File metadata and controls
83 lines (66 loc) · 2.3 KB
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
#include "PkgConfigHelper.hpp"
#include <string>
#include <boost/filesystem.hpp>
#include <boost/tokenizer.hpp>
#include <fstream>
using namespace orocos_cpp;
bool PkgConfigHelper::solveString(std::string &input, const std::string &replace, const std::string &by)
{
size_t start_pos = input.find(replace);
if(start_pos == std::string::npos)
return false;
input.replace(start_pos, replace.length(), by);
return true;
}
bool PkgConfigHelper::parsePkgConfig(const std::string& pkgConfigFileName, const std::vector< std::string > &searchedFields, std::vector< std::string > &result)
{
const char *pkgConfigPath = getenv("PKG_CONFIG_PATH");
if(!pkgConfigPath)
{
throw std::runtime_error("Internal Error, no pkgConfig path found.");
}
std::string pkgConfigPathS = pkgConfigPath;
boost::char_separator<char> sep(":");
boost::tokenizer<boost::char_separator<char> > paths(pkgConfigPathS, sep);
std::string searchedPath;
for(const std::string &path: paths)
{
std::string candidate = path + "/" + pkgConfigFileName;
if(boost::filesystem::exists(candidate))
{
searchedPath = candidate;
break;
}
}
if(searchedPath.empty())
{
throw std::runtime_error("Error, could not find pkg-config file " + pkgConfigFileName + " in the PKG_CONFIG_PATH");
}
std::vector<bool> found;
result.resize(searchedFields.size());
found.resize(searchedFields.size(), false);
std::ifstream fileStream(searchedPath);
while(!fileStream.eof())
{
std::string curLine;
std::getline(fileStream, curLine);
for(size_t i = 0; i < searchedFields.size(); i++)
{
const std::string searched(searchedFields[i] + "=");
if(curLine.substr(0, searched.size()) == searched)
{
result[i] = curLine.substr(searched.size(), curLine.size());
found[i] = true;
//check if we found all search values
bool doReturn = true;
for(bool f: found)
{
doReturn &= f;
}
if(doReturn)
return true;
}
}
}
return false;
}