From d23de3a9fb025b094000877e793262f22fd76eb5 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Fri, 15 Dec 2017 21:18:49 -0800 Subject: [PATCH] Move default resource directory computation to utils. This will be used by the test system as well. --- src/messages/initialize.cc | 18 ++---------------- src/utils.cc | 19 +++++++++++++++++++ src/utils.h | 2 ++ 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/messages/initialize.cc b/src/messages/initialize.cc index 4fc49675..601de012 100644 --- a/src/messages/initialize.cc +++ b/src/messages/initialize.cc @@ -5,9 +5,6 @@ #include -#define _STRINGIFY(x) #x -#define ENSURE_STRING_MACRO_ARGUMENT(x) _STRINGIFY(x) - namespace { struct Ipc_InitializeRequest : public IpcMessage { const static IpcId kIpcId = IpcId::Initialize; @@ -88,19 +85,8 @@ struct InitializeHandler : BaseMessageHandler { EnsureEndsInSlash(config->cacheDirectory); // Ensure there is a resource directory. - if (config->resourceDirectory.empty()) { - std::string defaultResourceDirectory = std::string( - 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); + if (config->resourceDirectory.empty()) + config->resourceDirectory = GetDefaultResourceDirectory(); LOG_S(INFO) << "Using -resource-dir=" << config->resourceDirectory; // Send initialization before starting indexers, so we don't send a diff --git a/src/utils.cc b/src/utils.cc index c568d8a0..e1be249f 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -19,6 +19,9 @@ #include #endif +#define _STRINGIFY(x) #x +#define ENSURE_STRING_MACRO_ARGUMENT(x) _STRINGIFY(x) + // See http://stackoverflow.com/a/217605 void TrimStart(std::string& s) { s.erase(s.begin(), @@ -377,3 +380,19 @@ std::string FormatMicroseconds(long long microseconds) { 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); +} \ No newline at end of file diff --git a/src/utils.h b/src/utils.h index 197f9bbb..f67775d5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -162,3 +162,5 @@ inline void hash_combine(std::size_t& seed, const T& v, Rest... rest) { float GetProcessMemoryUsedInMb(); std::string FormatMicroseconds(long long microseconds); + +std::string GetDefaultResourceDirectory(); \ No newline at end of file