From 1390eaeecdd01582c6ce29582f7811edbeab11c4 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Tue, 19 Aug 2014 22:14:32 -0400 Subject: [PATCH] Added windows function to find relative dir of exe for conf and runtime data --- src/openalpr/config.cpp | 13 +++++++++++++ src/openalpr/config.h | 1 + src/openalpr/constants.h | 1 + src/openalpr/support/platform.cpp | 21 +++++++++++++++++++++ src/openalpr/support/platform.h | 6 ++++++ 5 files changed, 42 insertions(+) diff --git a/src/openalpr/config.cpp b/src/openalpr/config.cpp index bf4d4d4..1ecda6f 100644 --- a/src/openalpr/config.cpp +++ b/src/openalpr/config.cpp @@ -46,6 +46,11 @@ Config::Config(const std::string country, const std::string config_file, const s configFile = envConfigFile; debug_message = "Config file location provided via environment variable: " + string(ENV_VARIABLE_CONFIG_FILE); } + else if (DirectoryExists(getExeDir().c_str()) && fileExists((getExeDir() + CONFIG_FILE).c_str())) + { + configFile = getExeDir() + CONFIG_FILE; + debug_message = "Config file location provided via exe location"; + } else { // Use the default @@ -83,6 +88,14 @@ Config::Config(const std::string country, const std::string config_file, const s this->runtimeBaseDir = runtime_dir; } + if ((DirectoryExists(this->runtimeBaseDir.c_str()) == false) && + (DirectoryExists((getExeDir() + RUNTIME_DIR).c_str()))) + { + // Runtime dir in the config is invalid and there is a runtime dir in the same dir as the exe. + this->runtimeBaseDir = getExeDir() + RUNTIME_DIR; + + } + if (DirectoryExists(this->runtimeBaseDir.c_str()) == false) { std::cerr << "--(!) Runtime directory '" << this->runtimeBaseDir << "' does not exist!" << endl; diff --git a/src/openalpr/config.h b/src/openalpr/config.h index 1d439dd..fa839af 100644 --- a/src/openalpr/config.h +++ b/src/openalpr/config.h @@ -24,6 +24,7 @@ #include "simpleini/simpleini.h" #include "support/filesystem.h" +#include "support/platform.h" #include "constants.h" diff --git a/src/openalpr/constants.h b/src/openalpr/constants.h index 53d2524..c62ecc5 100644 --- a/src/openalpr/constants.h +++ b/src/openalpr/constants.h @@ -21,6 +21,7 @@ +#define RUNTIME_DIR "/runtime_data" #define CONFIG_FILE "/openalpr.conf" #define KEYPOINTS_DIR "/keypoints" #define CASCADE_DIR "/region/" diff --git a/src/openalpr/support/platform.cpp b/src/openalpr/support/platform.cpp index dc99772..a14cd26 100644 --- a/src/openalpr/support/platform.cpp +++ b/src/openalpr/support/platform.cpp @@ -7,4 +7,25 @@ void sleep_ms(int sleepMs) #else usleep(sleepMs * 1000); // usleep takes sleep time in us (1 millionth of a second) #endif +} + +std::string getExeDir() +{ + #ifdef WINDOWS + TCHAR szEXEPath[2048]; + std::stringstream ss; + GetModuleFileName(NULL, szEXEPath, 2048); + ss << szEXEPath; + + std::string exeFile = ss.str(); + std::string directory; + const size_t last_slash_idx = exeFile.rfind('\\'); + if (std::string::npos != last_slash_idx) + { + directory = exeFile.substr(0, last_slash_idx); + } + return directory; + #else + return ""; + #endif } \ No newline at end of file diff --git a/src/openalpr/support/platform.h b/src/openalpr/support/platform.h index bb9adfc..4912f0c 100644 --- a/src/openalpr/support/platform.h +++ b/src/openalpr/support/platform.h @@ -1,6 +1,9 @@ #ifndef OPENALPR_PLATFORM_H #define OPENALPR_PLATFORM_H +#include +#include + #ifdef WINDOWS #include #else @@ -8,7 +11,10 @@ #endif + + void sleep_ms(int sleepMs); +std::string getExeDir(); #endif //OPENALPR_PLATFORM_H