aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-error-tool.c
blob: c6f1e0ff8c5041d7a41f2eec761142ebbc086bb5 (plain) (blame)
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

#include <errno.h>

#include <stdio.h>

#include <string.h>
#include <sys/types.h>
#include <dirent.h>

#include <unistd.h>

#include <libxml/parser.h>
#include <libxml/xmlmemory.h>

#include <glib.h>

static void
dump_cstring(FILE *out, const char *tmp)
{
    const char *p;
    int c;

    fprintf(out, "char *s = N_(\"");
    p = tmp;
    while ( (c = *p++) ) {
        switch (c) {
        case '\n':
            fprintf(out, "\\n\"\n\t\"");
            break;
        case '\r':
            fprintf(out, "\\r");
            break;
        case '\t':
            fprintf(out, "\\t");
            break;
        case '"':
        case '\\':
            fprintf(out, "\\%c", c);
            break;
        default:
            fputc(c, out);
        }
    }

    fprintf(out, "\");\n");
}

static void
extract_strings(FILE *out, const char *path)
{
    xmlDocPtr doc;
    xmlNodePtr root, error, scan;
    char *tmp, *domain;

    doc = xmlParseFile(path);
    if (doc == NULL) {
        g_warning("Error file '%s' not found", path);
        return;
    }

    root = xmlDocGetRootElement(doc);
    if (root == NULL
        || strcmp(root->name, "error-list") != 0
        || (domain = xmlGetProp(root, "domain")) == NULL) {
        g_warning("Error file '%s' invalid format", path);
        xmlFreeDoc(doc);
        return;
    }

    for (error = root->children;error;error = error->next) {
        char *id;

        if (strcmp(error->name, "error") != 0)
            continue;

        id = xmlGetProp(error, "id");
        if (id == NULL) {
            g_warning("Error format invalid, missing error id");
            _exit(1);
        }

        for (scan = error->children;scan;scan=scan->next) {
            if (!strcmp(scan->name, "primary")
                || !strcmp(scan->name, "secondary")
                || !strcmp(scan->name, "title")) {
                fprintf(out, "/* %s:%s %s */\n", domain, id, scan->name);
                tmp = xmlNodeGetContent(scan);
                if (tmp) {
                    dump_cstring(out, tmp);
                    xmlFree(tmp);
                }
            } else if (!strcmp(scan->name, "button")) {
                tmp = xmlGetProp(scan, "label");
                if (tmp) {
                    dump_cstring(out, tmp);
                    xmlFree(tmp);
                }
            }
        }
        xmlFree(id);
    }

    xmlFree(domain);

    xmlFreeDoc(doc);
}

int main(int argc, char **argv)
{
    int i;
    FILE *out;

    for (i=1;i<argc;i++) {
        char *name;

        name = g_strdup_printf("%s.h", argv[i]);
        out = fopen(name, "w");
        if (out == NULL) {
            fprintf(stderr, "Error creating %s: %s\n", name, strerror(errno));
            return 1;
        }
        extract_strings(out, argv[i]);
        if (fclose(out) != 0) {
            fprintf(stderr, "Error writing to %s: %s\n", name, strerror(errno));
            return 1;
        }
        g_free(name);
    }

    return 0;
}