2011-10-03 21:43:34 +00:00
|
|
|
// test program for XML parser
|
|
|
|
|
2012-08-01 20:04:05 +00:00
|
|
|
#include <stdio.h>
|
2013-05-27 18:45:10 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using std::string;
|
2012-08-01 20:04:05 +00:00
|
|
|
|
2012-08-11 05:47:18 +00:00
|
|
|
#include "parse.h"
|
|
|
|
|
2011-10-03 21:43:34 +00:00
|
|
|
void parse(FILE* f) {
|
|
|
|
bool flag;
|
|
|
|
MIOFILE mf;
|
|
|
|
XML_PARSER xp(&mf);
|
2012-02-14 21:12:57 +00:00
|
|
|
char name[64];
|
|
|
|
char foo[64];
|
2011-10-03 21:43:34 +00:00
|
|
|
int val;
|
|
|
|
double x;
|
2013-05-27 18:45:10 +00:00
|
|
|
string s;
|
2011-10-03 21:43:34 +00:00
|
|
|
|
2013-03-30 05:36:53 +00:00
|
|
|
strcpy(name, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
|
2011-10-03 21:43:34 +00:00
|
|
|
mf.init_file(f);
|
|
|
|
if (!xp.parse_start("blah")) {
|
|
|
|
printf("missing start tag\n");
|
|
|
|
return;
|
|
|
|
}
|
2012-02-14 21:12:57 +00:00
|
|
|
strcpy(foo, "xxx");
|
2011-10-03 21:43:34 +00:00
|
|
|
while (!xp.get_tag()) {
|
2013-03-20 17:41:04 +00:00
|
|
|
printf("get_tag(): is_tag %d text %s\n", xp.is_tag, xp.parsed_tag);
|
2011-10-03 21:43:34 +00:00
|
|
|
if (!xp.is_tag) {
|
|
|
|
printf("unexpected text: %s\n", xp.parsed_tag);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (xp.match_tag("/blah")) {
|
|
|
|
printf("success\n");
|
|
|
|
return;
|
2013-05-27 18:45:10 +00:00
|
|
|
} else if (xp.parse_str("str", name, 64)) {
|
|
|
|
//} else if (xp.parse_string("str", s)) {
|
|
|
|
printf("got str: [%s]\n", s.c_str());
|
2011-10-03 21:43:34 +00:00
|
|
|
} else if (xp.parse_int("int", val)) {
|
|
|
|
printf("got int: %d\n", val);
|
|
|
|
} else if (xp.parse_double("double", x)) {
|
|
|
|
printf("got double: %f\n", x);
|
|
|
|
} else if (xp.parse_bool("bool", flag)) {
|
|
|
|
printf("got bool: %d\n", flag);
|
|
|
|
} else {
|
|
|
|
printf("unparsed tag: %s\n", xp.parsed_tag);
|
|
|
|
xp.skip_unexpected(true, "xml test");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("unexpected EOF\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
FILE* f = fopen("foo.xml", "r");
|
2013-02-26 18:40:31 +00:00
|
|
|
if (!f) {
|
|
|
|
fprintf(stderr, "no file\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-10-03 21:43:34 +00:00
|
|
|
parse(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try it with something like:
|
|
|
|
|
|
|
|
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
|
|
|
<blah>
|
|
|
|
<x>
|
|
|
|
asdlfkj
|
|
|
|
<x> fj</x>
|
|
|
|
</x>
|
|
|
|
<str>blah</str>
|
|
|
|
<int> 6
|
|
|
|
</int>
|
|
|
|
<double>6.555</double>
|
|
|
|
<bool>0</bool>
|
|
|
|
</blah>
|
|
|
|
|
|
|
|
*/
|