Main Page   Modules   Compound List   File List   Compound Members   File Members   Related Pages  

wbxml2xml_tool.c

Go to the documentation of this file.
00001 /*
00002  * WBXML Lib, the WBXML Library.
00003  * Copyright (C) 2002-2003  Aymerick Jéhanne
00004  * 
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License (version 2.1) as published by the Free Software Foundation.
00008  * 
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  * LGPL v2.1: http://www.gnu.org/licenses/lgpl.txt
00019  *
00020  * Author Contact: libwbxml@jehanne.org
00021  * WBXML Lib home: http://libwbxml.jehanne.org
00022  */
00023  
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <memory.h>
00038 #include <string.h> /* for strcmp() */
00039 
00040 #include "getopt.h"
00041 
00042 #include "wbxml.h" /* libwbxml2 */
00043 
00044 
00045 #define INPUT_BUFFER_SIZE 1000
00046 
00047 
00048 static WBXMLLanguage get_lang(const WB_TINY *lang)
00049 {
00050 #if defined( WBXML_SUPPORT_WML )
00051     if (WBXML_STRCMP(lang, "WML10") == 0)
00052         return WBXML_LANG_WML10;
00053     if (WBXML_STRCMP(lang, "WML11") == 0)
00054         return WBXML_LANG_WML11;
00055     if (WBXML_STRCMP(lang, "WML12") == 0)
00056         return WBXML_LANG_WML12;
00057     if (WBXML_STRCMP(lang, "WML13") == 0)
00058         return WBXML_LANG_WML13;
00059 #endif /* WBXML_SUPPORT_WML */
00060 
00061 #if defined( WBXML_SUPPORT_WTA )
00062     if (WBXML_STRCMP(lang, "WTA10") == 0)
00063         return WBXML_LANG_WTA10;
00064     if (WBXML_STRCMP(lang, "WTAWML12") == 0)
00065         return WBXML_LANG_WTAWML12;
00066     if (WBXML_STRCMP(lang, "CHANNEL11") == 0)
00067         return WBXML_LANG_CHANNEL11;
00068     if (WBXML_STRCMP(lang, "CHANNEL12") == 0)
00069         return WBXML_LANG_CHANNEL12;
00070 #endif /* WBXML_SUPPORT_WTA */
00071 
00072 #if defined( WBXML_SUPPORT_SI )
00073     if (WBXML_STRCMP(lang, "SI10") == 0)
00074         return WBXML_LANG_SI10;
00075 #endif /* WBXML_SUPPORT_SI */
00076 
00077 #if defined( WBXML_SUPPORT_SL )
00078     if (WBXML_STRCMP(lang, "SL10") == 0)
00079         return WBXML_LANG_SL10;
00080 #endif /* WBXML_SUPPORT_SL */
00081 
00082 #if defined( WBXML_SUPPORT_CO )
00083     if (WBXML_STRCMP(lang, "CO10") == 0)
00084         return WBXML_LANG_CO10;
00085 #endif /* WBXML_SUPPORT_CO */
00086 
00087 #if defined( WBXML_SUPPORT_PROV )
00088     if (WBXML_STRCMP(lang, "PROV10") == 0)
00089         return WBXML_LANG_PROV10;
00090 #endif /* WBXML_SUPPORT_PROV */
00091 
00092 #if defined( WBXML_SUPPORT_EMN )
00093     if (WBXML_STRCMP(lang, "EMN10") == 0)
00094         return WBXML_LANG_EMN10;
00095 #endif /* WBXML_SUPPORT_EMN */
00096 
00097 #if defined( WBXML_SUPPORT_DRMREL )
00098     if (WBXML_STRCMP(lang, "DRMREL10") == 0)
00099         return WBXML_LANG_DRMREL10;
00100 #endif /* WBXML_SUPPORT_DRMREL */
00101 
00102 #if defined( WBXML_SUPPORT_OTA_SETTINGS )
00103     if (WBXML_STRCMP(lang, "OTA") == 0)
00104         return WBXML_LANG_OTA_SETTINGS;
00105 #endif /* WBXML_SUPPORT_OTA_SETTINGS */
00106 
00107 #if defined( WBXML_SUPPORT_SYNCML )
00108     if (WBXML_STRCMP(lang, "SYNCML10") == 0)
00109         return WBXML_LANG_SYNCML_SYNCML10;
00110     if (WBXML_STRCMP(lang, "DEVINF10") == 0)
00111         return WBXML_LANG_SYNCML_DEVINF10;
00112     if (WBXML_STRCMP(lang, "SYNCML11") == 0)
00113         return WBXML_LANG_SYNCML_SYNCML11;
00114     if (WBXML_STRCMP(lang, "DEVINF11") == 0)
00115         return WBXML_LANG_SYNCML_DEVINF11;
00116     if (WBXML_STRCMP(lang, "METINF11") == 0)
00117         return WBXML_LANG_SYNCML_METINF11;
00118 #endif /* WBXML_SUPPORT_SYNCML */
00119 
00120 #if defined( WBXML_SUPPORT_WV )
00121     if (WBXML_STRCMP(lang, "CSP11") == 0)
00122         return WBXML_LANG_WV_CSP11;
00123     if (WBXML_STRCMP(lang, "CSP12") == 0)
00124         return WBXML_LANG_WV_CSP12;
00125 #endif /* WBXML_SUPPORT_WV */
00126     
00127     return WBXML_LANG_UNKNOWN;
00128 }
00129 
00130 
00131 static void help(void) 
00132 {
00133     fprintf(stderr, "wbxml2xml [libwbxml2 %s] by Aymerick Jehanne\n", WBXML_LIB_VERSION);
00134     fprintf(stderr, "If you use this tool, please send feedbacks to libwbxml@jehanne.org\n");
00135     fprintf(stderr, "http://libwbxml.jehanne.org/\n\n");
00136     fprintf(stderr, "Usage: \n");
00137     fprintf(stderr, "  wbxml2xml -o output.xml input.wbxml\n");
00138     fprintf(stderr, "  wbxml2xml -i 4 -l CSP12 -o output.xml input.wbxml\n\n");
00139     fprintf(stderr, "Options: \n");
00140     fprintf(stderr, "    -o output.xml : output file\n");
00141     fprintf(stderr, "    -m X (Generation mode - Default: 1) with:\n");
00142     fprintf(stderr, "       0: Compact Generation\n");
00143     fprintf(stderr, "       1: Indent Generation\n");
00144     fprintf(stderr, "       2: Canonical Generation\n");
00145     fprintf(stderr, "    -i X (Indent delta when using mode '1' - Default: 1)\n");
00146     fprintf(stderr, "    -k (Keep Ignorable Whitespaces - Default: FALSE)\n");
00147     fprintf(stderr, "    -l X (Force Language Type of document to parse)\n");
00148 #if defined( WBXML_SUPPORT_WML )
00149     fprintf(stderr, "       WML10 : WML 1.0\n");
00150     fprintf(stderr, "       WML11 : WML 1.1\n");
00151     fprintf(stderr, "       WML12 : WML 1.2\n");
00152     fprintf(stderr, "       WML13 : WML 1.3\n");
00153 #endif /* WBXML_SUPPORT_WML */
00154 #if defined( WBXML_SUPPORT_WTA )
00155     fprintf(stderr, "       WTA10 : WTA 1.0\n");
00156     fprintf(stderr, "       WTAWML12 : WTAWML 1.2\n");
00157     fprintf(stderr, "       CHANNEL11 : CHANNEL 1.1\n");
00158     fprintf(stderr, "       CHANNEL12 : CHANNEL 1.2\n");
00159 #endif /* WBXML_SUPPORT_WTA */
00160 #if defined( WBXML_SUPPORT_SI )
00161     fprintf(stderr, "       SI10 : SL 1.0\n");
00162 #endif /* WBXML_SUPPORT_SI */
00163 #if defined( WBXML_SUPPORT_SL )
00164     fprintf(stderr, "       SL10 : SI 1.0\n");
00165 #endif /* WBXML_SUPPORT_SL */
00166 #if defined( WBXML_SUPPORT_CO )
00167     fprintf(stderr, "       CO10 : CO 1.0\n");
00168 #endif /* WBXML_SUPPORT_CO */
00169 #if defined( WBXML_SUPPORT_PROV )
00170     fprintf(stderr, "       PROV10 : PROV 1.0\n");
00171 #endif /* WBXML_SUPPORT_PROV */
00172 #if defined( WBXML_SUPPORT_EMN )
00173     fprintf(stderr, "       EMN10 : EMN 1.0\n");
00174 #endif /* WBXML_SUPPORT_EMN */
00175 #if defined( WBXML_SUPPORT_DRMREL )
00176     fprintf(stderr, "       DRMREL10 : DRMREL 1.0\n");
00177 #endif /* WBXML_SUPPORT_DRMREL */
00178 #if defined( WBXML_SUPPORT_OTA_SETTINGS )
00179     fprintf(stderr, "       OTA : OTA Settings\n");
00180 #endif /* WBXML_SUPPORT_OTA_SETTINGS */
00181 #if defined( WBXML_SUPPORT_SYNCML )
00182     fprintf(stderr, "       SYNCML10 : SYNCML 1.0\n");
00183     fprintf(stderr, "       DEVINF10 : DEVINF 1.0\n");
00184     fprintf(stderr, "       SYNCML11 : SYNCML 1.1\n");
00185     fprintf(stderr, "       DEVINF11 : DEVINF 1.1\n");
00186     fprintf(stderr, "       METINF11 : METINF 1.1\n");
00187 #endif /* WBXML_SUPPORT_SYNCML */
00188 #if defined( WBXML_SUPPORT_WV )
00189     fprintf(stderr, "       CSP11 : WV CSP 1.1\n");
00190     fprintf(stderr, "       CSP12 : WV CSP 1.2\n");
00191 #endif /* WBXML_SUPPORT_WV */
00192     fprintf(stderr, "\nNote: '-' can be used to mean stdin on input or stdout on output\n\n");
00193 }
00194 
00195 
00196 WB_LONG main(WB_LONG argc, WB_TINY **argv)
00197 {
00198     WB_UTINY *wbxml = NULL, *output = NULL, *xml = NULL;
00199     FILE *input_file = NULL, *output_file = NULL;    
00200     WB_LONG count = 0, wbxml_size = 0, total = 0;
00201     WB_TINY opt;
00202     WBXMLError ret = WBXML_OK;
00203     WB_UTINY input_buffer[INPUT_BUFFER_SIZE + 1];
00204     WBXMLConvWBXML2XMLParams params;
00205 
00206     /* Init Default Parameters */    
00207     params.lang = WBXML_LANG_UNKNOWN;
00208     params.gen_type = WBXML_ENCODER_XML_GEN_INDENT;
00209     params.indent = 1;
00210     params.keep_ignorable_ws = FALSE;
00211 
00212     while ((opt = getopt(argc, argv, "kh?o:m:i:l:")) != EOF) 
00213     {
00214         switch (opt) {
00215         case 'k':
00216             params.keep_ignorable_ws = TRUE;
00217             break;
00218         case 'i':
00219             params.indent = atoi((const WB_TINY*)optarg);
00220             break;
00221         case 'l':
00222             params.lang = get_lang((const WB_TINY*)optarg);
00223             break;
00224         case 'm':
00225             switch (atoi((const WB_TINY*)optarg)) {
00226             case 0:
00227                 params.gen_type = WBXML_ENCODER_XML_GEN_COMPACT;
00228                 break;
00229             case 1:
00230                 params.gen_type = WBXML_ENCODER_XML_GEN_INDENT;
00231                 break;
00232             case 2:
00233                 params.gen_type = WBXML_ENCODER_XML_GEN_CANONICAL;
00234                 break;
00235             default:
00236                 params.gen_type = WBXML_ENCODER_XML_GEN_INDENT;
00237             }
00238             break;
00239         case 'o':
00240             output = (WB_UTINY*) optarg;
00241             break;
00242         case 'h':
00243         case '?':
00244         default:
00245             help();
00246             return 0;
00247         }
00248     }
00249 
00250     if (optind >= argc) {
00251         fprintf(stderr, "Missing arguments\n");
00252         help();
00253         return 0;
00254     }
00255 
00256 #ifdef WBXML_USE_LEAKTRACKER
00257     lt_init_mem();
00258     lt_log_open_file("wbxml2xml.log");
00259     lt_log(0, "\n***************************\n Converting file: %s", argv[optind]);
00260 #endif
00261 
00262     /**********************************
00263      *  Read the WBXML Document
00264      */
00265 
00266     if (WBXML_STRCMP(argv[optind], "-") == 0) {
00267         input_file = stdin;
00268     } else {
00269         /* Open WBXML document */
00270         input_file = fopen(argv[optind], "rb");
00271         if (input_file == NULL) {
00272             fprintf(stderr, "Failed to open %s\n", argv[optind]);
00273             goto clean_up;
00274         }
00275     }
00276 
00277     /* Read WBXML document */
00278     while(!feof(input_file))    {
00279         count = fread(input_buffer, sizeof(WB_UTINY), INPUT_BUFFER_SIZE, input_file);
00280         if (ferror(input_file))      {
00281             fprintf(stderr, "Error while reading from file %s\n", argv[optind]);
00282             if (input_file != stdin)
00283                 fclose(input_file);
00284             if (wbxml != NULL)
00285                 wbxml_free(wbxml);
00286             goto clean_up;
00287         }
00288 
00289         total += count;
00290         wbxml = wbxml_realloc(wbxml, total);
00291         if (wbxml == NULL) {
00292             fprintf(stderr, "Not enought memory\n");
00293             if (input_file != stdin)
00294                 fclose(input_file);
00295             if (wbxml != NULL)
00296                 wbxml_free(wbxml);
00297             goto clean_up;
00298         }
00299 
00300         memcpy(wbxml + wbxml_size, input_buffer, count);
00301         wbxml_size += count;
00302     }
00303 
00304     if (input_file != stdin)
00305         fclose(input_file);
00306 
00307     /* Convert WBXML document */
00308     ret = wbxml_conv_wbxml2xml(wbxml, wbxml_size, &xml, &params);
00309     if (ret != WBXML_OK) {
00310         fprintf(stderr, "wbxml2xml failed: %s\n", wbxml_errors_string(ret));
00311     }
00312     else {
00313         fprintf(stderr, "wbxml2xml succeded: \n%s\n", xml);
00314 
00315         if (output != NULL) {
00316             if (WBXML_STRCMP(output, "-") == 0) {
00317                 output_file = stdout;
00318             } else {            
00319                 /* Open Output File */
00320                 output_file = fopen((const WB_TINY*) output, "w");
00321             }
00322             
00323             if (output_file == NULL) {
00324                 fprintf(stderr, "Failed to open output file: %s\n", output);
00325             }
00326 
00327             /* Write to Output File */
00328             fprintf(output_file, (WB_TINY*) xml);
00329             if (output_file != stdout)
00330                 fclose(output_file);
00331         }
00332 
00333         /* Clean-up */
00334         wbxml_free(xml);
00335     }
00336 
00337     wbxml_free(wbxml);
00338 
00339 clean_up:
00340 
00341 #ifdef WBXML_USE_LEAKTRACKER
00342     lt_check_leaks();
00343     lt_shutdown_mem();
00344     lt_log_close_file();
00345 #endif
00346 
00347     return 0;
00348 }

Generated on Mon Nov 24 20:09:42 2003 for WBXML Library by doxygen1.3-rc1