diff --git a/Makefile b/Makefile index 480801e..21647f0 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,8 @@ SRC = src/xmlparser.c \ src/list_utility.c \ src/helper.c \ src/xmlproperties.c \ - src/rawnode.c + src/rawnode.c \ + src/xml_destroy.c OBJ = $(SRC:%.c=%.o) diff --git a/include/xml.h b/include/xml.h index 1613aa9..aec9deb 100644 --- a/include/xml.h +++ b/include/xml.h @@ -29,4 +29,5 @@ struct node node *xmlparse(char *path); dictionary *property_add(dictionary *list, dictionary *property); -node *xml_parsenode(char **nodestr); \ No newline at end of file +node *xml_parsenode(char **nodestr); +void xml_destroy(node *n); \ No newline at end of file diff --git a/src/helper.c b/src/helper.c index 2606dce..0a7dccb 100644 --- a/src/helper.c +++ b/src/helper.c @@ -37,5 +37,6 @@ char *trimstr(char *str) len++; } } + trimed[len] = '\0'; return (trimed); } \ No newline at end of file diff --git a/src/list_utility.c b/src/list_utility.c index 36ef735..e6598e7 100644 --- a/src/list_utility.c +++ b/src/list_utility.c @@ -12,13 +12,13 @@ dictionary *property_add(dictionary *list, dictionary *property) { dictionary *listconst = list; + property->next = NULL; if (!list) return (property); else { while (list->next) list = list->next; list->next = property; - property->next = NULL; } return (listconst); } \ No newline at end of file diff --git a/src/parsenode.c b/src/parsenode.c index d16171d..671d8c3 100644 --- a/src/parsenode.c +++ b/src/parsenode.c @@ -78,21 +78,23 @@ node *xml_parsenode(char **nodestr) bool has_childs; char *p = my_strchr(*nodestr, '>'); + if (!n) + return (NULL); if ((*nodestr)[0] == '<') { - if (!n || !p || (*nodestr)[1] == '/') + if (!p || (*nodestr)[1] == '/') { + free(n); return (NULL); + } *p = '\0'; *nodestr += 1; n->name = xml_getname(nodestr, &has_params, &has_childs); - if (!n->name) - return (NULL); - return (xml_parseproperties(n, nodestr, has_params, has_childs)); + if (n->name) + return (xml_parseproperties(n, nodestr, has_params, has_childs)); } - else if ((*nodestr)[1] != '/') { - if (xml_getstringdata(n, nodestr) < 0) - return (NULL); + else if ((*nodestr)[1] != '/' && xml_getstringdata(n, nodestr) == 0) { n->next = xml_parsenode(nodestr); return (n); } + free(n); return (NULL); } \ No newline at end of file diff --git a/src/xml_destroy.c b/src/xml_destroy.c new file mode 100644 index 0000000..137d4b3 --- /dev/null +++ b/src/xml_destroy.c @@ -0,0 +1,33 @@ +/* +** EPITECH PROJECT, 2019 +** xmlparser +** File description: +** xml_destroy +*/ + +#include "xml.h" +#include + +void xml_free_dict(dictionary *dic) +{ + if (!dic) + return; + if (dic->next) + xml_free_dict(dic->next); + free(dic->key); + free(dic->value); + free(dic); +} + +void xml_destroy(node *n) +{ + if (!n) + return; + if (n->next) + xml_destroy(n->next); + if (n->child) + xml_destroy(n->child); + xml_free_dict(n->properties); + free(n->name); + free(n); +} \ No newline at end of file diff --git a/src/xmlparser.c b/src/xmlparser.c index fc194d8..db2bc98 100644 --- a/src/xmlparser.c +++ b/src/xmlparser.c @@ -50,6 +50,8 @@ node *xml_parsestr(char *nodestr) nodestr[i] = '\t'; } nodestr = trimstr(nodestr); + free(strconst); + strconst = nodestr; n = xml_parsenode(&nodestr); free(strconst); return (n); diff --git a/tests/test_main.c b/tests/test_main.c index 81d4508..6f5b1eb 100644 --- a/tests/test_main.c +++ b/tests/test_main.c @@ -7,12 +7,13 @@ #include "xml.h" #include +#include int main(int argc, char **argv) { if (argc != 2) return (printf("Usage: %s xml_path\n", argv[0]), 0); node *n = xmlparse(argv[1]); - (void)n; + xml_destroy(n); return (0); } \ No newline at end of file