mirror of https://github.com/BOINC/boinc.git
- lib: move XML parse test program to its own file.
Note: XML_PARSER::parse_str() doesn't currently work right for something like <foo><a>xx</a></foo>. It should return "<a>xx</a>". TODO. svn path=/trunk/boinc/; revision=24325
This commit is contained in:
parent
36fc4b7bb3
commit
57fc54ae53
|
@ -6730,3 +6730,16 @@ David 3 Oct 2011
|
|||
|
||||
html/user/
|
||||
profile_menu.php
|
||||
|
||||
David 3 Oct 2011
|
||||
- lib: move XML parse test program to its own file.
|
||||
|
||||
Note: XML_PARSER::parse_str() doesn't currently work right
|
||||
for something like <foo><a>xx</a></foo>.
|
||||
It should return "<a>xx</a>".
|
||||
TODO.
|
||||
|
||||
lib/
|
||||
parse.cpp,h
|
||||
parse_test.cpp (new)
|
||||
Makefile.am
|
||||
|
|
|
@ -8,7 +8,7 @@ bin_PROGRAMS =
|
|||
# Stuff needed for server builds goes here.
|
||||
if ENABLE_SERVER
|
||||
|
||||
bin_PROGRAMS += crypt_prog
|
||||
bin_PROGRAMS += crypt_prog parse_test
|
||||
|
||||
endif
|
||||
# end of "if ENABLE_SERVER"
|
||||
|
@ -221,4 +221,6 @@ msg_test_LDADD = $(LIBBOINC)
|
|||
crypt_prog_SOURCES = crypt_prog.cpp
|
||||
crypt_prog_CXXFLAGS = $(PTHREAD_CFLAGS)
|
||||
crypt_prog_LDADD = $(LIBBOINC_CRYPT_STATIC) $(LIBBOINC) $(SSL_LIBS)
|
||||
|
||||
parse_test_SOURCES = parse_test.cpp
|
||||
parse_test_CXXFLAGS = $(PTHREAD_CFLAGS)
|
||||
parse_test_LDADD = $(LIBBOINC)
|
||||
|
|
|
@ -649,7 +649,9 @@ int XML_PARSER::get_aux(char* buf, int len, char* attr_buf, int attr_len) {
|
|||
}
|
||||
}
|
||||
|
||||
bool XML_PARSER::get(char* buf, int len, bool& _is_tag, char* attr_buf, int attr_len) {
|
||||
bool XML_PARSER::get(
|
||||
char* buf, int len, bool& _is_tag, char* attr_buf, int attr_len
|
||||
) {
|
||||
switch (get_aux(buf, len, attr_buf, attr_len)) {
|
||||
case XML_PARSE_EOF: return true;
|
||||
case XML_PARSE_TAG:
|
||||
|
@ -1006,66 +1008,3 @@ int XML_PARSER::copy_element(string& out) {
|
|||
out += end_tag;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// sample use is shown below
|
||||
|
||||
#if 0
|
||||
void parse(FILE* f) {
|
||||
bool flag;
|
||||
MIOFILE mf;
|
||||
XML_PARSER xp(&mf);
|
||||
char name[256];
|
||||
int val;
|
||||
double x;
|
||||
|
||||
mf.init_file(f);
|
||||
if (!xp.parse_start("blah")) {
|
||||
printf("missing start tag\n");
|
||||
return;
|
||||
}
|
||||
while (!xp.get_tag()) {
|
||||
if (!xp.is_tag) {
|
||||
printf("unexpected text: %s\n", tag);
|
||||
continue;
|
||||
}
|
||||
if (xp.match_tag("/blah")) {
|
||||
printf("success\n");
|
||||
return;
|
||||
} else if (xp.parse_str("str", name, sizeof(name))) {
|
||||
printf("got str: %s\n", name);
|
||||
} 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");
|
||||
parse(f);
|
||||
}
|
||||
|
||||
... and run it against, e.g.:
|
||||
|
||||
<?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>
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -27,6 +27,8 @@
|
|||
#include "str_util.h"
|
||||
#include "cl_boinc.h"
|
||||
|
||||
// see parse_test.cpp for example usage of XML_PARSER
|
||||
|
||||
class XML_PARSER {
|
||||
bool scan_nonws(int&);
|
||||
int scan_comment();
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
// test program for XML parser
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "parse.h"
|
||||
|
||||
void parse(FILE* f) {
|
||||
bool flag;
|
||||
MIOFILE mf;
|
||||
XML_PARSER xp(&mf);
|
||||
char name[256];
|
||||
int val;
|
||||
double x;
|
||||
|
||||
mf.init_file(f);
|
||||
if (!xp.parse_start("blah")) {
|
||||
printf("missing start tag\n");
|
||||
return;
|
||||
}
|
||||
while (!xp.get_tag()) {
|
||||
if (!xp.is_tag) {
|
||||
printf("unexpected text: %s\n", xp.parsed_tag);
|
||||
continue;
|
||||
}
|
||||
if (xp.match_tag("/blah")) {
|
||||
printf("success\n");
|
||||
return;
|
||||
} else if (xp.parse_str("str", name, sizeof(name))) {
|
||||
printf("got str: %s\n", name);
|
||||
} 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");
|
||||
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>
|
||||
|
||||
*/
|
Loading…
Reference in New Issue