diff --git a/Makefile b/Makefile index 21647f0..96a35ac 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,8 @@ SRC = src/xmlparser.c \ src/helper.c \ src/xmlproperties.c \ src/rawnode.c \ - src/xml_destroy.c + src/xml_destroy.c \ + src/xmlget.c OBJ = $(SRC:%.c=%.o) diff --git a/include/xml.h b/include/xml.h index aec9deb..29bdc3e 100644 --- a/include/xml.h +++ b/include/xml.h @@ -28,6 +28,5 @@ struct node }; node *xmlparse(char *path); -dictionary *property_add(dictionary *list, dictionary *property); -node *xml_parsenode(char **nodestr); +char *xml_getproperty(node *n, char *key); void xml_destroy(node *n); \ No newline at end of file diff --git a/include/xml_internal.h b/include/xml_internal.h index 571c58a..651e7ac 100644 --- a/include/xml_internal.h +++ b/include/xml_internal.h @@ -14,4 +14,6 @@ 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); -int xml_checkclosing(node *n, char **nodestr); \ No newline at end of file +int xml_checkclosing(node *n, char **nodestr); +dictionary *property_add(dictionary *list, dictionary *property); +node *xml_parsenode(char **nodestr); \ No newline at end of file diff --git a/src/parsenode.c b/src/parsenode.c index 977227c..1a57992 100644 --- a/src/parsenode.c +++ b/src/parsenode.c @@ -29,6 +29,14 @@ int xml_checkclosing(node *n, char **nodestr) return (-1); } +void xml_parsenext(node *n, char **nodestr, int depth) +{ + if (depth != 0) + n->next = xml_parsenode(nodestr); + else + n->next = NULL; +} + int xml_parsechild(node *n, char **nodestr, bool has_child) { char endname[my_strlen(n->name + 3)]; @@ -50,10 +58,7 @@ int xml_parsechild(node *n, char **nodestr, bool has_child) depth--; } else n->child = NULL; - if (depth != 0) - n->next = xml_parsenode(nodestr); - else - n->next = NULL; + xml_parsenext(n, nodestr, depth); return (0); } @@ -74,24 +79,21 @@ node *xml_parseproperties(node *n, char **str, bool has_params, bool has_childs) node *xml_parsenode(char **nodestr) { node *n = malloc(sizeof(node)); - bool has_params; + bool has_param; bool has_childs; char *p = my_strchr(*nodestr, '>'); if (!n) return (NULL); if ((*nodestr)[0] == '<') { - if (!p || (*nodestr)[1] == '/') { - free(n); - return (NULL); + if (p && (*nodestr)[1] != '/') { + *p = '\0'; + *nodestr += 1; + n->name = xml_getname(nodestr, &has_param, &has_childs); + if (n->name) + return (xml_parseproperties(n, nodestr, has_param, has_childs)); } - *p = '\0'; - *nodestr += 1; - n->name = xml_getname(nodestr, &has_params, &has_childs); - if (n->name) - return (xml_parseproperties(n, nodestr, has_params, has_childs)); - } - else if ((*nodestr)[1] != '/' && xml_getstringdata(n, nodestr) == 0) { + } else if ((*nodestr)[1] != '/' && xml_getstringdata(n, nodestr) == 0) { n->next = xml_parsenode(nodestr); return (n); } diff --git a/src/xmlget.c b/src/xmlget.c index ad68f37..01ed64e 100644 --- a/src/xmlget.c +++ b/src/xmlget.c @@ -7,11 +7,13 @@ #include "xml.h" #include "my.h" +#include char *xml_getproperty(node *n, char *key) { for (dictionary *prop = n->properties; prop; prop = prop->next) { - if (!my_strcmp(key, prop->key)); + if (!my_strcmp(key, prop->key)) return (prop->value); } + return (NULL); } \ No newline at end of file diff --git a/src/xmlproperties.c b/src/xmlproperties.c index 496df1d..5410260 100644 --- a/src/xmlproperties.c +++ b/src/xmlproperties.c @@ -7,6 +7,7 @@ #include "my.h" #include "xml.h" +#include "xml_internal.h" #include #include @@ -35,7 +36,7 @@ char *xml_getname(char **nodestr, bool *has_parameters, bool *has_childs) return (name); } -dictionary *xml_getproperty(char **nodestr) +dictionary *xml_parseproperty(char **nodestr) { dictionary *property = malloc(sizeof(dictionary)); char *p; @@ -66,7 +67,7 @@ dictionary *xml_getproperties(char **nodestr, bool *can_have_child) dictionary *property = NULL; while ((*nodestr)[0] != '\0') { - property = xml_getproperty(nodestr); + property = xml_parseproperty(nodestr); if (!property) return (NULL); properties = property_add(properties, property); diff --git a/tests/test_basics.c b/tests/test_basics.c index 9ad4a06..680bc0a 100644 --- a/tests/test_basics.c +++ b/tests/test_basics.c @@ -6,6 +6,7 @@ */ #include "xml.h" +#include "xml_internal.h" #include #include