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