Move default resource directory computation to utils.

This will be used by the test system as well.
This commit is contained in:
Jacob Dufault 2017-12-15 21:18:49 -08:00
parent 883f886d2a
commit d23de3a9fb
3 changed files with 23 additions and 16 deletions

View File

@ -5,9 +5,6 @@
#include <loguru.hpp> #include <loguru.hpp>
#define _STRINGIFY(x) #x
#define ENSURE_STRING_MACRO_ARGUMENT(x) _STRINGIFY(x)
namespace { namespace {
struct Ipc_InitializeRequest : public IpcMessage<Ipc_InitializeRequest> { struct Ipc_InitializeRequest : public IpcMessage<Ipc_InitializeRequest> {
const static IpcId kIpcId = IpcId::Initialize; const static IpcId kIpcId = IpcId::Initialize;
@ -88,19 +85,8 @@ struct InitializeHandler : BaseMessageHandler<Ipc_InitializeRequest> {
EnsureEndsInSlash(config->cacheDirectory); EnsureEndsInSlash(config->cacheDirectory);
// Ensure there is a resource directory. // Ensure there is a resource directory.
if (config->resourceDirectory.empty()) { if (config->resourceDirectory.empty())
std::string defaultResourceDirectory = std::string( config->resourceDirectory = GetDefaultResourceDirectory();
ENSURE_STRING_MACRO_ARGUMENT(DEFAULT_RESOURCE_DIRECTORY));
if (defaultResourceDirectory.find("..") != std::string::npos) {
std::string executablePath = GetExecutablePath();
size_t pos = executablePath.find_last_of('/');
config->resourceDirectory = executablePath.substr(0, pos + 1);
config->resourceDirectory += defaultResourceDirectory;
} else {
config->resourceDirectory = defaultResourceDirectory;
}
}
config->resourceDirectory = NormalizePath(config->resourceDirectory);
LOG_S(INFO) << "Using -resource-dir=" << config->resourceDirectory; LOG_S(INFO) << "Using -resource-dir=" << config->resourceDirectory;
// Send initialization before starting indexers, so we don't send a // Send initialization before starting indexers, so we don't send a

View File

@ -19,6 +19,9 @@
#include <sparsepp/spp_memory.h> #include <sparsepp/spp_memory.h>
#endif #endif
#define _STRINGIFY(x) #x
#define ENSURE_STRING_MACRO_ARGUMENT(x) _STRINGIFY(x)
// See http://stackoverflow.com/a/217605 // See http://stackoverflow.com/a/217605
void TrimStart(std::string& s) { void TrimStart(std::string& s) {
s.erase(s.begin(), s.erase(s.begin(),
@ -377,3 +380,19 @@ std::string FormatMicroseconds(long long microseconds) {
return std::to_string(milliseconds) + "." + std::to_string(remaining) + "ms"; return std::to_string(milliseconds) + "." + std::to_string(remaining) + "ms";
} }
std::string GetDefaultResourceDirectory() {
std::string result;
std::string resource_directory = std::string(ENSURE_STRING_MACRO_ARGUMENT(DEFAULT_RESOURCE_DIRECTORY));
if (resource_directory.find("..") != std::string::npos) {
std::string executable_path = GetExecutablePath();
size_t pos = executable_path.find_last_of('/');
result = executable_path.substr(0, pos + 1);
result += resource_directory;
} else {
result = resource_directory;
}
return NormalizePath(result);
}

View File

@ -162,3 +162,5 @@ inline void hash_combine(std::size_t& seed, const T& v, Rest... rest) {
float GetProcessMemoryUsedInMb(); float GetProcessMemoryUsedInMb();
std::string FormatMicroseconds(long long microseconds); std::string FormatMicroseconds(long long microseconds);
std::string GetDefaultResourceDirectory();