OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ConfigFilePaths.cpp
Go to the documentation of this file.
1 
11 // Copyright 2017 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 // Internal Includes
30 
31 // Library/third-party includes
32 #include <boost/filesystem.hpp>
33 
34 // Standard includes
35 #include <vector>
36 
37 namespace osvr {
38  namespace server {
39 
40  inline std::string getUserConfigDirectory() {
41  namespace fs = boost::filesystem;
43 
44  fs::path configDir;
45  std::string configSubpath = "config";
46 
47 #if defined(OSVR_LINUX) && !defined(OSVR_ANDROID)
48  // $XDG_CONFIG_HOME defines the base directory relative to which user
49  // specific non-essential data files should be stored. If
50  // $XDG_CONFIG_HOME is either not set or empty, a default equal to
51  // $HOME/.config should be used.
52  auto xdg_cache_dir = getEnvironmentVariable("XDG_CONFIG_HOME");
53  if (xdg_cache_dir) {
54  configDir = *xdg_cache_dir;
55  }
56  else {
57  auto home_dir = getEnvironmentVariable("HOME");
58  configDir = fs::path(*home_dir) / ".config";
59  }
60  configDir /= fs::path("osvr");
61 #elif defined(OSVR_MACOSX)
62  auto home_dir = getEnvironmentVariable("HOME");
63  if (home_dir) {
64  configDir = *home_dir;
65  }
66  configDir /= "Library" / fs::path("Application Support") / fs::path("OSVR") / configSubpath;
67 #elif defined(OSVR_WINDOWS)
68  auto local_app_dir = getEnvironmentVariable("LocalAppData");
71  if (local_app_dir) {
72  configDir = *local_app_dir;
73  }
74  else {
75  configDir = "c:/";
76  }
77  configDir /= fs::path("OSVR") / configSubpath;
78 #endif
79 
80 #if defined(OSVR_ANDROID)
81  configDir = fs::path("/sdcard/osvr");
82 #endif
83  auto ret = configDir.string();
84  return ret;
85  }
86 
87  std::vector<std::string> getDefaultConfigFilePaths() {
88  namespace fs = boost::filesystem;
89 
90  std::vector<std::string> names;
91  std::string configFileName = getDefaultConfigFilename();
92 
93  auto userConfigDirectoryStr = getUserConfigDirectory();
94  fs::path userConfigDirectory(userConfigDirectoryStr);
95 
96  auto userConfig = userConfigDirectory / configFileName;
97 
98  names.push_back(userConfig.string());
99  names.push_back(configFileName);
100  return names;
101  }
102  }
103 }
Auto-configured header.
Platform specific search paths for osvr server config files.
boost::optional< std::string > getEnvironmentVariable(std::string const &var)
Gets an environment variable's value. On systems that don't distinguish between having a variable def...
OSVR_SERVER_EXPORT std::vector< std::string > getDefaultConfigFilePaths()
this returns a vector of default server configuration file paths.