Added extract_xml_record() to parse.[Ch]

svn path=/trunk/boinc/; revision=1500
This commit is contained in:
Eric J. Korpela 2003-06-16 19:06:08 +00:00
parent cd2be15c54
commit 6d384113f7
2 changed files with 41 additions and 0 deletions

View File

@ -30,6 +30,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string>
#include "error_numbers.h" #include "error_numbers.h"
#include "util.h" #include "util.h"
@ -219,3 +220,39 @@ char* sgets(char* buf, int len, char*& in) {
in = p+1; in = p+1;
return buf; return buf;
} }
bool extract_xml_record(const std::string &field, const char *tag, std::string &record) {
std::string::size_type start_pos,end_pos,m;
char end_tag[256];
sprintf(end_tag,"/%s",tag);
std::string::size_type i=0,j=0;
// find the end tag
do {
j=field.find(">",j+1);
end_pos=field.rfind(end_tag,j,j-i+1);
i=field.rfind("<",j,j-i);
} while ((end_pos==std::string::npos) && (j!=std::string::npos));
if (j!=std::string::npos) {
end_pos=j;
} else {
return false;
}
// find the start tag
j--; i=0;
do {
j=field.rfind(">",j-1);
start_pos=field.rfind(tag,j,j-i+1);
i=field.rfind("<",j,j-i+1);
m=field.rfind("/",j,j-i+1);
} while (((m!=std::string::npos) || (start_pos==std::string::npos)) && (i!=std::string::npos));
if (i!=std::string::npos) {
start_pos==i;
} else {
return false;
}
record=std::string(field,start_pos,end_pos-start_pos+1);
return true;
}

View File

@ -18,6 +18,8 @@
// //
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string>
extern bool parse(char*, char*); extern bool parse(char*, char*);
extern bool parse_int(char*, char*, int&); extern bool parse_int(char*, char*, int&);
@ -33,3 +35,5 @@ extern int read_file_malloc(char* pathname, char*& str);
extern void replace_element(char* buf, char* start, char* end, char* replacement); extern void replace_element(char* buf, char* start, char* end, char* replacement);
extern void extract_venue(char* in, char* venue_name, char* out); extern void extract_venue(char* in, char* venue_name, char* out);
extern char* sgets(char* buf, int len, char* &in); extern char* sgets(char* buf, int len, char* &in);
extern bool extract_xml_record(const std::string &field, const char *tag, std::string &record);