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) Lumiera.org
5  2011, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
29 #include "lib/error.hpp"
30 #include "lib/searchpath.hpp"
31 #include "lib/symbol.hpp"
32 
33 
37 #define GET_PATH_TO_EXECUTABLE "/proc/self/exe"
38 
39 
40 
41 namespace lib {
42 
43  using std::regex;
44  using std::regex_replace;
45 
46  const regex SearchPathSplitter::EXTRACT_PATHSPEC ("[^:]+");
47 
48 
52  string
54  {
55  static string buff(lib::STRING_MAX_RELEVANT+1, '\0' );
56  if (!buff[0])
57  {
58  ssize_t chars_read = readlink (GET_PATH_TO_EXECUTABLE, &buff[0], lib::STRING_MAX_RELEVANT);
59 
60  if (0 > chars_read || chars_read == ssize_t(lib::STRING_MAX_RELEVANT))
61  throw error::Fatal ("unable to discover path of running executable");
62 
63  buff.resize(chars_read);
64  }
65  return buff;
66  }
67 
68 
73  string
74  replaceMagicLinkerTokens (string const& src)
75  {
76  static const regex PICK_ORIGIN_TOKEN ("\\$?ORIGIN/?");
77  static const string expandedOriginDir
78  = fsys::path (findExePath()).parent_path().string() + "/";
79 
80  return regex_replace(src, PICK_ORIGIN_TOKEN, expandedOriginDir);
81  }
82 
83 
84 
85 
86 
87  string
88  resolveModulePath (fsys::path moduleName, string searchPath)
89  {
90  fsys::path modulePathName (moduleName);
91  SearchPathSplitter searchLocation(searchPath);
92 
93  while (!fsys::exists (modulePathName))
94  {
95  // try / continue search path
96  if (searchLocation.isValid())
97  modulePathName = fsys::path() / searchLocation.next() / moduleName;
98  else
99  throw error::Config ("Module \""+moduleName.string()+"\" not found"
100  + (searchPath.empty()? ".":" in search path: "+searchPath));
101  }
102 
103  TRACE (config, "found module %s", modulePathName.string().c_str());
104  return modulePathName.string();
105  }
106 
107 
108 
109 } // namespace lib
Helpers to handle directory search paths.
string findExePath()
retrieve the location of the executable
Definition: searchpath.cpp:53
const size_t STRING_MAX_RELEVANT
safety guard: maximum number of chars to process.
Definition: symbol-impl.cpp:55
Implementation namespace for support and library code.
string replaceMagicLinkerTokens(string const &src)
replace $ORIGIN tokens in the given string
Definition: searchpath.cpp:74
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
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:37
Lumiera error handling (C++ interface).
Helper: Access a path Specification as a sequence of filesystem Paths.
Definition: searchpath.hpp:71
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:88