(formats/xml) Cleanups

This commit is contained in:
twinaphex 2020-06-28 22:30:53 +02:00
parent b2dbbeb386
commit 47d93f7fb1

View file

@ -31,7 +31,8 @@
#define BUFSIZE 4096
struct rxml_parse_buffer {
struct rxml_parse_buffer
{
char xml[BUFSIZE];
char val[BUFSIZE];
rxml_node_t* stack[32];
@ -89,9 +90,9 @@ static void rxml_free_node(struct rxml_node *node)
rxml_document_t *rxml_load_document(const char *path)
{
rxml_document_t *doc;
rxml_document_t *doc = NULL;
char *memory_buffer = NULL;
long len = 0;
int64_t len = 0;
RFILE *file = filestream_open(path,
RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
@ -124,55 +125,78 @@ error:
rxml_document_t *rxml_load_document_string(const char *str)
{
yxml_t x;
rxml_document_t *doc = NULL;
struct rxml_parse_buffer *buf = NULL;
size_t stack_i = 0;
size_t level = 0;
int c = 0;
char *valptr = NULL;
yxml_t x;
rxml_node_t *node = NULL;
struct rxml_attrib_node *attr = NULL;
buf = (struct rxml_parse_buffer*)malloc(sizeof(*buf));
struct rxml_parse_buffer *buf = (struct rxml_parse_buffer*)
malloc(sizeof(*buf));
if (!buf)
goto error;
return NULL;
valptr = buf->val;
doc = (rxml_document_t*)calloc(1, sizeof(*doc));
doc = (rxml_document_t*)malloc(sizeof(*doc));
if (!doc)
goto error;
doc->root_node->name = NULL;
doc->root_node->data = NULL;
doc->root_node->attrib = NULL;
doc->root_node->children = NULL;
doc->root_node->next = NULL;
yxml_init(&x, buf->xml, BUFSIZE);
for (; *str; ++str) {
for (; *str; ++str)
{
yxml_ret_t r = yxml_parse(&x, *str);
if (r < 0)
goto error;
switch (r) {
switch (r)
{
case YXML_ELEMSTART:
if (node) {
if (level > stack_i) {
if (node)
{
if (level > stack_i)
{
buf->stack[stack_i] = node;
++stack_i;
node->children = (rxml_node_t*)calloc(1, sizeof(*node));
node->children = (rxml_node_t*)
malloc(sizeof(*node));
node->children->name = NULL;
node->children->data = NULL;
node->children->attrib = NULL;
node->children->children = NULL;
node->children->next = NULL;
node = node->children;
}
else {
node->next = (rxml_node_t*)calloc(1, sizeof(*node));
else
{
node->next = (rxml_node_t*)
malloc(sizeof(*node));
node->next->name = NULL;
node->next->data = NULL;
node->next->attrib = NULL;
node->next->children = NULL;
node->next->next = NULL;
node = node->next;
}
}
else {
node = doc->root_node = (rxml_node_t*)calloc(1, sizeof(*node));
}
else
node = doc->root_node = (rxml_node_t*)
calloc(1, sizeof(*node));
if (node->name)
free(node->name);
@ -186,7 +210,8 @@ rxml_document_t *rxml_load_document_string(const char *str)
case YXML_ELEMEND:
--level;
if (valptr > buf->val) {
if (valptr > buf->val)
{
*valptr = '\0';
/* Original code was broken here:
@ -210,14 +235,16 @@ rxml_document_t *rxml_load_document_string(const char *str)
valptr = buf->val;
}
if (level < stack_i) {
if (level < stack_i)
{
--stack_i;
node = buf->stack[stack_i];
}
break;
case YXML_CONTENT:
for (c = 0; c < sizeof(x.data) && x.data[c]; ++c) {
for (c = 0; c < sizeof(x.data) && x.data[c]; ++c)
{
*valptr = x.data[c];
++valptr;
}
@ -237,14 +264,16 @@ rxml_document_t *rxml_load_document_string(const char *str)
break;
case YXML_ATTRVAL:
for(c = 0; c < sizeof(x.data) && x.data[c]; ++c) {
for(c = 0; c < sizeof(x.data) && x.data[c]; ++c)
{
*valptr = x.data[c];
++valptr;
}
break;
case YXML_ATTREND:
if (valptr > buf->val) {
if (valptr > buf->val)
{
*valptr = '\0';
if (attr->value)