2002-04-30 22:22:54 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// 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
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
#include "windows.h"
|
|
|
|
#endif
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "parse.h"
|
2002-07-11 01:09:53 +00:00
|
|
|
#include "error_numbers.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
bool match_tag(char* buf, char* tag) {
|
2002-07-11 01:09:53 +00:00
|
|
|
if(buf==NULL) {
|
|
|
|
fprintf(stderr, "error: match_tag: unexpected NULL pointer buf\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(tag==NULL) {
|
|
|
|
fprintf(stderr, "error: match_tag: unexpected NULL pointer tag\n");
|
|
|
|
return false;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
if (strstr(buf, tag)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parse_int(char* buf, char* tag, int& x) {
|
2002-07-11 01:09:53 +00:00
|
|
|
if(buf==NULL) {
|
|
|
|
fprintf(stderr, "error: parse_int: unexpected NULL pointer buf\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(tag==NULL) {
|
|
|
|
fprintf(stderr, "error: parse_int: unexpected NULL pointer tag\n");
|
|
|
|
return false;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
char* p = strstr(buf, tag);
|
|
|
|
if (!p) return false;
|
|
|
|
x = atoi(p+strlen(tag));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parse_double(char* buf, char* tag, double& x) {
|
2002-07-11 01:09:53 +00:00
|
|
|
if(buf==NULL) {
|
|
|
|
fprintf(stderr, "error: parse_double: unexpected NULL pointer buf\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(tag==NULL) {
|
|
|
|
fprintf(stderr, "error: parse_double: unexpected NULL pointer tag\n");
|
|
|
|
return false;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
char* p = strstr(buf, tag);
|
|
|
|
if (!p) return false;
|
|
|
|
x = atof(p+strlen(tag));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parse_str(char* buf, char* tag, char* x) {
|
2002-07-11 01:09:53 +00:00
|
|
|
if(buf==NULL) {
|
|
|
|
fprintf(stderr, "error: parse_str: unexpected NULL pointer buf\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(tag==NULL) {
|
|
|
|
fprintf(stderr, "error: parse_str: unexpected NULL pointer tag\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(x==NULL) {
|
|
|
|
fprintf(stderr, "error: parse_str: unexpected NULL pointer x\n");
|
|
|
|
return false;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
char* p = strstr(buf, tag);
|
|
|
|
if (!p) return false;
|
2002-05-17 22:33:57 +00:00
|
|
|
p = strchr(p, '>');
|
2002-04-30 22:22:54 +00:00
|
|
|
char* q = strchr(p+1, '<');
|
|
|
|
*q = 0;
|
2002-05-17 22:33:57 +00:00
|
|
|
strcpy(x, p+1);
|
2002-04-30 22:22:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-05-17 22:33:57 +00:00
|
|
|
void parse_attr(char* buf, char* name, char* out) {
|
|
|
|
char* p, *q;
|
2002-07-11 01:09:53 +00:00
|
|
|
if(buf==NULL) {
|
|
|
|
fprintf(stderr, "error: parse_attr: unexpected NULL pointer buf\n");
|
|
|
|
}
|
|
|
|
if(name==NULL) {
|
|
|
|
fprintf(stderr, "error: parse_attr: unexpected NULL pointer name\n");
|
|
|
|
}
|
|
|
|
if(out==NULL) {
|
|
|
|
fprintf(stderr, "error: parse_attr: unexpected NULL pointer out\n");
|
|
|
|
}
|
2002-05-17 22:33:57 +00:00
|
|
|
strcpy(out, "");
|
|
|
|
p = strstr(buf, name);
|
|
|
|
if (!p) return;
|
|
|
|
p = strchr(p, '"');
|
|
|
|
if (!p) return;
|
|
|
|
q = strchr(p+1, '"');
|
|
|
|
if (!q) return;
|
|
|
|
*q = 0;
|
|
|
|
strcpy(out, p+1);
|
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
void copy_stream(FILE* in, FILE* out) {
|
|
|
|
char buf[1024];
|
|
|
|
int n, m;
|
2002-07-11 01:09:53 +00:00
|
|
|
if(in==NULL) {
|
|
|
|
fprintf(stderr, "error: copy_stream: unexpected NULL pointer in\n");
|
|
|
|
}
|
|
|
|
if(out==NULL) {
|
|
|
|
fprintf(stderr, "error: copy_stream: unexpected NULL pointer out\n");
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
while (1) {
|
|
|
|
n = fread(buf, 1, 1024, in);
|
|
|
|
m = fwrite(buf, 1, n, out);
|
|
|
|
if (n < 1024) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
void strcatdup(char*& p, char* buf) {
|
2002-07-11 01:09:53 +00:00
|
|
|
if(buf==NULL) {
|
|
|
|
fprintf(stderr, "error: strcatdup: unexpected NULL pointer buf\n");
|
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
p = (char*)realloc(p, strlen(p) + strlen(buf)+1);
|
|
|
|
if (!p) {
|
|
|
|
fprintf(stderr, "strcatdup: realloc failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
strcat(p, buf);
|
|
|
|
}
|
|
|
|
|
2002-06-21 06:52:47 +00:00
|
|
|
int dup_element_contents(FILE* in, char* end_tag, char** pp) {
|
|
|
|
char buf[256];
|
2002-07-11 01:09:53 +00:00
|
|
|
if(in==NULL) {
|
|
|
|
fprintf(stderr, "error: dup_element_contents: unexpected NULL pointer in\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
|
|
|
if(end_tag==NULL) {
|
|
|
|
fprintf(stderr, "error: dup_element_contents: unexpected NULL pointer end_tag\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
|
|
|
if(pp==NULL) {
|
|
|
|
fprintf(stderr, "error: dup_element_contents: unexpected NULL pointer pp\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
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");
|
|
|
|
return 1;
|
|
|
|
}
|
2002-07-07 20:39:24 +00:00
|
|
|
|
|
|
|
int read_file_malloc(char* pathname, char*& str) {
|
|
|
|
char buf[256];
|
|
|
|
FILE* f;
|
|
|
|
|
|
|
|
f = fopen(pathname, "r");
|
|
|
|
if (!f) return -1;
|
|
|
|
str = strdup("");
|
|
|
|
while (fgets(buf, 256, f)) {
|
|
|
|
strcatdup(str, buf);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|