Making the parser coding style friendly

This commit is contained in:
Tristan Roux
2019-12-06 11:19:31 +01:00
parent 767cf1f289
commit 3bc9701753
4 changed files with 92 additions and 81 deletions
+2 -1
View File
@@ -8,7 +8,8 @@
SRC = src/xmlparser.c \ SRC = src/xmlparser.c \
src/parsenode.c \ src/parsenode.c \
src/list_utility.c \ src/list_utility.c \
src/helper.c src/helper.c \
src/xmlproperties.c
OBJ = $(SRC:%.c=%.o) OBJ = $(SRC:%.c=%.o)
+4
View File
@@ -7,4 +7,8 @@
#pragma once #pragma once
#include <stdbool.h>
void xml_fillclosing_br(char *buffer, const char *name); 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);
+2 -80
View File
@@ -11,84 +11,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
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) int xml_checkclosing(node *n, char **nodestr)
{ {
if (!(*nodestr)[0] && (*nodestr)[1] == '/') { if (!(*nodestr)[0] && (*nodestr)[1] == '/') {
@@ -139,8 +61,8 @@ node *xml_parsenode(char **nodestr)
if (!n || !p || (*nodestr)[0] != '<' || (*nodestr)[1] == '/') if (!n || !p || (*nodestr)[0] != '<' || (*nodestr)[1] == '/')
return (NULL); return (NULL);
*p = '\0'; *p = '\0';
n->name = xml_getname(nodestr, &has_params, &has_childs); *nodestr += 1;
if (!n->name) if (!(n->name = xml_getname(nodestr, &has_params, &has_childs)))
return (NULL); return (NULL);
if (has_params) { if (has_params) {
n->properties = xml_getproperties(nodestr, &has_childs); n->properties = xml_getproperties(nodestr, &has_childs);
+84
View File
@@ -0,0 +1,84 @@
/*
** EPITECH PROJECT, 2019
** xmlparser
** File description:
** xmlproperties
*/
#include "my.h"
#include "xml.h"
#include <stdlib.h>
#include <stdbool.h>
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);
}