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
|
|
|
|
|
2006-04-18 18:21:12 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "boinc_win.h"
|
|
|
|
#else
|
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>
|
2004-06-10 07:49:50 +00:00
|
|
|
#endif
|
2003-10-03 06:46:22 +00:00
|
|
|
|
2006-08-22 21:52:44 +00:00
|
|
|
#include "miofile.h"
|
|
|
|
|
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);
|
2006-08-23 03:34:30 +00:00
|
|
|
bool copy_until_tag(char*, int);
|
2006-08-21 22:25:21 +00:00
|
|
|
public:
|
2009-02-25 19:18:41 +00:00
|
|
|
MIOFILE* f;
|
2006-08-22 21:52:44 +00:00
|
|
|
XML_PARSER(MIOFILE*);
|
2008-09-30 16:57:07 +00:00
|
|
|
bool get(char*, int, bool&, char* ab=0, int al=0);
|
2006-11-03 19:24:21 +00:00
|
|
|
bool parse_start(const char*);
|
|
|
|
bool parse_str(char*, const char*, char*, int);
|
|
|
|
bool parse_string(char*, const char*, std::string&);
|
|
|
|
bool parse_int(char*, const char*, int&);
|
|
|
|
bool parse_double(char*, const char*, double&);
|
|
|
|
bool parse_bool(char*, const char*, bool&);
|
2006-09-12 18:18:15 +00:00
|
|
|
int element_contents(const char*, char*, int);
|
2007-07-03 21:55:50 +00:00
|
|
|
void skip_unexpected(const char*, bool verbose, const char*);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2008-08-05 15:20:02 +00:00
|
|
|
int y = strtol(p+strlen(tag), 0, 0); // this parses 0xabcd correctly
|
|
|
|
if (errno == ERANGE) return false;
|
|
|
|
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;
|
|
|
|
y = atof(p+strlen(tag));
|
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
|
|
|
}
|
|
|
|
|
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);
|
2008-08-13 17:27:13 +00:00
|
|
|
extern void xml_escape(const char*, char*, int len);
|
|
|
|
extern void xml_unescape(const char*, char*, int len);
|
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
|