diff --git a/include/xml_internal.h b/include/xml_internal.h index d160c84..571c58a 100644 --- a/include/xml_internal.h +++ b/include/xml_internal.h @@ -13,4 +13,5 @@ void xml_fillclosing_br(char *buffer, const char *name); char *xml_getname(char **nodestr, bool *has_parameters, bool *has_childs); dictionary *xml_getproperties(char **nodestr, bool *can_have_child); char *trimstr(char *str); -int xml_getstringdata(node *n, char **nodestr); \ No newline at end of file +int xml_getstringdata(node *n, char **nodestr); +int xml_checkclosing(node *n, char **nodestr); \ No newline at end of file diff --git a/src/parsenode.c b/src/parsenode.c index 0a31328..d16171d 100644 --- a/src/parsenode.c +++ b/src/parsenode.c @@ -32,6 +32,7 @@ int xml_checkclosing(node *n, char **nodestr) int xml_parsechild(node *n, char **nodestr, bool has_child) { char endname[my_strlen(n->name + 3)]; + static int depth = 0; char *p; if (has_child) { @@ -40,14 +41,19 @@ int xml_parsechild(node *n, char **nodestr, bool has_child) if (!p) return (-1); *p = '\0'; + depth++; n->child = xml_parsenode(nodestr); if (!n->child) return (-1); if (xml_checkclosing(n, nodestr) < 0) return (-1); + depth--; } else n->child = NULL; - n->next = xml_parsenode(nodestr); + if (depth != 0) + n->next = xml_parsenode(nodestr); + else + n->next = NULL; return (0); } @@ -72,7 +78,7 @@ node *xml_parsenode(char **nodestr) bool has_childs; char *p = my_strchr(*nodestr, '>'); - if (*nodestr[0] == '<') { + if ((*nodestr)[0] == '<') { if (!n || !p || (*nodestr)[1] == '/') return (NULL); *p = '\0'; @@ -82,10 +88,11 @@ node *xml_parsenode(char **nodestr) return (NULL); return (xml_parseproperties(n, nodestr, has_params, has_childs)); } - else { + else if ((*nodestr)[1] != '/') { if (xml_getstringdata(n, nodestr) < 0) return (NULL); n->next = xml_parsenode(nodestr); return (n); } + return (NULL); } \ No newline at end of file diff --git a/src/rawnode.c b/src/rawnode.c index c42fe93..6a53007 100644 --- a/src/rawnode.c +++ b/src/rawnode.c @@ -6,6 +6,7 @@ */ #include "xml.h" +#include "xml_internal.h" #include "my.h" #include #include @@ -13,15 +14,11 @@ int xml_getstringdata(node *n, char **nodestr) { dictionary *prop = malloc(sizeof(dictionary)); - char *p = my_strchr(*nodestr, '<'); - if (!p) - return (-1); - *(p - 1) = '\0'; prop->key = my_strdup("data"); prop->value = my_strdup(*nodestr); prop->next = NULL; - *nodestr = p; + *nodestr += my_strlen(*nodestr); n->name = my_strdup("data"); n->child = NULL; n->properties = prop; diff --git a/src/xmlparser.c b/src/xmlparser.c index f1af649..fc194d8 100644 --- a/src/xmlparser.c +++ b/src/xmlparser.c @@ -14,29 +14,30 @@ #include #include -bool is_space_usefull(char *nodestr, int i) -{ - if (i == 0 || !is_alphanum(nodestr[i - 1])) - return (false); - if (i + 1 == my_strlen(nodestr) || !is_alphanum(i + 1)) - return (false); - return (true); -} - int xml_handle_prolog(char **nodestr) { char *strconst = *nodestr; - if (my_strstr(*nodestr, ""); - if (!*nodestr) { - free(strconst); - return (-1); + if (!*nodestr) { + free(strconst); + return (-1); + } + *nodestr += 2; } - *nodestr += 2; return (0); } +bool is_space_usefull(char *nodestr, int i) +{ + if (i == 0 || (!is_alphanum(nodestr[i - 1]) && nodestr[i - 1] != '"')) + return (false); + if (i + 1 == my_strlen(nodestr) || !is_alphanum(nodestr[i + 1])) + return (false); + return (true); +} + node *xml_parsestr(char *nodestr) { node *n; @@ -68,7 +69,7 @@ node *xmlparse(char *path) nodestr = malloc(stats.st_size + 1); if (nodestr) { count = read(fd, nodestr, stats.st_size); - nodestr[stats.st_size + 1] = '\0'; + nodestr[stats.st_size] = '\0'; if (count == stats.st_size) n = xml_parsestr(nodestr); } diff --git a/tests/test_basics.c b/tests/test_basics.c index 4ac03ab..9ad4a06 100644 --- a/tests/test_basics.c +++ b/tests/test_basics.c @@ -35,18 +35,20 @@ Test(xml, withparam) Test(xml, withnext) { - char *xml = strdup(""); + char *xml = strdup(""); node *n = xml_parsenode(&xml); cr_assert_str_eq(n->name, "yes"); - cr_assert_eq(n->child, NULL); + cr_assert_str_eq(n->child->name, "nop"); + cr_assert_eq(n->child->child, NULL); + cr_assert_eq(n->child->properties, NULL); + cr_assert_str_eq(n->child->next->name, "yep"); + cr_assert_eq(n->child->next->child, NULL); + cr_assert_eq(n->child->next->properties, NULL); + cr_assert_eq(n->child->next->next, NULL); cr_assert_str_eq(n->properties->key, "params"); cr_assert_str_eq(n->properties->value, "Test"); cr_assert_eq(n->properties->next, NULL); - cr_assert_str_eq(n->next->name, "nop"); - cr_assert_eq(n->next->child, NULL); - cr_assert_eq(n->next->properties, NULL); - cr_assert_eq(n->next->next, NULL); } Test(xml, withchild) diff --git a/tests/tests_realxml.c b/tests/tests_realxml.c index cc66597..b854b3a 100644 --- a/tests/tests_realxml.c +++ b/tests/tests_realxml.c @@ -20,4 +20,25 @@ Test(xml, complete) cr_assert_eq(n->child->child, NULL); cr_assert_eq(n->child->properties, NULL); cr_assert_eq(n->child->next, NULL); +} + +Test(xml, completewstring) +{ + node *n = xmlparse("tests/teststring.txt"); + + cr_assert_str_eq(n->name, "entity"); + cr_assert_eq(n->next, NULL); + cr_assert_str_eq(n->properties->key, "id"); + cr_assert_str_eq(n->properties->value, "0"); + cr_assert_eq(n->properties->next, NULL); + cr_assert_str_eq(n->child->name, "PositionComponent"); + cr_assert_eq(n->child->properties, NULL); + cr_assert_eq(n->child->next, NULL); + cr_assert_str_eq(n->child->child->name, "pos"); + cr_assert_str_eq(n->child->child->child->properties->value, "5,5"); + cr_assert_str_eq(n->child->child->next->name, "size"); + cr_assert_str_eq(n->child->child->next->properties->key, "x"); + cr_assert_str_eq(n->child->child->next->properties->value, "500"); + cr_assert_str_eq(n->child->child->next->properties->next->key, "y"); + cr_assert_str_eq(n->child->child->next->properties->next->value, "500"); } \ No newline at end of file diff --git a/tests/teststring.txt b/tests/teststring.txt new file mode 100644 index 0000000..e368301 --- /dev/null +++ b/tests/teststring.txt @@ -0,0 +1,6 @@ + + + 5, 5 + + + \ No newline at end of file