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 30-04-2009
* Add support for CDATA * Add support for CDATA
* Fix an off by one problem. * Fix an off by one problem.

View file

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