mirror of
https://github.com/zoriya/xmlParser.git
synced 2026-06-06 11:22:08 +00:00
Adding complete support of strings
This commit is contained in:
@@ -13,4 +13,5 @@ void xml_fillclosing_br(char *buffer, const char *name);
|
|||||||
char *xml_getname(char **nodestr, bool *has_parameters, bool *has_childs);
|
char *xml_getname(char **nodestr, bool *has_parameters, bool *has_childs);
|
||||||
dictionary *xml_getproperties(char **nodestr, bool *can_have_child);
|
dictionary *xml_getproperties(char **nodestr, bool *can_have_child);
|
||||||
char *trimstr(char *str);
|
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);
|
||||||
+10
-3
@@ -32,6 +32,7 @@ int xml_checkclosing(node *n, char **nodestr)
|
|||||||
int xml_parsechild(node *n, char **nodestr, bool has_child)
|
int xml_parsechild(node *n, char **nodestr, bool has_child)
|
||||||
{
|
{
|
||||||
char endname[my_strlen(n->name + 3)];
|
char endname[my_strlen(n->name + 3)];
|
||||||
|
static int depth = 0;
|
||||||
char *p;
|
char *p;
|
||||||
|
|
||||||
if (has_child) {
|
if (has_child) {
|
||||||
@@ -40,14 +41,19 @@ int xml_parsechild(node *n, char **nodestr, bool has_child)
|
|||||||
if (!p)
|
if (!p)
|
||||||
return (-1);
|
return (-1);
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
|
depth++;
|
||||||
n->child = xml_parsenode(nodestr);
|
n->child = xml_parsenode(nodestr);
|
||||||
if (!n->child)
|
if (!n->child)
|
||||||
return (-1);
|
return (-1);
|
||||||
if (xml_checkclosing(n, nodestr) < 0)
|
if (xml_checkclosing(n, nodestr) < 0)
|
||||||
return (-1);
|
return (-1);
|
||||||
|
depth--;
|
||||||
} else
|
} else
|
||||||
n->child = NULL;
|
n->child = NULL;
|
||||||
n->next = xml_parsenode(nodestr);
|
if (depth != 0)
|
||||||
|
n->next = xml_parsenode(nodestr);
|
||||||
|
else
|
||||||
|
n->next = NULL;
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +78,7 @@ node *xml_parsenode(char **nodestr)
|
|||||||
bool has_childs;
|
bool has_childs;
|
||||||
char *p = my_strchr(*nodestr, '>');
|
char *p = my_strchr(*nodestr, '>');
|
||||||
|
|
||||||
if (*nodestr[0] == '<') {
|
if ((*nodestr)[0] == '<') {
|
||||||
if (!n || !p || (*nodestr)[1] == '/')
|
if (!n || !p || (*nodestr)[1] == '/')
|
||||||
return (NULL);
|
return (NULL);
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
@@ -82,10 +88,11 @@ node *xml_parsenode(char **nodestr)
|
|||||||
return (NULL);
|
return (NULL);
|
||||||
return (xml_parseproperties(n, nodestr, has_params, has_childs));
|
return (xml_parseproperties(n, nodestr, has_params, has_childs));
|
||||||
}
|
}
|
||||||
else {
|
else if ((*nodestr)[1] != '/') {
|
||||||
if (xml_getstringdata(n, nodestr) < 0)
|
if (xml_getstringdata(n, nodestr) < 0)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
n->next = xml_parsenode(nodestr);
|
n->next = xml_parsenode(nodestr);
|
||||||
return (n);
|
return (n);
|
||||||
}
|
}
|
||||||
|
return (NULL);
|
||||||
}
|
}
|
||||||
+2
-5
@@ -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 <stddef.h>
|
#include <stddef.h>
|
||||||
@@ -13,15 +14,11 @@
|
|||||||
int xml_getstringdata(node *n, char **nodestr)
|
int xml_getstringdata(node *n, char **nodestr)
|
||||||
{
|
{
|
||||||
dictionary *prop = malloc(sizeof(dictionary));
|
dictionary *prop = malloc(sizeof(dictionary));
|
||||||
char *p = my_strchr(*nodestr, '<');
|
|
||||||
|
|
||||||
if (!p)
|
|
||||||
return (-1);
|
|
||||||
*(p - 1) = '\0';
|
|
||||||
prop->key = my_strdup("data");
|
prop->key = my_strdup("data");
|
||||||
prop->value = my_strdup(*nodestr);
|
prop->value = my_strdup(*nodestr);
|
||||||
prop->next = NULL;
|
prop->next = NULL;
|
||||||
*nodestr = p;
|
*nodestr += my_strlen(*nodestr);
|
||||||
n->name = my_strdup("data");
|
n->name = my_strdup("data");
|
||||||
n->child = NULL;
|
n->child = NULL;
|
||||||
n->properties = prop;
|
n->properties = prop;
|
||||||
|
|||||||
+16
-15
@@ -14,29 +14,30 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <stdbool.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)
|
int xml_handle_prolog(char **nodestr)
|
||||||
{
|
{
|
||||||
char *strconst = *nodestr;
|
char *strconst = *nodestr;
|
||||||
|
|
||||||
if (my_strstr(*nodestr, "<?xml") == *nodestr)
|
if (my_strstr(*nodestr, "<?xml") == *nodestr) {
|
||||||
*nodestr = my_strstr(*nodestr, "?>");
|
*nodestr = my_strstr(*nodestr, "?>");
|
||||||
if (!*nodestr) {
|
if (!*nodestr) {
|
||||||
free(strconst);
|
free(strconst);
|
||||||
return (-1);
|
return (-1);
|
||||||
|
}
|
||||||
|
*nodestr += 2;
|
||||||
}
|
}
|
||||||
*nodestr += 2;
|
|
||||||
return (0);
|
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 *xml_parsestr(char *nodestr)
|
||||||
{
|
{
|
||||||
node *n;
|
node *n;
|
||||||
@@ -68,7 +69,7 @@ node *xmlparse(char *path)
|
|||||||
nodestr = malloc(stats.st_size + 1);
|
nodestr = malloc(stats.st_size + 1);
|
||||||
if (nodestr) {
|
if (nodestr) {
|
||||||
count = read(fd, nodestr, stats.st_size);
|
count = read(fd, nodestr, stats.st_size);
|
||||||
nodestr[stats.st_size + 1] = '\0';
|
nodestr[stats.st_size] = '\0';
|
||||||
if (count == stats.st_size)
|
if (count == stats.st_size)
|
||||||
n = xml_parsestr(nodestr);
|
n = xml_parsestr(nodestr);
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-6
@@ -35,18 +35,20 @@ Test(xml, withparam)
|
|||||||
|
|
||||||
Test(xml, withnext)
|
Test(xml, withnext)
|
||||||
{
|
{
|
||||||
char *xml = strdup("<yes params=\"Test\"/><nop/>");
|
char *xml = strdup("<yes params=\"Test\"><nop/><yep/></yes>");
|
||||||
node *n = xml_parsenode(&xml);
|
node *n = xml_parsenode(&xml);
|
||||||
|
|
||||||
cr_assert_str_eq(n->name, "yes");
|
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->key, "params");
|
||||||
cr_assert_str_eq(n->properties->value, "Test");
|
cr_assert_str_eq(n->properties->value, "Test");
|
||||||
cr_assert_eq(n->properties->next, NULL);
|
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)
|
Test(xml, withchild)
|
||||||
|
|||||||
@@ -20,4 +20,25 @@ Test(xml, complete)
|
|||||||
cr_assert_eq(n->child->child, NULL);
|
cr_assert_eq(n->child->child, NULL);
|
||||||
cr_assert_eq(n->child->properties, NULL);
|
cr_assert_eq(n->child->properties, NULL);
|
||||||
cr_assert_eq(n->child->next, 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");
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
<entity id="0">
|
||||||
|
<PositionComponent>
|
||||||
|
<pos>5, 5</pos>
|
||||||
|
<size x="500" y="500"/>
|
||||||
|
</PositionComponent>
|
||||||
|
</entity>
|
||||||
Reference in New Issue
Block a user