2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2004-11-14 08:29:32 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC 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 3 of the License, or (at your option) any later version.
|
2003-10-03 06:46:22 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2004-06-12 18:44:53 +00:00
|
|
|
#ifndef PARSE_H
|
|
|
|
#define PARSE_H
|
|
|
|
|
2009-02-26 00:23:23 +00:00
|
|
|
#include <cstdio>
|
2008-04-06 03:15:36 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-08-05 15:20:02 +00:00
|
|
|
#include <errno.h>
|
2003-10-03 06:46:22 +00:00
|
|
|
|
2006-08-22 21:52:44 +00:00
|
|
|
#include "miofile.h"
|
2011-08-23 12:10:38 +00:00
|
|
|
#include "str_util.h"
|
2011-08-24 22:16:19 +00:00
|
|
|
#include "cl_boinc.h"
|
2006-08-22 21:52:44 +00:00
|
|
|
|
2011-10-03 21:43:34 +00:00
|
|
|
// see parse_test.cpp for example usage of XML_PARSER
|
|
|
|
|
2006-08-21 22:25:21 +00:00
|
|
|
class XML_PARSER {
|
|
|
|
bool scan_nonws(int&);
|
2007-06-11 21:30:26 +00:00
|
|
|
int scan_comment();
|
2008-09-30 16:57:07 +00:00
|
|
|
int scan_tag(char*, int, char* ab=0, int al=0);
|
2010-01-13 05:32:11 +00:00
|
|
|
int scan_cdata(char*, int);
|
2006-08-23 03:34:30 +00:00
|
|
|
bool copy_until_tag(char*, int);
|
2006-08-21 22:25:21 +00:00
|
|
|
public:
|
2011-08-10 17:11:08 +00:00
|
|
|
char parsed_tag[4096];
|
|
|
|
bool is_tag;
|
2009-02-25 19:18:41 +00:00
|
|
|
MIOFILE* f;
|
2006-08-22 21:52:44 +00:00
|
|
|
XML_PARSER(MIOFILE*);
|
2011-09-18 21:06:49 +00:00
|
|
|
void init(MIOFILE* mf) {
|
|
|
|
f = mf;
|
|
|
|
}
|
2008-09-30 16:57:07 +00:00
|
|
|
bool get(char*, int, bool&, char* ab=0, int al=0);
|
2011-08-10 17:11:08 +00:00
|
|
|
inline bool get_tag(char* ab=0, int al=0) {
|
|
|
|
return get(parsed_tag, sizeof(parsed_tag), is_tag, ab, al);
|
|
|
|
}
|
|
|
|
inline bool match_tag(const char* tag) {
|
|
|
|
return !strcmp(parsed_tag, tag);
|
|
|
|
}
|
2010-07-20 04:19:22 +00:00
|
|
|
int get_aux(char* buf, int len, char* attr_buf, int attr_len);
|
2006-11-03 19:24:21 +00:00
|
|
|
bool parse_start(const char*);
|
2011-08-10 17:11:08 +00:00
|
|
|
bool parse_str(const char*, char*, int);
|
|
|
|
bool parse_string(const char*, std::string&);
|
|
|
|
bool parse_int(const char*, int&);
|
|
|
|
bool parse_double(const char*, double&);
|
2011-09-15 04:24:40 +00:00
|
|
|
bool parse_ulong(const char*, unsigned long&);
|
|
|
|
bool parse_ulonglong(const char*, unsigned long long&);
|
2011-08-10 17:11:08 +00:00
|
|
|
bool parse_bool(const char*, bool&);
|
2006-09-12 18:18:15 +00:00
|
|
|
int element_contents(const char*, char*, int);
|
2011-08-27 16:52:04 +00:00
|
|
|
int copy_element(std::string&);
|
2007-07-03 21:55:50 +00:00
|
|
|
void skip_unexpected(const char*, bool verbose, const char*);
|
2011-08-11 06:17:33 +00:00
|
|
|
void skip_unexpected(bool verbose=false, const char* msg="") {
|
2011-08-10 17:11:08 +00:00
|
|
|
skip_unexpected(parsed_tag, verbose, msg);
|
|
|
|
}
|
2006-08-21 22:25:21 +00:00
|
|
|
};
|
|
|
|
|
2009-06-16 19:22:11 +00:00
|
|
|
extern bool boinc_is_finite(double);
|
|
|
|
|
2006-08-21 22:25:21 +00:00
|
|
|
/////////////// START DEPRECATED XML PARSER
|
|
|
|
// Deprecated because it makes assumptions about
|
|
|
|
// the format of the XML being parsed
|
|
|
|
///////////////
|
|
|
|
|
2006-04-18 17:55:14 +00:00
|
|
|
// return true if the tag appears in the line
|
|
|
|
//
|
|
|
|
inline bool match_tag(const char* buf, const char* tag) {
|
|
|
|
if (strstr(buf, tag)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool match_tag(const std::string &s, const char* tag) {
|
|
|
|
return match_tag(s.c_str(), tag);
|
|
|
|
}
|
|
|
|
|
2011-08-24 23:25:36 +00:00
|
|
|
#ifdef _WIN32
|
2011-08-25 08:18:12 +00:00
|
|
|
#define boinc_strtoull _strtoui64
|
2011-08-26 09:44:50 +00:00
|
|
|
#else
|
2011-09-28 17:33:26 +00:00
|
|
|
#if HAVE_STRTOULL
|
2011-08-25 08:18:12 +00:00
|
|
|
#define boinc_strtoull strtoull
|
|
|
|
#else
|
2011-08-25 08:21:32 +00:00
|
|
|
inline unsigned long long boinc_strtoull(const char *s, char **, int) {
|
2011-08-23 12:10:38 +00:00
|
|
|
char buf[64];
|
|
|
|
char *p;
|
|
|
|
unsigned long long y;
|
|
|
|
strncpy(buf, s, sizeof(buf)-1);
|
|
|
|
strip_whitespace(buf);
|
|
|
|
p = strstr(buf, "0x");
|
|
|
|
if (!p) p = strstr(buf, "0X");
|
|
|
|
if (p) {
|
2011-08-23 12:45:07 +00:00
|
|
|
sscanf(p, "%llx", &y);
|
2011-08-23 12:10:38 +00:00
|
|
|
} else {
|
2011-08-23 12:45:07 +00:00
|
|
|
sscanf(buf, "%llu", &y);
|
2011-08-23 12:10:38 +00:00
|
|
|
}
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
#endif
|
2011-08-26 09:44:50 +00:00
|
|
|
#endif
|
2011-08-23 12:10:38 +00:00
|
|
|
|
2006-04-18 17:55: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
|
|
|
|
//
|
|
|
|
inline bool parse_int(const char* buf, const char* tag, int& x) {
|
|
|
|
const char* p = strstr(buf, tag);
|
|
|
|
if (!p) return false;
|
2011-09-15 04:24:40 +00:00
|
|
|
errno = 0;
|
2008-08-05 15:20:02 +00:00
|
|
|
int y = strtol(p+strlen(tag), 0, 0); // this parses 0xabcd correctly
|
2011-09-15 04:24:40 +00:00
|
|
|
if (errno) return false;
|
2008-08-05 15:20:02 +00:00
|
|
|
x = y;
|
2006-04-18 17:55:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same, for doubles
|
|
|
|
//
|
|
|
|
inline bool parse_double(const char* buf, const char* tag, double& x) {
|
|
|
|
double y;
|
|
|
|
const char* p = strstr(buf, tag);
|
|
|
|
if (!p) return false;
|
2011-09-15 04:24:40 +00:00
|
|
|
errno = 0;
|
|
|
|
y = strtod(p+strlen(tag), NULL);
|
|
|
|
if (errno) return false;
|
2009-06-16 19:22:11 +00:00
|
|
|
if (!boinc_is_finite(y)) {
|
|
|
|
return false;
|
2006-04-18 17:55:14 +00:00
|
|
|
}
|
2009-06-16 19:22:11 +00:00
|
|
|
x = y;
|
|
|
|
return true;
|
2006-04-18 17:55:14 +00:00
|
|
|
}
|
|
|
|
|
2011-09-15 04:24:40 +00:00
|
|
|
#if 0
|
2011-08-23 11:29:11 +00:00
|
|
|
// Same, for unsigned long
|
|
|
|
//
|
|
|
|
inline bool parse_ulong(const char* buf, const char* tag, unsigned long& x) {
|
|
|
|
const char* p = strstr(buf, tag);
|
|
|
|
if (!p) return false;
|
2011-09-15 04:24:40 +00:00
|
|
|
errno = 0;
|
|
|
|
unsigned long y = strtoul(p+strlen(tag), NULL, 0);
|
|
|
|
if (errno) return false;
|
2011-08-23 11:29:11 +00:00
|
|
|
x = y;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-24 21:41:02 +00:00
|
|
|
// Same, for unsigned long long
|
|
|
|
//
|
2011-09-15 04:24:40 +00:00
|
|
|
inline bool parse_ulonglong(
|
|
|
|
const char* buf, const char* tag, unsigned long long& x
|
|
|
|
) {
|
2011-08-24 21:41:02 +00:00
|
|
|
const char* p = strstr(buf, tag);
|
|
|
|
if (!p) return false;
|
2011-09-15 04:24:40 +00:00
|
|
|
errno = 0;
|
|
|
|
unsigned long long y = boinc_strtoull(p+strlen(tag), NULL, 0);
|
|
|
|
if (errno) return false;
|
2011-08-24 08:12:45 +00:00
|
|
|
x = y;
|
2011-08-24 21:41:02 +00:00
|
|
|
return true;
|
2011-09-15 04:24:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-06-17 01:03:45 +00:00
|
|
|
extern bool parse(char* , char* );
|
|
|
|
extern bool parse_str(const char*, const char*, char*, int);
|
2004-06-30 18:17:21 +00:00
|
|
|
extern bool parse_str(const char* buf, const char* tag, std::string& dest);
|
2003-06-17 18:43:13 +00:00
|
|
|
extern void parse_attr(const char* buf, const char* attrname, char* out, int len);
|
2005-08-12 18:31:47 +00:00
|
|
|
extern bool parse_bool(const char*, const char*, bool&);
|
2006-08-21 22:25:21 +00:00
|
|
|
|
|
|
|
/////////////// END DEPRECATED XML PARSER
|
|
|
|
|
2007-07-10 23:42:05 +00:00
|
|
|
extern int copy_stream(FILE* in, FILE* out);
|
2006-03-02 22:51:41 +00:00
|
|
|
extern int strcatdup(char*& p, char* buf);
|
2003-06-17 01:03:45 +00:00
|
|
|
extern int dup_element_contents(FILE* in, const char* end_tag, char** pp);
|
2008-09-04 08:33:21 +00:00
|
|
|
extern int dup_element(FILE* in, const char* end_tag, char** pp);
|
2003-06-17 01:03:45 +00:00
|
|
|
extern int copy_element_contents(FILE* in, const char* end_tag, char* p, int len);
|
2004-06-30 18:17:21 +00:00
|
|
|
extern int copy_element_contents(FILE* in, const char* end_tag, std::string&);
|
2004-09-13 18:05:54 +00:00
|
|
|
extern 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
|
|
|
);
|
2005-02-16 23:17:43 +00:00
|
|
|
extern bool remove_element(char* buf, const char* start, const char* end);
|
|
|
|
extern bool str_replace(char* str, const char* old, const char* neww);
|
2003-05-20 00:03:39 +00:00
|
|
|
extern char* sgets(char* buf, int len, char* &in);
|
2011-05-25 16:40:19 +00:00
|
|
|
extern void non_ascii_escape(const char*, char*, int len);
|
2008-08-13 17:27:13 +00:00
|
|
|
extern void xml_escape(const char*, char*, int len);
|
2009-08-07 18:16:21 +00:00
|
|
|
extern void xml_unescape(std::string&);
|
2011-09-14 17:58:53 +00:00
|
|
|
extern void xml_unescape(char*);
|
2006-11-07 17:40:55 +00:00
|
|
|
extern void extract_venue(const char*, const char*, char*);
|
2007-06-08 07:55:27 +00:00
|
|
|
extern int skip_unrecognized(char* buf, MIOFILE&);
|
2004-06-12 18:44:53 +00:00
|
|
|
|
|
|
|
#endif
|