~ubuntu-branches/ubuntu/precise/inkscape/precise-updates

« back to all changes in this revision

Viewing changes to src/prefix.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alex Valavanis
  • Date: 2010-09-12 19:44:58 UTC
  • mfrom: (1.1.12 upstream) (45.1.3 maverick)
  • Revision ID: james.westby@ubuntu.com-20100912194458-4sjwmbl7dlsrk5dc
Tags: 0.48.0-1ubuntu1
* Merge with Debian unstable (LP: #628048, LP: #401567, LP: #456248, 
  LP: #463602, LP: #591986)
* debian/control: 
  - Ubuntu maintainers
  - Promote python-lxml, python-numpy, python-uniconvertor to Recommends.
  - Demote pstoedit to Suggests (universe package).
  - Suggests ttf-dejavu instead of ttf-bitstream-vera (LP: #513319)
* debian/rules:
  - Run intltool-update on build (Ubuntu-specific).
  - Add translation domain to .desktop files (Ubuntu-specific).
* debian/dirs:
  - Add usr/share/pixmaps.  Allow inkscape.xpm installation
* drop 50-poppler-API.dpatch (now upstream)
* drop 51-paste-in-unwritable-directory.dpatch (now upstream) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#endif
32
32
 
33
33
 
34
 
/* PLEASE NOTE:  We use GThreads now for portability */
35
 
/* @see http://developer.gnome.org/doc/API/2.0/glib/glib-Threads.html */
36
 
#ifndef BR_THREADS
37
 
    /* Change 1 to 0 if you don't want thread support */
38
 
    #define BR_THREADS 1
39
 
    #include <glib.h> //for GThreads
40
 
#endif /* BR_THREADS */
41
 
 
 
34
#include <glib.h>
42
35
#include <cstdlib>
43
36
#include <cstdio>
44
37
#include <cstring>
55
48
#define NULL ((void *) 0)
56
49
 
57
50
#ifdef __GNUC__
58
 
        #define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;}
 
51
    #define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;}
59
52
#else
60
 
        #define br_return_val_if_fail(expr,val) if (!(expr)) return val
 
53
    #define br_return_val_if_fail(expr,val) if (!(expr)) return val
61
54
#endif /* __GNUC__ */
62
55
 
63
56
 
72
65
 * br_locate:
73
66
 * symbol: A symbol that belongs to the app/library you want to locate.
74
67
 * Returns: A newly allocated string containing the full path of the
75
 
 *          app/library that func belongs to, or NULL on error. This
76
 
 *          string should be freed when not when no longer needed.
 
68
 *        app/library that func belongs to, or NULL on error. This
 
69
 *        string should be freed when not when no longer needed.
77
70
 *
78
71
 * Finds out to which application or library symbol belongs, then locate
79
72
 * the full path of that application or library.
85
78
 * #include "libfoo.h"
86
79
 *
87
80
 * int main (int argc, char *argv[]) {
88
 
 *      printf ("Full path of this app: %s\n", br_locate (&argc));
89
 
 *      libfoo_start ();
90
 
 *      return 0;
 
81
 *    printf ("Full path of this app: %s\n", br_locate (&argc));
 
82
 *    libfoo_start ();
 
83
 *    return 0;
91
84
 * }
92
85
 *
93
86
 * --> libfoo.c starts here
94
87
 * #include "prefix.h"
95
88
 *
96
89
 * void libfoo_start () {
97
 
 *      --> "" is a symbol that belongs to libfoo (because it's called
98
 
 *      --> from libfoo_start()); that's why this works.
99
 
 *      printf ("libfoo is located in: %s\n", br_locate (""));
 
90
 *    --> "" is a symbol that belongs to libfoo (because it's called
 
91
 *    --> from libfoo_start()); that's why this works.
 
92
 *    printf ("libfoo is located in: %s\n", br_locate (""));
100
93
 * }
101
94
 */
102
95
char *
103
96
br_locate (void *symbol)
104
97
{
105
 
        char line[5000];
106
 
        FILE *f;
107
 
        char *path;
108
 
 
109
 
        br_return_val_if_fail (symbol != NULL, NULL);
110
 
 
111
 
        f = fopen ("/proc/self/maps", "r");
112
 
        if (!f)
113
 
                return NULL;
114
 
 
115
 
        while (!feof (f))
116
 
        {
117
 
                unsigned long start, end;
118
 
 
119
 
                if (!fgets (line, sizeof (line), f))
120
 
                        continue;
121
 
                if (!strstr (line, " r-xp ") || !strchr (line, '/'))
122
 
                        continue;
123
 
 
124
 
                sscanf (line, "%lx-%lx ", &start, &end);
125
 
                if (symbol >= (void *) start && symbol < (void *) end)
126
 
                {
127
 
                        char *tmp;
128
 
                        size_t len;
129
 
 
130
 
                        /* Extract the filename; it is always an absolute path */
131
 
                        path = strchr (line, '/');
132
 
 
133
 
                        /* Get rid of the newline */
134
 
                        tmp = strrchr (path, '\n');
135
 
                        if (tmp) *tmp = 0;
136
 
 
137
 
                        /* Get rid of "(deleted)" */
138
 
                        len = strlen (path);
139
 
                        if (len > 10 && strcmp (path + len - 10, " (deleted)") == 0)
140
 
                        {
141
 
                                tmp = path + len - 10;
142
 
                                *tmp = 0;
143
 
                        }
144
 
 
145
 
                        fclose(f);
146
 
                        return strdup (path);
147
 
                }
148
 
        }
149
 
 
150
 
        fclose (f);
151
 
        return NULL;
 
98
    char line[5000];
 
99
    FILE *f;
 
100
    char *path;
 
101
 
 
102
    br_return_val_if_fail (symbol != NULL, NULL);
 
103
 
 
104
    f = fopen ("/proc/self/maps", "r");
 
105
    if (!f)
 
106
        return NULL;
 
107
 
 
108
    while (!feof (f))
 
109
    {
 
110
        unsigned long start, end;
 
111
 
 
112
        if (!fgets (line, sizeof (line), f))
 
113
            continue;
 
114
        if (!strstr (line, " r-xp ") || !strchr (line, '/'))
 
115
            continue;
 
116
 
 
117
        sscanf (line, "%lx-%lx ", &start, &end);
 
118
        if (symbol >= (void *) start && symbol < (void *) end)
 
119
        {
 
120
            char *tmp;
 
121
            size_t len;
 
122
 
 
123
            /* Extract the filename; it is always an absolute path */
 
124
            path = strchr (line, '/');
 
125
 
 
126
            /* Get rid of the newline */
 
127
            tmp = strrchr (path, '\n');
 
128
            if (tmp) *tmp = 0;
 
129
 
 
130
            /* Get rid of "(deleted)" */
 
131
            len = strlen (path);
 
132
            if (len > 10 && strcmp (path + len - 10, " (deleted)") == 0)
 
133
            {
 
134
                tmp = path + len - 10;
 
135
                *tmp = 0;
 
136
            }
 
137
 
 
138
            fclose(f);
 
139
            return strdup (path);
 
140
        }
 
141
    }
 
142
 
 
143
    fclose (f);
 
144
    return NULL;
152
145
}
153
146
 
154
147
 
168
161
char *
169
162
br_locate_prefix (void *symbol)
170
163
{
171
 
        char *path, *prefix;
172
 
 
173
 
        br_return_val_if_fail (symbol != NULL, NULL);
174
 
 
175
 
        path = br_locate (symbol);
176
 
        if (!path) return NULL;
177
 
 
178
 
        prefix = br_extract_prefix (path);
179
 
        free (path);
180
 
        return prefix;
 
164
    char *path, *prefix;
 
165
 
 
166
    br_return_val_if_fail (symbol != NULL, NULL);
 
167
 
 
168
    path = br_locate (symbol);
 
169
    if (!path) return NULL;
 
170
 
 
171
    prefix = br_extract_prefix (path);
 
172
    free (path);
 
173
    return prefix;
181
174
}
182
175
 
183
176
 
186
179
 * symbol: A symbol that belongs to the app/library you want to locate.
187
180
 * path: The path that you want to prepend the prefix to.
188
181
 * Returns: The new path, or NULL on error. This string should be freed when no
189
 
 *          longer needed.
 
182
 *        longer needed.
190
183
 *
191
184
 * Gets the prefix of the app/library that symbol belongs to. Prepend that prefix to path.
192
185
 * Note that symbol cannot be a pointer to a function. That will not work.
198
191
char *
199
192
br_prepend_prefix (void *symbol, char *path)
200
193
{
201
 
        char *tmp, *newpath;
202
 
 
203
 
        br_return_val_if_fail (symbol != NULL, NULL);
204
 
        br_return_val_if_fail (path != NULL, NULL);
205
 
 
206
 
        tmp = br_locate_prefix (symbol);
207
 
        if (!tmp) return NULL;
208
 
 
209
 
        if (strcmp (tmp, "/") == 0)
210
 
                newpath = strdup (path);
211
 
        else
212
 
                newpath = br_strcat (tmp, path);
213
 
 
214
 
        /* Get rid of compiler warning ("br_prepend_prefix never used") */
215
 
        if (0) br_prepend_prefix (NULL, NULL);
216
 
 
217
 
        free (tmp);
218
 
        return newpath;
 
194
    char *tmp, *newpath;
 
195
 
 
196
    br_return_val_if_fail (symbol != NULL, NULL);
 
197
    br_return_val_if_fail (path != NULL, NULL);
 
198
 
 
199
    tmp = br_locate_prefix (symbol);
 
200
    if (!tmp) return NULL;
 
201
 
 
202
    if (strcmp (tmp, "/") == 0)
 
203
        newpath = strdup (path);
 
204
    else
 
205
        newpath = br_strcat (tmp, path);
 
206
 
 
207
    /* Get rid of compiler warning ("br_prepend_prefix never used") */
 
208
    if (0) br_prepend_prefix (NULL, NULL);
 
209
 
 
210
    free (tmp);
 
211
    return newpath;
219
212
}
220
213
 
221
214
#endif /* ENABLE_BINRELOC */
264
257
const char *
265
258
br_thread_local_store (char *str)
266
259
{
267
 
        #if BR_THREADS
 
260
    #if BR_THREADS
268
261
                if (!g_thread_supported ())
269
262
                    {
270
263
                    g_thread_init ((GThreadFunctions *)NULL);
271
264
                    br_thread_key = g_private_new (g_free);
272
265
                    }
273
266
 
274
 
                char *specific = (char *) g_private_get (br_thread_key);
275
 
                if (specific)
 
267
        char *specific = (char *) g_private_get (br_thread_key);
 
268
        if (specific)
276
269
                    free (specific);
277
270
                g_private_set (br_thread_key, str);
278
271
 
279
 
        #else /* !BR_THREADS */
280
 
                static int initialized = 0;
281
 
 
282
 
                if (!initialized)
283
 
                {
284
 
                        atexit (br_free_last_value);
285
 
                        initialized = 1;
286
 
                }
287
 
 
288
 
                if (br_last_value)
289
 
                        free (br_last_value);
290
 
                br_last_value = str;
291
 
        #endif /* BR_THREADS */
292
 
 
293
 
        return (const char *) str;
 
272
    #else /* !BR_THREADS */
 
273
        static int initialized = 0;
 
274
 
 
275
        if (!initialized)
 
276
        {
 
277
            atexit (br_free_last_value);
 
278
            initialized = 1;
 
279
        }
 
280
 
 
281
        if (br_last_value)
 
282
            free (br_last_value);
 
283
        br_last_value = str;
 
284
    #endif /* BR_THREADS */
 
285
 
 
286
    return (const char *) str;
294
287
}
295
288
 
296
289
 
305
298
char *
306
299
br_strcat (const char *str1, const char *str2)
307
300
{
308
 
        char *result;
309
 
        size_t len1, len2;
310
 
 
311
 
        if (!str1) str1 = "";
312
 
        if (!str2) str2 = "";
313
 
 
314
 
        len1 = strlen (str1);
315
 
        len2 = strlen (str2);
316
 
 
317
 
        result = (char *) malloc (len1 + len2 + 1);
318
 
        memcpy (result, str1, len1);
319
 
        memcpy (result + len1, str2, len2);
320
 
        result[len1 + len2] = '\0';
321
 
 
322
 
        return result;
 
301
    char *result;
 
302
    size_t len1, len2;
 
303
 
 
304
    if (!str1) str1 = "";
 
305
    if (!str2) str2 = "";
 
306
 
 
307
    len1 = strlen (str1);
 
308
    len2 = strlen (str2);
 
309
 
 
310
    result = (char *) malloc (len1 + len2 + 1);
 
311
    memcpy (result, str1, len1);
 
312
    memcpy (result + len1, str2, len2);
 
313
    result[len1 + len2] = '\0';
 
314
 
 
315
    return result;
323
316
}
324
317
 
325
318
 
327
320
static char *
328
321
br_strndup (char *str, size_t size)
329
322
{
330
 
        char *result = (char*)NULL;
331
 
        size_t len;
332
 
 
333
 
        br_return_val_if_fail (str != (char*)NULL, (char*)NULL);
334
 
 
335
 
        len = strlen (str);
336
 
        if (!len) return strdup ("");
337
 
        if (size > len) size = len;
338
 
 
339
 
        result = (char *) calloc (sizeof (char), len + 1);
340
 
        memcpy (result, str, size);
341
 
        return result;
 
323
    char *result = (char*)NULL;
 
324
    size_t len;
 
325
 
 
326
    br_return_val_if_fail (str != (char*)NULL, (char*)NULL);
 
327
 
 
328
    len = strlen (str);
 
329
    if (!len) return strdup ("");
 
330
    if (size > len) size = len;
 
331
 
 
332
    result = (char *) calloc (sizeof (char), len + 1);
 
333
    memcpy (result, str, size);
 
334
    return result;
342
335
}
343
336
 
344
337
 
356
349
char *
357
350
br_extract_dir (const char *path)
358
351
{
359
 
        const char *end;
360
 
        char *result;
361
 
 
362
 
        br_return_val_if_fail (path != (char*)NULL, (char*)NULL);
363
 
 
364
 
        end = strrchr (path, '/');
365
 
        if (!end) return strdup (".");
366
 
 
367
 
        while (end > path && *end == '/')
368
 
                end--;
369
 
        result = br_strndup ((char *) path, end - path + 1);
370
 
        if (!*result)
371
 
        {
372
 
                free (result);
373
 
                return strdup ("/");
374
 
        } else
375
 
                return result;
 
352
    const char *end;
 
353
    char *result;
 
354
 
 
355
    br_return_val_if_fail (path != (char*)NULL, (char*)NULL);
 
356
 
 
357
    end = strrchr (path, '/');
 
358
    if (!end) return strdup (".");
 
359
 
 
360
    while (end > path && *end == '/')
 
361
        end--;
 
362
    result = br_strndup ((char *) path, end - path + 1);
 
363
    if (!*result)
 
364
    {
 
365
        free (result);
 
366
        return strdup ("/");
 
367
    } else
 
368
        return result;
376
369
}
377
370
 
378
371
 
392
385
char *
393
386
br_extract_prefix (const char *path)
394
387
{
395
 
        const char *end;
396
 
        char *tmp, *result;
397
 
 
398
 
        br_return_val_if_fail (path != (char*)NULL, (char*)NULL);
399
 
 
400
 
        if (!*path) return strdup ("/");
401
 
        end = strrchr (path, '/');
402
 
        if (!end) return strdup (path);
403
 
 
404
 
        tmp = br_strndup ((char *) path, end - path);
405
 
        if (!*tmp)
406
 
        {
407
 
                free (tmp);
408
 
                return strdup ("/");
409
 
        }
410
 
        end = strrchr (tmp, '/');
411
 
        if (!end) return tmp;
412
 
 
413
 
        result = br_strndup (tmp, end - tmp);
414
 
        free (tmp);
415
 
 
416
 
        if (!*result)
417
 
        {
418
 
                free (result);
419
 
                result = strdup ("/");
420
 
        }
421
 
 
422
 
        return result;
 
388
    const char *end;
 
389
    char *tmp, *result;
 
390
 
 
391
    br_return_val_if_fail (path != (char*)NULL, (char*)NULL);
 
392
 
 
393
    if (!*path) return strdup ("/");
 
394
    end = strrchr (path, '/');
 
395
    if (!end) return strdup (path);
 
396
 
 
397
    tmp = br_strndup ((char *) path, end - path);
 
398
    if (!*tmp)
 
399
    {
 
400
        free (tmp);
 
401
        return strdup ("/");
 
402
    }
 
403
    end = strrchr (tmp, '/');
 
404
    if (!end) return tmp;
 
405
 
 
406
    result = br_strndup (tmp, end - tmp);
 
407
    free (tmp);
 
408
 
 
409
    if (!*result)
 
410
    {
 
411
        free (result);
 
412
        result = strdup ("/");
 
413
    }
 
414
 
 
415
    return result;
423
416
}
424
417
 
425
418
 
442
435
/**
443
436
 * Return the directory of the .exe that is currently running
444
437
 */
445
 
static Glib::ustring win32_getExePath()
 
438
Glib::ustring win32_getExePath()
446
439
{
447
 
    char exeName[MAX_PATH+1];
448
 
    GetModuleFileName(NULL, exeName, MAX_PATH);
449
 
    char *slashPos = strrchr(exeName, '\\');
450
 
    if (slashPos)
451
 
        *slashPos = '\0';
452
 
    Glib::ustring s = exeName;
453
 
    return s;
 
440
    gunichar2 path[2048];
 
441
    GetModuleFileNameW(0, (WCHAR*) path, 2048);
 
442
    gchar *exe = g_utf16_to_utf8(path, -1, 0,0,0);
 
443
    gchar *dir = g_path_get_dirname(exe);
 
444
    Glib::ustring ret = dir;
 
445
    g_free(dir);
 
446
    g_free(exe);
 
447
    return ret;
454
448
}
455
449
 
456
450
 
461
455
static Glib::ustring win32_getDataDir()
462
456
{
463
457
    Glib::ustring dir = win32_getExePath();
464
 
    if (INKSCAPE_DATADIR  && *INKSCAPE_DATADIR &&
465
 
            strcmp(INKSCAPE_DATADIR, ".") != 0)
 
458
    if (INKSCAPE_DATADIR && *INKSCAPE_DATADIR &&
 
459
        strcmp(INKSCAPE_DATADIR, ".") != 0)
466
460
        {
467
461
        dir += "\\";
468
462
        dir += INKSCAPE_DATADIR;
487
481
 */ 
488
482
char *win32_relative_path(const char *childPath)
489
483
{
490
 
    static char *returnPath = NULL;
 
484
    static char *returnPath = 0;
491
485
    if (!childPath)
492
486
        childPath = "";
493
487
    Glib::ustring resourcePath = win32_getResourcePath(childPath);