Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
4cb3b393
Commit
4cb3b393
authored
Sep 05, 2011
by
Daniel Burke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ttxml: modified it to use a smaller buffer during testing
parent
63afde33
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
78 additions
and
109 deletions
+78
-109
ccan/ttxml/_info
ccan/ttxml/_info
+20
-0
ccan/ttxml/test/run.c
ccan/ttxml/test/run.c
+9
-35
ccan/ttxml/test/test.xml2
ccan/ttxml/test/test.xml2
+1
-9
ccan/ttxml/test/test.xml3
ccan/ttxml/test/test.xml3
+1
-1
ccan/ttxml/test/test.xml4
ccan/ttxml/test/test.xml4
+1
-1
ccan/ttxml/ttxml.c
ccan/ttxml/ttxml.c
+4
-62
ccan/ttxml/ttxml.h
ccan/ttxml/ttxml.h
+42
-1
No files found.
ccan/ttxml/_info
View file @
4cb3b393
...
...
@@ -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>
*/
...
...
ccan/ttxml/test/run.c
View file @
4cb3b393
#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
(
1
3
);
plan_tests
(
1
2
);
ok1
(
x
=
xml_load
(
"./test/test.xml
2
"
));
ok1
(
x
=
xml_load
(
"./test/test.xml
1
"
));
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. */
...
...
ccan/ttxml/test/test.xml2
View file @
4cb3b393
<xmlthisisaverylongtagnameIhopeitmesseswithyourstuff>
foobar
<one foobar barfoo="Hello \"World\"!" foo=bar>
<two/>
</one>
<one></one>
<one></one>
</xml>
<xml/><one barfoo3></one></xml>
ccan/ttxml/test/test.xml3
View file @
4cb3b393
<xml
/><one barfoo></one></xml
>
<xml
tag/
>
ccan/ttxml/test/test.xml4
View file @
4cb3b393
<xmltag
/
>
<xmltag>
ccan/ttxml/ttxml.c
View file @
4cb3b393
...
...
@@ -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
ccan/ttxml/ttxml.h
View file @
4cb3b393
#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 */
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment