2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-04-30 22:22:54 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-10-03 06:46:22 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
2003-10-03 06:46:22 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-07-01 20:37:09 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-10-03 06:46:22 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
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.
|
|
|
|
|
2002-06-06 18:50:12 +00:00
|
|
|
#ifdef _WIN32
|
2004-03-04 11:41:43 +00:00
|
|
|
#include "stdafx.h"
|
2002-06-06 18:50:12 +00:00
|
|
|
#endif
|
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifndef _WIN32
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2003-06-16 19:06:08 +00:00
|
|
|
#include <string>
|
2004-03-04 11:41:43 +00:00
|
|
|
using namespace std;
|
|
|
|
#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
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-09-22 23:27:14 +00:00
|
|
|
// return true if the tag appears in the line
|
|
|
|
//
|
2003-06-17 01:03:45 +00:00
|
|
|
bool match_tag(const char* buf, const char* tag) {
|
2004-01-22 17:57:41 +00:00
|
|
|
if (strstr(buf, tag)) return true;
|
2002-04-30 22:22:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-06-17 01:03:45 +00:00
|
|
|
bool match_tag(const std::string &s, const char* tag) {
|
2004-01-22 17:57:41 +00:00
|
|
|
return match_tag(s.c_str(), tag);
|
2003-06-16 21:46:22 +00:00
|
|
|
}
|
|
|
|
|
2002-09-22 23:27:14 +00:00
|
|
|
// parse an integer of the form <tag>1234</tag>
|
|
|
|
// return true if it's there
|
|
|
|
// Note: this doesn't check for the end tag
|
|
|
|
//
|
2003-06-17 01:03:45 +00:00
|
|
|
bool parse_int(const char* buf, const char* tag, int& x) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char* p = strstr(buf, tag);
|
|
|
|
if (!p) return false;
|
2002-10-03 18:33:46 +00:00
|
|
|
x = strtol(p+strlen(tag), 0, 0); // this parses 0xabcd correctly
|
2002-04-30 22:22:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-09-22 23:27:14 +00:00
|
|
|
// Same, for doubles
|
|
|
|
//
|
2003-06-17 01:03:45 +00:00
|
|
|
bool parse_double(const char* buf, const char* tag, double& x) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char* p = strstr(buf, tag);
|
|
|
|
if (!p) return false;
|
|
|
|
x = atof(p+strlen(tag));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
2003-10-03 06:46:22 +00:00
|
|
|
bool parse_str(const char* buf, const char* tag, string& dest) {
|
2004-02-02 21:09:05 +00:00
|
|
|
string str;
|
2003-10-03 06:46:22 +00:00
|
|
|
char const* p = strstr(buf, tag);
|
|
|
|
if (!p) return false;
|
|
|
|
p = strchr(p, '>');
|
|
|
|
++p;
|
|
|
|
char const* q = strchr(p, '<');
|
|
|
|
if (!q) return false;
|
2004-02-02 21:09:05 +00:00
|
|
|
str.assign(p, q-p);
|
|
|
|
strip_whitespace(str);
|
|
|
|
xml_unescape(str, dest);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parse_str(const char* buf, const char* tag, char* dest, int len) {
|
|
|
|
string str;
|
|
|
|
if (!parse_str(buf, tag, str)) return false;
|
|
|
|
safe_strncpy(dest, str.c_str(), len);
|
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) {
|
2002-05-17 22:33:57 +00:00
|
|
|
char* p, *q;
|
2002-07-31 05:59:43 +00:00
|
|
|
|
2002-09-22 23:27:14 +00:00
|
|
|
strcpy(dest, "");
|
2002-05-17 22:33:57 +00:00
|
|
|
p = strstr(buf, name);
|
|
|
|
if (!p) return;
|
|
|
|
p = strchr(p, '"');
|
|
|
|
if (!p) return;
|
|
|
|
q = strchr(p+1, '"');
|
|
|
|
if (!q) return;
|
2004-02-15 19:22:01 +00:00
|
|
|
if (len > q-p) len = q-p;
|
2003-03-06 00:42:18 +00:00
|
|
|
safe_strncpy(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
|
|
|
|
//
|
2002-07-05 05:33:40 +00:00
|
|
|
void strcatdup(char*& p, char* buf) {
|
|
|
|
p = (char*)realloc(p, strlen(p) + strlen(buf)+1);
|
|
|
|
if (!p) {
|
|
|
|
fprintf(stderr, "strcatdup: realloc failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
strcat(p, buf);
|
|
|
|
}
|
|
|
|
|
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];
|
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;
|
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
strcatdup(p, buf);
|
2002-06-21 06:52:47 +00:00
|
|
|
}
|
|
|
|
fprintf(stderr, "dup_element_contents(): no end tag\n");
|
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];
|
|
|
|
|
|
|
|
strcpy(p, "");
|
|
|
|
while (fgets(buf, 256, in)) {
|
|
|
|
if (strstr(buf, end_tag)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
strcat(p, buf);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "copy_element_contents(): no end tag\n");
|
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;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "copy_element_contents(): no end tag\n");
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
strcatdup(str, buf);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-10-14 23:10:12 +00:00
|
|
|
|
2002-12-20 02:12:27 +00:00
|
|
|
|
2002-10-14 23:10:12 +00:00
|
|
|
// replace XML element contents. not currently used
|
|
|
|
//
|
|
|
|
void replace_element(char* buf, char* start, char* end, char* replacement) {
|
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);
|
2003-03-08 23:49:35 +00:00
|
|
|
safe_strncpy(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
|
|
|
|
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;
|
|
|
|
safe_strncpy(buf, in, len);
|
|
|
|
*p = '\n';
|
|
|
|
in = p+1;
|
|
|
|
return buf;
|
|
|
|
}
|
2003-06-16 19:06:08 +00:00
|
|
|
|
2004-02-02 19:44:47 +00:00
|
|
|
void xml_escape(string& in, string& out) {
|
|
|
|
int i;
|
|
|
|
out = "";
|
|
|
|
for (i=0; i<(int)in.length(); i++) {
|
|
|
|
if (in[i] == '<') {
|
|
|
|
out += "<";
|
|
|
|
} else if (in[i] == '&') {
|
|
|
|
out += "&";
|
|
|
|
} else {
|
|
|
|
out += in[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void xml_unescape(string& in, string& out) {
|
|
|
|
int i;
|
|
|
|
out = "";
|
|
|
|
for (i=0; i<(int)in.length(); i++) {
|
|
|
|
if (in.substr(i, 4) == "<") {
|
|
|
|
out += "<";
|
|
|
|
i += 3;
|
|
|
|
} else if (in.substr(i, 5) == "&") {
|
|
|
|
out += "&";
|
|
|
|
i += 4;
|
|
|
|
} else {
|
|
|
|
out += in[i];
|
|
|
|
}
|
|
|
|
}
|
2004-02-02 21:09:05 +00:00
|
|
|
}
|
|
|
|
|