File: dump-modules.c

package info (click to toggle)
gwyddion 2.47-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,180 kB
  • ctags: 37,276
  • sloc: ansic: 317,531; sh: 5,122; xml: 3,583; python: 3,067; makefile: 2,694; cpp: 2,550; pascal: 418; perl: 154; ruby: 130; sed: 17
file content (312 lines) | stat: -rw-r--r-- 8,510 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 *  @(#) $Id: dump-modules.c 16229 2014-05-29 21:01:35Z yeti-dn $
 *  Copyright (C) 2003,2004 David Necas (Yeti), Petr Klapetek.
 *  E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include <libgwyddion/gwymacros.h>
#include <libgwymodule/gwymodule.h>
#include <libgwyddion/gwyutils.h>
#include <libgwyddion/gwyddion.h>
#include <app/settings.h>
#include <app/gwytool.h>
#include <app/app.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>

typedef struct {
    const gchar *name;
    const GwyModuleInfo *info;
} ModInfo;

typedef struct {
    gchar *target;
    guint use_count;
} MapInfo;

static GHashTable *user_guide = NULL;
static GSList *tag_stack = NULL;

static void
read_user_guide(const gchar *filename)
{
    gchar *buf = NULL;

    g_file_get_contents(filename, &buf, NULL, NULL);
    user_guide = g_hash_table_new(g_str_hash, g_str_equal);
    while (buf && *buf) {
        MapInfo *minfo;
        gchar *key = gwy_str_next_line(&buf);
        gchar *val = strchr(key, ' ');

        if (!val)
            continue;
        *val = '\0';
        minfo = g_new0(MapInfo, 1);
        minfo->target = val+1;
        g_hash_table_insert(user_guide, key, minfo);
    };
}

/* For module list sorting */
static gint
compare_modules(gconstpointer a, gconstpointer b)
{
    return strcmp(((const ModInfo*)a)->name,
                  ((const ModInfo*)b)->name);
}

/* For finding module of given name */
static gint
find_module(gconstpointer a, gconstpointer b)
{
    return strcmp(((const ModInfo*)a)->name, (const gchar*)b);
}

/* Print a single tag with content, handling sensitive characters */
static void
tag_print(const gchar *tag, const gchar *contents)
{
    gchar *s;

    if (!contents || !*contents) {
        printf("<%s/>\n", tag);
        return;
    }

    s = g_markup_escape_text(contents, strlen(contents));
    printf("<%s>%s</%s>\n", tag, s, tag);
    g_free(s);
}

/* Print an opening tag and push it onto stack */
static void
tag_open(const gchar *tag)
{
    printf("<%s>\n", tag);
    tag_stack = g_slist_prepend(tag_stack, g_strdup(tag));
}

/* Pop currently open tag and close it */
static void
tag_close(void)
{
    gchar *s;

    g_return_if_fail(tag_stack);
    s = tag_stack->data;
    printf("</%s>\n", s);
    tag_stack = g_slist_delete_link(tag_stack, tag_stack);
    g_free(s);
}

static void
menu_path_print(const gchar *path, const gchar *tag)
{
    gchar *s;
    gint i;

    path = gettext(path);
    s = gwy_strkill(gwy_strreplace(path + (path[0] == '/'),
                                   "/", " → ", -1), "_");
    i = strlen(s);
    do {
        s[i] = '\0';
        i--;
    } while (s[i] == '.');
    tag_print(tag, s);
    g_free(s);
}

/* Remove <...> substrings from a string. */
static gchar*
kill_mail(const gchar *authors)
{
    const gchar *a;
    gchar *s, *p;

    p = s = g_new0(gchar, strlen(authors) + 1);
    while (authors) {
        a = strchr(authors, '<');
        if (!a) {
            strcpy(p, authors);
            break;
        }
        else {
            while (a > authors && !g_ascii_isspace(*a))
                a--;
            strncpy(p, authors, a - authors);
            p += a - authors;
            authors = strchr(a+1, '>');
            if (authors)
                authors++;
        }
    }

    p = s + strlen(s);
    while (p >= s && (!*p || g_ascii_isspace(*p))) {
        *p = '\0';
        p--;
    }

    return s;
}

static void
add_info(const gchar *name,
         const GwyModuleInfo *info,
         GSList **list)
{
    ModInfo *i;

    i = g_new(ModInfo, 1);
    i->name = name;
    i->info = info;
    *list = g_slist_prepend(*list, i);
}

static void
warn_unused(const gchar *name,
            const MapInfo *minfo)
{
    if (!minfo->use_count)
        fprintf(stderr, "Unused map entry %s -> %s\n", name, minfo->target);
}

/* Main */
int
main(int argc,
     char *argv[])
{
    /* modules we know we don't like to see in the list (XXX: hack) */
    const gchar *filter_modules[] = {
        "threshold-example",
    };
    /* modules we know we don't want to see functions of (XXX: hack) */
    const gchar *no_func_modules[] = {
        "plugin-proxy", "pygwy",
    };

    gchar **module_dirs;
    GSList *m, *modules = NULL;
    gsize i;

    gwy_type_init();
    if (argc < 3) {
        g_printerr("Usage: dump-modules USER-GUIDE-MAP LANGUAGE.\n");
        return 1;
    }
    g_setenv("LC_MESSAGES", argv[2], TRUE);
    g_setenv("LANGUAGE", argv[2], TRUE);
    setlocale(LC_MESSAGES, argv[2]);
    gwy_app_init_i18n();
    read_user_guide(argv[1]);

    module_dirs = gwy_app_settings_get_module_dirs();
    gwy_module_register_modules((const gchar**)module_dirs);
    gwy_module_foreach((GHFunc)add_info, &modules);
    modules = g_slist_sort(modules, &compare_modules);

    for (i = 0; i < G_N_ELEMENTS(filter_modules); i++) {
        m = g_slist_find_custom(modules, filter_modules[i], find_module);
        if (!m)
            continue;
        modules = g_slist_delete_link(modules, m);
    }

    puts("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    tag_open("modulelist");
    for (m = modules; m; m = g_slist_next(m)) {
        ModInfo *info = (ModInfo*)m->data;
        GSList *f = gwy_module_get_functions(info->name);
        MapInfo *minfo;
        gchar *s;

        tag_open("module");
        tag_print("name", info->name);
        tag_print("version", info->info->version);
        s = kill_mail(info->info->author);
        tag_print("author", s);
        g_free(s);
        tag_print("copyright", info->info->copyright);
        tag_print("date", info->info->date);
        if ((minfo = g_hash_table_lookup(user_guide, info->name))) {
            tag_print("userguide", minfo->target);
            minfo->use_count++;
        }
        else
            fprintf(stderr, "No user guide entry for %s\n", info->name);
        tag_print("description", gettext(info->info->blurb));
        for (i = 0; i < G_N_ELEMENTS(no_func_modules); i++) {
            if (gwy_strequal(info->name, no_func_modules[i]))
                break;
        }
        if (i < G_N_ELEMENTS(no_func_modules)) {
            tag_print("funclist", NULL);
            tag_close();
            continue;
        }

        tag_open("funclist");
        for ( ; f; f = g_slist_next(f)) {
            gchar *name, *class = g_strdup(f->data);

            tag_open("func");
            /* parse class::name string */
            name = strchr(class, ':');
            g_assert(name != NULL && name > class && name[1] && name[2]);
            *name = '\0';
            name += 2;
            tag_print("class", class);
            tag_print("name", name);

            /* dig more info about particular function types */
            if (gwy_strequal(class, "proc"))
                menu_path_print(gwy_process_func_get_menu_path(name), "info");
            else if (gwy_strequal(class, "graph"))
                menu_path_print(gwy_graph_func_get_menu_path(name), "info");
            else if (gwy_strequal(class, "file"))
                menu_path_print(gwy_file_func_get_description(name), "info");
            else if (gwy_strequal(class, "tool")) {
                GwyToolClass *tool_class;

                tool_class = g_type_class_peek(g_type_from_name(name));
                tag_print("info", gwy_tool_class_get_tooltip(tool_class));
            }

            g_free(class);
            tag_close();
        }
        tag_close();
        tag_close();
    }
    tag_close();

    g_hash_table_foreach(user_guide, (GHFunc)warn_unused, NULL);

    g_slist_free(modules);
    g_strfreev(module_dirs);

    return 0;
}

/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */