00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <memory.h>
00038 #include <string.h>
00039
00040 #include "getopt.h"
00041
00042 #include "wbxml.h"
00043
00044
00045 #define INPUT_BUFFER_SIZE 5000
00046
00047
00048 static WBXMLVersion get_version(const WB_TINY *lang)
00049 {
00050 if (WBXML_STRCMP(lang, "1.0") == 0)
00051 return WBXML_VERSION_10;
00052 if (WBXML_STRCMP(lang, "1.1") == 0)
00053 return WBXML_VERSION_11;
00054 if (WBXML_STRCMP(lang, "1.2") == 0)
00055 return WBXML_VERSION_12;
00056 if (WBXML_STRCMP(lang, "1.3") == 0)
00057 return WBXML_VERSION_13;
00058
00059 return WBXML_VERSION_UNKNOWN;
00060 }
00061
00062
00063 static void help(void) {
00064 fprintf(stderr, "xml2wbxml [libwbxml2 %s] by Aymerick Jehanne\n", WBXML_LIB_VERSION);
00065 fprintf(stderr, "If you use this tool, please send feedbacks to libwbxml@jehanne.org\n");
00066 fprintf(stderr, "http://libwbxml.jehanne.org/\n\n");
00067 fprintf(stderr, "Usage: \n");
00068 fprintf(stderr, " xml2wbxml -o output.wbxml input.xml\n");
00069 fprintf(stderr, " xml2wbxml -k -n -v 1.1 -o output.wbxml input.xml\n\n");
00070 fprintf(stderr, "Options: \n");
00071 fprintf(stderr, " -o output.wbxml : output file\n");
00072 fprintf(stderr, " -k : keep ignorable whitespaces (Default: ignore)\n");
00073 fprintf(stderr, " -n : do NOT generate String Table (Default: generate)\n");
00074 fprintf(stderr, " -v X (WBXML Version of output document)\n");
00075 fprintf(stderr, " 1.0 : WBXML 1.0\n");
00076 fprintf(stderr, " 1.1 : WBXML 1.1\n");
00077 fprintf(stderr, " 1.2 : WBXML 1.2\n");
00078 fprintf(stderr, " 1.3 : WBXML 1.3\n");
00079 fprintf(stderr, "\nNote: '-' can be used to mean stdin on input or stdout on output\n\n");
00080 }
00081
00082
00083 WB_LONG main(WB_LONG argc, WB_TINY **argv)
00084 {
00085 WB_UTINY *wbxml = NULL, *output = NULL, *xml = NULL;
00086 FILE *input_file = NULL, *output_file = NULL;
00087 WB_ULONG wbxml_len = 0;
00088 WB_LONG count = 0, xml_size = 0, total = 0;
00089 WB_TINY opt;
00090 WBXMLError ret = WBXML_OK;
00091 WB_UTINY input_buffer[INPUT_BUFFER_SIZE + 1];
00092 WBXMLConvXML2WBXMLParams params;
00093
00094
00095 params.wbxml_version = WBXML_VERSION_13;
00096 params.use_strtbl = TRUE;
00097 params.keep_ignorable_ws = FALSE;
00098
00099 while ((opt = getopt(argc, argv, "nkh?o:v:")) != EOF)
00100 {
00101 switch (opt) {
00102 case 'v':
00103 params.wbxml_version = get_version((const WB_TINY*)optarg);
00104 break;
00105 case 'n':
00106 params.use_strtbl = FALSE;
00107 break;
00108 case 'k':
00109 params.keep_ignorable_ws = TRUE;
00110 break;
00111 case 'o':
00112 output = (WB_UTINY*) optarg;
00113 break;
00114 case 'h':
00115 case '?':
00116 default:
00117 help();
00118 return 0;
00119 }
00120 }
00121
00122 if (optind >= argc) {
00123 fprintf(stderr, "Missing arguments\n");
00124 help();
00125 return 0;
00126 }
00127
00128 #ifdef WBXML_USE_LEAKTRACKER
00129 lt_init_mem();
00130 lt_log_open_file("xml2wbxml.log");
00131 lt_log(0, "\n***************************\n Converting file: %s", argv[optind]);
00132 #endif
00133
00134
00135
00136
00137
00138 if (WBXML_STRCMP(argv[optind], "-") == 0) {
00139 input_file = stdin;
00140 } else {
00141
00142 input_file = fopen(argv[optind], "r");
00143 if (input_file == NULL) {
00144 printf("Failed to open %s\n", argv[optind]);
00145 goto clean_up;
00146 }
00147 }
00148
00149
00150 while(!feof(input_file)) {
00151 count = fread(input_buffer, sizeof(WB_UTINY), INPUT_BUFFER_SIZE, input_file);
00152 if (ferror(input_file)) {
00153 fprintf(stderr, "Error while reading from file %s\n", argv[1]);
00154 if (input_file != stdin)
00155 fclose(input_file);
00156 if (xml != NULL)
00157 wbxml_free(xml);
00158 goto clean_up;
00159 }
00160
00161 total += count;
00162 xml = wbxml_realloc(xml, total + 1);
00163 if (xml == NULL) {
00164 fprintf(stderr, "Not enought memory\n");
00165 if (input_file != stdin)
00166 fclose(input_file);
00167 goto clean_up;
00168 }
00169
00170 memcpy(xml + xml_size, input_buffer, count);
00171 xml_size += count;
00172 }
00173
00174 if (input_file != stdin)
00175 fclose(input_file);
00176
00177 xml[xml_size] = '\0';
00178
00179
00180 ret = wbxml_conv_xml2wbxml(xml, &wbxml, &wbxml_len, ¶ms);
00181 if (ret != WBXML_OK) {
00182 fprintf(stderr, "xml2wbxml failed: %s\n", wbxml_errors_string(ret));
00183 }
00184 else {
00185 fprintf(stderr, "xml2wbxml succeded\n");
00186
00187 if (output != NULL) {
00188 if (WBXML_STRCMP(output, "-") == 0) {
00189 output_file = stdout;
00190 }
00191 else {
00192
00193 output_file = fopen((const WB_TINY*) output, "wb");
00194 }
00195
00196 if (output_file == NULL) {
00197 fprintf(stderr, "Failed to open output file: %s\n", output);
00198 }
00199 else {
00200
00201 if (fwrite(wbxml, sizeof(WB_UTINY), wbxml_len, output_file) < wbxml_len)
00202 fprintf(stderr, "Error while writing to file: %s\n", output);
00203 else
00204 fprintf(stderr, "Written %u bytes to file: %s\n", wbxml_len, output);
00205
00206 if (output_file != stdout)
00207 fclose(output_file);
00208 }
00209 }
00210
00211
00212 if (wbxml != NULL)
00213 wbxml_free(wbxml);
00214 }
00215
00216 if (xml != NULL)
00217 wbxml_free(xml);
00218
00219 clean_up:
00220
00221 #ifdef WBXML_USE_LEAKTRACKER
00222 lt_check_leaks();
00223 lt_shutdown_mem();
00224 lt_log_close_file();
00225 #endif
00226
00227 return 0;
00228 }