1
0
Fork 0

* Various bugfixes, required to get fgrun working

* add testxml as sort of a stress test application
This commit is contained in:
ehofman 2009-05-06 14:29:12 +00:00 committed by Tim Moore
parent 7e4393bfab
commit 7f16fbec70
5 changed files with 255 additions and 97 deletions

View file

@ -1,3 +1,7 @@
05-05-2009
* Various bugfixes, required to get fgrun working
* add testxml as sort of a stress test application
30-04-2009
* Add support for CDATA
* Fix an off by one problem.

View file

@ -1,5 +1,5 @@
noinst_PROGRAMS = xmlgrep printxml
xmlgrep_SOURCES = xmlgrep.c xml.c xml.h
noinst_PROGRAMS = testxml printxml xmlgrep
testxml_SOURCES = testxml.c xml.c
printxml_SOURCES = printxml.c xml.c
xmlgrep_SOURCES = xmlgrep.c xml.c xml.h

68
utils/xmlgrep/sample.xml Normal file
View file

@ -0,0 +1,68 @@
<?xml version="1.0"?>
<Configuration>
<output>
<frequency-hz>48000</frequency-hz>
<interval-hz>20</interval-hz>
<num-speakers>2</num-speakers>
<tmp><!-- --></tmp>
<test n="0" ëlémènt="bjôrn"/>
<test n="1"/>
<test n="2"/>
<menu><name>* Traffic, # taxiing to runway (.</name></menu>
<menu><name>* Traffic, # holding short runway (.</name></menu>
<!--
- x is positive to the right
- y is positive upwards
- z is positive backwards
-->
<speaker n="0">
<channel>0</channel>
<volume-norm>1.0</volume-norm>
<desc><!-- empty --></desc>
</speaker>
<speaker n="1">
<channel>1</channel>
<volume-norm>1.0</volume-norm>
<desc>
<!--
empty --></desc>
</speaker>
<script><![CDATA[
getprop(call(sprintf, size(arg));
c--; x >>= 33;
// --> comment ]>
;]]></script>
</output>
<backend>
<name type="stereo">ALSA Hardware</name>
<Output>
<renderer>hw:0</renderer>
<channels>2</channels>
<!-- periods>16</periods -->
<frequency-hz>44100</frequency-hz>
<bits-per-sample>16</bits-per-sample>
</Output>
<Input>
<renderer>default</renderer>
<frequency-hz>44100</frequency-hz>
</Input>
</backend>
<nasal>
<YF23>
<script><![CDATA[
# If the ground-roll-heading-hold has been reset (<-999) set:
if(agl > 50) {
]]></script>
</YF23>
</nasal>
</Configuration>

123
utils/xmlgrep/testxml.c Normal file
View file

@ -0,0 +1,123 @@
#include <stdio.h>
#include <malloc.h>
#include "xml.h"
#define ROOTNODE "/Configuration/output/menu"
#define LEAFNODE "name"
#define PATH ROOTNODE"/"LEAFNODE
#define BUFLEN 4096
int main()
{
void *root_id;
root_id = xmlOpen("sample.xml");
if (root_id)
{
void *path_id, *node_id;
path_id = xmlNodeGet(root_id, PATH);
node_id = xmlNodeGet(root_id, ROOTNODE);
if (path_id && node_id)
{
char buf[BUFLEN];
size_t len;
char *s;
len = xmlNodeCopyString(root_id, PATH, buf, BUFLEN);
printf("%s = '%s'\n", PATH, buf);
printf("Testing value of /Configuration/output/test:\t\t\t\t");
s = xmlNodeGetString(root_id , "/Configuration/output/test");
if (s)
{
printf("failed.\n\t'%s' shoudl be empty\n", s);
free(s);
}
else
printf("succes.\n");
printf("Testing xmlNodeCopyString against xmlGetString:\t\t\t\t");
if ((s = xmlGetString(path_id)) != 0)
{
if (strcmp(s, buf)) /* not the same */
printf("failed.\n\t'%s' differs from '%s'\n", s, buf);
else
printf("succes.\n");
printf("Testing xmlCopyString against xmlGetString:\t\t\t\t");
xmlCopyString(path_id, buf, BUFLEN);
if (strcmp(s, buf)) /* not the same */
printf("failed.\n\t'%s' differs from '%s'\n", s, buf);
else
printf("succes.\n");
free(s);
}
else
printf("Error while fetching node's value.\n");
printf("Testing xmlCopyString against xmlCompareString:\t\t\t\t");
if (xmlCompareString(path_id, buf)) /* not the same */
printf ("failed.\n\t'%s' differs\n", buf);
else
printf("succes.\n");
printf("Testing xmlCopyString against xmlNodeCompareString:\t\t\t");
if (xmlNodeCompareString(node_id, LEAFNODE, buf)) /* not the same */
printf ("failed.\n\t'%s' differs\n", buf);
else
printf("succes.\n");
printf("Testing xmlCopyString against xmlNodeGetString:\t\t\t\t");
if ((s = xmlNodeGetString(node_id, LEAFNODE)) != 0)
{
if (strcmp(s, buf)) /* not the same */
printf("failed.\n\t'%s' differs from '%s'\n", s, buf);
else
printf("succes.\n");
free(s);
}
else
printf("Error while fetching value from node.\n");
free(path_id);
path_id = xmlNodeGet(root_id, "/Configuration/backend/name");
if (path_id)
{
xmlAttributeCopyString(path_id, "type", buf, BUFLEN);
printf("Testing xmlAttributeCopyString against xmlAttributeCompareString:\t");
if (xmlAttributeCompareString(path_id, "type", buf)) /* no match */
printf("failed.\n\t'%s' differs\n", buf);
else
printf("succes.\n");
printf("Testing xmlAttributeCopyString against xmlAttributeGetString:\t\t");
if ((s = xmlAttributeGetString(path_id, "type")) != 0)
{
if (strcmp(s, buf)) /* not the same */
printf("failed.\n\t'%s' differs from '%s'\n", s, buf);
else
printf("succes.\n");
free(s);
}
else
printf("Error while fetching value from attribute.\n");
}
else
printf("Error while fetching node's attribute.\n");
free(node_id);
free(path_id);
}
else
{
printf("Error: %s\n", xmlErrorGetString(root_id, 1));
}
xmlClose(root_id);
}
return 0;
}

View file

@ -45,6 +45,7 @@ typedef struct
# include <stdio.h>
#endif
#include <stdlib.h> /* free, malloc */
#include <malloc.h>
#include <string.h> /* memcmp */
#ifndef _MSC_VER
#include <strings.h> /* strncasecmp */
@ -117,7 +118,6 @@ struct _xml_id
#endif
};
static char *__xmlNodeCopy(const char *, size_t *, const char **);
static char *__xmlNodeGetPath(const char *, size_t *, char **, size_t *);
static char *__xmlNodeGet(const char *, size_t *, char **, size_t *, size_t *);
static char *__xmlProcessCDATA(char **, size_t *);
@ -496,16 +496,15 @@ xmlCopyString(const void *id, char *buffer, size_t buflen)
assert(buffer != 0);
assert(buflen > 0);
*buffer = '\0';
if (xid->len)
{
size_t len;
char *ps,
char *p;
*buffer = '\0';
ps = xid->start;
p = xid->start;
len = xid->len;
__xmlPrepareData(&ps, &len);
__xmlPrepareData(&p, &len);
if (len)
{
if (len >= buflen)
@ -513,10 +512,10 @@ xmlCopyString(const void *id, char *buffer, size_t buflen)
len = buflen-1;
xmlErrorSet(xid, 0, XML_TRUNCATE_RESULT);
}
memcpy(buffer, ps, len);
memcpy(buffer, p, len);
*(buffer+len) = 0;
ret = len;
}
ret = len;
}
return ret;
@ -556,23 +555,26 @@ xmlNodeGetString(const void *id, const char *path)
if (xid->len)
{
char *p, *node = (char *)path;
size_t slen = strlen(node);
size_t len = xid->len;
char *node = (char *)path;
str = __xmlNodeCopy(xid->start, &len, &path);
slen = strlen(node);
p = __xmlNodeGetPath(xid->start, &len, &node, &slen);
if (p && len)
{
__xmlPrepareData(&p, &len);
str = malloc(len+1);
if (str)
{
size_t len;
char *ps, *pe;
ps = str;
len = strlen(str);
__xmlPrepareData(&ps, &len);
pe = ps + len;
*++pe = 0;
if (len && (ps>str)) memmove(str, ps, len);
else if (!len) *str = 0;
memcpy(str, p, len);
*(str+len) = '\0';
}
else
{
xmlErrorSet(xid, 0, XML_OUT_OF_MEMORY);
}
}
else
{
@ -594,30 +596,29 @@ xmlNodeCopyString(const void *id, const char *path, char *buffer, size_t buflen)
assert(buffer != 0);
assert(buflen > 0);
*buffer = '\0';
if (xid->len)
{
char *str, *node;
size_t slen, len;
char *p, *node = (char *)path;
size_t slen = strlen(node);
size_t len = xid->len;
*buffer = '\0';
len = xid->len;
slen = strlen(path);
node = (char *)path;
str = __xmlNodeGetPath(xid->start, &len, &node, &slen);
if (str)
p = __xmlNodeGetPath(xid->start, &len, &node, &slen);
if (p)
{
__xmlPrepareData(&p, &len);
if (len)
{
char *ps = str;
__xmlPrepareData(&ps, &len);
if (len >= buflen)
{
len = buflen-1;
xmlErrorSet(xid, 0, XML_TRUNCATE_RESULT);
}
memcpy(buffer, ps, len);
*(buffer + len) = '\0';
ret = len;
memcpy(buffer, p, len);
*(buffer+len) = '\0';
}
ret = 0;
}
else if (slen == 0)
{
@ -1299,42 +1300,10 @@ static const char *__xml_error_str[XML_MAX_ERROR] =
};
#endif
char *
__xmlNodeCopy(const char *start, size_t *len, const char **path)
{
char *node, *p, *ret = 0;
size_t rlen, slen;
rlen = *len;
slen = strlen(*path);
node = (char *)*path;
p = __xmlNodeGetPath(start, &rlen, &node, &slen);
if (p && rlen)
{
ret = malloc(rlen+1);
if (ret)
{
memcpy(ret, p, rlen);
*(ret+rlen) = '\0';
}
else
{
xmlErrorSet(0, 0, XML_OUT_OF_MEMORY);
}
}
else if (slen == 0)
{
*path = node;
*len = rlen;
}
return ret;
}
char *
__xmlNodeGetPath(const char *start, size_t *len, char **name, size_t *nlen)
{
char *node;
char *path;
char *ret = 0;
assert(start != 0);
@ -1347,36 +1316,37 @@ __xmlNodeGetPath(const char *start, size_t *len, char **name, size_t *nlen)
if (*nlen > *len) return 0;
node = *name;
if (*node == '/') node++;
if (*node != 0)
path = *name;
if (*path == '/') path++;
if (*path != '\0')
{
size_t blen, plen, slen;
char *path;
size_t num;
size_t num, blocklen, pathlen, nodelen;
char *node;
slen = strlen(node);
node = path;
pathlen = strlen(path);
path = strchr(node, '/');
if (!path) plen = slen;
else plen = path++ - node;
if (!path) nodelen = pathlen;
else nodelen = path++ - node;
num = 0;
blen = *len;
ret = __xmlNodeGet(start, &blen, &node, &plen, &num);
blocklen = *len;
ret = __xmlNodeGet(start, &blocklen, &node, &nodelen, &num);
if (ret)
{
if (path)
{
plen = slen - (path - *name);
ret = __xmlNodeGetPath(ret, &blen, &path, &plen);
ret = __xmlNodeGetPath(ret, &blocklen, &path, &pathlen);
*name = path;
*len = blocklen;
*nlen = pathlen;
}
else
{
*name = node;
*nlen = plen;
*len = blen;
*nlen = nodelen;
*len = blocklen;
}
}
else
@ -1594,13 +1564,6 @@ __xmlNodeGet(const char *start, size_t *len, char **name, size_t *rlen, size_t *
if (start_tag)
{
*len = new-ret-1;
#if 0
if (cdata == ret)
{
ret += 9; /* ![CDATA[[ */
*len -= 12; /* ![CDATA[[ ]]> */
}
#endif
open_element = start_tag;
cdata = (char *)start;
start_tag = 0;