File: xml.h

package info (click to toggle)
btanks 0.9.8083-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 43,616 kB
  • sloc: cpp: 46,425; ansic: 12,005; xml: 4,262; python: 313; sh: 13; makefile: 13
file content (84 lines) | stat: -rw-r--r-- 2,644 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef __BT_XML_H__
#define __BT_XML_H__

/* M-runtime for c++
 * Copyright (C) 2005-2008 Vladimir Menshakov
 *
 * This library 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.

 * This library 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.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/


#include <map>
#include <string>
#include <expat.h>

#include "exception.h"
#include "export_mrt.h"

namespace mrt {
class BaseFile;

DERIVE_EXCEPTION(MRTAPI, XMLException);

class MRTAPI XMLParser {
public:
	struct Attrs : public std::map<const std::string, std::string> {
		inline bool get(const std::string &name, bool defv) const {
			const_iterator i = find(name);
			return i != end()? (i->second == "true" || i->second == "yes"): defv;
		}
		inline int get(const std::string &name, int defv) const {
			const_iterator i = find(name);
			return i != end()? (int)atoi(i->second.c_str()): defv;
		}
		inline float get(const std::string &name, float defv) const {
			const_iterator i = find(name);
			return i != end()? (float)atof(i->second.c_str()): defv;
		}
		inline const std::string get(const std::string &name, const char * defv) const {
			const_iterator i = find(name);
			return i != end()? i->second: std::string(defv);
		}
		inline const std::string get(const std::string &name, const std::string &defv) const {
			const_iterator i = find(name);
			return i != end()? i->second: defv;
		}
	};

	static void get_file_stats(int &tags, const std::string &fname);
	static void get_file_stats(int &tags, const mrt::BaseFile &file);
	virtual void parse_file(const std::string &fname);
	virtual void parse_file(const mrt::BaseFile &file);
	
	virtual void start(const std::string &name, Attrs &attr) = 0;
	virtual void end(const std::string &name) = 0;
	virtual void cdata(const std::string &data);
	
	void clear();
	XMLParser();
	virtual ~XMLParser();

	XMLParser(const XMLParser &);
	const XMLParser& operator=(const XMLParser &);
	
	static const std::string escape(const std::string &str);

private:
	const std::string getErrorMessage() const;
	XML_Parser _parser;
};
}

#endif