From 52c2fe8b33fcb5802e371c334ed97ee2433385af Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 11 Jun 2007 21:30:26 +0000 Subject: [PATCH] - change new XML parser to handle XML comments () client/ log_flags.C lib/ parse.C,h svn path=/trunk/boinc/; revision=12894 --- checkin_notes | 8 ++++++ client/log_flags.C | 15 ++++++----- lib/parse.C | 65 ++++++++++++++++++++++++++++++++++------------ lib/parse.h | 3 ++- 4 files changed, 66 insertions(+), 25 deletions(-) diff --git a/checkin_notes b/checkin_notes index 32b881e1b4..41a28cf88c 100755 --- a/checkin_notes +++ b/checkin_notes @@ -6066,3 +6066,11 @@ David 11 June 2007 client/ cs_statefile.C + +David 11 June 2007 + - change new XML parser to handle XML comments () + + client/ + log_flags.C + lib/ + parse.C,h diff --git a/client/log_flags.C b/client/log_flags.C index 03b0cbc339..b37ee6c14b 100644 --- a/client/log_flags.C +++ b/client/log_flags.C @@ -246,14 +246,15 @@ int CONFIG::parse(FILE* f) { if (!strcmp(tag, "log_flags")) { log_flags.parse(xp); continue; - } else if (!strcmp(tag, "options")) { - parse_options(xp); - } else { - msg_printf(NULL, MSG_USER_ERROR, "Unparsed tag in %s: <%s>\n", - CONFIG_FILE, tag - ); - xp.skip_unexpected(tag); } + if (!strcmp(tag, "options")) { + parse_options(xp); + continue; + } + msg_printf(NULL, MSG_USER_ERROR, "Unparsed tag in %s: <%s>\n", + CONFIG_FILE, tag + ); + xp.skip_unexpected(tag); } msg_printf(NULL, MSG_USER_ERROR, "Missing end tag in %s", CONFIG_FILE); return ERR_XML_PARSE; diff --git a/lib/parse.C b/lib/parse.C index 4144c6cce6..26fbced06d 100644 --- a/lib/parse.C +++ b/lib/parse.C @@ -436,24 +436,52 @@ bool XML_PARSER::scan_nonws(int& first_char) { } } +int XML_PARSER::scan_comment() { + char buf[256]; + char* p = buf; + while (1) { + int c = f->_getc(); + if (c == EOF) return 2; + *p++ = c; + *p = 0; + if (strstr(buf, "-->")) { + return 1; + } + if (strlen(buf) > 32) { + strcpy(buf, buf+16); + p = buf; + } + } +} + // we just read a <; read until we find a >, // and copy intervening text (except spaces) to buf. -// Return true iff reached EOF +// Return: +// 0 if got a tag +// 1 if got a comment (ignore) +// 2 if reached EOF // TODO: parse attributes too // -bool XML_PARSER::scan_tag(char* buf, int len) { +int XML_PARSER::scan_tag(char* buf, int len) { int c; - while (1) { + char* buf_start = buf; + for (int i=0; ; i++) { c = f->_getc(); - if (c == EOF) return true; + if (c == EOF) return 2; if (isspace(c)) continue; if (c == '>') { *buf = 0; - return false; + return 0; } if (--len > 0) { *buf++ = c; } + + // check for comment start + // + if (i==2 && !strncmp(buf_start, "!--", 3)) { + return scan_comment(); + } } } @@ -485,20 +513,23 @@ bool XML_PARSER::get(char* buf, int len, bool& is_tag) { bool eof; int c; - eof = scan_nonws(c); - if (eof) return true; - if (c == '<') { - eof = scan_tag(buf, len); + while (1) { + eof = scan_nonws(c); if (eof) return true; - is_tag = true; - } else { - buf[0] = c; - eof = copy_until_tag(buf+1, len-1); - if (eof) return true; - is_tag = false; + if (c == '<') { + int retval = scan_tag(buf, len); + if (retval == 2) return true; + if (retval == 1) continue; + is_tag = true; + } else { + buf[0] = c; + eof = copy_until_tag(buf+1, len-1); + if (eof) return true; + is_tag = false; + } + strip_whitespace(buf); + return false; } - strip_whitespace(buf); - return false; } // We just parsed "parsed_tag". diff --git a/lib/parse.h b/lib/parse.h index 4dc0269438..3438e96de9 100644 --- a/lib/parse.h +++ b/lib/parse.h @@ -38,7 +38,8 @@ class XML_PARSER { MIOFILE* f; bool scan_nonws(int&); - bool scan_tag(char*, int); + int scan_comment(); + int scan_tag(char*, int); bool copy_until_tag(char*, int); public: XML_PARSER(MIOFILE*);