-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsoap.h
151 lines (109 loc) · 2.86 KB
/
soap.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef __SOAP_H
#define __SOAP_H
namespace soap
{
class string
{
protected:
char* ptr;
int size;
public:
string(void):ptr(0),size(0) {}
~string(void) { clear(); }
void clear(void);
int length(void) const { return size; }
const char* c_str(void) const { return ptr?ptr:""; }
void swap(string& s);
void trim_right(void);
friend class string_builder;
friend class ctx;
};
struct chunk
{
enum { max_size=64 };
char* ptr;
int size;
chunk* next;
};
class string_builder
{
protected:
chunk *beg,*end;
int add_chunk(void);
public:
string_builder(void):beg(0),end(0) {}
~string_builder(void) { clear(); }
void clear(void);
void add(int ch)
{
if(!end || end->size>=chunk::max_size)
if(add_chunk())
return;
((unsigned char*)end->ptr)[end->size++]=ch;
}
void add(const char* s,int len);
void swap(string& s);
};
struct node
{
node* parent;
node* next;
node* beg;
node* end;
char* name;
char* data;
int len;
node(void):parent(0),next(0),beg(0),end(0),name(0),data(0),len(0) {}
~node(void) { clear(); }
void init(void) { parent=next=beg=end=0; name=data=0; len=0; }
node* add_node(void);
void clear(void);
node* find_child(const char* s,int l);
node* find(const char* s);
const char* operator[](const char* s)
{
return find_data(s);
}
const char* find_data(const char* s)
{
node* pp=find(s);
if(pp && pp->data)
return pp->data;
return "";
}
};
class ctx
{
protected:
node* cur;
short st;
short st_close_tag;
short st_quot;
short st_text;
short err;
string_builder data;
enum { max_tok_size=64 };
char tok[max_tok_size];
int tok_size;
void tok_add(unsigned char ch)
{
if(tok_size<max_tok_size-1)
((unsigned char*)tok)[tok_size++]=ch;
}
void tok_reset(void) { tok_size=0; }
void ch_push(unsigned char ch) { data.add(ch); }
void tok_push(void);
void tag_open(const char* s,int len);
void tag_close(const char* s,int len);
void data_push(void);
public:
int line;
public:
ctx(node* root):cur(root),st(0),err(0),line(0),tok_size(0),st_close_tag(0),st_quot(0),st_text(0) {}
void begin(void);
int parse(const char* buf,int len);
int end(void);
};
int parse(const char* s,int l,node* root);
}
#endif /* __SOAP_H */