[beast: 8/57] BSE: platform: add Bse::executable_path() and Bse::executable_name()



commit a43dc06d2a4e3df57d5a5e9480e7320c40aa4756
Author: Tim Janik <timj gnu org>
Date:   Fri Jul 14 01:32:42 2017 +0200

    BSE: platform: add Bse::executable_path() and Bse::executable_name()
    
    The executable_path() function is based on the MinGW function
    pathtools.c:get_executable_path(), licensed CC0.
    
    Signed-off-by: Tim Janik <timj gnu org>

 sfi/Makefile.am |    4 +-
 sfi/platform.cc |   69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 sfi/platform.hh |   14 +++++++++++
 3 files changed, 85 insertions(+), 2 deletions(-)
---
diff --git a/sfi/Makefile.am b/sfi/Makefile.am
index 113e627..4c11c22 100644
--- a/sfi/Makefile.am
+++ b/sfi/Makefile.am
@@ -6,7 +6,7 @@ DEFS        += @DEFINE__FILE_DIR__@ -DG_LOG_DOMAIN=\"SFI\" -DG_DISABLE_CONST_RET
 AM_CPPFLAGS += -I$(top_srcdir)
 
 sfi_public_headers = $(strip \
-       bcore.hh        \
+       bcore.hh        platform.hh     \
        cxxaux.hh       \
        sfistore.hh     sficomwire.hh   sfifilecrawler.hh \
        glib-extra.hh                   sfiwrapper.hh    \
@@ -19,7 +19,7 @@ sfi_public_headers = $(strip \
        gbsearcharray.hh \
 )
 sfi_all_sources = $(strip \
-       bcore.cc        \
+       bcore.cc        platform.cc     \
        sfistore.cc     sficomwire.cc   sfifilecrawler.cc \
        glib-extra.cc                   sfiwrapper.cc   sfiprimitives.cc \
        sfivmarshal.cc  sfiglue.cc      sfigluecodec.cc sfiglueproxy.cc \
diff --git a/sfi/platform.cc b/sfi/platform.cc
new file mode 100644
index 0000000..841e479
--- /dev/null
+++ b/sfi/platform.cc
@@ -0,0 +1,69 @@
+// This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
+#include "platform.hh"
+#include <unistd.h>
+#if defined __APPLE__
+#include <mach-o/dyld.h>        // _NSGetExecutablePath
+#endif // __APPLE__
+#ifdef  _WIN32                  // includes _WIN64
+#include <windows.h>
+#endif  // _WIN32
+#include <cstring>
+
+namespace Bse {
+
+static std::string
+get_executable_path()
+{
+  const ssize_t max_size = 4096;
+  char system_result[max_size + 1] = { 0, };
+  ssize_t system_result_size = -1;
+
+#if defined __linux__ || defined __CYGWIN__ || defined __MSYS__
+  system_result_size = readlink ("/proc/self/exe", system_result, max_size);
+#elif defined __APPLE__
+  {
+    uint32_t bufsize = max_size;
+    if (_NSGetExecutablePath (system_result, &bufsize) == 0)
+      system_result_size = bufsize;
+  }
+#elif defined _WIN32
+  unsigned long bufsize = max_size;
+  system_result_size = GetModuleFileNameA (0, system_result, bufsize);
+  if (system_result_size == 0 || system_result_size == bufsize)
+    system_result_size = -1;    /* Error, possibly not enough space. */
+  else
+    {
+      system_result[system_result_size] = '\0';
+      /* Early conversion to unix slashes instead of more changes everywhere else .. */
+      char *winslash;
+      while ((winslash = strchr (system_result, '\\')) != NULL)
+        *winslash = '/';
+    }
+#else
+#warning "Platform lacks executable_path() implementation"
+#endif
+
+  if (system_result_size != -1)
+    return std::string (system_result);
+  return std::string();
+}
+
+std::string
+executable_path()
+{
+  static std::string cached_executable_path = get_executable_path();
+  return cached_executable_path;
+}
+
+std::string
+executable_name()
+{
+  static std::string cached_executable_name = [] () {
+    std::string path = executable_path();
+    const char *slash = strrchr (path.c_str(), '/');
+    return slash ? slash + 1 : path;
+  } ();
+  return cached_executable_name;
+}
+
+} // Bse
diff --git a/sfi/platform.hh b/sfi/platform.hh
new file mode 100644
index 0000000..4616534
--- /dev/null
+++ b/sfi/platform.hh
@@ -0,0 +1,14 @@
+// This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
+#ifndef __BSE_PLATFORM_HH__
+#define __BSE_PLATFORM_HH__
+
+#include <sfi/glib-extra.hh>
+
+namespace Bse {
+
+std::string     executable_path();
+std::string     executable_name();
+
+} // Bse
+
+#endif // __BSE_PLATFORM_HH__


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]