File: wbxml2xml_tool.c

package info (click to toggle)
wbxml2 0.9.2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,712 kB
  • ctags: 2,793
  • sloc: ansic: 21,545; makefile: 134; cpp: 7; sh: 6
file content (382 lines) | stat: -rw-r--r-- 12,576 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * libwbxml, the WBXML Library.
 * Copyright (C) 2002-2005 Aymerick Jehanne <aymerick@jehanne.org>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * LGPL v2.1: http://www.gnu.org/copyleft/lesser.txt
 * 
 * Contact: libwbxml@aymerick.com
 * Home: http://libwbxml.aymerick.com
 */

/**
 * @file wbxml2xml_tool.c
 * @ingroup wbxml2xml_tool
 *
 * @author Aymerick Jhanne <libwbxml@aymerick.com>
 * @date 03/02/22
 *
 * @brief WBXML to XML Converter Tool
 *
 */

#if !defined ( __SYMBIAN32__ )
#include <memory.h>
#endif /* ! __SYMBIAN32__ */

#include "wbxml.h" /* libwbxml2 */
#include "getopt.h"

/*
 * This sytem include is here instead of the *.c files because
 * we want it to be included AFTER 'e32def.h' (in 'wbxml.h') on Symbian.
 * If not so, Warnings are displayed ('NULL' : macro redefinition)
 */
#include <stdio.h>


#define INPUT_BUFFER_SIZE 1000


static WBXMLLanguage get_lang(const WB_TINY *lang)
{
#if defined( WBXML_SUPPORT_WML )
    if (WBXML_STRCMP(lang, "WML10") == 0)
        return WBXML_LANG_WML10;
    if (WBXML_STRCMP(lang, "WML11") == 0)
        return WBXML_LANG_WML11;
    if (WBXML_STRCMP(lang, "WML12") == 0)
        return WBXML_LANG_WML12;
    if (WBXML_STRCMP(lang, "WML13") == 0)
        return WBXML_LANG_WML13;
#endif /* WBXML_SUPPORT_WML */

#if defined( WBXML_SUPPORT_WTA )
    if (WBXML_STRCMP(lang, "WTA10") == 0)
        return WBXML_LANG_WTA10;
    if (WBXML_STRCMP(lang, "WTAWML12") == 0)
        return WBXML_LANG_WTAWML12;
    if (WBXML_STRCMP(lang, "CHANNEL11") == 0)
        return WBXML_LANG_CHANNEL11;
    if (WBXML_STRCMP(lang, "CHANNEL12") == 0)
        return WBXML_LANG_CHANNEL12;
#endif /* WBXML_SUPPORT_WTA */

#if defined( WBXML_SUPPORT_SI )
    if (WBXML_STRCMP(lang, "SI10") == 0)
        return WBXML_LANG_SI10;
#endif /* WBXML_SUPPORT_SI */

#if defined( WBXML_SUPPORT_SL )
    if (WBXML_STRCMP(lang, "SL10") == 0)
        return WBXML_LANG_SL10;
#endif /* WBXML_SUPPORT_SL */

#if defined( WBXML_SUPPORT_CO )
    if (WBXML_STRCMP(lang, "CO10") == 0)
        return WBXML_LANG_CO10;
#endif /* WBXML_SUPPORT_CO */

#if defined( WBXML_SUPPORT_PROV )
    if (WBXML_STRCMP(lang, "PROV10") == 0)
        return WBXML_LANG_PROV10;
#endif /* WBXML_SUPPORT_PROV */

#if defined( WBXML_SUPPORT_EMN )
    if (WBXML_STRCMP(lang, "EMN10") == 0)
        return WBXML_LANG_EMN10;
#endif /* WBXML_SUPPORT_EMN */

#if defined( WBXML_SUPPORT_DRMREL )
    if (WBXML_STRCMP(lang, "DRMREL10") == 0)
        return WBXML_LANG_DRMREL10;
#endif /* WBXML_SUPPORT_DRMREL */

#if defined( WBXML_SUPPORT_OTA_SETTINGS )
    if (WBXML_STRCMP(lang, "OTA") == 0)
        return WBXML_LANG_OTA_SETTINGS;
#endif /* WBXML_SUPPORT_OTA_SETTINGS */

#if defined( WBXML_SUPPORT_SYNCML )
    if (WBXML_STRCMP(lang, "SYNCML10") == 0)
        return WBXML_LANG_SYNCML_SYNCML10;
    if (WBXML_STRCMP(lang, "DEVINF10") == 0)
        return WBXML_LANG_SYNCML_DEVINF10;
    if (WBXML_STRCMP(lang, "SYNCML11") == 0)
        return WBXML_LANG_SYNCML_SYNCML11;
    if (WBXML_STRCMP(lang, "DEVINF11") == 0)
        return WBXML_LANG_SYNCML_DEVINF11;
    if (WBXML_STRCMP(lang, "METINF11") == 0)
        return WBXML_LANG_SYNCML_METINF11;
    if (WBXML_STRCMP(lang, "SYNCML12") == 0)
        return WBXML_LANG_SYNCML_SYNCML12;
    if (WBXML_STRCMP(lang, "DEVINF12") == 0)
        return WBXML_LANG_SYNCML_DEVINF12;
    if (WBXML_STRCMP(lang, "METINF12") == 0)
        return WBXML_LANG_SYNCML_METINF12;
#endif /* WBXML_SUPPORT_SYNCML */

#if defined( WBXML_SUPPORT_WV )
    if (WBXML_STRCMP(lang, "CSP11") == 0)
        return WBXML_LANG_WV_CSP11;
    if (WBXML_STRCMP(lang, "CSP12") == 0)
        return WBXML_LANG_WV_CSP12;
#endif /* WBXML_SUPPORT_WV */

#if defined( WBXML_SUPPORT_AIRSYNC )
    if (WBXML_STRCMP(lang, "AIRSYNC") == 0)
        return WBXML_LANG_AIRSYNC;
#endif /* WBXML_SUPPORT_AIRSYNC */

    return WBXML_LANG_UNKNOWN;
}


