1 /*
2 * Inkscape::Debug::demangle - demangle C++ symbol names
3 *
4 * Authors:
5 * MenTaLguY <mental@rydia.net>
6 *
7 * Copyright (C) 2006 MenTaLguY
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
12 #include <stdio.h>
13 #include <string.h>
14 #include <map>
15 #include "debug/demangle.h"
16 #include "util/format.h"
17 #include "gc-alloc.h"
19 namespace Inkscape {
21 namespace Debug {
23 namespace {
25 char const *demangle_helper(char const *name) {
26 char buffer[1024];
27 char const *result;
28 FILE *stream=popen(Util::format("c++filt %s", name), "r");
29 if (fgets(buffer, sizeof(buffer), stream)) {
30 size_t len=strlen(buffer);
31 if ( buffer[len-1] == '\n' ) {
32 buffer[len-1] = '\000';
33 }
34 result = strdup(buffer);
35 } else {
36 result = name;
37 }
38 pclose(stream);
39 return result;
40 }
42 struct string_less_than {
43 bool operator()(char const *a, char const *b) const {
44 return ( strcmp(a, b) < 0 );
45 }
46 };
48 typedef std::map<char const *, char const *, string_less_than> MangleCache;
49 MangleCache mangle_cache;
51 }
53 Util::ptr_shared<char> demangle(char const *name) {
54 MangleCache::iterator found=mangle_cache.find(name);
56 char const *result;
57 if ( found != mangle_cache.end() ) {
58 result = (*found).second;
59 } else {
60 result = demangle_helper(name);
61 mangle_cache[name] = result;
62 }
64 return Util::share_unsafe(result);
65 }
67 }
69 }
71 /*
72 Local Variables:
73 mode:c++
74 c-file-style:"stroustrup"
75 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
76 indent-tabs-mode:nil
77 fill-column:99
78 End:
79 */
80 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :