Skip to content

Commit

Permalink
Move path constants to their own header
Browse files Browse the repository at this point in the history
  • Loading branch information
dscharrer committed Aug 3, 2012
1 parent 402aadd commit b35342e
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 172 deletions.
18 changes: 3 additions & 15 deletions CMakeLists.txt
Expand Up @@ -488,21 +488,6 @@ else()
set(CONFIG_DIR_PREFIXES "$XDG_CONFIG_HOME")
endif()
endif()
if(DATA_DIR STREQUAL "")
unset(DATA_DIR)
endif()
if(USER_DIR STREQUAL "")
unset(USER_DIR)
endif()
if(CONFIG_DIR STREQUAL "")
unset(CONFIG_DIR)
endif()
if(DATA_DIR_PREFIXES STREQUAL "")
unset(DATA_DIR_PREFIXES)
endif()
if(USER_DIR_PREFIXES STREQUAL "")
unset(USER_DIR_PREFIXES)
endif()

# Check that all required functionality is available.
if(CMAKE_USE_PTHREADS_INIT AND ARX_HAVE_SYS_TYPES_H AND ARX_HAVE_GETPID)
Expand Down Expand Up @@ -939,6 +924,9 @@ create_source_groups(ALL_FILES_FOR_GROUPS)

configure_file("${SRC_DIR}/Configure.h.in" "Configure.h" ESCAPE_QUOTES)

configure_file("${SRC_DIR}/io/fs/PathConstants.h.in" "io/fs/PathConstants.h"
ESCAPE_QUOTES)

file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/platform")
set(PLATFORM_CONFIG_H "platform/PlatformConfig.h")
configure_file("${SRC_DIR}/${PLATFORM_CONFIG_H}.in" "${PLATFORM_CONFIG_H}" ESCAPE_QUOTES)
Expand Down
38 changes: 0 additions & 38 deletions src/Configure.h.in
Expand Up @@ -65,42 +65,4 @@
// Build system
#cmakedefine UNITY_BUILD

// Default paths
#cmakedefine DATA_DIR
#ifdef DATA_DIR
#undef DATA_DIR
#define DATA_DIR "${DATA_DIR}"
#endif
#cmakedefine USER_DIR
#ifdef USER_DIR
#undef USER_DIR
#define USER_DIR "${USER_DIR}"
#endif
#cmakedefine CONFIG_DIR
#ifdef CONFIG_DIR
#undef CONFIG_DIR
#define CONFIG_DIR "${CONFIG_DIR}"
#endif
#cmakedefine DATA_DIR_PREFIXES
#ifdef DATA_DIR_PREFIXES
#undef DATA_DIR_PREFIXES
#define DATA_DIR_PREFIXES "${DATA_DIR_PREFIXES}"
#endif
#cmakedefine USER_DIR_PREFIXES
#ifdef USER_DIR_PREFIXES
#undef USER_DIR_PREFIXES
#define USER_DIR_PREFIXES "${USER_DIR_PREFIXES}"
#endif
#cmakedefine CONFIG_DIR_PREFIXES
#ifdef CONFIG_DIR_PREFIXES
#undef CONFIG_DIR_PREFIXES
#define CONFIG_DIR_PREFIXES "${CONFIG_DIR_PREFIXES}"
#endif

#cmakedefine CMAKE_INSTALL_FULL_LIBEXECDIR
#ifdef CMAKE_INSTALL_FULL_LIBEXECDIR
#undef CMAKE_INSTALL_FULL_LIBEXECDIR
#define CMAKE_INSTALL_FULL_LIBEXECDIR "${CMAKE_INSTALL_FULL_LIBEXECDIR}"
#endif

#endif // ARX_CONFIGURE_H
198 changes: 84 additions & 114 deletions src/core/Startup.cpp
Expand Up @@ -38,12 +38,12 @@
#include "core/Config.h"
#include "io/fs/FilePath.h"
#include "io/fs/Filesystem.h"
#include "io/fs/PathConstants.h"
#include "io/log/Logger.h"
#include "platform/CrashHandler.h"
#include "platform/Environment.h"
#include "platform/String.h"

#include "Configure.h"

namespace po = boost::program_options;

Expand Down Expand Up @@ -94,28 +94,26 @@ static void findDataDirectory() {
return;
}

#ifdef DATA_DIR

fs::path dir = expandEnvironmentVariables(DATA_DIR);

