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
|
#include <assert.h> #include <ctype.h> #include <err.h> #include <expat.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>
#define BUFFER_SIZE (1 << 16)
static XML_Char **column_names; static size_t num_column_names; static XML_Char **first_row_values; static size_t num_first_row_values; static XML_Char *elem_text; static size_t elem_text_len; static int first_column; static int reading_value;
static void handle_elem_start(void *data, const XML_Char *el, const XML_Char **attr); static void handle_elem_text(void *userData, const XML_Char *s, int len); static void handle_elem_end(void *data, const XML_Char *el);
static void output_csv_row(XML_Char **values, size_t num); static void output_csv_text(const char *s, size_t len); static void add_string(XML_Char ***arrayp, size_t *lengthp, const XML_Char *string, size_t len); static void add_chars(XML_Char **strp, size_t *lenp, const XML_Char *string, size_t nchars); static size_t xml_strlen(const XML_Char *string); static void free_strings(XML_Char ***arrayp, size_t *lengthp); static void usage(void);
int main(int argc, char **argv) { char buf[BUFFER_SIZE]; int want_column_names = 1; XML_Parser p; FILE *fp; size_t r; int i;
while ((i = getopt(argc, argv, "hN")) != -1) { switch (i) { case 'N': want_column_names = 0; break; case 'h': usage(); exit(0); case '?': default: usage(); exit(1); } } argv += optind; argc -= optind; switch (argc) { case 0: fp = stdin; break; case 1: if ((fp = fopen(argv[0], "r")) == NULL) err(1, "%s", argv[0]); break; default: usage(); exit(1); }
if (want_column_names) { if ((column_names = malloc(10 * sizeof(*column_names))) == NULL) err(1, "malloc"); if ((first_row_values = malloc(10 * sizeof(*first_row_values))) == NULL) err(1, "malloc"); }
if ((p = XML_ParserCreate(NULL)) == NULL) errx(1, "can't initialize parser"); XML_SetElementHandler(p, handle_elem_start, handle_elem_end); XML_SetCharacterDataHandler(p, handle_elem_text);
while (1) { if ((r = fread(buf, 1, sizeof(buf), fp)) == 0 && ferror(fp)) errx(1, "error reading input"); if (XML_Parse(p, buf, r, r == 0) == XML_STATUS_ERROR) errx(1, "line %u: %s", (unsigned int)XML_GetCurrentLineNumber(p), XML_ErrorString(XML_GetErrorCode(p))); if (r == 0) break; }
XML_ParserFree(p); fclose(fp);
return 0; }
static void handle_elem_start(void *data, const XML_Char *name, const XML_Char **attr) { if (strcmp(name, "row") == 0) first_column = 1; else if (strcmp(name, "field") == 0) { if (column_names != NULL) { while (*attr != NULL && strcmp(*attr, "name") != 0) attr += 2; if (*attr == NULL) errx(1, "\"field\" element is missing \"name\" attribute"); add_string(&column_names, &num_column_names, attr[1], xml_strlen(attr[1])); } else { if (!first_column) putchar(','); putchar('"'); } reading_value = 1; } }
static void handle_elem_text(void *userData, const XML_Char *s, int len) { if (!reading_value) return; if (column_names != NULL) add_chars(&elem_text, &elem_text_len, s, len); else output_csv_text(s, len); }
static void handle_elem_end(void *data, const XML_Char *name) { if (strcmp(name, "row") == 0) { if (column_names != NULL) { output_csv_row(column_names, num_column_names); output_csv_row(first_row_values, num_first_row_values); free_strings(&column_names, &num_column_names); free_strings(&first_row_values, &num_first_row_values); } else putchar('\n'); } else if (strcmp(name, "field") == 0) { if (column_names != NULL) { add_string(&first_row_values, &num_first_row_values, elem_text, elem_text_len); free(elem_text); elem_text = NULL; elem_text_len = 0; } else putchar('"'); first_column = 0; reading_value = 0; } }
static void output_csv_row(XML_Char **values, size_t num_columns) { int i;
for (i = 0; i < num_columns; i++) { if (i > 0) putchar(','); putchar('"'); output_csv_text(values[i], xml_strlen(values[i])); putchar('"'); } putchar('\n'); }
static void output_csv_text(const XML_Char *s, size_t len) { while (len-- > 0) { if (*s == '"') putchar('"'); putchar(*s); s++; } }
static void add_string(XML_Char ***arrayp, size_t *lengthp, const XML_Char *string, size_t nchars) { char **new_array;
if ((new_array = realloc(*arrayp, (*lengthp + 1) * sizeof(**arrayp))) == NULL) err(1, "malloc"); *arrayp = new_array; if (((*arrayp)[*lengthp] = malloc((nchars + 1) * sizeof(XML_Char))) == NULL) err(1, "malloc"); memcpy((*arrayp)[*lengthp], string, nchars * sizeof(XML_Char)); (*arrayp)[*lengthp][nchars] = (XML_Char)0; (*lengthp)++; }
static void add_chars(XML_Char **strp, size_t *lenp, const XML_Char *string, size_t nchars) { XML_Char *new_array;
if ((new_array = realloc(*strp, (*lenp + nchars) * sizeof(XML_Char))) == NULL) err(1, "malloc"); *strp = new_array; memcpy(*strp + *lenp, string, nchars * sizeof(XML_Char)); *lenp += nchars; }
static size_t xml_strlen(const XML_Char *string) { size_t len;
len = 0; while (string[len] != (XML_Char)0) len++; return len; }
static void free_strings(char ***arrayp, size_t *lengthp) { while (*lengthp > 0) free((*arrayp)[--*lengthp]); free(*arrayp); *arrayp = NULL; }
static void usage(void) { fprintf(stderr, "Usage: mysql-xml-to-csv [options] [file.xml]\n"); fprintf(stderr, "Options:\n"); fprintf(stderr, " -N\tDo not output column names as the first row\n"); fprintf(stderr, " -h\tShow this usage info\n"); }
|