static void help(void)
{
    fprintf(stderr, "wbxml2xml [libwbxml %s] by Aymerick Jehanne <aymerick@jehanne.org>\n", WBXML_LIB_VERSION);
    fprintf(stderr, "If you use this tool, please send feedbacks to libwbxml@aymerick.com\n");
    fprintf(stderr, "http://libwbxml.aymerick.com/\n");
#if defined( HAVE_EXPAT )
    fprintf(stderr, "This tool is linked with Expat (http://expat.sourceforge.net)\n\n");
#endif /* HAVE_EXPAT */
    fprintf(stderr, "Usage: \n");
    fprintf(stderr, "  wbxml2xml -o output.xml input.wbxml\n");
    fprintf(stderr, "  wbxml2xml -i 4 -l CSP12 -o output.xml input.wbxml\n\n");
    fprintf(stderr, "Options: \n");
    fprintf(stderr, "    -o output.xml : output file\n");
    fprintf(stderr, "    -m X (Generation mode - Default: 1) with:\n");
    fprintf(stderr, "       0: Compact Generation\n");
    fprintf(stderr, "       1: Indent Generation\n");
    fprintf(stderr, "       2: Canonical Generation\n");
    fprintf(stderr, "    -i X (Indent delta when using mode '1' - Default: 1)\n");
    fprintf(stderr, "    -k (Keep Ignorable Whitespaces - Default: FALSE)\n");
    fprintf(stderr, "    -l X (Force Language Type of document to parse)\n");
#if defined( WBXML_SUPPORT_WML )
    fprintf(stderr, "       WML10 : WML 1.0\n");
    fprintf(stderr, "       WML11 : WML 1.1\n");
    fprintf(stderr, "       WML12 : WML 1.2\n");
    fprintf(stderr, "       WML13 : WML 1.3\n");
#endif /* WBXML_SUPPORT_WML */
#if defined( WBXML_SUPPORT_WTA )
    fprintf(stderr, "       WTA10 : WTA 1.0\n");
    fprintf(stderr, "       WTAWML12 : WTAWML 1.2\n");
    fprintf(stderr, "       CHANNEL11 : CHANNEL 1.1\n");
    fprintf(stderr, "       CHANNEL12 : CHANNEL 1.2\n");
#endif /* WBXML_SUPPORT_WTA */
#if defined( WBXML_SUPPORT_SI )
    fprintf(stderr, "       SI10 : SI 1.0\n");
#endif /* WBXML_SUPPORT_SI */
#if defined( WBXML_SUPPORT_SL )
    fprintf(stderr, "       SL10 : SL 1.0\n");
#endif /* WBXML_SUPPORT_SL */
#if defined( WBXML_SUPPORT_CO )
    fprintf(stderr, "       CO10 : CO 1.0\n");
#endif /* WBXML_SUPPORT_CO */
#if defined( WBXML_SUPPORT_PROV )
    fprintf(stderr, "       PROV10 : PROV 1.0\n");
#endif /* WBXML_SUPPORT_PROV */
#if defined( WBXML_SUPPORT_EMN )
    fprintf(stderr, "       EMN10 : EMN 1.0\n");
#endif /* WBXML_SUPPORT_EMN */
#if defined( WBXML_SUPPORT_DRMREL )
    fprintf(stderr, "       DRMREL10 : DRMREL 1.0\n");
#endif /* WBXML_SUPPORT_DRMREL */
#if defined( WBXML_SUPPORT_OTA_SETTINGS )
    fprintf(stderr, "       OTA : OTA Settings\n");
#endif /* WBXML_SUPPORT_OTA_SETTINGS */
#if defined( WBXML_SUPPORT_SYNCML )
    fprintf(stderr, "       SYNCML10 : SYNCML 1.0\n");
    fprintf(stderr, "       DEVINF10 : DEVINF 1.0\n");
    fprintf(stderr, "       SYNCML11 : SYNCML 1.1\n");
    fprintf(stderr, "       DEVINF11 : DEVINF 1.1\n");
    fprintf(stderr, "       METINF11 : METINF 1.1\n");
    fprintf(stderr, "       SYNCML12 : SYNCML 1.2\n");
    fprintf(stderr, "       DEVINF12 : DEVINF 1.2\n");
    fprintf(stderr, "       METINF12 : METINF 1.2\n");
#endif /* WBXML_SUPPORT_SYNCML */
#if defined( WBXML_SUPPORT_WV )
    fprintf(stderr, "       CSP11 : WV CSP 1.1\n");
    fprintf(stderr, "       CSP12 : WV CSP 1.2\n");
#endif /* WBXML_SUPPORT_WV */
#if defined( WBXML_SUPPORT_AIRSYNC )
    fprintf(stderr, "       AIRSYNC : Microsoft AirSync\n");
#endif /* WBXML_SUPPORT_AIRSYNC */
    fprintf(stderr, "\nNote: '-' can be used to mean stdin on input or stdout on output\n\n");
}


