Adding complete support of strings

This commit is contained in:
Tristan Roux
2019-12-06 17:46:08 +01:00
parent 2cd96e3360
commit 61ad0b62b4
7 changed files with 65 additions and 30 deletions

View File

@@ -13,4 +13,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);
int xml_getstringdata(node *n, char **nodestr);
int xml_getstringdata(node *n, char **nodestr);
int xml_checkclosing(node *n, char **nodestr);

View File

@@ -32,6 +32,7 @@ int xml_checkclosing(node *n, char **nodestr)
int xml_parsechild(node *n, char **nodestr, bool has_child)
{
char endname[my_strlen(n->name + 3)];
static int depth = 0;
char *p;
if (has_child) {
@@ -40,14 +41,19 @@ int xml_parsechild(node *n, char **nodestr, bool has_child)
if (!p)
return (-1);
*p = '\0';
depth++;
n->child = xml_parsenode(nodestr);
if (!n->child)
return (-1);
if (xml_checkclosing(n, nodestr) < 0)
return (-1);
depth--;
} else
n->child = NULL;
n->next = xml_parsenode(nodestr);
if (depth != 0)
n->next = xml_parsenode(nodestr);
else
n->next = NULL;
return (0);
}
@@ -72,7 +78,7 @@ node *xml_parsenode(char **nodestr)
bool has_childs;
char *p = my_strchr(*nodestr, '>');
if (*nodestr[0] == '<') {
if ((*nodestr)[0] == '<') {
if (!n || !p || (*nodestr)[1] == '/')
return (NULL);
*p = '\0';
@@ -82,10 +88,11 @@ node *xml_parsenode(char **nodestr)
return (NULL);
return (xml_parseproperties(n, nodestr, has_params, has_childs));
}
else {
else if ((*nodestr)[1] != '/') {
if (xml_getstringdata(n, nodestr) < 0)
return (NULL);
n->next = xml_parsenode(nodestr);
return (n);
}
return (NULL);
}

View File

@@ -6,6 +6,7 @@
*/
#include "xml.h"
#include "xml_internal.h"
#include "my.h"
#include <stdlib.h>
#include <stddef.h>
@@ -13,15 +14,11 @@
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;
*nodestr += my_strlen(*nodestr);
n->name = my_strdup("data");
n->child = NULL;
n->properties = prop;

View File

@@ -14,29 +14,30 @@
#include <sys/stat.h>
#include <stdbool.h>
bool is_space_usefull(char *nodestr, int i)
{
if (i == 0 || !is_alphanum(nodestr[i - 1]))
return (false);
if (i + 1 == my_strlen(nodestr) || !is_alphanum(i + 1))
return (false);
return (true);
}
int xml_handle_prolog(char **nodestr)
{
char *strconst = *nodestr;
if (my_strstr(*nodestr, "<?xml") == *nodestr)
if (my_strstr(*nodestr, "<?xml") == *nodestr) {
*nodestr = my_strstr(*nodestr, "?>");
if (!*nodestr) {
free(strconst);
return (-1);
if (!*nodestr) {
free(strconst);
return (-1);
}
*nodestr += 2;
}
*nodestr += 2;
return (0);
}
bool is_space_usefull(char *nodestr, int i)
{
if (i == 0 || (!is_alphanum(nodestr[i - 1]) && nodestr[i - 1] != '"'))
return (false);
if (i + 1 == my_strlen(nodestr) || !is_alphanum(nodestr[i + 1]))
return (false);
return (true);
}
node *xml_parsestr(char *nodestr)
{
node *n;
@@ -68,7 +69,7 @@ node *xmlparse(char *path)
nodestr = malloc(stats.st_size + 1);
if (nodestr) {
count = read(fd, nodestr, stats.st_size);
nodestr[stats.st_size + 1] = '\0';
nodestr[stats.st_size] = '\0';
if (count == stats.st_size)
n = xml_parsestr(nodestr);
}

View File

@@ -35,18 +35,20 @@ Test(xml, withparam)
Test(xml, withnext)
{
char *xml = strdup("<yes params=\"Test\"/><nop/>");
char *xml = strdup("<yes params=\"Test\"><nop/><yep/></yes>");
node *n = xml_parsenode(&xml);
cr_assert_str_eq(n->name, "yes");
cr_assert_eq(n->child, 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_str_eq(n->child->next->name, "yep");
cr_assert_eq(n->child->next->child, NULL);
cr_assert_eq(n->child->next->properties, NULL);
cr_assert_eq(n->child->next->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->next->name, "nop");
cr_assert_eq(n->next->child, NULL);
cr_assert_eq(n->next->properties, NULL);
cr_assert_eq(n->next->next, NULL);
}
Test(xml, withchild)

View File

@@ -20,4 +20,25 @@ Test(xml, complete)
cr_assert_eq(n->child->child, NULL);
cr_assert_eq(n->child->properties, NULL);
cr_assert_eq(n->child->next, NULL);
}
Test(xml, completewstring)
{
node *n = xmlparse("tests/teststring.txt");
cr_assert_str_eq(n->name, "entity");
cr_assert_eq(n->next, NULL);
cr_assert_str_eq(n->properties->key, "id");
cr_assert_str_eq(n->properties->value, "0");
cr_assert_eq(n->properties->next, NULL);
cr_assert_str_eq(n->child->name, "PositionComponent");
cr_assert_eq(n->child->properties, NULL);
cr_assert_eq(n->child->next, NULL);
cr_assert_str_eq(n->child->child->name, "pos");
cr_assert_str_eq(n->child->child->child->properties->value, "5,5");
cr_assert_str_eq(n->child->child->next->name, "size");
cr_assert_str_eq(n->child->child->next->properties->key, "x");
cr_assert_str_eq(n->child->child->next->properties->value, "500");
cr_assert_str_eq(n->child->child->next->properties->next->key, "y");
cr_assert_str_eq(n->child->child->next->properties->next->value, "500");
}

6
tests/teststring.txt Normal file
View File

@@ -0,0 +1,6 @@
<entity id="0">
<PositionComponent>
<pos>5, 5</pos>
<size x="500" y="500"/>
</PositionComponent>
</entity>