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 
31 namespace armarx
32 {
33  /**
34  * \class ArmarXDataPath
35  * \ingroup core-utility
36  * The ArmarXDataPath class provides static methods to handle ArmarX data directories.
37  *
38  * Data paths can be defined via the environment variable ArmarXData_DIRS (semicolon separated list).
39  * Additionally ArmarX Properties (ArmarX.DataPath) can be used either by command line or by config files.
40  *
41  * The search order is as follows
42  * 1. Environment variable
43  * 2. Properties
44  *
45  * Usually, the paths are initialized by the framework automatically (see Application).
46  *
47  * To convert a filename given relatively to one of the data directories use the following syntax
48  * @code
49  * std::string relativeFilename("robot/ArmarIII/ArmarIII.xml");
50  * std::string absoluteFilename;
51  * bool fileFound = ArmarXDataPath::getAbsolutePath(relativeFilename, absoluteFilename);
52  * if (fileFound)
53  * load(absoluteFilename);
54  * @endcode
55  *
56  */
58  {
59  public:
60  /*!
61  * This method searches all data paths for a valid file.
62  * The precedence of the datapaths is in the insertion order.
63  * The datapaths given to this function have the highest precedence.
64  * \param relativeFilename The file relatively to one of the data paths
65  * \param storeAbsoluteFilename In case a valid absolute filename can be found, it is stored here.
66  * \param additionalSearchPaths Additional search paths that are not in the globally registered variable.
67  * \param verbose If set to true, the function will print all available paths and the requested file to the console on failure.
68  * \return true on success.
69  */
70  static bool getAbsolutePath(const std::string& relativeFilename,
71  std::string& storeAbsoluteFilename,
72  const std::vector<std::string>& additionalSearchPaths = {},
73  bool verbose = true);
74 
75  /*!
76  * This method searches all data paths for a valid file.
77  * The precedence of the datapaths is in the insertion order.
78  * The datapaths given to this function have the highest precedence.
79  * \param relativeFilename The file relatively to one of the data paths
80  * \param additionalSearchPaths Additional search paths that are not in the globally registered variable.
81  * \param verbose If set to true, the function will print all available paths and the requested file to the console on failure.
82  * \return In case a valid absolute filename can be found, it is returned (otherwise an empty string is returned).
83  */
84  static std::string
85  getAbsolutePath(const std::string& relativeFilename,
86  const std::vector<std::string>& additionalSearchPaths = {},
87  bool verbose = true);
88 
89  static bool SearchReadableFile(const std::string& querryFileName,
90  std::string& resultFileName,
91  bool verbose = true);
92  /*!
93  * Search all project data paths and reports within which project the file can be located.
94  * First hit is reported.
95  * If none of the project's data paths contains the file, an empty string is returned.
96  * \param projects List of ArmarXProjects
97  * \param relativeFilename Filename relative to a project's data directory.
98  * \return The armarx project that contains the given file in its data directory.
99  */
100  static std::string getProject(const std::vector<std::string>& projects,
101  const std::string& relativeFilename);
102 
103  static std::string cleanPath(const std::string& filepathStr);
104 
105 
106  /**
107  * @brief This method tries to morph a given absolute path into a
108  * relative path to a ArmarXDataPath.
109  * @param absolutePathString String of an absolute (file)path
110  * @return The relative path to the given absolute (file)path, if possible.
111  * Otherwise the given absolute path.
112  * @see addDataPaths()
113  */
114  static std::string getRelativeArmarXPath(const std::string& absolutePathString);
115 
116  /**
117  * @brief Transform an absolute filepath into a relative path of the other absolute filepath.
118  *
119  * For example:
120  * @snippet ArmarXCore/core/test/DataPathTest.cpp DataPath MakeRelative
121  * @param from Base path, to which the path should be made relative
122  * @param to Path that should be made relative
123  * @return Relative path
124  * @throws LocalException
125  */
126  static std::string relativeTo(const std::string& from, const std::string& to);
127  static bool mergePaths(std::string path, std::string subPath, std::string& result);
128 
129  /*!
130  * Add data paths to the internal search list.
131  * Multiple paths are separated by a semicolon ";" and environment
132  * variables specified with "${}" inside the paths are expanded.
133  * The latest additions have the highest precedence, when solving relative paths.
134  * \code
135  * addDataPaths("/root;/usr");
136  * addDataPaths("/home;/var");
137  * \endcode
138  * This would result in the following precedence order: home, var, root, usr.
139  * \param dataPathList A semicolon separated list of absolute paths.
140  * Each path is checked for existance and in case it is not present it is silently ignored.
141  */
142  static void addDataPaths(const std::string& dataPathList);
143  static void addDataPaths(const std::vector<std::string>& dataPathList);
144  static void addDataPath(const std::string& dataPath);
145 
146  /*!
147  * The base directory of ArmarX.
148  * Checks the environment variables for ArmarXHome_DIR and returns the entry.
149  */
150  [[deprecated("ArmarXHome_DIR is not used anymore. Please remove usages of this function. "
151  "Keep the behaviour resulting by this function returning an empty string.")]]
152  static std::string 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:542
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:410
armarx::ArmarXDataPath::addDataPath
static void addDataPath(const std::string &dataPath)
Definition: ArmarXDataPath.cpp:576
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:521
armarx::ArmarXDataPath::cleanPath
static std::string cleanPath(const std::string &filepathStr)
Definition: ArmarXDataPath.cpp:331
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:829
armarx::ArmarXDataPath::getHomePath
static std::string getHomePath()
Definition: ArmarXDataPath.cpp:583
armarx::ArmarXDataPath::getDataPaths
static std::vector< std::string > getDataPaths()
Definition: ArmarXDataPath.cpp:620
armarx::ArmarXDataPath::getProject
static std::string getProject(const std::vector< std::string > &projects, const std::string &relativeFilename)
Definition: ArmarXDataPath.cpp:301
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:513
armarx::ArmarXDataPath::GetCachePath
static std::string GetCachePath()
The base Cache directory of ArmarX.
Definition: ArmarXDataPath.cpp:734
armarx::ArmarXDataPath
Definition: ArmarXDataPath.h:57
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:382
armarx::ArmarXDataPath::initDataPaths
static void initDataPaths(const std::string &dataPathList)
Definition: ArmarXDataPath.cpp:477
armarx::ArmarXDataPath::GetDefaultUserConfigPath
static std::string GetDefaultUserConfigPath()
The user config directory of ArmarX.
Definition: ArmarXDataPath.cpp:782
armarx::ArmarXDataPath::SearchReadableFile
static bool SearchReadableFile(const std::string &querryFileName, std::string &resultFileName, bool verbose=true)
Definition: ArmarXDataPath.cpp:218
armarx::ArmarXDataPath::addDataPaths
static void addDataPaths(const std::string &dataPathList)
Definition: ArmarXDataPath.cpp:559
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:483
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:111
armarx::ArmarXDataPath::mergePaths
static bool mergePaths(std::string path, std::string subPath, std::string &result)
Definition: ArmarXDataPath.cpp:456
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28