File: main.c

package info (click to toggle)
haskell-hexml 0.3.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 116 kB
  • sloc: ansic: 557; haskell: 209; makefile: 6
file content (46 lines) | stat: -rw-r--r-- 1,202 bytes parent folder | download
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>&gt;piyopiyo&lt;</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;
    }
}