YARP
Yet Another Robot Platform
dirs.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 
7 #ifndef YARP_CONF_DIRS_H
8 #define YARP_CONF_DIRS_H
9 
10 #include <yarp/conf/filesystem.h>
11 #include <yarp/conf/environment.h>
12 
13 #include <string>
14 #include <vector>
15 
16 namespace yarp {
17 namespace conf {
18 namespace dirs {
19 
20 #ifndef DOXYGEN_SHOULD_SKIP_THIS
21 #ifndef SWIG
22 
23 // Constants defining environment variables names
24 static constexpr const char YARP_DATA_HOME[]{"YARP_DATA_HOME"};
25 static constexpr const char YARP_DATA_DIRS[]{"YARP_DATA_DIRS"};
26 static constexpr const char YARP_CONFIG_HOME[]{"YARP_CONFIG_HOME"};
27 static constexpr const char YARP_CONFIG_DIRS[]{"YARP_CONFIG_DIRS"};
28 static constexpr const char YARP_CACHE_HOME[]{"YARP_CACHE_HOME"};
29 static constexpr const char YARP_RUNTIME_DIR[]{"YARP_RUNTIME_DIR"};
30 
31 static constexpr const char USER[]{"USER"};
32 static constexpr const char USERNAME[]{"USERNAME"};
33 static constexpr const char HOME[]{"HOME"};
34 static constexpr const char USERPROFILE[]{"USERPROFILE"};
35 static constexpr const char TMP[]{"TMP"};
36 static constexpr const char TEMP[]{"TEMP"};
37 static constexpr const char TMPDIR[]{"TMPDIR"};
38 static constexpr const char XDG_DATA_HOME[]{"XDG_DATA_HOME"};
39 static constexpr const char XDG_DATA_DIRS[]{"XDG_DATA_DIRS"};
40 static constexpr const char XDG_CONFIG_HOME[]{"XDG_CONFIG_HOME"};
41 static constexpr const char XDG_CONFIG_DIRS[]{"XDG_CONFIG_DIRS"};
42 static constexpr const char XDG_CACHE_HOME[]{"XDG_CACHE_HOME"};
43 static constexpr const char XDG_RUNTIME_DIR[]{"XDG_RUNTIME_DIR"};
44 static constexpr const char APPDATA[]{"APPDATA"};
45 static constexpr const char LOCALAPPDATA[]{"LOCALAPPDATA"};
46 static constexpr const char ALLUSERSPROFILE[]{"ALLUSERSPROFILE"};
47 
48 // Paths and suffixes
49 static constexpr const char XDG_DATA_HOME_SUFFIX[]{"/.local/share"};
50 static constexpr const char XDG_CONFIG_HOME_SUFFIX[]{"/.config"};
51 static constexpr const char XDG_CACHE_HOME_SUFFIX[]{"/.cache"};
52 static constexpr const char XDG_DATA_DIRS_DEFAULT[]{"/usr/local/share:/usr/share"};
53 static constexpr const char XDG_CONFIG_DIRS_DEFAULT[]{"/etc/xdg"};
54 
55 static constexpr const char UNIX_TMP_DIR_DEFAULT[]{"/tmp"};
56 static constexpr const char WIN_APPDATA_SUFFIX[]{"\\AppData\\Roaming"};
57 static constexpr const char WIN_LOCALAPPDATA_SUFFIX[]{"\\AppData\\Local"};
58 static constexpr const char WIN_APPDATA_LOCAL_TEMP_SUFFIX[]{"\\AppData\\Local\\Temp"};
59 static constexpr const char WIN_ALLUSERSPROFILE_DEFAULT[]{"C:\\ProgramData"};
60 static constexpr const char MACOS_DATAHOME_SUFFIX[]{"/Library/Application Support"};
61 static constexpr const char MACOS_CONFIGHOME_SUFFIX[]{"/Library/Preferences"};
62 static constexpr const char MACOS_DATA_DIRS_DEFAULT[]{"/usr/local/share:/usr/share"};
63 static constexpr const char MACOS_CONFIG_DIRS_DEFAULT[]{"/etc:/Library/Preferences"};
64 static constexpr const char MACOS_CACHEHOME_SUFFIX[]{"/Library/Caches"};
65 
66 // FIXME C++17 Use string_view?
67 static constexpr const char YARP_SUFFIX[]{ yarp::conf::filesystem::preferred_separator, 'y', 'a', 'r', 'p', '\0' }; // "/yarp" or "\\yarp"
68 static constexpr const char YARP_CONFIG_SUFFIX[]{ yarp::conf::filesystem::preferred_separator, 'y', 'a', 'r', 'p', yarp::conf::filesystem::preferred_separator, 'c', 'o', 'n', 'f', 'i', 'g', '\0' }; // "yarp/config" or "\\yarp\\config"
69 static constexpr const char RUNTIME_SUFFIX[]{ yarp::conf::filesystem::preferred_separator, 'r', 'u', 'n', 't', 'i', 'm', 'e', '\0' }; // "/runtime" or "\\runtime"
70 static constexpr const char RUNTIME_YARP_SUFFIX[]{ yarp::conf::filesystem::preferred_separator, 'r', 'u', 'n', 't', 'i', 'm', 'e', yarp::conf::filesystem::preferred_separator, 'y', 'a', 'r', 'p', '\0' }; // "/runtime/yarp" or "\\runtime\\yarp"
71 
72 #endif // SWIG
73 #endif // DOXYGEN_SHOULD_SKIP_THIS
74 
76 // Basic paths
77 
86 inline std::string home()
87 {
88 #if defined(_WIN32)
89  return yarp::conf::environment::get_string(USERPROFILE);
90 #else
92 #endif
93 }
94 
103 inline std::string tempdir()
104 {
105 #if defined(_WIN32)
106  return yarp::conf::environment::get_string(TEMP, yarp::conf::dirs::home() + WIN_APPDATA_LOCAL_TEMP_SUFFIX);
107 #else
108  return yarp::conf::environment::get_string(TMPDIR, UNIX_TMP_DIR_DEFAULT);
109 #endif
110 }
111 
112 
115 // XDG Base Directory specifications (and equivalents for windows and macos)
116 
127 inline std::string datahome()
128 {
129 #if defined(_WIN32)
130  return yarp::conf::environment::get_string(APPDATA, yarp::conf::dirs::home() + WIN_APPDATA_SUFFIX);
131 #elif defined(__APPLE__)
132  return yarp::conf::dirs::home() + MACOS_DATAHOME_SUFFIX;
133 #else
134  return yarp::conf::environment::get_string(XDG_DATA_HOME, yarp::conf::dirs::home() + XDG_DATA_HOME_SUFFIX);
135 #endif
136 }
137 
147 inline std::vector<std::string> datadirs()
148 {
149 #if defined(_WIN32)
150  return yarp::conf::environment::get_path(ALLUSERSPROFILE, WIN_ALLUSERSPROFILE_DEFAULT);
151 #elif defined(__APPLE__)
152  return yarp::conf::environment::split_path(MACOS_DATA_DIRS_DEFAULT);
153 #else
154  return yarp::conf::environment::get_path(XDG_DATA_DIRS, XDG_DATA_DIRS_DEFAULT);
155 #endif
156 }
157 
168 inline std::string confighome()
169 {
170 #if defined(_WIN32)
171  return yarp::conf::environment::get_string(APPDATA, yarp::conf::dirs::home() + WIN_APPDATA_SUFFIX);
172 #elif defined(__APPLE__)
173  return yarp::conf::dirs::home() + MACOS_CONFIGHOME_SUFFIX;
174 #else
175  return yarp::conf::environment::get_string(XDG_CONFIG_HOME, yarp::conf::dirs::home() + XDG_CONFIG_HOME_SUFFIX);
176 #endif
177 }
178 
188 inline std::vector<std::string> configdirs()
189 {
190 #if defined(_WIN32)
191  return yarp::conf::environment::get_path(ALLUSERSPROFILE, WIN_ALLUSERSPROFILE_DEFAULT);
192 #elif defined(__APPLE__)
193  return yarp::conf::environment::split_path(MACOS_CONFIG_DIRS_DEFAULT);
194 #else
195  return yarp::conf::environment::get_path(XDG_CONFIG_DIRS, XDG_CONFIG_DIRS_DEFAULT);
196 #endif
197 }
198 
209 inline std::string cachehome()
210 {
211 #if defined(_WIN32)
212  return yarp::conf::environment::get_string(LOCALAPPDATA, yarp::conf::dirs::home() + WIN_LOCALAPPDATA_SUFFIX);
213 #elif defined(__APPLE__)
214  return yarp::conf::dirs::home() + MACOS_CACHEHOME_SUFFIX;
215 #else
216  return yarp::conf::environment::get_string(XDG_CACHE_HOME, yarp::conf::dirs::home() + XDG_CACHE_HOME_SUFFIX);
217 #endif
218 }
219 
230 inline std::string runtimedir()
231 {
232 #if defined(_WIN32)
233  return yarp::conf::dirs::tempdir() + RUNTIME_SUFFIX;
234 #elif defined(__APPLE__)
235  return yarp::conf::dirs::tempdir() + RUNTIME_SUFFIX + '-' + yarp::conf::environment::get_string(USER);
236 #else
237  return yarp::conf::environment::get_string(XDG_RUNTIME_DIR, yarp::conf::dirs::tempdir() + RUNTIME_SUFFIX + '-' + yarp::conf::environment::get_string(USER));
238 #endif
239 }
240 
241 
244 // YARP Base Directory specifications
245 
256 inline std::string yarpdatahome()
257 {
258 #if defined(_WIN32)
259  return yarp::conf::environment::get_string(YARP_DATA_HOME, APPDATA, yarp::conf::dirs::home() + WIN_APPDATA_SUFFIX, YARP_SUFFIX);
260 #elif defined(__APPLE__)
261  return yarp::conf::environment::get_string(YARP_DATA_HOME, yarp::conf::dirs::home() + MACOS_DATAHOME_SUFFIX + YARP_SUFFIX);
262 #else
263  return yarp::conf::environment::get_string(YARP_DATA_HOME, XDG_DATA_HOME, yarp::conf::dirs::home() + XDG_DATA_HOME_SUFFIX, YARP_SUFFIX);
264 #endif
265 }
266 
276 inline std::vector<std::string> yarpdatadirs()
277 {
278 #if defined(_WIN32)
279  return yarp::conf::environment::get_path(YARP_DATA_DIRS, ALLUSERSPROFILE, WIN_ALLUSERSPROFILE_DEFAULT, YARP_SUFFIX);
280 #elif defined(__APPLE__)
281  return yarp::conf::environment::get_path(YARP_DATA_DIRS, "", MACOS_DATA_DIRS_DEFAULT, YARP_SUFFIX);
282 #else
283  return yarp::conf::environment::get_path(YARP_DATA_DIRS, XDG_DATA_DIRS, XDG_DATA_DIRS_DEFAULT, YARP_SUFFIX);
284 #endif
285 }
286 
297 inline std::string yarpconfighome()
298 {
299 #if defined(_WIN32)
300  return yarp::conf::environment::get_string(YARP_CONFIG_HOME, APPDATA, yarp::conf::dirs::home() + WIN_APPDATA_SUFFIX, YARP_CONFIG_SUFFIX);
301 #elif defined(__APPLE__)
302  return yarp::conf::environment::get_string(YARP_CONFIG_HOME, yarp::conf::dirs::home() + MACOS_CONFIGHOME_SUFFIX + YARP_SUFFIX);
303 #else
304  return yarp::conf::environment::get_string(YARP_CONFIG_HOME, XDG_CONFIG_HOME, yarp::conf::dirs::home() + XDG_CONFIG_HOME_SUFFIX, YARP_SUFFIX);
305 #endif
306 }
307 
322 inline std::vector<std::string> yarpconfigdirs()
323 {
324 #if defined(_WIN32)
325  return yarp::conf::environment::get_path(YARP_CONFIG_DIRS, ALLUSERSPROFILE, WIN_ALLUSERSPROFILE_DEFAULT, YARP_CONFIG_SUFFIX);
326 #elif defined(__APPLE__)
327  return yarp::conf::environment::get_path(YARP_CONFIG_DIRS, "", MACOS_CONFIG_DIRS_DEFAULT, YARP_SUFFIX);
328 #else
329  return yarp::conf::environment::get_path(YARP_CONFIG_DIRS, XDG_CONFIG_DIRS, XDG_CONFIG_DIRS_DEFAULT, YARP_SUFFIX);
330 #endif
331 }
332 
343 inline std::string yarpcachehome()
344 {
345 #if defined(_WIN32)
346  return yarp::conf::environment::get_string(YARP_CACHE_HOME, LOCALAPPDATA, yarp::conf::dirs::home() + WIN_LOCALAPPDATA_SUFFIX, YARP_SUFFIX);
347 #elif defined(__APPLE__)
348  return yarp::conf::environment::get_string(YARP_CACHE_HOME, yarp::conf::dirs::home() + MACOS_CACHEHOME_SUFFIX + YARP_SUFFIX);
349 #else
350  return yarp::conf::environment::get_string(YARP_CACHE_HOME, XDG_CACHE_HOME, yarp::conf::dirs::home() + XDG_CACHE_HOME_SUFFIX, YARP_SUFFIX);
351 #endif
352 }
353 
364 inline std::string yarpruntimedir()
365 {
366 #if defined(_WIN32)
367  return yarp::conf::environment::get_string(YARP_RUNTIME_DIR, yarp::conf::dirs::tempdir() + RUNTIME_YARP_SUFFIX);
368 #elif defined(__APPLE__)
369  return yarp::conf::environment::get_string(YARP_RUNTIME_DIR, yarp::conf::dirs::tempdir() + RUNTIME_SUFFIX + '-' + yarp::conf::environment::get_string(USER) + YARP_SUFFIX);
370 #else
371  return yarp::conf::environment::get_string(YARP_RUNTIME_DIR, XDG_RUNTIME_DIR, yarp::conf::dirs::tempdir() + RUNTIME_SUFFIX + '-' + yarp::conf::environment::get_string(USER), YARP_SUFFIX);
372 #endif
373 }
376 } // namespace dirs
377 } // namespace conf
378 } // namespace yarp
379 
380 
381 #endif // YARP_CONF_DIRS_H
std::string yarpdatahome()
Returns the directory where user-specific YARP data files should be written.
Definition: dirs.h:256
std::string yarpruntimedir()
Returns the directory where user-specific runtime YARP files and other YARP file objects should be pl...
Definition: dirs.h:364
std::string tempdir()
Returns the directory for temporary files.
Definition: dirs.h:103
std::string confighome()
Returns the directory where user-specific configuration files should be written.
Definition: dirs.h:168
std::string cachehome()
Returns the directory where user-specific non-essential (cached) data should be written.
Definition: dirs.h:209
std::vector< std::string > datadirs()
Returns the directories where data files should be searched.
Definition: dirs.h:147
std::vector< std::string > yarpconfigdirs()
Returns the directories where YARP configuration files should be searched.
Definition: dirs.h:322
std::string yarpconfighome()
Returns the directory where user-specific YARP configuration files should be written.
Definition: dirs.h:297
std::string home()
Returns the home directory for current user.
Definition: dirs.h:86
std::string datahome()
Returns the directory where user-specific data files should be written.
Definition: dirs.h:127
std::vector< std::string > yarpdatadirs()
Returns the directories where YARP data files should be searched.
Definition: dirs.h:276
std::vector< std::string > configdirs()
Returns the directories where configuration files should be searched.
Definition: dirs.h:188
std::string yarpcachehome()
Returns the directory where user-specific non-essential (cached) YARP data should be written.
Definition: dirs.h:343
std::string runtimedir()
Returns the directory where user-specific runtime files and other file objects should be placed.
Definition: dirs.h:230
ContainerT split_path(const typename ContainerT::value_type &s)
Utility to split a string containing a path separated by the path_separator, which depends on the sys...
Definition: environment.h:41
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:68
std::vector< std::string > get_path(const std::string &key, bool *found=nullptr)
Read a path from an environment variable.
Definition: environment.h:120
static constexpr value_type preferred_separator
Definition: filesystem.h:23
The main, catch-all namespace for YARP.
Definition: dirs.h:16