Code

Filters. Custom predefined filters update and new ABC filters.
[inkscape.git] / src / prefix.cpp
index 3c9ec6caea49e4d88d656065d53988dcfa71e759..92409a7d2c3dc3aff3e60c7c89b9a8acff1eb221 100644 (file)
@@ -3,7 +3,7 @@
  * Written by: Mike Hearn <mike@theoretic.com>
  *             Hongli Lai <h.lai@chello.nl>
  * http://autopackage.org/
- * 
+ *
  * This source code is public domain. You can relicense this code
  * under whatever license you want.
  *
 #endif
 
 
-/* PLEASE NOTE:  We use GThreads now for portability */
-/* @see http://developer.gnome.org/doc/API/2.0/glib/glib-Threads.html */
-#ifndef BR_THREADS
-    /* Change 1 to 0 if you don't want thread support */
-    #define BR_THREADS 1
-    #include <glib.h> //for GThreads
-#endif /* BR_THREADS */
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <limits.h> 
-#include <string.h>
+#include <glib.h>
+#include <cstdlib>
+#include <cstdio>
+#include <cstring>
+#include <limits.h>
 #include "prefix.h"
 
+
 #ifdef __cplusplus
 extern "C" {
 #endif /* __cplusplus */
@@ -54,9 +48,9 @@ extern "C" {
 #define NULL ((void *) 0)
 
 #ifdef __GNUC__
-       #define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;}
+    #define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;}
 #else
-       #define br_return_val_if_fail(expr,val) if (!(expr)) return val
+    #define br_return_val_if_fail(expr,val) if (!(expr)) return val
 #endif /* __GNUC__ */
 
 
@@ -71,8 +65,8 @@ extern "C" {
  * br_locate:
  * symbol: A symbol that belongs to the app/library you want to locate.
  * Returns: A newly allocated string containing the full path of the
- *         app/library that func belongs to, or NULL on error. This
- *         string should be freed when not when no longer needed.
+ *        app/library that func belongs to, or NULL on error. This
+ *        string should be freed when not when no longer needed.
  *
  * Finds out to which application or library symbol belongs, then locate
  * the full path of that application or library.
@@ -84,70 +78,70 @@ extern "C" {
  * #include "libfoo.h"
  *
  * int main (int argc, char *argv[]) {
- *     printf ("Full path of this app: %s\n", br_locate (&argc));
- *     libfoo_start ();
- *     return 0;
+ *    printf ("Full path of this app: %s\n", br_locate (&argc));
+ *    libfoo_start ();
+ *    return 0;
  * }
  *
  * --> libfoo.c starts here
  * #include "prefix.h"
  *
  * void libfoo_start () {
- *     --> "" is a symbol that belongs to libfoo (because it's called
- *     --> from libfoo_start()); that's why this works.
- *     printf ("libfoo is located in: %s\n", br_locate (""));
+ *    --> "" is a symbol that belongs to libfoo (because it's called
+ *    --> from libfoo_start()); that's why this works.
+ *    printf ("libfoo is located in: %s\n", br_locate (""));
  * }
  */
 char *
 br_locate (void *symbol)
 {
-       char line[5000];
-       FILE *f;
-       char *path;
-
-       br_return_val_if_fail (symbol != NULL, NULL);
-
-       f = fopen ("/proc/self/maps", "r");
-       if (!f)
-               return NULL;
-
-       while (!feof (f))
-       {
-               unsigned long start, end;
-
-               if (!fgets (line, sizeof (line), f))
-                       continue;
-               if (!strstr (line, " r-xp ") || !strchr (line, '/'))
-                       continue;
-
-               sscanf (line, "%lx-%lx ", &start, &end);
-               if (symbol >= (void *) start && symbol < (void *) end)
-               {
-                       char *tmp;
-                       size_t len;
-
-                       /* Extract the filename; it is always an absolute path */
-                       path = strchr (line, '/');
-
-                       /* Get rid of the newline */
-                       tmp = strrchr (path, '\n');
-                       if (tmp) *tmp = 0;
-
-                       /* Get rid of "(deleted)" */
-                       len = strlen (path);
-                       if (len > 10 && strcmp (path + len - 10, " (deleted)") == 0)
-                       {
-                               tmp = path + len - 10;
-                               *tmp = 0;
-                       }
-
-                       fclose(f);
-                       return strdup (path);
-               }
-       }
-
-       fclose (f);
-       return NULL;
+    char line[5000];
+    FILE *f;
+    char *path;
+
+    br_return_val_if_fail (symbol != NULL, NULL);
+
+    f = fopen ("/proc/self/maps", "r");
+    if (!f)
+        return NULL;
+
+    while (!feof (f))
+    {
+        unsigned long start, end;
+
+        if (!fgets (line, sizeof (line), f))
+            continue;
+        if (!strstr (line, " r-xp ") || !strchr (line, '/'))
+            continue;
+
+        sscanf (line, "%lx-%lx ", &start, &end);
+        if (symbol >= (void *) start && symbol < (void *) end)
+        {
+            char *tmp;
+            size_t len;
+
+            /* Extract the filename; it is always an absolute path */
+            path = strchr (line, '/');
+
+            /* Get rid of the newline */
+            tmp = strrchr (path, '\n');
+            if (tmp) *tmp = 0;
+
+            /* Get rid of "(deleted)" */
+            len = strlen (path);
+            if (len > 10 && strcmp (path + len - 10, " (deleted)") == 0)
+            {
+                tmp = path + len - 10;
+                *tmp = 0;
+            }
+
+            fclose(f);
+            return strdup (path);
+        }
+    }
+
+    fclose (f);
+    return NULL;
 }
 
 
@@ -167,16 +161,16 @@ br_locate (void *symbol)
 char *
 br_locate_prefix (void *symbol)
 {
-       char *path, *prefix;
+    char *path, *prefix;
 
-       br_return_val_if_fail (symbol != NULL, NULL);
+    br_return_val_if_fail (symbol != NULL, NULL);
 
-       path = br_locate (symbol);
-       if (!path) return NULL;
+    path = br_locate (symbol);
+    if (!path) return NULL;
 
-       prefix = br_extract_prefix (path);
-       free (path);
-       return prefix;
+    prefix = br_extract_prefix (path);
+    free (path);
+    return prefix;
 }
 
 
@@ -185,7 +179,7 @@ br_locate_prefix (void *symbol)
  * symbol: A symbol that belongs to the app/library you want to locate.
  * path: The path that you want to prepend the prefix to.
  * Returns: The new path, or NULL on error. This string should be freed when no
- *         longer needed.
+ *        longer needed.
  *
  * Gets the prefix of the app/library that symbol belongs to. Prepend that prefix to path.
  * Note that symbol cannot be a pointer to a function. That will not work.
@@ -197,24 +191,24 @@ br_locate_prefix (void *symbol)
 char *
 br_prepend_prefix (void *symbol, char *path)
 {
-       char *tmp, *newpath;
+    char *tmp, *newpath;
 
-       br_return_val_if_fail (symbol != NULL, NULL);
-       br_return_val_if_fail (path != NULL, NULL);
+    br_return_val_if_fail (symbol != NULL, NULL);
+    br_return_val_if_fail (path != NULL, NULL);
 
-       tmp = br_locate_prefix (symbol);
-       if (!tmp) return NULL;
+    tmp = br_locate_prefix (symbol);
+    if (!tmp) return NULL;
 
-       if (strcmp (tmp, "/") == 0)
-               newpath = strdup (path);
-       else
-               newpath = br_strcat (tmp, path);
+    if (strcmp (tmp, "/") == 0)
+        newpath = strdup (path);
+    else
+        newpath = br_strcat (tmp, path);
 
-       /* Get rid of compiler warning ("br_prepend_prefix never used") */
-       if (0) br_prepend_prefix (NULL, NULL);
+    /* Get rid of compiler warning ("br_prepend_prefix never used") */
+    if (0) br_prepend_prefix (NULL, NULL);
 
-       free (tmp);
-       return newpath;
+    free (tmp);
+    return newpath;
 }
 
 #endif /* ENABLE_BINRELOC */
@@ -263,33 +257,33 @@ br_free_last_value ()
 const char *
 br_thread_local_store (char *str)
 {
-       #if BR_THREADS
+    #if BR_THREADS
                 if (!g_thread_supported ())
                     {
                     g_thread_init ((GThreadFunctions *)NULL);
                     br_thread_key = g_private_new (g_free);
                     }
 
-               char *specific = (char *) g_private_get (br_thread_key);
-               if (specific)
+        char *specific = (char *) g_private_get (br_thread_key);
+        if (specific)
                     free (specific);
                 g_private_set (br_thread_key, str);
 
-       #else /* !BR_THREADS */
-               static int initialized = 0;
+    #else /* !BR_THREADS */
+        static int initialized = 0;
 
-               if (!initialized)
-               {
-                       atexit (br_free_last_value);
-                       initialized = 1;
-               }
+        if (!initialized)
+        {
+            atexit (br_free_last_value);
+            initialized = 1;
+        }
 
-               if (br_last_value)
-                       free (br_last_value);
-               br_last_value = str;
-       #endif /* BR_THREADS */
+        if (br_last_value)
+            free (br_last_value);
+        br_last_value = str;
+    #endif /* BR_THREADS */
 
-       return (const char *) str;
+    return (const char *) str;
 }
 
 
@@ -304,21 +298,21 @@ br_thread_local_store (char *str)
 char *
 br_strcat (const char *str1, const char *str2)
 {
-       char *result;
-       size_t len1, len2;
+    char *result;
+    size_t len1, len2;
 
-       if (!str1) str1 = "";
-       if (!str2) str2 = "";
+    if (!str1) str1 = "";
+    if (!str2) str2 = "";
 
-       len1 = strlen (str1);
-       len2 = strlen (str2);
+    len1 = strlen (str1);
+    len2 = strlen (str2);
 
-       result = (char *) malloc (len1 + len2 + 1);
-       memcpy (result, str1, len1);
-       memcpy (result + len1, str2, len2);
-       result[len1 + len2] = '\0';
+    result = (char *) malloc (len1 + len2 + 1);
+    memcpy (result, str1, len1);
+    memcpy (result + len1, str2, len2);
+    result[len1 + len2] = '\0';
 
-       return result;
+    return result;
 }
 
 
@@ -326,18 +320,18 @@ br_strcat (const char *str1, const char *str2)
 static char *
 br_strndup (char *str, size_t size)
 {
-       char *result = (char*)NULL;
-       size_t len;
+    char *result = (char*)NULL;
+    size_t len;
 
-       br_return_val_if_fail (str != (char*)NULL, (char*)NULL);
+    br_return_val_if_fail (str != (char*)NULL, (char*)NULL);
 
-       len = strlen (str);
-       if (!len) return strdup ("");
-       if (size > len) size = len;
+    len = strlen (str);
+    if (!len) return strdup ("");
+    if (size > len) size = len;
 
-       result = (char *) calloc (sizeof (char), len + 1);
-       memcpy (result, str, size);
-       return result;
+    result = (char *) calloc (sizeof (char), len + 1);
+    memcpy (result, str, size);
+    return result;
 }
 
 
@@ -355,22 +349,23 @@ br_strndup (char *str, size_t size)
 char *
 br_extract_dir (const char *path)
 {
-       char *end, *result;
-
-       br_return_val_if_fail (path != (char*)NULL, (char*)NULL);
-
-       end = strrchr (path, '/');
-       if (!end) return strdup (".");
-
-       while (end > path && *end == '/')
-               end--;
-       result = br_strndup ((char *) path, end - path + 1);
-       if (!*result)
-       {
-               free (result);
-               return strdup ("/");
-       } else
-               return result;
+    const char *end;
+    char *result;
+
+    br_return_val_if_fail (path != (char*)NULL, (char*)NULL);
+
+    end = strrchr (path, '/');
+    if (!end) return strdup (".");
+
+    while (end > path && *end == '/')
+        end--;
+    result = br_strndup ((char *) path, end - path + 1);
+    if (!*result)
+    {
+        free (result);
+        return strdup ("/");
+    } else
+        return result;
 }
 
 
@@ -390,38 +385,114 @@ br_extract_dir (const char *path)
 char *
 br_extract_prefix (const char *path)
 {
-       char *end, *tmp, *result;
+    const char *end;
+    char *tmp, *result;
+
+    br_return_val_if_fail (path != (char*)NULL, (char*)NULL);
+
+    if (!*path) return strdup ("/");
+    end = strrchr (path, '/');
+    if (!end) return strdup (path);
+
+    tmp = br_strndup ((char *) path, end - path);
+    if (!*tmp)
+    {
+        free (tmp);
+        return strdup ("/");
+    }
+    end = strrchr (tmp, '/');
+    if (!end) return tmp;
+
+    result = br_strndup (tmp, end - tmp);
+    free (tmp);
+
+    if (!*result)
+    {
+        free (result);
+        result = strdup ("/");
+    }
+
+    return result;
+}
 
-       br_return_val_if_fail (path != (char*)NULL, (char*)NULL);
 
-       if (!*path) return strdup ("/");
-       end = strrchr (path, '/');
-       if (!end) return strdup (path);
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
 
-       tmp = br_strndup ((char *) path, end - path);
-       if (!*tmp)
-       {
-               free (tmp);
-               return strdup ("/");
-       }
-       end = strrchr (tmp, '/');
-       if (!end) return tmp;
 
-       result = br_strndup (tmp, end - tmp);
-       free (tmp);
 
-       if (!*result)
-       {
-               free (result);
-               result = strdup ("/");
-       }
+#ifdef __WIN32__
 
-       return result;
+/**
+ * Provide a similar mechanism for Win32.  Enable a macro,
+ * WIN32_DATADIR, that can look up subpaths for inkscape resources 
+ */ 
+#include <windows.h>
+#include <glibmm/ustring.h>
+
+/**
+ * Return the directory of the .exe that is currently running
+ */
+Glib::ustring win32_getExePath()
+{
+    gunichar2 path[2048];
+    GetModuleFileNameW(0, (WCHAR*) path, 2048);
+    gchar *exe = g_utf16_to_utf8(path, -1, 0,0,0);
+    gchar *dir = g_path_get_dirname(exe);
+    Glib::ustring ret = dir;
+    g_free(dir);
+    g_free(exe);
+    return ret;
 }
 
 
-#ifdef __cplusplus
+/**
+ * Return the relocatable version of the datadir,
+ * probably c:\inkscape 
+ */
+static Glib::ustring win32_getDataDir()
+{
+    Glib::ustring dir = win32_getExePath();
+    if (INKSCAPE_DATADIR && *INKSCAPE_DATADIR &&
+        strcmp(INKSCAPE_DATADIR, ".") != 0)
+        {
+        dir += "\\";
+        dir += INKSCAPE_DATADIR;
+        }
+    return dir;
 }
-#endif /* __cplusplus */
+
+static Glib::ustring win32_getResourcePath(const Glib::ustring &childPath)
+{
+    Glib::ustring dir = win32_getDataDir();
+    if (childPath.size() > 0)
+        {
+        dir += "\\";
+        dir += childPath;
+        }
+    return dir;
+}
+
+
+/**
+ * This is the visible utility function
+ */ 
+char *win32_relative_path(const char *childPath)
+{
+    static char *returnPath = 0;
+    if (!childPath)
+        childPath = "";
+    Glib::ustring resourcePath = win32_getResourcePath(childPath);
+    if (returnPath)
+        free(returnPath);
+    returnPath = strdup(resourcePath.c_str());
+    return returnPath;
+}
+#endif /* __WIN32__ */
+
+
+
 
 #endif /* _PREFIX_C */