From 3bc97017535992dd8e3dd7aa11a10d0a9dd9beca Mon Sep 17 00:00:00 2001 From: Tristan Roux Date: Fri, 6 Dec 2019 11:19:31 +0100 Subject: [PATCH] Making the parser coding style friendly --- Makefile | 3 +- include/xml_internal.h | 4 ++ src/parsenode.c | 82 +---------------------------------------- src/xmlproperties.c | 84 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 81 deletions(-) create mode 100644 src/xmlproperties.c diff --git a/Makefile b/Makefile index 663fc36..ef064c6 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,8 @@ SRC = src/xmlparser.c \ src/parsenode.c \ src/list_utility.c \ - src/helper.c + src/helper.c \ + src/xmlproperties.c OBJ = $(SRC:%.c=%.o) diff --git a/include/xml_internal.h b/include/xml_internal.h index 73eed19..910705a 100644 --- a/include/xml_internal.h +++ b/include/xml_internal.h @@ -7,4 +7,8 @@ #pragma once +#include + 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); \ No newline at end of file diff --git a/src/parsenode.c b/src/parsenode.c index 557e7a0..676eb53 100644 --- a/src/parsenode.c +++ b/src/parsenode.c @@ -11,84 +11,6 @@ #include #include -char *xml_getname(char **nodestr, bool *has_parameters, bool *has_childs) -{ - char *p = my_strchr(*nodestr, ' '); - char *name; - int i = 1; - - *nodestr += 1; - if (p) { - *has_parameters = true; - *p = '\0'; - } else { - *has_parameters = false; - p = my_strchr(*nodestr, '/'); - if (!p) { - p = my_strchr(*nodestr, '>'); - *has_childs = true; - } else { - i++; - *has_childs = false; - } - if (p) - *p = '\0'; - else - p = *nodestr + my_strlen(*nodestr); - } - name = my_strdup(*nodestr); - *nodestr = p + i; - return (name); -} - -dictionary *xml_getproperty(char **nodestr) -{ - dictionary *property = malloc(sizeof(dictionary)); - char *p; - - if (!property) - return (NULL); - p = my_strchr(*nodestr, '='); - if (!p) - return (NULL); - *p = '\0'; - property->key = my_strdup(*nodestr); - *nodestr = p + 1; - if ((*nodestr)[0] != '"') - return (NULL); - *nodestr += 1; - p = my_strchr(*nodestr, '"'); - if (!p) - return (NULL); - *p = '\0'; - property->value = my_strdup(*nodestr); - *nodestr = p + 1; - return (property); -} - -dictionary *xml_getproperties(char **nodestr, bool *can_have_child) -{ - dictionary *properties = NULL; - dictionary *property = NULL; - - while ((*nodestr)[0] != '\0') { - property = xml_getproperty(nodestr); - if (!property) - return (NULL); - properties = property_add(properties, property); - if ((*nodestr)[0] == ' ') - *nodestr += 1; - if ((*nodestr)[0] == '/') { - *nodestr += 1; - *can_have_child = false; - } - else - *can_have_child = true; - } - *nodestr += 1; - return (properties); -} - int xml_checkclosing(node *n, char **nodestr) { if (!(*nodestr)[0] && (*nodestr)[1] == '/') { @@ -139,8 +61,8 @@ node *xml_parsenode(char **nodestr) if (!n || !p || (*nodestr)[0] != '<' || (*nodestr)[1] == '/') return (NULL); *p = '\0'; - n->name = xml_getname(nodestr, &has_params, &has_childs); - if (!n->name) + *nodestr += 1; + if (!(n->name = xml_getname(nodestr, &has_params, &has_childs))) return (NULL); if (has_params) { n->properties = xml_getproperties(nodestr, &has_childs); diff --git a/src/xmlproperties.c b/src/xmlproperties.c new file mode 100644 index 0000000..496df1d --- /dev/null +++ b/src/xmlproperties.c @@ -0,0 +1,84 @@ +/* +** EPITECH PROJECT, 2019 +** xmlparser +** File description: +** xmlproperties +*/ + +#include "my.h" +#include "xml.h" +#include +#include + +char *xml_getname(char **nodestr, bool *has_parameters, bool *has_childs) +{ + char *p = my_strchr(*nodestr, ' '); + char *name; + int i = 1; + + if (p) + *has_parameters = true; + else { + *has_parameters = false; + if (!(p = my_strchr(*nodestr, '/'))) { + p = my_strchr(*nodestr, '>'); + *has_childs = true; + } else { + i++; + *has_childs = false; + } if (!p) + p = *nodestr + my_strlen(*nodestr); + } + *p = '\0'; + name = my_strdup(*nodestr); + *nodestr = p + i; + return (name); +} + +dictionary *xml_getproperty(char **nodestr) +{ + dictionary *property = malloc(sizeof(dictionary)); + char *p; + + if (!property) + return (NULL); + p = my_strchr(*nodestr, '='); + if (!p) + return (NULL); + *p = '\0'; + property->key = my_strdup(*nodestr); + *nodestr = p + 1; + if ((*nodestr)[0] != '"') + return (NULL); + *nodestr += 1; + p = my_strchr(*nodestr, '"'); + if (!p) + return (NULL); + *p = '\0'; + property->value = my_strdup(*nodestr); + *nodestr = p + 1; + return (property); +} + +dictionary *xml_getproperties(char **nodestr, bool *can_have_child) +{ + dictionary *properties = NULL; + dictionary *property = NULL; + + while ((*nodestr)[0] != '\0') { + property = xml_getproperty(nodestr); + if (!property) + return (NULL); + properties = property_add(properties, property); + if ((*nodestr)[0] == ' ') + *nodestr += 1; + if ((*nodestr)[0] == '/') { + *nodestr += 1; + *can_have_child = false; + } + else + *can_have_child = true; + } + *nodestr += 1; + return (properties); +} \ No newline at end of file