aboutsummaryrefslogtreecommitdiff
path: root/tests/main.c
blob: 7ba19f55a61833d5ee39e3ca2d78d3b665e8e6e9 (plain)
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
#include "../include/HtmlTree.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char* file_get_content(const char *path, long *filesize) {
    char *data;
    FILE *file = fopen(path, "rb");
    if(!file) {
        perror(path);
        return NULL;
    }

    fseek(file, 0, SEEK_END);
    *filesize = ftell(file);
    fseek(file, 0, SEEK_SET);

    data = malloc(*filesize);
    fread(data, 1, *filesize, file);
    fclose(file);
    return data;
}

static void html_attributes_print(HtmlAttribute *attr) {
    while(attr) {
        printf("%.*s=\"%.*s\" ", (int)attr->key.size, attr->key.data, (int)attr->value.size, attr->value.data);
        attr = attr->next;
    }
}

static void html_node_print(HtmlNode *node);
static void html_node_child_print(HtmlNodeChild *node_child) {
    while(node_child) {
        html_node_print(&node_child->node);
        node_child = node_child->next;
    }
}

void html_node_print(HtmlNode *node) {
    switch(node->node_type) {
        case HTML_NODE_NODE: {
            printf("<%.*s ", (int)node->name_or_value.size, node->name_or_value.data);
            html_attributes_print(node->first_attr);
            printf(">\n");
            html_node_child_print(node->first_child);
            printf("</%.*s>\n", (int)node->name_or_value.size, node->name_or_value.data);
            break;
        }
        case HTML_NODE_TEXT: {
            printf("%.*s", (int)node->name_or_value.size, node->name_or_value.data);
            break;
        }
        case HTML_NODE_JS: {
            printf("%.*s", (int)node->name_or_value.size, node->name_or_value.data);
            break;
        }
    }
}

int main(void) {
    int result;
    HtmlTree html_tree;
    long filesize;
    char *file_data;

    file_data = file_get_content("depends/html-parser/tests/hotexamples.html", &filesize);
    if(!file_data) {
        fprintf(stderr, "Failed to read from file: depends/html-parser/tests/hotexamples.html\n");
        return 1;
    }

    result = html_parse_to_tree(&html_tree, file_data, filesize);
    if(result != 0)
        return result;

    html_node_print(&html_tree.root_node);

    html_tree_deinit(&html_tree);
    free(file_data);
    return 0;
}