From ea5f7b56ecceaa638133f406a954d324f8e720c6 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Mon, 31 Oct 2011 10:29:42 -0500 Subject: src/Makefile.am,FbTk/StringUtil.cc: Search relative to the executable. On Windows, prepend /DUMMYPREFIX to default paths, and replace it at runtime with the prefix relative to the exe directory. --- src/FbTk/StringUtil.cc | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Makefile.am | 20 ++++++++++------ 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/src/FbTk/StringUtil.cc b/src/FbTk/StringUtil.cc index ad9739e..2cc47fd 100644 --- a/src/FbTk/StringUtil.cc +++ b/src/FbTk/StringUtil.cc @@ -21,6 +21,7 @@ #include "StringUtil.hh" +#include "../defaults.hh" #ifdef HAVE_CSTDIO #include @@ -57,6 +58,7 @@ #include #include #include +#include using std::string; using std::transform; @@ -167,8 +169,64 @@ const char *strcasestr(const char *str, const char *ptn) { return 0; } + +#ifdef _WIN32 + +#include +#define WIN32_LEAN_AND_MEAN 1 +#define NOMINMAX +#include + +static void removeTrailingPathSeparators(std::string & path) { + // Remove any trailing path separators + size_t beforeLastPathSep = path.find_last_not_of("/\\"); + if (beforeLastPathSep != path.size() - 1) { + path.erase(beforeLastPathSep + 1); + } +} + +static std::string getFluxboxPrefix() { + static std::string ret; + static bool init = false; + if (!init) { + char buffer[1024]; + HMODULE module = GetModuleHandle(NULL); + DWORD size = GetModuleFileName(module, buffer, sizeof(buffer)); + if (sizeof(buffer) > 0) + { + buffer[sizeof(buffer) - 1] = 0; + } + static const char slash = '/'; + static const char backslash = '\\'; + char * lastslash = std::find_end(buffer, buffer+size, &slash, &slash + 1); + char * lastbackslash = std::find_end(buffer, buffer+size, &backslash, &backslash + 1); + ret.assign(buffer); + + // Remove the filename + size_t lastPathSep = ret.find_last_of("/\\"); + if (lastPathSep != std::string::npos) { + ret.erase(lastPathSep); + } + + removeTrailingPathSeparators(ret); + + // If the last directory is bin, remove that too. + lastPathSep = ret.find_last_of("/\\"); + if (lastPathSep != std::string::npos && ret.substr(lastPathSep + 1) == "bin") { + ret.erase(lastPathSep); + } + + removeTrailingPathSeparators(ret); + } + return ret; +} + +#endif // _WIN32 + /** if ~ then expand it to home of user + if /DUMMYPREFIX on Windows then expand it to the prefix relative to the + executable on Windows. returns expanded filename */ string expandFilename(const string &filename) { @@ -188,6 +246,13 @@ string expandFilename(const string &filename) { retval = filename; //return unmodified value } +#if defined(_WIN32) && defined(DUMMYPREFIX) + if (retval.find(DUMMYPREFIX) == 0) { + static const std::string dummyPrefix = DUMMYPREFIX; + retval.replace(0, dummyPrefix.size(), getFluxboxPrefix()); + } +#endif + return retval; } diff --git a/src/Makefile.am b/src/Makefile.am index 1a04e4e..9a84897 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -48,13 +48,19 @@ defaults.hh: Makefile echo '// This file is generated from Makefile. Do not edit!'; \ echo '#include '; \ echo ''; \ - echo '#define DEFAULTMENU "$(DEFAULT_MENU)"'; \ - echo '#define DEFAULTSTYLE "$(DEFAULT_STYLE)"'; \ - echo '#define DEFAULTKEYSFILE "$(DEFAULT_KEYSFILE)"'; \ - echo '#define DEFAULT_APPSFILE "$(DEFAULT_APPSFILE)"'; \ - echo '#define DEFAULT_OVERLAY "$(DEFAULT_OVERLAY)"'; \ - echo '#define DEFAULT_INITFILE "$(DEFAULT_INITFILE)"'; \ - echo '#define DEFAULT_WINDOWMENU "$(DEFAULT_WINDOWMENU)"'; \ + echo '#ifdef _WIN32'; \ + echo '#define DUMMYPREFIX "/DUMMYPREFIX"'; \ + echo '#define PATHPREFIX DUMMYPREFIX'; \ + echo '#else'; \ + echo '#define PATHPREFIX'; \ + echo '#endif'; \ + echo '#define DEFAULTMENU PATHPREFIX "$(DEFAULT_MENU)"'; \ + echo '#define DEFAULTSTYLE PATHPREFIX "$(DEFAULT_STYLE)"'; \ + echo '#define DEFAULTKEYSFILE PATHPREFIX "$(DEFAULT_KEYSFILE)"'; \ + echo '#define DEFAULT_APPSFILE PATHPREFIX "$(DEFAULT_APPSFILE)"'; \ + echo '#define DEFAULT_OVERLAY PATHPREFIX "$(DEFAULT_OVERLAY)"'; \ + echo '#define DEFAULT_INITFILE PATHPREFIX "$(DEFAULT_INITFILE)"'; \ + echo '#define DEFAULT_WINDOWMENU PATHPREFIX "$(DEFAULT_WINDOWMENU)"'; \ echo '#define PROGRAM_PREFIX "$(PROGRAM_PREFIX:NONE=)"'; \ echo '#define PROGRAM_SUFFIX "$(PROGRAM_SUFFIX:NONE=)"'; \ echo 'std::string realProgramName(const std::string& name);'; \ -- cgit v0.11.2