Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
searchpath.cpp
Go to the documentation of this file.
1/*
2 Searchpath - helpers for searching directory lists and locating modules
3
4 Copyright (C)
5 2011, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
14
20#include "lib/error.hpp"
21#include "lib/searchpath.hpp"
22#include "lib/format-string.hpp"
23#include "lib/symbol.hpp"
24
25
29#define GET_PATH_TO_EXECUTABLE "/proc/self/exe"
30
31
32
33namespace lib {
34
35 using util::_Fmt;
36 using std::regex;
37 using std::regex_replace;
38
39 const regex SearchPathSplitter::ACCEPT_PATHELEMENT{"(^|:)\\s*([^:]+)", regex::optimize};
40
41
45 fs::path
47 {
48 fs::path selfExe{GET_PATH_TO_EXECUTABLE};
49 if (not fs::exists (selfExe))
50 throw error::Fatal ("unable to discover path of running executable");
51 return fs::canonical (selfExe);
52 }
53
54
59 string
60 replaceMagicLinkerTokens (string const& src)
61 {
62 static const regex PICK_ORIGIN_TOKEN{"\\$?ORIGIN/?", regex::optimize};
63 static const string expandedOriginDir
64 = findExePath().parent_path().generic_string() + "/";
65
66 return regex_replace (src, PICK_ORIGIN_TOKEN, expandedOriginDir);
67 }
68
69
70
71
72
73 string
74 resolveModulePath (fs::path moduleName, string searchPath)
75 {
76 fs::path modulePathName (moduleName);
77 SearchPathSplitter searchLocation(searchPath);
78
79 while (not fs::exists (modulePathName))
80 {
81 // try / continue search path
82 if (not searchLocation)
83 throw error::Config{_Fmt{"Module %s not found%s"}
84 % moduleName
85 %(searchPath.empty()? ".":" in search path: "+searchPath)};
86 modulePathName = *searchLocation / moduleName;
87 ++searchLocation;
88 }
89
90 TRACE (config, "found module %s", cStr(modulePathName.generic_string()));
91 return modulePathName.generic_string();
92 }
93
94
95
96} // namespace lib
Helper: Access a path Specification as a sequence of filesystem Paths.
static const regex ACCEPT_PATHELEMENT
A front-end for using printf-style formatting.
Lumiera error handling (C++ interface).
Front-end for printf-style string template interpolation.
Implementation namespace for support and library code.
string resolveModulePath(fs::path moduleName, string searchPath)
helper to establish the location to search for loadable modules, configuration files,...
fs::path findExePath()
retrieve the location of the executable
string replaceMagicLinkerTokens(string const &src)
replace $ORIGIN tokens in the given string
LumieraError< LERR_(FATAL), Logic > Fatal
Definition error.hpp:208
LumieraError< LERR_(CONFIG), Invalid > Config
Definition error.hpp:212
#define GET_PATH_TO_EXECUTABLE
how to retrieve the absolute path of the currently running executable on a Linux system: read the lin...
Helpers to handle directory search paths.
Marker types to indicate a literal string and a Symbol.
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition symbol.hpp:60