Commit 4cb3b393 authored by Daniel Burke's avatar Daniel Burke

ttxml: modified it to use a smaller buffer during testing

parent 63afde33
......@@ -6,6 +6,26 @@
*
* This parses an XML file into a convenient data structure.
*
* Example:
* #include <ccan/ttxml/ttxml.h>
* #include <stdio.h>
*
* int main(int argc, char *argv[])
* {
* XmlNode *xml, *tmp;
*
* xml = xml_load("./test/test.xml2");
* if(!xml)return 1;
*
* tmp = xml_find(xml, "childnode");
*
* printf("%s: %s\n", xml->name, xml_attr(tmp, "attribute"));
*
* xml_free(xml);
*
* return 0;
* }
*
* License: GPL
* Author: Daniel Burke <dan.p.burke@gmail.com>
*/
......
#include <ccan/ttxml/ttxml.h>
/* Include the C files directly. */
#define BUFFER 40 /* use a stupidly small buffer to stomp out bugs */
#include <ccan/ttxml/ttxml.c>
#include <ccan/tap/tap.h>
......@@ -35,41 +38,13 @@ static int test_load(const char * filename)
return 1;
}
static int test_find(void)
{
char *ctmp;
XmlNode *xtmp, *xml = xml_load("./test/test.xml2");
if(!xml)return 0;
xp(xml, 0, 20);
xtmp = xml_find(xml, "one");
if(!xtmp)
{
printf("Failed to find node\n");
return 0;
}
printf("node is...\n");
xp(xtmp, 0, 20);
ctmp = xml_attr(xtmp, "barfoo");
if(!ctmp)
{
printf("Failed to find attribute\n");
return 0;
}
return 1;
}
int main(void)
{
XmlNode *x, *t;
/* This is how many tests you plan to run */
plan_tests(13);
plan_tests(12);
ok1(x = xml_load("./test/test.xml2"));
ok1(x = xml_load("./test/test.xml1"));
ok1(!xml_find(x, "Doesn't Exist"));
ok1(t = xml_find(x, "one"));
ok1(xml_find(t, "two"));
......@@ -79,11 +54,10 @@ int main(void)
xml_free(x);
/* Simple thing we expect to succeed */
ok1(!test_load("does not exist")); /* A file that doesn't exist */
ok1(test_load("./test/test.xml")); /* A very large xml file. */
ok1(test_load("./test/test.xml2")); /* A basic xml file. */
ok1(test_load("./test/test.xml3")); /* Very small well-formed xml file. */
ok1(test_load("./test/test.xml4")); /* Smallest well-formed xml file. */
ok1(test_load("./test/test.xml5")); /* A single unclosed tag. */
ok1(test_load("./test/test.xml1")); /* A basic xml file. */
ok1(test_load("./test/test.xml2")); /* Very small well-formed xml file. */
ok1(test_load("./test/test.xml3")); /* Smallest well-formed xml file. */
ok1(test_load("./test/test.xml4")); /* A single unclosed tag. */
/* Same, with an explicit description of the test. */
// ok(some_test(), "%s with no args should return 1", "some_test")
/* How to print out messages for debugging. */
......
<xmlthisisaverylongtagnameIhopeitmesseswithyourstuff>
foobar
<one foobar barfoo="Hello \"World\"!" foo=bar>
<two/>
</one>
<one></one>
<one></one>
</xml>
<xml/><one barfoo3></one></xml>
<xml/><one barfoo></one></xml>
<xmltag/>
......@@ -6,8 +6,10 @@
#include "ttxml.h"
#ifndef BUFFER
#define BUFFER 3264
#endif
#define XML_LETTER 1
#define XML_NUMBER 2
......@@ -33,7 +35,7 @@ typedef struct XMLBUF
/* Allocate a new XmlNode */
XmlNode* xml_new(char * name)
static XmlNode* xml_new(char * name)
{
XmlNode * ret = malloc(sizeof(XmlNode));
if(!ret)return NULL;
......@@ -95,7 +97,6 @@ static void xml_read_file(XMLBUF *xml)
size = fread( xml->buf, 1, xml->len, xml->fptr);
if( size != xml->len )
{
printf("Buffer reduction\n");
xml->len = size;
xml->buf[size]=0;
xml->eof = 1;
......@@ -333,10 +334,7 @@ XmlNode* xml_load(const char * filename)
xml.read_index = 0;
xml.fptr = fopen(filename, "rb");
if(!xml.fptr)
{
printf("Opening file failed\n");
return NULL;
}
xml.buf = malloc(BUFFER+1);
xml.buf[BUFFER]=0;
......@@ -384,59 +382,3 @@ char* xml_attr(XmlNode *x, const char *name)
}
#ifdef TEST
/* print out the heirarchy of an XML file, useful for debugging */
void xp(XmlNode *x, int level, int max)
{
int i;
char text[] = "text";
char *name = text;
if(level > max)return;
if(!x)return;
if(x->name)name = x->name;
for(i=0; i<level; i++)printf(" ");
printf("%s:", name);
if(x->name)
for(i=0; i<x->nattrib; i++)
printf("%s=\"%s\",", x->attrib[i*2], x->attrib[i*2+1]);
else printf("%s", x->attrib[0]);
printf("\n");
if(x->child)xp(x->child, level+1, max);
if(x->next)xp(x->next, level, max);
}
int main(int argc, char *argv[])
{
XmlNode *x, *tmp;
if(!argv[1])
{
printf("USAGE: %s name\n\t reads name where name is an XML file.\n",
argv[0]);
return 1;
}
#ifdef PROFILE
for(int i=0; i<1000; i++)
{
#endif
x = xml_load(argv[1]);
if(!x)
{
printf("Failed to load.\n");
return 2;
}
#ifndef PROFILE
xp(x, 1, 20);
#endif
xml_free(x);
#ifdef PROFILE
}
#endif
return 0;
}
#endif
#ifndef CCAN_TTXML_H
#define CCAN_TTXML_H
/**
* ttxml - tiny XML library for parsing (trusted!) XML documents.
*
* This parses an XML file into a convenient data structure.
*
* Example:
* #include <ccan/ttxml/ttxml.h>
* #include <stdio.h>
*
* int main(int argc, char *argv[])
* {
* XmlNode *xml, *tmp;
*
* xml = xml_load("./test/test.xml2");
* if(!xml)return 1;
*
* tmp = xml_find(xml, "childnode");
*
* printf("%s: %s\n", xml->name, xml_attr(tmp, "attribute"));
*
* xml_free(xml);
*
* return 0;
* }
*
* License: GPL
* Author: Daniel Burke <dan.p.burke@gmail.com>
*/
/* Every node is one of these */
typedef struct XmlNode {
char * name;
char ** attrib;
......@@ -7,10 +39,19 @@ typedef struct XmlNode {
struct XmlNode * next;
} XmlNode;
/* It's all pretty straight forward except for the attrib.
*
* Attrib is an array of char*, that is 2x the size of nattrib.
* Each pair of char* points to the attribute name & the attribute value,
* if present.
*
* If it's a text node, then name = "text", and attrib[1] = the body of text.
* This is the only case where there will be an attribute with a null name.
*/
XmlNode* xml_new(char * name);
XmlNode* xml_load(const char * filename);
void xml_free(XmlNode *target);
char* xml_attr(XmlNode *x, const char *name);
XmlNode * xml_find(XmlNode *xml, const char *name);
#endif /* CCAN_TTXML_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment