aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-09-18 18:16:31 +0200
committerdec05eba <dec05eba@protonmail.com>2021-09-18 18:16:31 +0200
commitc17cc9a2bcf393b8e6af553dd14a129ec9cc9462 (patch)
tree1645b014c46a2dd01087ff2e74abb918cc7b5efe
parent534c441fd8172322ff5eaad54a1d26b9d8492c39 (diff)
Remove unecessary malloced node for child nodes (the child node item with the next item included is malloced instead)
-rw-r--r--include/HtmlTree.h2
-rw-r--r--src/HtmlTree.c57
-rw-r--r--tests/main.c2
3 files changed, 27 insertions, 34 deletions
diff --git a/include/HtmlTree.h b/include/HtmlTree.h
index 6bb3c5f..4378152 100644
--- a/include/HtmlTree.h
+++ b/include/HtmlTree.h
@@ -29,7 +29,7 @@ struct HtmlNode {
};
struct HtmlNodeChild {
- HtmlNode *node;
+ HtmlNode node;
HtmlNodeChild *next;
};
diff --git a/src/HtmlTree.c b/src/HtmlTree.c
index f4deb88..faef08a 100644
--- a/src/HtmlTree.c
+++ b/src/HtmlTree.c
@@ -1,5 +1,6 @@
#include "../include/HtmlTree.h"
#include <stdlib.h>
+#include <assert.h>
static void html_node_deinit(HtmlNode *self);
@@ -13,45 +14,35 @@ static void html_node_init(HtmlNode *self) {
self->node_type = HTML_NODE_NODE;
}
+/* |parent| can't be NULL */
static HtmlNode* html_node_create(HtmlStringView name_or_value, HtmlNode *parent, HtmlNodeType node_type) {
- HtmlNode *new_node = malloc(sizeof(HtmlNode));
- if(!new_node)
+ HtmlNodeChild *node_child = malloc(sizeof(HtmlNodeChild));
+ assert(parent);
+ if(!node_child)
return NULL;
- new_node->name_or_value = name_or_value;
- new_node->parent = parent;
- new_node->first_child = NULL;
- new_node->last_child = NULL;
- new_node->first_attr = NULL;
- new_node->node_type = node_type;
-
- if(parent) {
- HtmlNodeChild *node_child = malloc(sizeof(HtmlNodeChild));
- if(!node_child) {
- free(new_node);
- return NULL;
- }
-
- node_child->node = new_node;
- node_child->next = NULL;
-
- if(!parent->first_child) {
- parent->first_child = node_child;
- parent->last_child = node_child;
- } else {
- parent->last_child->next = node_child;
- parent->last_child = node_child;
- }
+ node_child->node.name_or_value = name_or_value;
+ node_child->node.parent = parent;
+ node_child->node.first_child = NULL;
+ node_child->node.last_child = NULL;
+ node_child->node.first_attr = NULL;
+ node_child->node.node_type = node_type;
+
+ node_child->next = NULL;
+
+ if(!parent->first_child) {
+ parent->first_child = node_child;
+ parent->last_child = node_child;
+ } else {
+ parent->last_child->next = node_child;
+ parent->last_child = node_child;
}
- return new_node;
+ return &node_child->node;
}
static void html_node_child_deinit(HtmlNodeChild *self) {
- html_node_deinit(self->node);
- free(self->node);
- self->node = NULL;
-
+ html_node_deinit(&self->node);
if(self->next) {
html_node_child_deinit(self->next);
free(self->next);
@@ -87,6 +78,7 @@ void html_node_deinit(HtmlNode *self) {
}
typedef struct {
+ HtmlNode *root_node;
HtmlNode *current_node;
HtmlAttribute *current_node_last_attribute;
} ParseUserdata;
@@ -106,7 +98,7 @@ static int parse_callback(HtmlParser *html_parser, HtmlParseType parse_type, voi
}
case HTML_PARSE_TAG_END: {
HtmlNode *parent_node = parse_userdata->current_node->parent;
- if(parent_node) {
+ if(parent_node && parent_node != parse_userdata->root_node) {
parse_userdata->current_node = parent_node;
parse_userdata->current_node_last_attribute = NULL;
}
@@ -148,6 +140,7 @@ int html_parse_to_tree(HtmlTree *self, const char *html_source, size_t len) {
ParseUserdata parse_userdata;
html_node_init(&self->root_node);
+ parse_userdata.root_node = &self->root_node;
parse_userdata.current_node = &self->root_node;
parse_userdata.current_node_last_attribute = NULL;
result = html_parser_parse(html_source, len, parse_callback, &parse_userdata);
diff --git a/tests/main.c b/tests/main.c
index 3b22f8a..4c9e93c 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -30,7 +30,7 @@ static void html_attributes_print(HtmlAttribute *attr) {
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);
+ html_node_print(&node_child->node);
node_child = node_child->next;
}
}