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
|
#include "hexml.h"
#include <stdio.h>
#include <malloc.h>
#include <time.h>
#include <string.h>
#include <assert.h>
char* readFile(char* file)
{
FILE* f = fopen(file, "rb");
fseek(f, 0, SEEK_END);
int len = ftell(f);
fseek(f, 0, SEEK_SET);
char* res = malloc(len + 1);
assert(res);
res[len] = 0;
size_t n = fread(res, 1, len, f);
assert(n == len);
fclose(f);
return res;
}
char* example = "<?xml version=\"1.0\" lang=\"ja\"?><foo><bar baz=\"qux\">quux</bar><bar baz=\"bar\">hoge</bar><piyo>>piyopiyo<</piyo></foo><foo2 />";
int main(int argc, char **argv)
{
setbuf(stdout, NULL);
char* body = argv[1] == NULL ? example : readFile(argv[1]);
document* doc = hexml_document_parse(body, -1);
char* err = hexml_document_error(doc);
if (err == NULL)
{
int len = hexml_node_render(doc, hexml_document_node(doc), NULL, 0);
char* s = malloc(len + 1);
assert(s);
hexml_node_render(doc, hexml_document_node(doc), s, len);
s[len] = 0;
printf("Parse successful\n"); // , %s\n", s);
return 0;
}
else
{
printf("Parse failed, %s\n", err);
return 1;
}
}
|