Handling childs

This commit is contained in:
AnonymusRaccoon
2019-12-06 00:16:39 +01:00
parent 6a9b1a2a67
commit 9dc32d681a
6 changed files with 111 additions and 28 deletions
+2 -1
View File
@@ -7,7 +7,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
OBJ = $(SRC:%.c=%.o) OBJ = $(SRC:%.c=%.o)
+10
View File
@@ -0,0 +1,10 @@
/*
** EPITECH PROJECT, 2019
** xmlParser
** File description:
** xml_internal
*/
#pragma once
void xml_fillclosing_br(char *buffer, const char *name);
+20
View File
@@ -0,0 +1,20 @@
/*
** EPITECH PROJECT, 2019
** xmlParser
** File description:
** helper
*/
#include "my.h"
void xml_fillclosing_br(char *buffer, const char *name)
{
int i = my_strlen(name) + 2;
buffer[0] = '<';
buffer[1] = '/';
buffer[2] = '\0';
my_strcat(buffer, name);
buffer[i] = '>';
buffer[i + 1] = '\0';
}
+47 -26
View File
@@ -6,6 +6,7 @@
*/ */
#include "xml.h" #include "xml.h"
#include "xml_internal.h"
#include "my.h" #include "my.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
@@ -14,9 +15,8 @@ char *xml_getname(char **nodestr, bool *has_parameters)
{ {
char *p = my_strchr(*nodestr, ' '); char *p = my_strchr(*nodestr, ' ');
char *name; char *name;
int i = 1;
if ((*nodestr)[0] != '<')
return (NULL);
*nodestr += 1; *nodestr += 1;
if (p) { if (p) {
*has_parameters = true; *has_parameters = true;
@@ -25,12 +25,14 @@ char *xml_getname(char **nodestr, bool *has_parameters)
p = my_strchr(*nodestr, '/'); p = my_strchr(*nodestr, '/');
if (!p) if (!p)
p = my_strchr(*nodestr, '>'); p = my_strchr(*nodestr, '>');
else
i++;
if (!p) if (!p)
return (NULL); return (NULL);
} }
*p = '\0'; *p = '\0';
name = my_strdup(*nodestr); name = my_strdup(*nodestr);
*nodestr = p + 1; *nodestr = p + i;
return (name); return (name);
} }
@@ -64,7 +66,7 @@ dictionary *xml_getproperties(char **nodestr, bool *can_have_child)
dictionary *properties = NULL; dictionary *properties = NULL;
dictionary *property = NULL; dictionary *property = NULL;
while ((*nodestr)[0] != '>') { while ((*nodestr)[0] != '\0') {
property = xml_getproperty(nodestr); property = xml_getproperty(nodestr);
if (!property) if (!property)
return (NULL); return (NULL);
@@ -78,27 +80,47 @@ dictionary *xml_getproperties(char **nodestr, bool *can_have_child)
else else
*can_have_child = true; *can_have_child = true;
} }
*nodestr += 1;
return (properties); 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] == '/') {
*nodestr += 2; if (my_strstr(*nodestr + 1, n->name) != *nodestr + 2)
return (0); return (-1);
} *nodestr += 2 + my_strlen(n->name);
if ((*nodestr)[0] == '>') { if ((*nodestr)[0] != '>')
return (-1);
*nodestr += 1; *nodestr += 1;
return (0); return (0);
} }
if ((*nodestr)[0] != '<' || (*nodestr)[1] != '/') if (!(*nodestr)[0]) {
return (-1); *nodestr += 1;
if (my_strstr(*nodestr, n->name) != *nodestr + 2) return (0);
return (-1); }
*nodestr += 2 + my_strlen(n->name); return (-1);
if ((*nodestr)[0] != '>') }
return (-1);
*nodestr += 1; int xml_parsechild(node *n, char **nodestr, bool has_child)
{
char endname[my_strlen(n->name + 3)];
char *p;
if (has_child) {
xml_fillclosing_br(endname, n->name);
p = my_strstr(*nodestr, endname);
if (!p)
return (-1);
*p = '\0';
n->child = xml_parsenode(nodestr);
if (!n->child)
return (-1);
if (xml_checkclosing(n, nodestr) < 0)
return (-1);
} else
n->child = NULL;
n->next = xml_parsenode(nodestr);
return (0); return (0);
} }
@@ -106,23 +128,22 @@ node *xml_parsenode(char **nodestr)
{ {
node *n = malloc(sizeof(node)); node *n = malloc(sizeof(node));
bool has_next_value; bool has_next_value;
char *p = my_strchr(*nodestr, '>');
if (!n) if (!n || !p || (*nodestr)[0] != '<' || (*nodestr)[1] == '/')
return (NULL); return (NULL);
*p = '\0';
n->name = xml_getname(nodestr, &has_next_value); n->name = xml_getname(nodestr, &has_next_value);
if (!n->name)
return (NULL);
if (has_next_value) { if (has_next_value) {
if (!(n->properties = xml_getproperties(nodestr, &has_next_value))) n->properties = xml_getproperties(nodestr, &has_next_value);
if (!n->properties)
return (NULL); return (NULL);
} }
else else
n->properties = NULL; n->properties = NULL;
if (has_next_value) { if (xml_parsechild(n, nodestr, has_next_value) < 0)
if (!(n->child = xml_parsenode(nodestr)))
return (NULL);
} else
n->child = NULL;
if (!n->name || xml_checkclosing(n, nodestr) < 0)
return (NULL); return (NULL);
n->next = xml_parsenode(nodestr);
return (n); return (n);
} }
+31
View File
@@ -47,4 +47,35 @@ Test(xml, withnext)
cr_assert_eq(n->next->child, NULL); cr_assert_eq(n->next->child, NULL);
cr_assert_eq(n->next->properties, NULL); cr_assert_eq(n->next->properties, NULL);
cr_assert_eq(n->next->next, NULL); cr_assert_eq(n->next->next, NULL);
}
Test(xml, withchild)
{
char *xml = strdup("<yes><nop/></yes>");
node *n = xml_parsenode(&xml);
cr_assert_str_eq(n->name, "yes");
cr_assert_eq(n->next, NULL);
cr_assert_eq(n->properties, NULL);
cr_assert_eq(n->properties->next, 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_eq(n->child->next, NULL);
}
Test(xml, withchildwparams)
{
char *xml = strdup("<yes params=\"Test\"><nop/></yes>");
node *n = xml_parsenode(&xml);
cr_assert_str_eq(n->name, "yes");
cr_assert_eq(n->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->child->name, "nop");
cr_assert_eq(n->child->child, NULL);
cr_assert_eq(n->child->properties, NULL);
cr_assert_eq(n->child->next, NULL);
} }
+1 -1
View File
@@ -1 +1 @@
<yes/> <yes params="Test"><nop/></yes>