From 767cf1f289c16d93d8642430a45b2b0c0ba453b4 Mon Sep 17 00:00:00 2001 From: Tristan Roux Date: Fri, 6 Dec 2019 10:55:41 +0100 Subject: [PATCH] Solving a bug with no params but a child --- src/parsenode.c | 21 ++++++++++++--------- tests/test_basics.c | 3 +-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/parsenode.c b/src/parsenode.c index be9d9cb..557e7a0 100644 --- a/src/parsenode.c +++ b/src/parsenode.c @@ -11,7 +11,7 @@ #include #include -char *xml_getname(char **nodestr, bool *has_parameters) +char *xml_getname(char **nodestr, bool *has_parameters, bool *has_childs) { char *p = my_strchr(*nodestr, ' '); char *name; @@ -24,10 +24,13 @@ char *xml_getname(char **nodestr, bool *has_parameters) } else { *has_parameters = false; p = my_strchr(*nodestr, '/'); - if (!p) + if (!p) { p = my_strchr(*nodestr, '>'); - else + *has_childs = true; + } else { i++; + *has_childs = false; + } if (p) *p = '\0'; else @@ -129,24 +132,24 @@ int xml_parsechild(node *n, char **nodestr, bool has_child) node *xml_parsenode(char **nodestr) { node *n = malloc(sizeof(node)); - bool has_next_value; + bool has_params; + bool has_childs; char *p = my_strchr(*nodestr, '>'); if (!n || !p || (*nodestr)[0] != '<' || (*nodestr)[1] == '/') return (NULL); *p = '\0'; - n->name = xml_getname(nodestr, &has_next_value); + n->name = xml_getname(nodestr, &has_params, &has_childs); if (!n->name) return (NULL); - //IF THE NODE HAS NO PARAMTERS BUT A CHILD, THE SYSTEM TELLS THAT IT HAS NETHER. HERE CHILDS ARE USED ONLY WHEN THE NODE AS PARAMS - if (has_next_value) { - n->properties = xml_getproperties(nodestr, &has_next_value); + if (has_params) { + n->properties = xml_getproperties(nodestr, &has_childs); if (!n->properties) return (NULL); } else n->properties = NULL; - if (xml_parsechild(n, nodestr, has_next_value) < 0) + if (xml_parsechild(n, nodestr, has_childs) < 0) return (NULL); return (n); } \ No newline at end of file diff --git a/tests/test_basics.c b/tests/test_basics.c index 156ce09..4ac03ab 100644 --- a/tests/test_basics.c +++ b/tests/test_basics.c @@ -55,9 +55,8 @@ Test(xml, withchild) node *n = xml_parsenode(&xml); cr_assert_str_eq(n->name, "yes"); - cr_assert_eq(n->next, NULL); + cr_expect_eq(n->next, NULL); cr_assert_eq(n->properties, NULL); - cr_assert_eq(n->properties->next, NULL); cr_assert_str_eq(n->child->name, "nop"); cr_assert_eq(n->child->child, NULL); cr_assert_eq(n->child->properties, NULL);