1 | /** \file Parse.cpp - parse a string |
---|
2 | ** |
---|
3 | ** Written: 1999-Feb-10 grymse@alhem.net |
---|
4 | **/ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 1999-2007 Anders Hedstrom |
---|
8 | |
---|
9 | This library is made available under the terms of the GNU GPL. |
---|
10 | |
---|
11 | If you would like to use this library in a closed-source application, |
---|
12 | a separate license agreement is available. For information about |
---|
13 | the closed-source license agreement for the C++ sockets library, |
---|
14 | please visit http://www.alhem.net/Sockets/license.html and/or |
---|
15 | email license@alhem.net. |
---|
16 | |
---|
17 | This program is free software; you can redistribute it and/or |
---|
18 | modify it under the terms of the GNU General Public License |
---|
19 | as published by the Free Software Foundation; either version 2 |
---|
20 | of the License, or (at your option) any later version. |
---|
21 | |
---|
22 | This program is distributed in the hope that it will be useful, |
---|
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
25 | GNU General Public License for more details. |
---|
26 | |
---|
27 | You should have received a copy of the GNU General Public License |
---|
28 | along with this program; if not, write to the Free Software |
---|
29 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
30 | */ |
---|
31 | #include <stdlib.h> |
---|
32 | #include <string.h> |
---|
33 | |
---|
34 | #include "Parse.h" |
---|
35 | |
---|
36 | |
---|
37 | #ifdef SOCKETS_NAMESPACE |
---|
38 | namespace SOCKETS_NAMESPACE { |
---|
39 | #endif |
---|
40 | |
---|
41 | |
---|
42 | /* implementation of class Parse */ |
---|
43 | |
---|
44 | Parse::Parse() |
---|
45 | :pa_the_str("") |
---|
46 | ,pa_splits("") |
---|
47 | ,pa_ord("") |
---|
48 | ,pa_the_ptr(0) |
---|
49 | ,pa_breakchar(0) |
---|
50 | ,pa_enable(0) |
---|
51 | ,pa_disable(0) |
---|
52 | ,pa_nospace(0) |
---|
53 | ,pa_quote(false) |
---|
54 | { |
---|
55 | } |
---|
56 | |
---|
57 | Parse::Parse(const std::string&s) |
---|
58 | :pa_the_str(s) |
---|
59 | ,pa_splits("") |
---|
60 | ,pa_ord("") |
---|
61 | ,pa_the_ptr(0) |
---|
62 | ,pa_breakchar(0) |
---|
63 | ,pa_enable(0) |
---|
64 | ,pa_disable(0) |
---|
65 | ,pa_nospace(0) |
---|
66 | ,pa_quote(false) |
---|
67 | { |
---|
68 | } |
---|
69 | |
---|
70 | Parse::Parse(const std::string&s,const std::string&sp) |
---|
71 | :pa_the_str(s) |
---|
72 | ,pa_splits(sp) |
---|
73 | ,pa_ord("") |
---|
74 | ,pa_the_ptr(0) |
---|
75 | ,pa_breakchar(0) |
---|
76 | ,pa_enable(0) |
---|
77 | ,pa_disable(0) |
---|
78 | ,pa_nospace(0) |
---|
79 | ,pa_quote(false) |
---|
80 | { |
---|
81 | } |
---|
82 | |
---|
83 | Parse::Parse(const std::string&s,const std::string&sp,short /*nospace*/) |
---|
84 | :pa_the_str(s) |
---|
85 | ,pa_splits(sp) |
---|
86 | ,pa_ord("") |
---|
87 | ,pa_the_ptr(0) |
---|
88 | ,pa_breakchar(0) |
---|
89 | ,pa_enable(0) |
---|
90 | ,pa_disable(0) |
---|
91 | ,pa_nospace(1) |
---|
92 | ,pa_quote(false) |
---|
93 | { |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | Parse::~Parse() |
---|
98 | { |
---|
99 | } |
---|
100 | |
---|
101 | #define C ((pa_the_ptr<pa_the_str.size()) ? pa_the_str[pa_the_ptr] : 0) |
---|
102 | |
---|
103 | short Parse::issplit(const char c) |
---|
104 | { |
---|
105 | for (size_t i = 0; i < pa_splits.size(); i++) |
---|
106 | if (pa_splits[i] == c) |
---|
107 | return 1; |
---|
108 | return 0; |
---|
109 | } |
---|
110 | |
---|
111 | void Parse::getsplit() |
---|
112 | { |
---|
113 | size_t x; |
---|
114 | |
---|
115 | if (C == '=') |
---|
116 | { |
---|
117 | x = pa_the_ptr++; |
---|
118 | } else |
---|
119 | { |
---|
120 | while (C && (issplit(C))) |
---|
121 | pa_the_ptr++; |
---|
122 | x = pa_the_ptr; |
---|
123 | while (C && !issplit(C) && C != '=') |
---|
124 | pa_the_ptr++; |
---|
125 | } |
---|
126 | if (x == pa_the_ptr && C == '=') |
---|
127 | pa_the_ptr++; |
---|
128 | pa_ord = (x < pa_the_str.size()) ? pa_the_str.substr(x,pa_the_ptr - x) : ""; |
---|
129 | } |
---|
130 | |
---|
131 | std::string Parse::getword() |
---|
132 | { |
---|
133 | size_t x; |
---|
134 | int disabled = 0; |
---|
135 | int quote = 0; |
---|
136 | int rem = 0; |
---|
137 | |
---|
138 | if (pa_nospace) |
---|
139 | { |
---|
140 | while (C && issplit(C)) |
---|
141 | pa_the_ptr++; |
---|
142 | x = pa_the_ptr; |
---|
143 | while (C && !issplit(C) && (C != pa_breakchar || !pa_breakchar || disabled)) |
---|
144 | { |
---|
145 | if (pa_breakchar && C == pa_disable) |
---|
146 | disabled = 1; |
---|
147 | if (pa_breakchar && C == pa_enable) |
---|
148 | disabled = 0; |
---|
149 | if (pa_quote && C == '"') |
---|
150 | quote = 1; |
---|
151 | pa_the_ptr++; |
---|
152 | while (quote && C && C != '"') |
---|
153 | { |
---|
154 | pa_the_ptr++; |
---|
155 | } |
---|
156 | if (pa_quote && C == '"') |
---|
157 | { |
---|
158 | pa_the_ptr++; |
---|
159 | } |
---|
160 | quote = 0; |
---|
161 | } |
---|
162 | } else |
---|
163 | { |
---|
164 | if (C == pa_breakchar && pa_breakchar) |
---|
165 | { |
---|
166 | x = pa_the_ptr++; |
---|
167 | rem = 1; |
---|
168 | } else |
---|
169 | { |
---|
170 | while (C && (C == ' ' || C == 9 || C == 13 || C == 10 || issplit(C))) |
---|
171 | pa_the_ptr++; |
---|
172 | x = pa_the_ptr; |
---|
173 | while (C && C != ' ' && C != 9 && C != 13 && C != 10 && !issplit(C) && |
---|
174 | (C != pa_breakchar || !pa_breakchar || disabled)) |
---|
175 | { |
---|
176 | if (pa_breakchar && C == pa_disable) |
---|
177 | disabled = 1; |
---|
178 | if (pa_breakchar && C == pa_enable) |
---|
179 | disabled = 0; |
---|
180 | if (pa_quote && C == '"') |
---|
181 | { |
---|
182 | quote = 1; |
---|
183 | pa_the_ptr++; |
---|
184 | while (quote && C && C != '"') |
---|
185 | { |
---|
186 | pa_the_ptr++; |
---|
187 | } |
---|
188 | if (pa_quote && C == '"') |
---|
189 | { |
---|
190 | pa_the_ptr++; |
---|
191 | } |
---|
192 | } |
---|
193 | else |
---|
194 | pa_the_ptr++; |
---|
195 | quote = 0; |
---|
196 | } |
---|
197 | pa_the_ptr++; |
---|
198 | rem = 1; |
---|
199 | } |
---|
200 | if (x == pa_the_ptr && C == pa_breakchar && pa_breakchar) |
---|
201 | pa_the_ptr++; |
---|
202 | } |
---|
203 | if (x < pa_the_str.size()) |
---|
204 | { |
---|
205 | pa_ord = pa_the_str.substr(x,pa_the_ptr - x - rem); |
---|
206 | } |
---|
207 | else |
---|
208 | { |
---|
209 | pa_ord = ""; |
---|
210 | } |
---|
211 | return pa_ord; |
---|
212 | } |
---|
213 | |
---|
214 | void Parse::getword(std::string&s) |
---|
215 | { |
---|
216 | s = Parse::getword(); |
---|
217 | } |
---|
218 | |
---|
219 | void Parse::getsplit(std::string&s) |
---|
220 | { |
---|
221 | Parse::getsplit(); |
---|
222 | s = pa_ord; |
---|
223 | } |
---|
224 | |
---|
225 | void Parse::getword(std::string&s,std::string&fill,int l) |
---|
226 | { |
---|
227 | Parse::getword(); |
---|
228 | s = ""; |
---|
229 | while (s.size() + pa_ord.size() < (size_t)l) |
---|
230 | s += fill; |
---|
231 | s += pa_ord; |
---|
232 | } |
---|
233 | |
---|
234 | std::string Parse::getrest() |
---|
235 | { |
---|
236 | std::string s; |
---|
237 | while (C && (C == ' ' || C == 9 || issplit(C))) |
---|
238 | pa_the_ptr++; |
---|
239 | s = (pa_the_ptr < pa_the_str.size()) ? pa_the_str.substr(pa_the_ptr) : ""; |
---|
240 | return s; |
---|
241 | } |
---|
242 | |
---|
243 | void Parse::getrest(std::string&s) |
---|
244 | { |
---|
245 | while (C && (C == ' ' || C == 9 || issplit(C))) |
---|
246 | pa_the_ptr++; |
---|
247 | s = (pa_the_ptr < pa_the_str.size()) ? pa_the_str.substr(pa_the_ptr) : ""; |
---|
248 | } |
---|
249 | |
---|
250 | long Parse::getvalue() |
---|
251 | { |
---|
252 | Parse::getword(); |
---|
253 | return atol(pa_ord.c_str()); |
---|
254 | } |
---|
255 | |
---|
256 | void Parse::setbreak(const char c) |
---|
257 | { |
---|
258 | pa_breakchar = c; |
---|
259 | } |
---|
260 | |
---|
261 | int Parse::getwordlen() |
---|
262 | { |
---|
263 | size_t x,y = pa_the_ptr,len; |
---|
264 | |
---|
265 | if (C == pa_breakchar && pa_breakchar) |
---|
266 | { |
---|
267 | x = pa_the_ptr++; |
---|
268 | } else |
---|
269 | { |
---|
270 | while (C && (C == ' ' || C == 9 || C == 13 || C == 10 || issplit(C))) |
---|
271 | pa_the_ptr++; |
---|
272 | x = pa_the_ptr; |
---|
273 | while (C && C != ' ' && C != 9 && C != 13 && C != 10 && !issplit(C) && (C != pa_breakchar || !pa_breakchar)) |
---|
274 | pa_the_ptr++; |
---|
275 | } |
---|
276 | if (x == pa_the_ptr && C == pa_breakchar && pa_breakchar) |
---|
277 | pa_the_ptr++; |
---|
278 | len = pa_the_ptr - x; |
---|
279 | pa_the_ptr = y; |
---|
280 | return (int)len; |
---|
281 | } |
---|
282 | |
---|
283 | int Parse::getrestlen() |
---|
284 | { |
---|
285 | size_t y = pa_the_ptr; |
---|
286 | size_t len; |
---|
287 | |
---|
288 | while (C && (C == ' ' || C == 9 || issplit(C))) |
---|
289 | pa_the_ptr++; |
---|
290 | len = strlen(pa_the_str.c_str() + pa_the_ptr); |
---|
291 | pa_the_ptr = y; |
---|
292 | return (int)len; |
---|
293 | } |
---|
294 | |
---|
295 | void Parse::getline() |
---|
296 | { |
---|
297 | size_t x; |
---|
298 | |
---|
299 | x = pa_the_ptr; |
---|
300 | while (C && C != 13 && C != 10) |
---|
301 | pa_the_ptr++; |
---|
302 | pa_ord = (x < pa_the_str.size()) ? pa_the_str.substr(x,pa_the_ptr - x) : ""; |
---|
303 | if (C == 13) |
---|
304 | pa_the_ptr++; |
---|
305 | if (C == 10) |
---|
306 | pa_the_ptr++; |
---|
307 | } |
---|
308 | |
---|
309 | void Parse::getline(std::string&s) |
---|
310 | { |
---|
311 | getline(); |
---|
312 | s = pa_ord; |
---|
313 | } |
---|
314 | |
---|
315 | /* end of implementation of class Parse */ |
---|
316 | /***************************************************/ |
---|
317 | #ifdef SOCKETS_NAMESPACE |
---|
318 | } |
---|
319 | #endif |
---|
320 | |
---|
321 | |
---|