-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.h
82 lines (65 loc) · 1.81 KB
/
xml.h
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
#ifndef YB_XML_H__
#define YB_XML_H__
#include <string.h>
#include <map>
#include <string>
namespace xml {
template<class T,int N>
struct st_stack_t {
struct T_ { char d[sizeof(T)]; };
int n;
T_ data[N];
st_stack_t() { n = 0; }
int size() const { return n; }
bool empty() const { return n == 0; }
T& at(int i) { return *reinterpret_cast<T*>( &data[i].d[0] ); }
T& push() { return * new( &at(n++) ) T(); }
void pop() { at(--n).~T(); }
T& back() { return at(n-1); }
};
typedef std::map<std::string,std::string> AttrMap;
struct XTag {
enum { Open, Close, SelfClose, PI, DTD, Comment };
int type;
std::string name;
AttrMap attr;
void print( std::ostream& s );
void print( std::ostream& s, const char* p, size_t len );
};
struct XParser;
struct XException {
const char* desc_;
XParser* parser_;
const char* p_;
XException( const char* desc, XParser* parser, const char* p );
};
struct XParser {
// cb
struct Events {
virtual void onTag( XParser&, XTag& ) {}
virtual void onTagText( XParser&, XTag&, const char*, size_t ) {}
};
// data
Events* ev_;
const char* doc_;
const char* tagpos_;
st_stack_t<XTag,64> stack_;
// utils
static bool is_name1( char c );
static bool is_name( char c );
static bool is_space( char c );
static const char* skip_space( const char* p );
static const char* rskip_space( const char* p );
// parsing
const char* parse_comment( const char* p );
const char* parse_dtd( const char* p );
const char* parse_pi( const char* p );
const char* parse_open_tag( const char* p );
const char* parse_close_tag( const char* p );
const char* parse_tag( XTag& tag, const char* p );
const char* parse_text( const char* p );
// public interface
void parse( const char* s, Events* ev );
};
} // namesapce xml
#endif // #ifndef YB_XML_H__