#include "../include/HtmlTree.h" #include #include #include 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("\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; }