From 6d384113f7dc2e23ca017595b6b194f3e388f952 Mon Sep 17 00:00:00 2001 From: "Eric J. Korpela" Date: Mon, 16 Jun 2003 19:06:08 +0000 Subject: [PATCH] Added extract_xml_record() to parse.[Ch] svn path=/trunk/boinc/; revision=1500 --- lib/parse.C | 37 +++++++++++++++++++++++++++++++++++++ lib/parse.h | 4 ++++ 2 files changed, 41 insertions(+) diff --git a/lib/parse.C b/lib/parse.C index fb046084ba..ad4f220784 100644 --- a/lib/parse.C +++ b/lib/parse.C @@ -30,6 +30,7 @@ #include #include +#include #include "error_numbers.h" #include "util.h" @@ -219,3 +220,39 @@ char* sgets(char* buf, int len, char*& in) { in = p+1; 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; +} diff --git a/lib/parse.h b/lib/parse.h index 85fb0ad4f0..5834289b6d 100644 --- a/lib/parse.h +++ b/lib/parse.h @@ -18,6 +18,8 @@ // #include +#include +#include extern bool parse(char*, char*); 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 extract_venue(char* in, char* venue_name, char* out); extern char* sgets(char* buf, int len, char* &in); +extern bool extract_xml_record(const std::string &field, const char *tag, std::string &record); +