path-prefix.cpp 4.31 KB
Newer Older
Max Gaukler's avatar
Max Gaukler committed
1 2 3
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * path-prefix.cpp - Inkscape specific prefix handling *//*
4
 * Authors:
Patrick Storz's avatar
Patrick Storz committed
5
 *   Patrick Storz <eduard.braun2@gmx.de>
6
 *
Max Gaukler's avatar
Max Gaukler committed
7 8
 * Copyright (C) 2018 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 10 11
 */

#ifdef HAVE_CONFIG_H
12
# include "config.h"  // only include where actually required!
13 14
#endif

15 16 17 18 19 20 21 22
#ifdef _WIN32
#include <windows.h> // for GetModuleFileNameW
#endif

#ifdef __APPLE__
#include <mach-o/dyld.h> // for _NSGetExecutablePath
#endif

23
#include <glib.h>
24

25 26
#include "path-prefix.h"

27
/**
28 29
 * Determine the location of the Inkscape data directory (typically the share/ folder
 * from where Inkscape should be loading resources) and append a relative path
30
 *
31 32
 *  - by default use the compile time value of INKSCAPE_DATADIR
 *  - on Windows inkscape_datadir will be relative to the called executable by default
33
 *    (typically inkscape/share but also handles the case where the executable is in a /bin subfolder)
34
 *  - if the environment variable INKSCAPE_DATADIR is set it will override all of the above
35
 */
36
char *append_inkscape_datadir(const char *relative_path)
37
{
Patrick Storz's avatar
Patrick Storz committed
38
    static gchar const *inkscape_datadir;
39
    if (!inkscape_datadir) {
40
        gchar *datadir;
41 42
        gchar const *datadir_env = g_getenv("INKSCAPE_DATADIR");
        if (datadir_env) {
43
            datadir = g_strdup(datadir_env);
44
        } else {
45
#ifdef _WIN32
46
            gchar *module_path = g_win32_get_package_installation_directory_of_module(NULL);
47
            datadir = g_build_filename(module_path, "share", NULL);
48
            g_free(module_path);
49
#else
50
            datadir = g_strdup(INKSCAPE_DATADIR);
51
#endif
52
        }
53 54 55 56 57 58 59 60 61 62 63

#if GLIB_CHECK_VERSION(2,58,0)
        inkscape_datadir = g_canonicalize_filename(datadir, NULL);
        g_free(datadir);
#else
        inkscape_datadir = datadir;
#endif
    }

    if (!relative_path) {
        relative_path = "";
64 65
    }

66 67 68
#if GLIB_CHECK_VERSION(2,58,0)
    return g_canonicalize_filename(relative_path, inkscape_datadir);
#else
69
    return g_build_filename(inkscape_datadir, relative_path, NULL);
70
#endif
71 72
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/**
 * Gets the the currently running program's executable name (including full path)
 *
 * @return executable name (including full path) encoded as UTF-8
 *         or NULL if it can't be determined
 */
gchar *get_program_name()
{
    static gchar *program_name = NULL;

    if (program_name == NULL) {
        // There is no portable way to get an executable's name including path, so we need to do it manually.
        // TODO: Re-evaluate boost::dll::program_location() once we require Boost >= 1.61
        //
        // The following platform-specific code is partially based on GdxPixbuf's get_toplevel()
        // See also https://stackoverflow.com/a/1024937
#if defined(_WIN32)
        wchar_t module_file_name[MAX_PATH];
        if (GetModuleFileNameW(NULL, module_file_name, MAX_PATH)) {
            program_name = g_utf16_to_utf8((gunichar2 *)module_file_name, -1, NULL, NULL, NULL);
        } else {
            g_warning("get_program_name() - GetModuleFileNameW failed");
        }
#elif defined(__APPLE__)
        char pathbuf[PATH_MAX + 1];
        uint32_t bufsize = sizeof(pathbuf);
        if (_NSGetExecutablePath(pathbuf, &bufsize) == 0) {
            program_name = g_strdup(pathbuf);
        } else {
            g_warning("get_program_name() - _NSGetExecutablePath failed");
        }
#elif defined(__linux__)
        program_name = g_file_read_link("/proc/self/exe", NULL);
        if (!program_name) {
            g_warning("get_program_name() - g_file_read_link failed");
        }
#else
#warning get_program_name() - no known way to obtain executable name on this platform
        g_info("get_program_name() - no known way to obtain executable name on this platform");
#endif
    }

    return program_name;
}

/**
 * Gets the the full path to the directory containing the currently running program's executable
 *
 * @return full path to directory encoded as UTF-8
 *         or NULL if it can't be determined
 */
gchar *get_program_dir()
{
    return g_path_get_dirname(get_program_name());
}

129 130 131 132 133 134 135 136 137 138
/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :