2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2004-11-14 08:29:32 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
2003-10-03 06:46:22 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-05-17 22:33:57 +00:00
|
|
|
// A very crude interface for parsing XML files;
|
|
|
|
// assumes all elements are either single-line or
|
|
|
|
// have start and end tags on separate lines.
|
|
|
|
// This is meant to be used ONLY for parsing XML files produced
|
|
|
|
// by the BOINC scheduling server or client.
|
|
|
|
// Could replace this with a more general parser.
|
|
|
|
|
2005-07-14 16:46:38 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__) && !defined(_BOINC_WIN_) && !defined(_AFX_STDAFX_H_)
|
|
|
|
#include "boinc_win.h"
|
|
|
|
#endif
|
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifndef _WIN32
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
2003-06-16 19:06:08 +00:00
|
|
|
#include <string>
|
2005-09-17 21:28:25 +00:00
|
|
|
#include <math.h>
|
2005-09-20 04:36:26 +00:00
|
|
|
#if HAVE_IEEEFP_H
|
|
|
|
#include <ieeefp.h>
|
|
|
|
#endif
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
2004-03-03 19:00:42 +00:00
|
|
|
|
2002-07-11 01:09:53 +00:00
|
|
|
#include "error_numbers.h"
|
2003-03-06 00:42:18 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "parse.h"
|
2004-01-22 02:01:09 +00:00
|
|
|
|
2004-04-30 23:18:56 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
#include "fcgi_stdio.h"
|
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2004-06-30 22:16:26 +00:00
|
|
|
using std::string;
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2005-08-12 18:31:47 +00:00
|
|
|
|
|
|
|
// Parse a boolean; tag is of form "foobar"
|
|
|
|
// Accept either <foobar/> or <foobar>1</foobar>
|
|
|
|
//
|
|
|
|
bool parse_bool(const char* buf, const char* tag, bool& result) {
|
|
|
|
char single_tag[256], start_tag[256];
|
|
|
|
int x;
|
|
|
|
|
|
|
|
sprintf(single_tag, "<%s/>", tag);
|
|
|
|
if (match_tag(buf, single_tag)) {
|
|
|
|
result = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
sprintf(start_tag, "<%s>", tag);
|
|
|
|
if (parse_int(buf, start_tag, x)) {
|
|
|
|
result = (x != 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-11-02 23:08:06 +00:00
|
|
|
// parse a string of the form ...<tag attrs>string</tag>...;
|
2003-10-18 19:35:58 +00:00
|
|
|
// returns the "string" part.
|
2004-02-02 21:09:05 +00:00
|
|
|
// Does XML unescaping (replace < with <)
|
2004-01-21 07:07:16 +00:00
|
|
|
// "string" may not include '<'
|
2003-11-02 23:08:06 +00:00
|
|
|
// Strips white space from ends.
|
|
|
|
// Use "<tag", not "<tag>", if there might be attributes
|
2002-09-22 23:27:14 +00:00
|
|
|
//
|
2006-04-17 22:41:29 +00:00
|
|
|
bool parse_str(const char* buf, const char* tag, char* dest, int destlen) {
|
2004-02-02 21:09:05 +00:00
|
|
|
string str;
|
2005-07-06 09:14:43 +00:00
|
|
|
const char* p;
|
2006-04-17 22:41:29 +00:00
|
|
|
char tempbuf[1024];
|
|
|
|
int len;
|
2004-11-28 11:31:54 +00:00
|
|
|
|
|
|
|
// sanity check on NULL and empty cases.
|
|
|
|
if (!buf || !tag || !strlen(tag))
|
2005-01-13 01:00:46 +00:00
|
|
|
return false;
|
2004-11-28 11:31:54 +00:00
|
|
|
|
|
|
|
p = strstr(buf, tag);
|
2003-10-03 06:46:22 +00:00
|
|
|
if (!p) return false;
|
|
|
|
p = strchr(p, '>');
|
2006-04-18 17:55:14 +00:00
|
|
|
p++;
|
2005-07-06 09:14:43 +00:00
|
|
|
const char* q = strchr(p, '<');
|
2003-10-03 06:46:22 +00:00
|
|
|
if (!q) return false;
|
2006-04-17 22:41:29 +00:00
|
|
|
len = (int)(q-p);
|
|
|
|
if (len >= destlen) len = destlen-1;
|
2006-04-18 17:55:14 +00:00
|
|
|
memcpy(tempbuf, p, len);
|
2006-04-17 22:41:29 +00:00
|
|
|
tempbuf[len] = 0;
|
|
|
|
strip_whitespace(tempbuf);
|
|
|
|
xml_unescape(tempbuf, dest);
|
2004-02-02 21:09:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-04-17 22:41:29 +00:00
|
|
|
bool parse_str(const char* buf, const char* tag, string& dest) {
|
|
|
|
char tempbuf[1024];
|
|
|
|
if (!parse_str(buf, tag, tempbuf, 1024)) return false;
|
|
|
|
dest = tempbuf;
|
2002-04-30 22:22:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-01-21 07:07:16 +00:00
|
|
|
// parse a string of the form name="string";
|
|
|
|
// returns string in dest
|
2002-09-22 23:27:14 +00:00
|
|
|
//
|
2003-06-17 01:03:45 +00:00
|
|
|
void parse_attr(const char* buf, const char* name, char* dest, int len) {
|
2005-08-29 03:33:28 +00:00
|
|
|
const char* p;
|
|
|
|
const char *q;
|
2002-07-31 05:59:43 +00:00
|
|
|
|
2002-09-22 23:27:14 +00:00
|
|
|
strcpy(dest, "");
|
2005-07-20 10:26:47 +00:00
|
|
|
p = strstr(buf, name);
|
2002-05-17 22:33:57 +00:00
|
|
|
if (!p) return;
|
|
|
|
p = strchr(p, '"');
|
|
|
|
if (!p) return;
|
|
|
|
q = strchr(p+1, '"');
|
|
|
|
if (!q) return;
|
2004-12-06 00:08:04 +00:00
|
|
|
if (len > q-p) len = (int)(q-p);
|
2006-01-05 06:42:45 +00:00
|
|
|
strlcpy(dest, p+1, len);
|
2002-05-17 22:33:57 +00:00
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
void copy_stream(FILE* in, FILE* out) {
|
|
|
|
char buf[1024];
|
|
|
|
int n, m;
|
|
|
|
while (1) {
|
2004-03-06 09:45:25 +00:00
|
|
|
n = (int)fread(buf, 1, 1024, in);
|
|
|
|
m = (int)fwrite(buf, 1, n, out);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (n < 1024) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-22 23:27:14 +00:00
|
|
|
// append to a malloc'd string
|
|
|
|
//
|
2006-03-02 22:51:41 +00:00
|
|
|
int strcatdup(char*& p, char* buf) {
|
2002-07-05 05:33:40 +00:00
|
|
|
p = (char*)realloc(p, strlen(p) + strlen(buf)+1);
|
|
|
|
if (!p) {
|
2006-03-02 22:51:41 +00:00
|
|
|
return ERR_MALLOC;
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
strcat(p, buf);
|
2006-03-02 22:51:41 +00:00
|
|
|
return 0;
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
|
2002-09-22 23:27:14 +00:00
|
|
|
// copy from a file to a malloc'd string until the end tag is reached
|
|
|
|
//
|
2003-06-17 01:03:45 +00:00
|
|
|
int dup_element_contents(FILE* in, const char* end_tag, char** pp) {
|
2002-06-21 06:52:47 +00:00
|
|
|
char buf[256];
|
2006-03-02 22:51:41 +00:00
|
|
|
int retval;
|
2002-07-31 05:59:43 +00:00
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
char* p = strdup("");
|
2002-06-21 06:52:47 +00:00
|
|
|
while (fgets(buf, 256, in)) {
|
|
|
|
if (strstr(buf, end_tag)) {
|
|
|
|
*pp = p;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-03-02 22:51:41 +00:00
|
|
|
retval = strcatdup(p, buf);
|
|
|
|
if (retval) return retval;
|
2002-06-21 06:52:47 +00:00
|
|
|
}
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_XML_PARSE;
|
2002-06-21 06:52:47 +00:00
|
|
|
}
|
2002-07-07 20:39:24 +00:00
|
|
|
|
2003-02-11 00:52:44 +00:00
|
|
|
// copy from a file to static buffer
|
|
|
|
//
|
2003-06-17 01:03:45 +00:00
|
|
|
int copy_element_contents(FILE* in, const char* end_tag, char* p, int len) {
|
2003-02-11 00:52:44 +00:00
|
|
|
char buf[256];
|
2004-09-22 21:08:26 +00:00
|
|
|
int n;
|
2003-02-11 00:52:44 +00:00
|
|
|
|
|
|
|
strcpy(p, "");
|
|
|
|
while (fgets(buf, 256, in)) {
|
|
|
|
if (strstr(buf, end_tag)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-10-25 20:16:30 +00:00
|
|
|
n = (int)strlen(buf);
|
2004-09-22 21:08:26 +00:00
|
|
|
if (n >= len-1) return ERR_XML_PARSE;
|
2003-02-11 00:52:44 +00:00
|
|
|
strcat(p, buf);
|
2004-09-22 21:08:26 +00:00
|
|
|
len -= n;
|
2003-02-11 00:52:44 +00:00
|
|
|
}
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_XML_PARSE;
|
2003-02-11 00:52:44 +00:00
|
|
|
}
|
|
|
|
|
2003-11-28 19:19:11 +00:00
|
|
|
int copy_element_contents(FILE* in, const char* end_tag, string& str) {
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
str = "";
|
|
|
|
while (fgets(buf, 256, in)) {
|
|
|
|
if (strstr(buf, end_tag)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
str += buf;
|
|
|
|
}
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
|
|
|
|
2005-08-05 22:00:19 +00:00
|
|
|
void file_to_str(FILE* in, string& str) {
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
str = "";
|
|
|
|
while (fgets(buf, 256, in)) {
|
|
|
|
str += buf;
|
|
|
|
}
|
|
|
|
}
|
2003-11-28 19:19:11 +00:00
|
|
|
|
2002-09-22 23:27:14 +00:00
|
|
|
// read a file into a malloc'd string
|
|
|
|
//
|
2003-06-17 01:03:45 +00:00
|
|
|
int read_file_malloc(const char* pathname, char*& str) {
|
2002-07-07 20:39:24 +00:00
|
|
|
char buf[256];
|
|
|
|
FILE* f;
|
2006-03-02 22:51:41 +00:00
|
|
|
int retval;
|
2002-07-07 20:39:24 +00:00
|
|
|
|
|
|
|
f = fopen(pathname, "r");
|
2003-10-21 04:06:55 +00:00
|
|
|
if (!f) return ERR_FOPEN;
|
2002-07-07 20:39:24 +00:00
|
|
|
str = strdup("");
|
|
|
|
while (fgets(buf, 256, f)) {
|
2006-03-02 22:51:41 +00:00
|
|
|
retval = strcatdup(str, buf);
|
|
|
|
if (retval) return retval;
|
2002-07-07 20:39:24 +00:00
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-10-14 23:10:12 +00:00
|
|
|
|
2002-12-20 02:12:27 +00:00
|
|
|
|
2004-09-13 18:05:54 +00:00
|
|
|
// replace XML element contents (element must be present)
|
2002-10-14 23:10:12 +00:00
|
|
|
//
|
2004-09-13 18:05:54 +00:00
|
|
|
void replace_element_contents(
|
2005-02-16 23:17:43 +00:00
|
|
|
char* buf, const char* start, const char* end, const char* replacement
|
2004-09-13 18:05:54 +00:00
|
|
|
) {
|
2002-12-20 20:20:56 +00:00
|
|
|
char temp[4096], *p, *q;
|
2002-10-14 23:10:12 +00:00
|
|
|
|
|
|
|
p = strstr(buf, start);
|
|
|
|
p += strlen(start);
|
|
|
|
q = strstr(p, end);
|
2006-01-05 06:42:45 +00:00
|
|
|
strlcpy(temp, q, sizeof(temp));
|
2002-10-14 23:10:12 +00:00
|
|
|
strcpy(p, replacement);
|
|
|
|
strcat(p, temp);
|
|
|
|
}
|
2002-12-20 02:12:27 +00:00
|
|
|
|
2004-09-27 19:44:40 +00:00
|
|
|
// if the string contains a substring of the form X...Y,
|
|
|
|
// remove the first such.
|
2005-02-16 23:17:43 +00:00
|
|
|
bool remove_element(char* buf, const char* start, const char* end) {
|
2004-09-27 19:44:40 +00:00
|
|
|
char* p, *q;
|
|
|
|
p = strstr(buf, start);
|
|
|
|
if (!p) return false;
|
|
|
|
q = strstr(p+strlen(start), end);
|
|
|
|
if (!q) return false;
|
|
|
|
strcpy(p, q+strlen(end));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-09-13 18:05:54 +00:00
|
|
|
// replace a substring. Do at most one instance.
|
|
|
|
//
|
2005-02-16 23:17:43 +00:00
|
|
|
bool str_replace(char* str, const char* substr, const char* replacement) {
|
2004-09-13 18:05:54 +00:00
|
|
|
char temp[4096], *p;
|
|
|
|
|
|
|
|
p = strstr(str, substr);
|
|
|
|
if (!p) return false;
|
2004-10-25 20:16:30 +00:00
|
|
|
int n = (int)strlen(substr);
|
2004-09-13 18:05:54 +00:00
|
|
|
strcpy(temp, p+n);
|
|
|
|
strcpy(p, replacement);
|
|
|
|
strcat(p, temp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-03-06 00:42:18 +00:00
|
|
|
// if the given XML has an element of the form
|
|
|
|
// <venue name="venue_name">
|
|
|
|
// ...
|
|
|
|
// </venue>
|
|
|
|
// then return the contents of that element.
|
|
|
|
// Otherwise strip out all <venue> elements
|
|
|
|
//
|
2004-03-31 19:38:25 +00:00
|
|
|
void extract_venue(char* in, char* venue_name, char* out) {
|
2003-03-06 00:42:18 +00:00
|
|
|
char* p, *q;
|
|
|
|
char buf[256];
|
|
|
|
sprintf(buf, "<venue name=\"%s\">", venue_name);
|
|
|
|
p = strstr(in, buf);
|
|
|
|
if (p) {
|
|
|
|
p += strlen(buf);
|
|
|
|
strcpy(out, p);
|
|
|
|
q = strstr(out, "</venue");
|
|
|
|
if (q) *q = 0;
|
|
|
|
} else {
|
|
|
|
strcpy(out, in);
|
|
|
|
while (1) {
|
|
|
|
p = strstr(out, "<venue");
|
|
|
|
if (!p) break;
|
|
|
|
q = strstr(p, "</venue>\n");
|
|
|
|
if (!q) break;
|
|
|
|
strcpy(p, q+strlen("</venue>\n"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-05-20 00:03:39 +00:00
|
|
|
|
|
|
|
// copy a line from the given string.
|
|
|
|
// kinda like fgets() when you're reading from a string
|
|
|
|
//
|
|
|
|
char* sgets(char* buf, int len, char*& in) {
|
|
|
|
char* p;
|
|
|
|
|
|
|
|
p = strstr(in, "\n");
|
|
|
|
if (!p) return NULL;
|
|
|
|
*p = 0;
|
2006-01-05 06:42:45 +00:00
|
|
|
strlcpy(buf, in, len);
|
2003-05-20 00:03:39 +00:00
|
|
|
*p = '\n';
|
|
|
|
in = p+1;
|
|
|
|
return buf;
|
|
|
|
}
|
2003-06-16 19:06:08 +00:00
|
|
|
|
2006-04-17 22:41:29 +00:00
|
|
|
// NOTE: these used to take std::string instead of char* args.
|
|
|
|
// But this performed poorly.
|
|
|
|
//
|
2006-06-01 19:59:57 +00:00
|
|
|
// NOTE: output buffer should be 6X size of input
|
|
|
|
//
|
2006-04-17 22:41:29 +00:00
|
|
|
void xml_escape(const char* in, char* out) {
|
|
|
|
char buf[256], *p;
|
2005-01-13 01:00:46 +00:00
|
|
|
|
2006-04-17 22:41:29 +00:00
|
|
|
p = out;
|
|
|
|
|
|
|
|
for (; *in; in++) {
|
|
|
|
int x = (int) *in;
|
2005-01-13 01:00:46 +00:00
|
|
|
x &= 0xff; // just in case
|
2006-04-17 22:41:29 +00:00
|
|
|
if (x == '<') {
|
|
|
|
strcpy(p, "<");
|
|
|
|
p += 4;
|
|
|
|
} else if (x == '&') {
|
|
|
|
strcpy(p, "&");
|
|
|
|
p += 5;
|
2005-01-14 03:32:16 +00:00
|
|
|
} else if (x>127) {
|
2005-01-13 01:00:46 +00:00
|
|
|
sprintf(buf, "&#%d;", x);
|
2006-04-17 22:41:29 +00:00
|
|
|
strcpy(p, buf);
|
|
|
|
p += strlen(buf);
|
2005-01-14 03:32:16 +00:00
|
|
|
} else if (x<32) {
|
|
|
|
switch(x) {
|
|
|
|
case 9:
|
|
|
|
case 10:
|
|
|
|
case 13:
|
|
|
|
sprintf(buf, "&#%d;", x);
|
2006-04-17 22:41:29 +00:00
|
|
|
strcpy(p, buf);
|
|
|
|
p += strlen(buf);
|
2005-01-14 03:32:16 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-01-13 01:00:46 +00:00
|
|
|
} else {
|
2006-04-17 22:41:29 +00:00
|
|
|
*p++ = x;
|
2005-01-13 01:00:46 +00:00
|
|
|
}
|
|
|
|
}
|
2006-04-17 22:41:29 +00:00
|
|
|
*p = 0;
|
2004-02-02 19:44:47 +00:00
|
|
|
}
|
|
|
|
|
2006-06-01 19:59:57 +00:00
|
|
|
// output buffer need not be larger than input
|
|
|
|
//
|
2006-04-17 22:41:29 +00:00
|
|
|
void xml_unescape(const char* in, char* out) {
|
|
|
|
char* p = out;
|
|
|
|
while (*in) {
|
|
|
|
if (!strncmp(in, "<", 4)) {
|
|
|
|
*p++ = '<';
|
|
|
|
in += 4;
|
|
|
|
} else if (!strncmp(in, "&", 5)) {
|
|
|
|
*p++ = '&';
|
|
|
|
in += 5;
|
|
|
|
} else if (!strncmp(in, "&#", 2)) {
|
|
|
|
in += 2;
|
|
|
|
char c = atoi(in);
|
|
|
|
*p++ = c;
|
|
|
|
in = strchr(in, ';');
|
|
|
|
if (in) in++;
|
2005-01-13 01:00:46 +00:00
|
|
|
} else {
|
2006-04-17 22:41:29 +00:00
|
|
|
*p++ = *in++;
|
2005-01-13 01:00:46 +00:00
|
|
|
}
|
|
|
|
}
|
2006-04-17 22:41:29 +00:00
|
|
|
*p = 0;
|
2004-02-02 21:09:05 +00:00
|
|
|
}
|
2004-08-11 23:52:22 +00:00
|
|
|
|
2005-05-31 21:59:29 +00:00
|
|
|
// we got an unrecognized line.
|
|
|
|
// If it has two <'s (e.g. <foo>xx</foo>) return 0.
|
|
|
|
// If it's of the form <foo> then scan for </foo> and return 0.
|
|
|
|
// Otherwise return ERR_XML_PARSE
|
|
|
|
//
|
|
|
|
int skip_unrecognized(char* buf, FILE* in) {
|
|
|
|
char* p, *q, buf2[256];
|
|
|
|
std::string close_tag;
|
|
|
|
|
|
|
|
p = strchr(buf, '<');
|
|
|
|
if (!p) {
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
|
|
|
if (strchr(p+1, '<')) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
q = strchr(p+1, '>');
|
|
|
|
if (!q) {
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
|
|
|
*q = 0;
|
|
|
|
close_tag = string("</") + string(p+1) + string(">");
|
|
|
|
while (fgets(buf2, 256, in)) {
|
|
|
|
if (strstr(buf2, close_tag.c_str())) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2006-05-25 05:22:10 +00:00
|
|
|
// Get next XML element or tag.
|
|
|
|
// If it's a close tag, or the contents pointer is NULL, just return the tag.
|
2006-06-13 20:27:35 +00:00
|
|
|
// Otherwise return the contents also.
|
|
|
|
//
|
|
|
|
// Skips comments, single- or multi-line (<!-- ... -->)
|
|
|
|
//
|
|
|
|
// Returns false if text was found that wasn't a tag.
|
|
|
|
// (can just call again in this case).
|
|
|
|
//
|
2006-05-25 05:22:10 +00:00
|
|
|
// Note: this is not a general XML parser, but it can parse both
|
|
|
|
// <foo>X</foo>
|
|
|
|
// and
|
|
|
|
// <foo>
|
|
|
|
// X
|
|
|
|
// </foo>
|
|
|
|
//
|
|
|
|
bool get_tag(FILE* f, char* tag, char* contents) {
|
|
|
|
char buf[1024], *p, *q;
|
|
|
|
|
|
|
|
if (contents) *contents = 0;
|
|
|
|
while (1) {
|
2006-06-13 20:27:35 +00:00
|
|
|
if (!fgets(buf, 1024, f)) return false;
|
|
|
|
if (strstr(buf, "<!--")) {
|
|
|
|
while (1) {
|
|
|
|
if (strstr(buf, "-->")) break;
|
|
|
|
if (!fgets(buf, 1024, f)) return false;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-25 05:22:10 +00:00
|
|
|
p = strchr(buf, '<');
|
|
|
|
if (p) break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
|
|
|
|
q = strchr(p, '>');
|
|
|
|
if (!q) return false;
|
|
|
|
|
|
|
|
// see if it's a self-closed tag (like <foo/>)
|
|
|
|
//
|
|
|
|
if (q[-1]=='/') {
|
|
|
|
q[-1] = 0;
|
|
|
|
strcpy(tag, p);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
*q = 0;
|
|
|
|
strcpy(tag, p);
|
|
|
|
|
|
|
|
// see if this is a close tag
|
|
|
|
//
|
|
|
|
if (*p == '/') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!contents) return true;
|
|
|
|
|
|
|
|
// see if close tag is on the same line; copy contents if so
|
|
|
|
//
|
|
|
|
q++;
|
|
|
|
p = strchr(q, '<');
|
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
2006-05-26 19:40:11 +00:00
|
|
|
if (contents) {
|
|
|
|
strcpy(contents, q);
|
|
|
|
strip_whitespace(contents);
|
|
|
|
}
|
2006-05-25 05:22:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close tag is not on same line.
|
|
|
|
// Copy contents until find close tag
|
|
|
|
//
|
|
|
|
while (1) {
|
2006-06-13 20:27:35 +00:00
|
|
|
if (!fgets(buf, 1024, f)) return false;
|
2006-05-25 05:22:10 +00:00
|
|
|
if (strchr(buf, '<')) return true;
|
|
|
|
strip_whitespace(buf);
|
|
|
|
strcat(contents, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_bool(char* contents) {
|
|
|
|
if (!strlen(contents)) return true;
|
|
|
|
if (atoi(contents)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_int(char* contents) {
|
|
|
|
return strtol(contents, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
double get_double(char* contents) {
|
|
|
|
double x = atof(contents);
|
|
|
|
if (finite(x)) return x;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_3f3de9eb18 = "$Id$";
|