diff --git a/Makefile b/Makefile index 2d6ba6c..480801e 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,8 @@ SRC = src/xmlparser.c \ src/parsenode.c \ src/list_utility.c \ src/helper.c \ - src/xmlproperties.c + src/xmlproperties.c \ + src/rawnode.c OBJ = $(SRC:%.c=%.o) diff --git a/include/xml_internal.h b/include/xml_internal.h index 09f053a..d160c84 100644 --- a/include/xml_internal.h +++ b/include/xml_internal.h @@ -12,4 +12,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); \ No newline at end of file +char *trimstr(char *str); +int xml_getstringdata(node *n, char **nodestr); \ No newline at end of file diff --git a/src/helper.c b/src/helper.c index 7cb1157..2606dce 100644 --- a/src/helper.c +++ b/src/helper.c @@ -31,7 +31,7 @@ char *trimstr(char *str) } trimed = malloc(sizeof(char) * (len + 1)); len = 0; - for(int i = 0; str[i]; i++) { + for (int i = 0; str[i]; i++) { if (str[i] != '\t' && str[i] != '\n' && str[i] != '\r') { trimed[len] = str[i]; len++; diff --git a/src/parsenode.c b/src/parsenode.c index 676eb53..0a31328 100644 --- a/src/parsenode.c +++ b/src/parsenode.c @@ -51,6 +51,20 @@ int xml_parsechild(node *n, char **nodestr, bool has_child) return (0); } +node *xml_parseproperties(node *n, char **str, bool has_params, bool has_childs) +{ + if (has_params) { + n->properties = xml_getproperties(str, &has_childs); + if (!n->properties) + return (NULL); + } + else + n->properties = NULL; + if (xml_parsechild(n, str, has_childs) < 0) + return (NULL); + return (n); +} + node *xml_parsenode(char **nodestr) { node *n = malloc(sizeof(node)); @@ -58,20 +72,20 @@ node *xml_parsenode(char **nodestr) bool has_childs; char *p = my_strchr(*nodestr, '>'); - if (!n || !p || (*nodestr)[0] != '<' || (*nodestr)[1] == '/') - return (NULL); - *p = '\0'; - *nodestr += 1; - if (!(n->name = xml_getname(nodestr, &has_params, &has_childs))) - return (NULL); - if (has_params) { - n->properties = xml_getproperties(nodestr, &has_childs); - if (!n->properties) + if (*nodestr[0] == '<') { + if (!n || !p || (*nodestr)[1] == '/') 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)); + } + else { + if (xml_getstringdata(n, nodestr) < 0) + return (NULL); + n->next = xml_parsenode(nodestr); + return (n); } - else - n->properties = NULL; - if (xml_parsechild(n, nodestr, has_childs) < 0) - return (NULL); - return (n); } \ No newline at end of file diff --git a/src/rawnode.c b/src/rawnode.c new file mode 100644 index 0000000..c42fe93 --- /dev/null +++ b/src/rawnode.c @@ -0,0 +1,29 @@ +/* +** EPITECH PROJECT, 2019 +** xmlparser +** File description: +** rawnode +*/ + +#include "xml.h" +#include "my.h" +#include +#include + +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; + n->name = my_strdup("data"); + n->child = NULL; + n->properties = prop; + return (0); +} \ No newline at end of file diff --git a/tests/testprolog.txt b/tests/testprolog.txt index 69e7354..b8236ad 100644 --- a/tests/testprolog.txt +++ b/tests/testprolog.txt @@ -1,4 +1,4 @@ - \ No newline at end of file +