blob: 1be9ba5f72fb7aa5c00a8287af9aed1f766d2e27 [file] [log] [blame]
Alexandre Lision94f06ba2013-12-09 16:28:33 -05001/* $Id$ */
Tristan Matthews0a329cc2013-07-17 13:20:14 -04002/*
3 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#ifndef __PJPP_SCANNER_HPP__
21#define __PJPP_SCANNER_HPP__
22
23#include <pjlib-util/scanner.h>
24#include <pj++/string.hpp>
25
26class Pj_Cis;
27class Pj_Cis_Buffer;
28class Pj_Scanner;
29
30class Pj_Cis_Buffer
31{
32 friend class Pj_Cis;
33
34public:
35 Pj_Cis_Buffer()
36 {
37 pj_cis_buf_init(&buf_);
38 }
39
40private:
41 pj_cis_buf_t buf_;
42};
43
44
45class Pj_Cis
46{
47 friend class Pj_Scanner;
48
49public:
50 Pj_Cis(Pj_Cis_Buffer *buf)
51 {
52 pj_cis_init(&buf->buf_, &cis_);
53 }
54
55 Pj_Cis(const Pj_Cis &rhs)
56 {
57 pj_cis_dup(&cis_, (pj_cis_t*)&rhs.cis_);
58 }
59
60 void add_range(int start, int end)
61 {
62 pj_cis_add_range(&cis_, start, end);
63 }
64
65 void add_alpha()
66 {
67 pj_cis_add_alpha(&cis_);
68 }
69
70 void add_num()
71 {
72 pj_cis_add_num(&cis_);
73 }
74
75 void add_str(const char *str)
76 {
77 pj_cis_add_str(&cis_, str);
78 }
79
80 void add_cis(const Pj_Cis &rhs)
81 {
82 pj_cis_add_cis(&cis_, &rhs.cis_);
83 }
84
85 void del_range(int start, int end)
86 {
87 pj_cis_del_range(&cis_, start, end);
88 }
89
90 void del_str(const char *str)
91 {
92 pj_cis_del_str(&cis_, str);
93 }
94
95 void invert()
96 {
97 pj_cis_invert(&cis_);
98 }
99
100 bool match(int c) const
101 {
102 return pj_cis_match(&cis_, c) != 0;
103 }
104
105private:
106 pj_cis_t cis_;
107};
108
109
110
111class Pj_Scanner
112{
113public:
114 Pj_Scanner() {}
115
116 enum
117 {
118 SYNTAX_ERROR = 101
119 };
120 static void syntax_error_handler_throw_pj(pj_scanner *);
121
122 typedef pj_scan_state State;
123
124 void init(char *buf, int len, unsigned options=PJ_SCAN_AUTOSKIP_WS,
125 pj_syn_err_func_ptr callback = &syntax_error_handler_throw_pj)
126 {
127 pj_scan_init(&scanner_, buf, len, options, callback);
128 }
129
130 void fini()
131 {
132 pj_scan_fini(&scanner_);
133 }
134
135 int eof() const
136 {
137 return pj_scan_is_eof(&scanner_);
138 }
139
140 int peek_char() const
141 {
142 return *scanner_.curptr;
143 }
144
145 int peek(const Pj_Cis *cis, Pj_String *out)
146 {
147 return pj_scan_peek(&scanner_, &cis->cis_, out);
148 }
149
150 int peek_n(pj_size_t len, Pj_String *out)
151 {
152 return pj_scan_peek_n(&scanner_, len, out);
153 }
154
155 int peek_until(const Pj_Cis *cis, Pj_String *out)
156 {
157 return pj_scan_peek_until(&scanner_, &cis->cis_, out);
158 }
159
160 void get(const Pj_Cis *cis, Pj_String *out)
161 {
162 pj_scan_get(&scanner_, &cis->cis_, out);
163 }
164
165 void get_n(unsigned N, Pj_String *out)
166 {
167 pj_scan_get_n(&scanner_, N, out);
168 }
169
170 int get_char()
171 {
172 return pj_scan_get_char(&scanner_);
173 }
174
175 void get_quote(int begin_quote, int end_quote, Pj_String *out)
176 {
177 pj_scan_get_quote(&scanner_, begin_quote, end_quote, out);
178 }
179
180 void get_newline()
181 {
182 pj_scan_get_newline(&scanner_);
183 }
184
185 void get_until(const Pj_Cis *cis, Pj_String *out)
186 {
187 pj_scan_get_until(&scanner_, &cis->cis_, out);
188 }
189
190 void get_until_ch(int until_ch, Pj_String *out)
191 {
192 pj_scan_get_until_ch(&scanner_, until_ch, out);
193 }
194
195 void get_until_chr(const char *spec, Pj_String *out)
196 {
197 pj_scan_get_until_chr(&scanner_, spec, out);
198 }
199
200 void advance_n(unsigned N, bool skip_ws=true)
201 {
202 pj_scan_advance_n(&scanner_, N, skip_ws);
203 }
204
205 int strcmp(const char *s, int len)
206 {
207 return pj_scan_strcmp(&scanner_, s, len);
208 }
209
210 int stricmp(const char *s, int len)
211 {
212 return pj_scan_stricmp(&scanner_, s, len);
213 }
214
215 void skip_ws()
216 {
217 pj_scan_skip_whitespace(&scanner_);
218 }
219
220 void save_state(State *state) const
221 {
222 pj_scan_save_state(&scanner_, state);
223 }
224
225 void restore_state(State *state)
226 {
227 pj_scan_restore_state(&scanner_, state);
228 }
229
230 int get_pos_line() const
231 {
232 return scanner_.line;
233 }
234
235 int get_pos_col() const
236 {
237 return pj_scan_get_col(&scanner_);
238 }
239
240
241private:
242 pj_scanner scanner_;
243};
244
245#endif /* __PJPP_SCANNER_HPP__ */
246