#ifdef DATA_DIR_PREFIXES
if(dir.is_relative()) {
config.paths.data = findSubdirectory(DATA_DIR_PREFIXES, dir);
if(!config.paths.data.empty()) {
LogDebug("Got data directory from DATA_DIR_PREFIXES: " << config.paths.data);
if(fs::data_dir) {

fs::path dir = expandEnvironmentVariables(fs::data_dir);

if(fs::data_dir_prefixes && dir.is_relative()) {
config.paths.data = findSubdirectory(fs::data_dir_prefixes, dir);
if(!config.paths.data.empty()) {
LogDebug("Got data directory from DATA_DIR_PREFIXES: " << config.paths.data);
return;
}
}

if(fs::is_directory(dir)) {
config.paths.data = dir;
LogDebug("Got data directory from DATA_DIR: " << config.paths.data);
return;
}
}
#endif // DATA_DIR_PREFIXES

if(fs::is_directory(dir)) {
config.paths.data = dir;
LogDebug("Got data directory from DATA_DIR: " << config.paths.data);
return;

}

#endif // DATA_DIR

LogDebug("No data directory found.");
}

Expand All @@ -130,41 +128,39 @@ static bool findUserDirectory() {
return true;
}

#ifdef USER_DIR

fs::path dir = expandEnvironmentVariables(USER_DIR);

fs::path to_create;
#ifdef USER_DIR_PREFIXES
if(dir.is_relative()) {
config.paths.user = findSubdirectory(USER_DIR_PREFIXES, dir, &to_create);
if(!config.paths.user.empty()) {
LogDebug("Got user directory from USER_DIR_PREFIXES: " << config.paths.user);
return true;
if(fs::user_dir) {
fs::path dir = expandEnvironmentVariables(fs::user_dir);
fs::path to_create;
if(fs::user_dir_prefixes && dir.is_relative()) {
config.paths.user = findSubdirectory(fs::user_dir_prefixes, dir, &to_create);
if(!config.paths.user.empty()) {
LogDebug("Got user directory from USER_DIR_PREFIXES: " << config.paths.user);
return true;
}
}
}
#endif // USER_DIR_PREFIXES

if(fs::is_directory(dir)) {
config.paths.user = dir;
LogDebug("Got user directory from USER_DIR: " << config.paths.user);
return true;
}

// Create a new user directory.
if(!config.paths.data.empty()) {
if(!to_create.empty()) {
config.paths.user = to_create;
LogDebug("Selected new user directory from USER_DIR_PREFIXES: " << config.paths.user);
} else {

if(fs::is_directory(dir)) {
config.paths.user = dir;
LogDebug("Selected new user directory from USER_DIR: " << config.paths.user);
LogDebug("Got user directory from USER_DIR: " << config.paths.user);
return true;
}
return true;

// Create a new user directory.
if(!config.paths.data.empty()) {
if(!to_create.empty()) {
config.paths.user = to_create;
LogDebug("Selected new user directory from USER_DIR_PREFIXES: " << config.paths.user);
} else {
config.paths.user = dir;
LogDebug("Selected new user directory from USER_DIR: " << config.paths.user);
}
return true;
}

}

#endif // USER_DIR

// Use the current directory for both data and config files.
config.paths.user = ".";
LogDebug("Using working directory as user directory: " << config.paths.user);
Expand All @@ -175,52 +171,48 @@ static void findConfigDirectory(bool create) {

config.paths.config.clear();

#ifdef CONFIG_DIR

fs::path dir = expandEnvironmentVariables(CONFIG_DIR);

fs::path to_create;
#ifdef CONFIG_DIR_PREFIXES
if(dir.is_relative()) {
config.paths.config = findSubdirectory(CONFIG_DIR_PREFIXES, dir, &to_create);
if(!config.paths.config.empty()) {
LogDebug("Got config directory from CONFIG_DIR_PREFIXES: " << config.paths.config);
return;
if(fs::config_dir) {
fs::path dir = expandEnvironmentVariables(fs::config_dir);
fs::path to_create;
if(fs::config_dir_prefixes && dir.is_relative()) {
config.paths.config = findSubdirectory(fs::config_dir_prefixes, dir, &to_create);
if(!config.paths.config.empty()) {
LogDebug("Got config directory from CONFIG_DIR_PREFIXES: " << config.paths.config);
return;
}
}
}
#endif // USER_DIR_PREFIXES

if(fs::is_directory(dir)) {
config.paths.config = dir;
LogDebug("Got config directory from CONFIG_DIR: " << config.paths.config);
return;
}

// Create a new config directory.
if(!config.paths.data.empty() || create) {
if(!to_create.empty()) {
config.paths.config = to_create;
LogDebug("Selected new config directory from CONFIG_DIR_PREFIXES: "
<< config.paths.config);
} else {

if(fs::is_directory(dir)) {
config.paths.config = dir;
LogDebug("Selected new config directory from CONFIG_DIR: " << config.paths.config);
LogDebug("Got config directory from CONFIG_DIR: " << config.paths.config);
return;
}
return;

// Create a new config directory.
if(!config.paths.data.empty() || create) {
if(!to_create.empty()) {
config.paths.config = to_create;
LogDebug("Selected new config directory from CONFIG_DIR_PREFIXES: "
<< config.paths.config);
} else {
config.paths.config = dir;
LogDebug("Selected new config directory from CONFIG_DIR: " << config.paths.config);
}
return;
}

}

#else
ARX_UNUSED(create);
#endif // CONFIG_DIR

// Use the user directory as the config directory.
config.paths.config = config.paths.user;
LogDebug("Using user directory as config directory: " << config.paths.config);
}

static void listDirectoriesFor(std::ostream & os, const string & regKey,
const string & suffix = string(),
const string & where = string()) {
const char * suffix = NULL,
const char * where = NULL) {

#if ARX_PLATFORM == ARX_PLATFORM_WIN32
if(!regKey.empty()) {
Expand All @@ -234,21 +226,21 @@ static void listDirectoriesFor(std::ostream & os, const string & regKey,
ARX_UNUSED(regKey);
#endif

if(suffix.empty()) {
if(!suffix) {
return;
}
fs::path dir = expandEnvironmentVariables(suffix);

if(!where.empty() && dir.is_relative()) {
if(where && dir.is_relative()) {

string prefixes = expandEnvironmentVariables(where);

os << " - \"" << suffix << '"';
if(dir.string() != suffix) {
if(dir.string().compare(suffix) != 0) {
os << " = " << fs::path(dir);
}
os << " in one of \"" << where << '"';
if(prefixes != where) {
if(prefixes.compare(where) != 0) {
os << "\n = \"" << prefixes << '"';
}
os << ":\n";
Expand All @@ -272,7 +264,7 @@ static void listDirectoriesFor(std::ostream & os, const string & regKey,
}

os << " - \"" << suffix << '"';
if(dir.string() != suffix) {
if(dir.string().compare(suffix) != 0) {
os << " = " << fs::path(dir);
}
os << '\n';
Expand All @@ -284,15 +276,7 @@ static void listDirectories(std::ostream & os, bool data, bool user, bool cfg) {
if(data) {
os << "\nData directories (data files):\n";
os << " - --data-dir (-d) command-line parameter\n";
#ifdef DATA_DIR
# ifdef DATA_DIR_PREFIXES
listDirectoriesFor(os, "DataDir", DATA_DIR, DATA_DIR_PREFIXES);
# else
listDirectoriesFor(os, "DataDir", DATA_DIR);
# endif // DATA_DIR_PREFIXES
#else // DATA_DIR
listDirectoriesFor(os, "DataDir");
#endif // DATA_DIR
listDirectoriesFor(os, "DataDir", fs::data_dir, fs::data_dir_prefixes);
os << "selected: ";
if(config.paths.data.empty()) {
os << "(none)\n";
Expand All @@ -304,15 +288,7 @@ static void listDirectories(std::ostream & os, bool data, bool user, bool cfg) {
if(user) {
os << "\nUser directories (save files, data files):\n";
os << " - --user-dir (-u) command-line parameter\n";
#ifdef USER_DIR
# ifdef USER_DIR_PREFIXES
listDirectoriesFor(os, "UserDir", USER_DIR, USER_DIR_PREFIXES);
# else
listDirectoriesFor(os, "UserDir", USER_DIR);
# endif // USER_DIR_PREFIXES
#else // USER_DIR
listDirectoriesFor(os, "UserDir");
#endif // USER_DIR
listDirectoriesFor(os, "UserDir", fs::user_dir, fs::user_dir_prefixes);
os << " - Current working directory\n";
os << "selected: ";
if(config.paths.user.empty()) {
Expand All @@ -325,13 +301,7 @@ static void listDirectories(std::ostream & os, bool data, bool user, bool cfg) {
if(cfg) {
os << "\nConfig directories:\n";
os << " - --config-dir (-c) command-line parameter\n";
#ifdef CONFIG_DIR
# ifdef CONFIG_DIR_PREFIXES
listDirectoriesFor(os, std::string(), CONFIG_DIR, CONFIG_DIR_PREFIXES);
# else
listDirectoriesFor(os, std::string(), CONFIG_DIR);
# endif // USER_DIR_PREFIXES
#endif // CONFIG_DIR
listDirectoriesFor(os, std::string(), fs::config_dir, fs::config_dir_prefixes);
os << " - The selected user directory\n";
os << "selected: ";
if(config.paths.config.empty()) {
Expand Down
26 changes: 26 additions & 0 deletions src/io/fs/PathConstants.h.in
@@ -0,0 +1,26 @@

// Data directories

namespace fs {

#define ARX_GET_STR(str) (((str)[0] != '\0') ? (str) : NULL)

typedef const char * const str_t;

static str_t data_dir = ARX_GET_STR("${DATA_DIR}");

static str_t user_dir = ARX_GET_STR("${USER_DIR}");

static str_t config_dir = ARX_GET_STR("${CONFIG_DIR}");

static str_t data_dir_prefixes = ARX_GET_STR("${DATA_DIR_PREFIXES}");

static str_t user_dir_prefixes = ARX_GET_STR("${USER_DIR_PREFIXES}");

static str_t config_dir_prefixes = ARX_GET_STR("${CONFIG_DIR_PREFIXES}");

static str_t libexec_dir = ARX_GET_STR("${CMAKE_INSTALL_FULL_LIBEXECDIR}");

#undef ARX_GET_STR

} // namespace fs

0 comments on commit b35342e

Please sign in to comment.