Lumiera  0.pre.03
»edit your freedom«
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/symbol.hpp"
23 
24 
28 #define GET_PATH_TO_EXECUTABLE "/proc/self/exe"
29 
30 
31 
32 namespace lib {
33 
34  using std::regex;
35  using std::regex_replace;
36 
37  const regex SearchPathSplitter::EXTRACT_PATHSPEC ("[^:]+");
38 
39 
43  string
45  {
46  static string buff(lib::STRING_MAX_RELEVANT+1, '\0' );
47  if (!buff[0])
48  {
49  ssize_t chars_read = readlink (GET_PATH_TO_EXECUTABLE, &buff[0], lib::STRING_MAX_RELEVANT);
50 
51  if (0 > chars_read || chars_read == ssize_t(lib::STRING_MAX_RELEVANT))
52  throw error::Fatal ("unable to discover path of running executable");
53 
54  buff.resize(chars_read);
55  }
56  return buff;
57  }
58 
59 
64  string
65  replaceMagicLinkerTokens (string const& src)
66  {
67  static const regex PICK_ORIGIN_TOKEN ("\\$?ORIGIN/?");
68  static const string expandedOriginDir
69  = fsys::path (findExePath()).parent_path().string() + "/";
70 
71  return regex_replace(src, PICK_ORIGIN_TOKEN, expandedOriginDir);
72  }
73 
74 
75 
76 
77 
78  string
79  resolveModulePath (fsys::path moduleName, string searchPath)
80  {
81  fsys::path modulePathName (moduleName);
82  SearchPathSplitter searchLocation(searchPath);
83 
84  while (!fsys::exists (modulePathName))
85  {
86  // try / continue search path
87  if (searchLocation.isValid())
88  modulePathName = fsys::path() / searchLocation.next() / moduleName;
89  else
90  throw error::Config ("Module \""+moduleName.string()+"\" not found"
91  + (searchPath.empty()? ".":" in search path: "+searchPath));
92  }
93 
94  TRACE (config, "found module %s", modulePathName.string().c_str());
95  return modulePathName.string();
96  }
97 
98 
99 
100 } // namespace lib
Helpers to handle directory search paths.
string findExePath()
retrieve the location of the executable
Definition: searchpath.cpp:44
const size_t STRING_MAX_RELEVANT
safety guard: maximum number of chars to process.
Definition: symbol-impl.cpp:46
Implementation namespace for support and library code.
string replaceMagicLinkerTokens(string const &src)
replace $ORIGIN tokens in the given string
Definition: searchpath.cpp:65
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
Marker types to indicate a literal string and a Symbol.
#define GET_PATH_TO_EXECUTABLE
how to retrieve the absolute path of the currently running executable on a Linux system: read the lin...
Definition: searchpath.cpp:28
Lumiera error handling (C++ interface).
Helper: Access a path Specification as a sequence of filesystem Paths.
Definition: searchpath.hpp:62
string resolveModulePath(fsys::path moduleName, string searchPath)
helper to establish the location to search for loadable modules, configuration files, icons and further resources.
Definition: searchpath.cpp:79