ArmarXDataPath.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::core
19 * @author Nikolaus Vahrenkamp
20 * @date 2012
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
27 #include <string>
28 #include <vector>
29 
30 namespace armarx
31 {
32  /**
33  * \class ArmarXDataPath
34  * \ingroup core-utility
35  * The ArmarXDataPath class provides static methods to handle ArmarX data directories.
36  *
37  * Data paths can be defined via the environment variable ArmarXData_DIRS (semicolon separated list).
38  * Additionally ArmarX Properties (ArmarX.DataPath) can be used either by command line or by config files.
39  *
40  * The search order is as follows
41  * 1. Environment variable
42  * 2. Properties
43  *
44  * Usually, the paths are initialized by the framework automatically (see Application).
45  *
46  * To convert a filename given relatively to one of the data directories use the following syntax
47  * @code
48  * std::string relativeFilename("robot/ArmarIII/ArmarIII.xml");
49  * std::string absoluteFilename;
50  * bool fileFound = ArmarXDataPath::getAbsolutePath(relativeFilename, absoluteFilename);
51  * if (fileFound)
52  * load(absoluteFilename);
53  * @endcode
54  *
55  */
57  {
58  public:
59  /*!
60  * This method searches all data paths for a valid file.
61  * The precedence of the datapaths is in the insertion order.
62  * The datapaths given to this function have the highest precedence.
63  * \param relativeFilename The file relatively to one of the data paths
64  * \param storeAbsoluteFilename In case a valid absolute filename can be found, it is stored here.
65  * \param additionalSearchPaths Additional search paths that are not in the globally registered variable.
66  * \param verbose If set to true, the function will print all available paths and the requested file to the console on failure.
67  * \return true on success.
68  */
69  static bool getAbsolutePath(const std::string& relativeFilename,
70  std::string& storeAbsoluteFilename,
71  const std::vector<std::string>& additionalSearchPaths = {},
72  bool verbose = true);
73 
74  /*!
75  * This method searches all data paths for a valid file.
76  * The precedence of the datapaths is in the insertion order.
77  * The datapaths given to this function have the highest precedence.
78  * \param relativeFilename The file relatively to one of the data paths
79  * \param additionalSearchPaths Additional search paths that are not in the globally registered variable.
80  * \param verbose If set to true, the function will print all available paths and the requested file to the console on failure.
81  * \return In case a valid absolute filename can be found, it is returned (otherwise an empty string is returned).
82  */
83  static std::string
84  getAbsolutePath(const std::string& relativeFilename,
85  const std::vector<std::string>& additionalSearchPaths = {},
86  bool verbose = true);
87 
88  static bool SearchReadableFile(const std::string& querryFileName,
89  std::string& resultFileName,
90  bool verbose = true);
91  /*!
92  * Search all project data paths and reports within which project the file can be located.
93  * First hit is reported.
94  * If none of the project's data paths contains the file, an empty string is returned.
95  * \param projects List of ArmarXProjects
96  * \param relativeFilename Filename relative to a project's data directory.
97  * \return The armarx project that contains the given file in its data directory.
98  */
99  static std::string getProject(const std::vector<std::string>& projects,
100  const std::string& relativeFilename);
101 
102  static std::string cleanPath(const std::string& filepathStr);
103 
104 
105  /**
106  * @brief This method tries to morph a given absolute path into a
107  * relative path to a ArmarXDataPath.
108  * @param absolutePathString String of an absolute (file)path
109  * @return The relative path to the given absolute (file)path, if possible.
110  * Otherwise the given absolute path.
111  * @see addDataPaths()
112  */
113  static std::string getRelativeArmarXPath(const std::string& absolutePathString);
114 
115  /**
116  * @brief Transform an absolute filepath into a relative path of the other absolute filepath.
117  *
118  * For example:
119  * @snippet ArmarXCore/core/test/DataPathTest.cpp DataPath MakeRelative
120  * @param from Base path, to which the path should be made relative
121  * @param to Path that should be made relative
122  * @return Relative path
123  * @throws LocalException
124  */
125  static std::string relativeTo(const std::string& from, const std::string& to);
126  static bool mergePaths(std::string path, std::string subPath, std::string& result);
127 
128  /*!
129  * Add data paths to the internal search list.
130  * Multiple paths are separated by a semicolon ";" and environment
131  * variables specified with "${}" inside the paths are expanded.
132  * The latest additions have the highest precedence, when solving relative paths.
133  * \code
134  * addDataPaths("/root;/usr");
135  * addDataPaths("/home;/var");
136  * \endcode
137  * This would result in the following precedence order: home, var, root, usr.
138  * \param dataPathList A semicolon separated list of absolute paths.
139  * Each path is checked for existance and in case it is not present it is silently ignored.
140  */
141  static void addDataPaths(const std::string& dataPathList);
142  static void addDataPaths(const std::vector<std::string>& dataPathList);
143  static void addDataPath(const std::string& dataPath);
144 
145  /*!
146  * The base directory of ArmarX.
147  * Checks the environment variables for ArmarXHome_DIR and returns the entry.
148  */
149  [[deprecated("ArmarXHome_DIR is not used anymore. Please remove usages of this function. "
150  "Keep the behaviour resulting by this function returning an empty "
151  "string.")]] static std::string
152  getHomePath();
153 
154  /*!
155  * Returns all absolute data paths.
156  */
157  static std::vector<std::string> getDataPaths();
158 
159  /*!
160  * Initializes data paths. Automatically called by an Application.
161  */
162  static void initDataPaths(const std::string& dataPathList);
163 
164  /**
165  * @brief ReplaceEnvVars replaces environment variables in a string with
166  * their values, if the env. variable is set. E.g. "${HOME}/.armarx/default.cfg"
167  * @param string String in which the environment variables are to be replaced
168  * @return returns true if atleast one replacement has been done.
169  */
170  static bool ReplaceEnvVars(std::string& string);
171 
172  /**
173  * @brief Replaces all occurences of variables in bash notation,
174  * e.g. ${HOME}, with the given value in the string.
175  * @param string String to be modified
176  * @param varName Name of the variable
177  * @param varValue Value to be inserted in place of varName occurrences
178  */
179  static void
180  ReplaceVar(std::string& string, const std::string varName, const std::string& varValue);
181 
182  /**
183  * @brief Resolves a path like ~/myfile.txt or $HOME/myfile.txt to /home/user/myfile.txt.
184  * @param string
185  */
186  static void ResolveHomePath(std::string& path);
187 
188  /**
189  * @brief Resolves environment variables and home paths and tries to make path absolute.
190  * @param path The path, may contain env. variables and be a relative data path.
191  * @param verbose Passed to `getAbsolutePath()`.
192  * @return The resolved path.
193  */
194  static std::string resolvePath(const std::string& path, bool verbose = true);
195 
196  /**
197  * @brief The base Cache directory of ArmarX.
198  * @return returns the cache path of ArmarX or an empty String
199  * if there is no valid Application instance
200  */
201  static std::string GetCachePath();
202 
203  /**
204  * @brief The user config directory of ArmarX.
205  * @return returns the default path of ArmarX (usually $ARMARX_WORKSPACE/armarx_config)
206  * or the user set path via $ARMARX_CONFIG_DIR
207  */
208  static std::string GetDefaultUserConfigPath();
209 
210  /**
211  * @brief Search for the package and add its data path if it was found.
212  * @return returns whether the package was found
213  */
214  static bool FindPackageAndAddDataPath(const std::string& packageName);
215 
216  private:
217  //! Initializes and searches the environment variable ArmarXData_DIR for data directories.
218  static void init();
219 
220  static bool __pathIsValid(const std::string& path);
221  static bool __addPath(const std::string& path);
222  static bool __addPaths(const std::string& pathList);
223  static std::vector<std::string> __separatePaths(const std::string& pathList);
224 
225  ArmarXDataPath(); // no instantiation allowed
226  };
227 } // namespace armarx
armarx::ArmarXDataPath::resolvePath
static std::string resolvePath(const std::string &path, bool verbose=true)
Resolves environment variables and home paths and tries to make path absolute.
Definition: ArmarXDataPath.cpp:538
armarx::ArmarXDataPath::relativeTo
static std::string relativeTo(const std::string &from, const std::string &to)
Transform an absolute filepath into a relative path of the other absolute filepath.
Definition: ArmarXDataPath.cpp:407
armarx::ArmarXDataPath::addDataPath
static void addDataPath(const std::string &dataPath)
Definition: ArmarXDataPath.cpp:571
armarx::ArmarXDataPath::ResolveHomePath
static void ResolveHomePath(std::string &path)
Resolves a path like ~/myfile.txt or $HOME/myfile.txt to /home/user/myfile.txt.
Definition: ArmarXDataPath.cpp:518
armarx::ArmarXDataPath::cleanPath
static std::string cleanPath(const std::string &filepathStr)
Definition: ArmarXDataPath.cpp:328
armarx::ArmarXDataPath::FindPackageAndAddDataPath
static bool FindPackageAndAddDataPath(const std::string &packageName)
Search for the package and add its data path if it was found.
Definition: ArmarXDataPath.cpp:824
armarx::ArmarXDataPath::getHomePath
static std::string getHomePath()
Definition: ArmarXDataPath.cpp:578
armarx::ArmarXDataPath::getDataPaths
static std::vector< std::string > getDataPaths()
Definition: ArmarXDataPath.cpp:615
armarx::ArmarXDataPath::getProject
static std::string getProject(const std::vector< std::string > &projects, const std::string &relativeFilename)
Definition: ArmarXDataPath.cpp:299
armarx::ArmarXDataPath::ReplaceVar
static void ReplaceVar(std::string &string, const std::string varName, const std::string &varValue)
Replaces all occurences of variables in bash notation, e.g.
Definition: ArmarXDataPath.cpp:510
armarx::ArmarXDataPath::GetCachePath
static std::string GetCachePath()
The base Cache directory of ArmarX.
Definition: ArmarXDataPath.cpp:729
armarx::ArmarXDataPath
Definition: ArmarXDataPath.h:56
armarx::ArmarXDataPath::getRelativeArmarXPath
static std::string getRelativeArmarXPath(const std::string &absolutePathString)
This method tries to morph a given absolute path into a relative path to a ArmarXDataPath.
Definition: ArmarXDataPath.cpp:379
armarx::ArmarXDataPath::initDataPaths
static void initDataPaths(const std::string &dataPathList)
Definition: ArmarXDataPath.cpp:474
armarx::ArmarXDataPath::GetDefaultUserConfigPath
static std::string GetDefaultUserConfigPath()
The user config directory of ArmarX.
Definition: ArmarXDataPath.cpp:777
armarx::ArmarXDataPath::SearchReadableFile
static bool SearchReadableFile(const std::string &querryFileName, std::string &resultFileName, bool verbose=true)
Definition: ArmarXDataPath.cpp:216
armarx::ArmarXDataPath::addDataPaths
static void addDataPaths(const std::string &dataPathList)
Definition: ArmarXDataPath.cpp:554
armarx::ArmarXDataPath::ReplaceEnvVars
static bool ReplaceEnvVars(std::string &string)
ReplaceEnvVars replaces environment variables in a string with their values, if the env.
Definition: ArmarXDataPath.cpp:480
armarx::ArmarXDataPath::getAbsolutePath
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
Definition: ArmarXDataPath.cpp:109
armarx::ArmarXDataPath::mergePaths
static bool mergePaths(std::string path, std::string subPath, std::string &result)
Definition: ArmarXDataPath.cpp:453
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27