summaryrefslogtreecommitdiff
path: root/app/help/genhelp.c
blob: be20ff492d8f9e512084422868eebbc955f2bda8 (plain)
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

/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2018 Martin Fischer
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "cJSON.h"

#define I18NHEADERFILE "i18n.h"

typedef enum { BALLOONHELP, BALLOONHELPI18N } mode_e;

int process(mode_e mode, char * json, FILE * outFile)
{
    const cJSON *messages = NULL;
    const cJSON *messageLine = NULL;
    int status = 0;

    cJSON *message_json = cJSON_Parse(json);
    if (message_json == NULL) {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL) {
            fprintf(stderr, "Error before: %s\n", error_ptr);
        }
        status = 0;
        goto end;
    }

    fputs("/*\n * DO NOT EDIT! This file has been automatically created by genhelp.\n * Changes to this file will be overwritten.\n */\n",
          outFile);
    fprintf(outFile, "#include <stdio.h>\n");
    fprintf(outFile, "#include \"wlib.h\"\n");
    if (mode == BALLOONHELPI18N) {
        fprintf(outFile, "#include \"" I18NHEADERFILE "\"\n");
    }

    fprintf(outFile, "wBalloonHelp_t balloonHelp[] = {\n");


    messages = cJSON_GetObjectItemCaseSensitive(message_json, "messages");
    cJSON_ArrayForEach(messageLine, messages) {
        cJSON *line = cJSON_GetObjectItemCaseSensitive(messageLine, "line");
        cJSON *contents = cJSON_GetObjectItemCaseSensitive(messageLine, "contents");

        if (!cJSON_IsString(line) || !cJSON_IsString(contents)) {
            status = 0;
            goto end;
        }

        if (contents->valuestring != NULL) {
            if (mode == BALLOONHELP) {
                fprintf(outFile, "\t{ \"%s\", \"%s\" },\n", line->valuestring,
                        contents->valuestring);
            } else {
                if (contents->valuestring[0]) {
                    fprintf(outFile, "\t{ \"%s\", N_(\"%s\") },\n", line->valuestring,
                            contents->valuestring);
                } else {
                    fprintf(outFile, "\t{ \"%s\", \"\" },\n", line->valuestring);
                }
            }
        } else {
            fprintf(outFile, "\t{ \"%s\", \"No help\" },\n", line->valuestring);
            fprintf(stderr, "INFO: %s has an empty help text\n", line->valuestring);
        }
    }

    fprintf(outFile, "\t{ NULL, NULL } };\n");
end:
    cJSON_Delete(message_json);
    return status;
}


int main(int argc, char * argv[])
{
    FILE * inFile, * outFile;
    char *jsonData;

    mode_e mode;
    if (argc != 4) {
        fprintf(stderr, "Usage: %s (-bh|-bhi) JSONFILE OUTFILE\n", argv[0]);
        exit(1);
    }

    if (strcmp(argv[1], "-bh") == 0) {
        mode = BALLOONHELP;
    } else if (strcmp(argv[1], "-bhi") == 0) {
        mode = BALLOONHELPI18N;
    } else {
        fprintf(stderr, "Bad mode: %s\n", argv[1]);
        exit(1);
    }

    inFile = fopen(argv[2], "r");
    if (inFile == NULL) {
        perror(argv[2]);
        exit(1);
    }

    if (inFile) {
        unsigned int length;
        fseek(inFile, 0, SEEK_END);
        length = ftell(inFile);
        fseek(inFile, 0, SEEK_SET);
        jsonData = malloc(length + 1);
        if (jsonData) {
            fread(jsonData, 1, length, inFile);
            jsonData[length] = '\0';
        }
        fclose(inFile);
    }

    outFile = fopen(argv[3], "w");
    if (outFile == NULL) {
        perror(argv[3]);
        exit(1);
    }

    int ret = process(mode, jsonData, outFile);
    exit(ret);
}