JayBeams  0.1
Another project to have fun coding.
config_files_location.cpp
Go to the documentation of this file.
2 
3 #include <boost/filesystem.hpp>
4 #include <stdexcept>
5 
6 namespace fs = boost::filesystem;
7 
9  boost::filesystem::path const& argv0,
10  std::function<char const*(char const*)> getenv,
11  char const* program_root_variable)
12  : search_path_() {
13  // Discover the name of the configuration directory for the platform
14  fs::path sysconfdir = jb::sysconfdir();
15  fs::path bindir = jb::bindir();
16  fs::path sysconf_leaf = sysconfdir.filename();
17  fs::path bin_leaf = bindir.filename();
18 
19  // If there is a *_ROOT environment variable defined, use that ...
20  if (program_root_variable != nullptr) {
21  char const* program_root = getenv(program_root_variable);
22  if (program_root != nullptr) {
23  fs::path dir = fs::path(program_root) / sysconf_leaf;
24  search_path_.push_back(dir);
25  }
26  }
27 
28  // ... otherwise, if ${SYSTEM}_ROOT is defined, use that ...
29  char const* system_root = getenv("JAYBEAMS_ROOT");
30  if (system_root != nullptr) {
31  fs::path dir = fs::path(system_root) / sysconf_leaf;
32  search_path_.push_back(dir);
33  }
34 
35  // ... otherwise, search in the ${SYSTEM}_SYSCONFDIR defined at
36  // compile-time ...
37  search_path_.push_back(sysconfdir);
38 
39  // ... otherwise, discover where the program is running and guess
40  // where the data directory might be ...
41  fs::path program(argv0);
42  if (program.parent_path().filename() == bin_leaf) {
43  fs::path dir = program.parent_path().parent_path() / sysconf_leaf;
44  search_path_.push_back(dir);
45  } else if (program.parent_path() != "") {
46  search_path_.push_back(program.parent_path());
47  }
48 }
49 
51  boost::filesystem::path const& argv0,
52  std::function<char const*(char const*)> getenv)
53  : config_files_locations_base(argv0, getenv, nullptr) {
54 }
55 
56 char const* jb::default_getenv::operator()(char const* name) {
57  return std::getenv(name);
58 }
59 
60 bool jb::default_validator::operator()(fs::path const& dir) {
61  return fs::exists(dir);
62 }
63 
64 // This is an ugly mess of #ifdefs, but better here than sprinkled
65 // around in the code.
66 
67 #ifndef JB_DEFAULT_SYSCONFDIR
68 #define JB_DEFAULT_SYSCONFDIR "/etc"
69 #endif // JB_DEFAULT_SYSCONFDIR
70 #ifndef JB_DEFAULT_BINDIR
71 #define JB_DEFAULT_BINDIR "/usr/bin"
72 #endif // JB_DEFAULT_BINDIR
73 
74 char const* jb::sysconfdir() {
75 #if defined(JB_SYSCONFDIR)
76  return JB_SYSCONFDIR;
77 #else
78  return JB_DEFAULT_SYSCONFDIR;
79 #endif // JB_SYSCONFDIR
80 }
81 
82 char const* jb::bindir() {
83 #if defined(JB_BINDIR)
84  return JB_BINDIR;
85 #else
86  return JB_DEFAULT_BINDIR;
87 #endif // JB_BINDIR
88 }
std::vector< boost::filesystem::path > search_path_
#define JB_DEFAULT_BINDIR
char const * operator()(char const *name)
bool operator()(boost::filesystem::path const &path)
Define the configuration file search algorithm for JayBeams.
config_files_locations_base(boost::filesystem::path const &argv0, std::function< char const *(char const *)> getenv, char const *program_root_variable)
Constructor.
char const * bindir()
Return the binary installation directory directory.
#define JB_DEFAULT_SYSCONFDIR
char const * sysconfdir()
Return the system configuration directory.