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::conf::dirs {
17 
18 #ifndef DOXYGEN_SHOULD_SKIP_THIS
19 #ifndef SWIG
20 
21 // Constants defining environment variables names
22 static constexpr const char YARP_DATA_HOME[]{"YARP_DATA_HOME"};
23 static constexpr const char YARP_DATA_DIRS[]{"YARP_DATA_DIRS"};
24 static constexpr const char YARP_CONFIG_HOME[]{"YARP_CONFIG_HOME"};
25 static constexpr const char YARP_CONFIG_DIRS[]{"YARP_CONFIG_DIRS"};
26 static constexpr const char YARP_CACHE_HOME[]{"YARP_CACHE_HOME"};
27 static constexpr const char YARP_RUNTIME_DIR[]{"YARP_RUNTIME_DIR"};
28 
29 static constexpr const char USER[]{"USER"};
30 static constexpr const char USERNAME[]{"USERNAME"};
31 static constexpr const char HOME[]{"HOME"};
32 static constexpr const char USERPROFILE[]{"USERPROFILE"};
33 static constexpr const char TMP[]{"TMP"};
34 static constexpr const char TEMP[]{"TEMP"};
35 static constexpr const char TMPDIR[]{"TMPDIR"};
36 static constexpr const char XDG_DATA_HOME[]{"XDG_DATA_HOME"};
37 static constexpr const char XDG_DATA_DIRS[]{"XDG_DATA_DIRS"};
38 static constexpr const char XDG_CONFIG_HOME[]{"XDG_CONFIG_HOME"};
39 static constexpr const char XDG_CONFIG_DIRS[]{"XDG_CONFIG_DIRS"};
40 static constexpr const char XDG_CACHE_HOME[]{"XDG_CACHE_HOME"};
41 static constexpr const char XDG_RUNTIME_DIR[]{"XDG_RUNTIME_DIR"};
42 static constexpr const char APPDATA[]{"APPDATA"};
43 static constexpr const char LOCALAPPDATA[]{"LOCALAPPDATA"};
44 static constexpr const char ALLUSERSPROFILE[]{"ALLUSERSPROFILE"};
45 
46 // Paths and suffixes
47 static constexpr const char XDG_DATA_HOME_SUFFIX[]{"/.local/share"};
48 static constexpr const char XDG_CONFIG_HOME_SUFFIX[]{"/.config"};
49 static constexpr const char XDG_CACHE_HOME_SUFFIX[]{"/.cache"};
50 static constexpr const char XDG_DATA_DIRS_DEFAULT[]{"/usr/local/share:/usr/share"};
51 static constexpr const char XDG_CONFIG_DIRS_DEFAULT[]{"/etc/xdg"};
52 
53 static constexpr const char UNIX_TMP_DIR_DEFAULT[]{"/tmp"};
54 static constexpr const char WIN_APPDATA_SUFFIX[]{"\\AppData\\Roaming"};
55 static constexpr const char WIN_LOCALAPPDATA_SUFFIX[]{"\\AppData\\Local"};
56 static constexpr const char WIN_APPDATA_LOCAL_TEMP_SUFFIX[]{"\\AppData\\Local\\Temp"};
57 static constexpr const char WIN_ALLUSERSPROFILE_DEFAULT[]{"C:\\ProgramData"};
58 static constexpr const char MACOS_DATAHOME_SUFFIX[]{"/Library/Application Support"};
59 static constexpr const char MACOS_CONFIGHOME_SUFFIX[]{"/Library/Preferences"};
60 static constexpr const char MACOS_DATA_DIRS_DEFAULT[]{"/usr/local/share:/usr/share"};
61 static constexpr const char MACOS_CONFIG_DIRS_DEFAULT[]{"/etc:/Library/Preferences"};
62 static constexpr const char MACOS_CACHEHOME_SUFFIX[]{"/Library/Caches"};
63 
64 // FIXME C++17 Use string_view?
65 static constexpr const char YARP_SUFFIX[]{ yarp::conf::filesystem::preferred_separator, 'y', 'a', 'r', 'p', '\0' }; // "/yarp" or "\\yarp"
66 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"
67 static constexpr const char RUNTIME_SUFFIX[]{ yarp::conf::filesystem::preferred_separator, 'r', 'u', 'n', 't', 'i', 'm', 'e', '\0' }; // "/runtime" or "\\runtime"
68 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"
69 
70 #endif // SWIG
71 #endif // DOXYGEN_SHOULD_SKIP_THIS
72 
74 // Basic paths
75 
84 inline std::string home()
85 {
86 #if defined(_WIN32)
87  return yarp::conf::environment::get_string(USERPROFILE);
88 #else
90 #endif
91 }
92 
101 inline std::string tempdir()
102 {
103 #if defined(_WIN32)
104  return yarp::conf::environment::get_string(TEMP, yarp::conf::dirs::home() + WIN_APPDATA_LOCAL_TEMP_SUFFIX);
105 #else
106  return yarp::conf::environment::get_string(TMPDIR, UNIX_TMP_DIR_DEFAULT);
107 #endif
108 }
109 
110 
113 // XDG Base Directory specifications (and equivalents for windows and macos)
114 
125 inline std::string datahome()
126 {
127 #if defined(_WIN32)
128  return yarp::conf::environment::get_string(APPDATA, yarp::conf::dirs::home() + WIN_APPDATA_SUFFIX);
129 #elif defined(__APPLE__)
130  return yarp::conf::dirs::home() + MACOS_DATAHOME_SUFFIX;
131 #else
132  return yarp::conf::environment::get_string(XDG_DATA_HOME, yarp::conf::dirs::home() + XDG_DATA_HOME_SUFFIX);
133 #endif
134 }
135 
145 inline std::vector<std::string> datadirs()
146 {
147 #if defined(_WIN32)
148  return yarp::conf::environment::get_path(ALLUSERSPROFILE, WIN_ALLUSERSPROFILE_DEFAULT);
149 #elif defined(__APPLE__)
150  return yarp::conf::environment::split_path(MACOS_DATA_DIRS_DEFAULT);
151 #else
152  return yarp::conf::environment::get_path(XDG_DATA_DIRS, XDG_DATA_DIRS_DEFAULT);
153 #endif
154 }
155 
166 inline std::string confighome()
167 {
168 #if defined(_WIN32)
169  return yarp::conf::environment::get_string(APPDATA, yarp::conf::dirs::home() + WIN_APPDATA_SUFFIX);
170 #elif defined(__APPLE__)
171  return yarp::conf::dirs::home() + MACOS_CONFIGHOME_SUFFIX;
172 #else
173  return yarp::conf::environment::get_string(XDG_CONFIG_HOME, yarp::conf::dirs::home() + XDG_CONFIG_HOME_SUFFIX);
174 #endif
175 }
176 
186 inline std::vector<std::string> configdirs()
187 {
188 #if defined(_WIN32)
189  return yarp::conf::environment::get_path(ALLUSERSPROFILE, WIN_ALLUSERSPROFILE_DEFAULT);
190 #elif defined(__APPLE__)
191  return yarp::conf::environment::split_path(MACOS_CONFIG_DIRS_DEFAULT);
192 #else
193  return yarp::conf::environment::get_path(XDG_CONFIG_DIRS, XDG_CONFIG_DIRS_DEFAULT);
194 #endif
195 }
196 
207 inline std::string cachehome()
208 {
209 #if defined(_WIN32)
210  return yarp::conf::environment::get_string(LOCALAPPDATA, yarp::conf::dirs::home() + WIN_LOCALAPPDATA_SUFFIX);
211 #elif defined(__APPLE__)
212  return yarp::conf::dirs::home() + MACOS_CACHEHOME_SUFFIX;
213 #else
214  return yarp::conf::environment::get_string(XDG_CACHE_HOME, yarp::conf::dirs::home() + XDG_CACHE_HOME_SUFFIX);
215 #endif
216 }
217 
228 inline std::string runtimedir()
229 {
230 #if defined(_WIN32)
231  return yarp::conf::dirs::tempdir() + RUNTIME_SUFFIX;
232 #elif defined(__APPLE__)
233  return yarp::conf::dirs::tempdir() + RUNTIME_SUFFIX + '-' + yarp::conf::environment::get_string(USER);
234 #else
235  return yarp::conf::environment::get_string(XDG_RUNTIME_DIR, yarp::conf::dirs::tempdir() + RUNTIME_SUFFIX + '-' + yarp::conf::environment::get_string(USER));
236 #endif
237 }
238 
239 
242 // YARP Base Directory specifications
243 
254 inline std::string yarpdatahome()
255 {
256 #if defined(_WIN32)
257  return yarp::conf::environment::get_string(YARP_DATA_HOME, APPDATA, yarp::conf::dirs::home() + WIN_APPDATA_SUFFIX, YARP_SUFFIX);
258 #elif defined(__APPLE__)
259  return yarp::conf::environment::get_string(YARP_DATA_HOME, yarp::conf::dirs::home() + MACOS_DATAHOME_SUFFIX + YARP_SUFFIX);
260 #else
261  return yarp::conf::environment::get_string(YARP_DATA_HOME, XDG_DATA_HOME, yarp::conf::dirs::home() + XDG_DATA_HOME_SUFFIX, YARP_SUFFIX);
262 #endif
263 }
264 
274 inline std::vector<std::string> yarpdatadirs()
275 {
276 #if defined(_WIN32)
277  return yarp::conf::environment::get_path(YARP_DATA_DIRS, ALLUSERSPROFILE, WIN_ALLUSERSPROFILE_DEFAULT, YARP_SUFFIX);
278 #elif defined(__APPLE__)
279  return yarp::conf::environment::get_path(YARP_DATA_DIRS, "", MACOS_DATA_DIRS_DEFAULT, YARP_SUFFIX);
280 #else
281  return yarp::conf::environment::get_path(YARP_DATA_DIRS, XDG_DATA_DIRS, XDG_DATA_DIRS_DEFAULT, YARP_SUFFIX);
282 #endif
283 }
284 
295 inline std::string yarpconfighome()
296 {
297 #if defined(_WIN32)
298  return yarp::conf::environment::get_string(YARP_CONFIG_HOME, APPDATA, yarp::conf::dirs::home() + WIN_APPDATA_SUFFIX, YARP_CONFIG_SUFFIX);
299 #elif defined(__APPLE__)
300  return yarp::conf::environment::get_string(YARP_CONFIG_HOME, yarp::conf::dirs::home() + MACOS_CONFIGHOME_SUFFIX + YARP_SUFFIX);
301 #else
302  return yarp::conf::environment::get_string(YARP_CONFIG_HOME, XDG_CONFIG_HOME, yarp::conf::dirs::home() + XDG_CONFIG_HOME_SUFFIX, YARP_SUFFIX);
303 #endif
304 }
305 
320 inline std::vector<std::string> yarpconfigdirs()
321 {
322 #if defined(_WIN32)
323  return yarp::conf::environment::get_path(YARP_CONFIG_DIRS, ALLUSERSPROFILE, WIN_ALLUSERSPROFILE_DEFAULT, YARP_CONFIG_SUFFIX);
324 #elif defined(__APPLE__)
325  return yarp::conf::environment::get_path(YARP_CONFIG_DIRS, "", MACOS_CONFIG_DIRS_DEFAULT, YARP_SUFFIX);
326 #else
327  return yarp::conf::environment::get_path(YARP_CONFIG_DIRS, XDG_CONFIG_DIRS, XDG_CONFIG_DIRS_DEFAULT, YARP_SUFFIX);
328 #endif
329 }
330 
341 inline std::string yarpcachehome()
342 {
343 #if defined(_WIN32)
344  return yarp::conf::environment::get_string(YARP_CACHE_HOME, LOCALAPPDATA, yarp::conf::dirs::home() + WIN_LOCALAPPDATA_SUFFIX, YARP_SUFFIX);
345 #elif defined(__APPLE__)
346  return yarp::conf::environment::get_string(YARP_CACHE_HOME, yarp::conf::dirs::home() + MACOS_CACHEHOME_SUFFIX + YARP_SUFFIX);
347 #else
348  return yarp::conf::environment::get_string(YARP_CACHE_HOME, XDG_CACHE_HOME, yarp::conf::dirs::home() + XDG_CACHE_HOME_SUFFIX, YARP_SUFFIX);
349 #endif
350 }
351 
362 inline std::string yarpruntimedir()
363 {
364 #if defined(_WIN32)
365  return yarp::conf::environment::get_string(YARP_RUNTIME_DIR, yarp::conf::dirs::tempdir() + RUNTIME_YARP_SUFFIX);
366 #elif defined(__APPLE__)
367  return yarp::conf::environment::get_string(YARP_RUNTIME_DIR, yarp::conf::dirs::tempdir() + RUNTIME_SUFFIX + '-' + yarp::conf::environment::get_string(USER) + YARP_SUFFIX);
368 #else
369  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);
370 #endif
371 }
374 } // namespace yarp::conf::dirs
375 
376 
377 #endif // YARP_CONF_DIRS_H
std::string yarpdatahome()
Returns the directory where user-specific YARP data files should be written.
Definition: dirs.h:254
std::string yarpruntimedir()
Returns the directory where user-specific runtime YARP files and other YARP file objects should be pl...
Definition: dirs.h:362
std::string tempdir()
Returns the directory for temporary files.
Definition: dirs.h:101
std::string confighome()
Returns the directory where user-specific configuration files should be written.
Definition: dirs.h:166
std::string cachehome()
Returns the directory where user-specific non-essential (cached) data should be written.
Definition: dirs.h:207
std::vector< std::string > datadirs()
Returns the directories where data files should be searched.
Definition: dirs.h:145
std::vector< std::string > yarpconfigdirs()
Returns the directories where YARP configuration files should be searched.
Definition: dirs.h:320
std::string yarpconfighome()
Returns the directory where user-specific YARP configuration files should be written.
Definition: dirs.h:295
std::string home()
Returns the home directory for current user.
Definition: dirs.h:84
std::string datahome()
Returns the directory where user-specific data files should be written.
Definition: dirs.h:125
std::vector< std::string > yarpdatadirs()
Returns the directories where YARP data files should be searched.
Definition: dirs.h:274
std::vector< std::string > configdirs()
Returns the directories where configuration files should be searched.
Definition: dirs.h:186
std::string yarpcachehome()
Returns the directory where user-specific non-essential (cached) YARP data should be written.
Definition: dirs.h:341
std::string runtimedir()
Returns the directory where user-specific runtime files and other file objects should be placed.
Definition: dirs.h:228
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:39
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:66
std::vector< std::string > get_path(const std::string &key, bool *found=nullptr)
Read a path from an environment variable.
Definition: environment.h:118
static constexpr value_type preferred_separator
Definition: filesystem.h:21