WB_LONG main(WB_LONG argc, WB_TINY **argv)
{
    WB_UTINY *wbxml = NULL, *output = NULL, *xml = NULL;
    FILE *input_file = NULL, *output_file = NULL;
    WB_LONG count = 0, wbxml_len = 0, total = 0;
    WB_ULONG xml_len = 0;
    WB_TINY opt;
    WBXMLError ret = WBXML_OK;
    WB_UTINY input_buffer[INPUT_BUFFER_SIZE + 1];
    WBXMLGenXMLParams params;

    /* Init Default Parameters */
    params.lang = WBXML_LANG_UNKNOWN;
    params.gen_type = WBXML_GEN_XML_INDENT;
    params.indent = 1;
    params.keep_ignorable_ws = FALSE;

    while ((opt = (WB_TINY) getopt(argc, argv, "kh?o:m:i:l:")) != EOF)
    {
        switch (opt) {
        case 'k':
            params.keep_ignorable_ws = TRUE;
            break;
        case 'i':
            params.indent = (WB_UTINY) atoi((const WB_TINY*)optarg);
            break;
        case 'l':
            params.lang = get_lang((const WB_TINY*)optarg);
            break;
        case 'm':
            switch (atoi((const WB_TINY*)optarg)) {
            case 0:
                params.gen_type = WBXML_GEN_XML_COMPACT;
                break;
            case 1:
                params.gen_type = WBXML_GEN_XML_INDENT;
                break;
            case 2:
                params.gen_type = WBXML_GEN_XML_CANONICAL;
                break;
            default:
                params.gen_type = WBXML_GEN_XML_INDENT;
            }
            break;
        case 'o':
            output = (WB_UTINY*) optarg;
            break;
        case 'h':
        case '?':
        default:
            help();
            return 0;
        }
    }

    if (optind >= argc) {
        fprintf(stderr, "Missing arguments\n");
        help();
        return 0;
    }

#ifdef WBXML_USE_LEAKTRACKER
    lt_init_mem();
    lt_log_open_file("wbxml2xml.log");
    lt_log(0, "\n***************************\n Converting file: %s", argv[optind]);
#endif

    /**********************************
     *  Read the WBXML Document
     */

    if (WBXML_STRCMP(argv[optind], "-") == 0) {
        input_file = stdin;
    } else {
        /* Open WBXML document */
        input_file = fopen(argv[optind], "rb");
        if (input_file == NULL) {
            fprintf(stderr, "Failed to open %s\n", argv[optind]);
            goto clean_up;
        }
    }

    /* Read WBXML document */
    while(!feof(input_file))    {
        count = fread(input_buffer, sizeof(WB_UTINY), INPUT_BUFFER_SIZE, input_file);
        if (ferror(input_file))      {
            fprintf(stderr, "Error while reading from file %s\n", argv[optind]);
            if (input_file != stdin)
                fclose(input_file);
            if (wbxml != NULL)
                wbxml_free(wbxml);
            goto clean_up;
        }

        total += count;
        wbxml = wbxml_realloc(wbxml, total);
        if (wbxml == NULL) {
            fprintf(stderr, "Not enought memory\n");
            if (input_file != stdin)
                fclose(input_file);
            if (wbxml != NULL)
                wbxml_free(wbxml);
            goto clean_up;
        }

        memcpy(wbxml + wbxml_len, input_buffer, count);
        wbxml_len += count;
    }

    if (input_file != stdin)
        fclose(input_file);

    /* Convert WBXML document */
    ret = wbxml_conv_wbxml2xml_withlen(wbxml, wbxml_len, &xml, &xml_len, &params);
    if (ret != WBXML_OK) {
        fprintf(stderr, "wbxml2xml failed: %s\n", wbxml_errors_string(ret));
    }
    else {
        /* fprintf(stderr, "wbxml2xml succeded: \n%s\n", xml); */
        fprintf(stderr, "wbxml2xml succeded\n");

        if (output != NULL) {
            if (WBXML_STRCMP(output, "-") == 0) {
                output_file = stdout;
            } else {
                /* Open Output File */
                output_file = fopen((const WB_TINY*) output, "w");
            }

            if (output_file == NULL) {
                fprintf(stderr, "Failed to open output file: %s\n", output);
            }

            /* Write to Output File */
            if (fwrite(xml, sizeof(WB_UTINY), xml_len, output_file) < xml_len)
                fprintf(stderr, "Error while writing to file: %s\n", output);
            /*
            else
                fprintf(stderr, "Written %u bytes to file: %s\n", xml_len, output);
            */

            if (output_file != stdout)
                fclose(output_file);
        }

        /* Clean-up */
        wbxml_free(xml);
    }

    wbxml_free(wbxml);

clean_up:

#ifdef WBXML_USE_LEAKTRACKER
    lt_check_leaks();
    lt_shutdown_mem();
    lt_log_close_file();
#endif

    return 0;
}