on master help sign in
46 lines 1.2 kB history raw
1#include "hexml.h"
2#include <stdio.h>
3#include <malloc.h>
4#include <time.h>
5#include <string.h>
6#include <assert.h>
8char* readFile(char* file)
9{
10 FILE* f = fopen(file, "rb");
11 fseek(f, 0, SEEK_END);
12 int len = ftell(f);
13 fseek(f, 0, SEEK_SET);
14 char* res = malloc(len + 1);
15 assert(res);
16 res[len] = 0;
17 size_t n = fread(res, 1, len, f);
18 assert(n == len);
19 fclose(f);
20 return res;
23char* example = "<?xml version=\"1.0\" lang=\"ja\"?><foo><bar baz=\"qux\">quux</bar><bar baz=\"bar\">hoge</bar><piyo>&gt;piyopiyo&lt;</piyo></foo><foo2 />";
25int main(int argc, char **argv)
27 setbuf(stdout, NULL);
28 char* body = argv[1] == NULL ? example : readFile(argv[1]);
29 document* doc = hexml_document_parse(body, -1);
30 char* err = hexml_document_error(doc);
31 if (err == NULL)
32 {
33 int len = hexml_node_render(doc, hexml_document_node(doc), NULL, 0);
34 char* s = malloc(len + 1);
35 assert(s);
36 hexml_node_render(doc, hexml_document_node(doc), s, len);
37 s[len] = 0;
38 printf("Parse successful\n"); // , %s\n", s);
39 return 0;
40 }
41 else
42 {
43 printf("Parse failed, %s\n", err);
44 return 1;
45 }