switched from PracticalSocket to libasio
[anytun.git] / src / Sockets / Parse.cpp
blob6de56f0a9eabfdfd86ca8138fcfd9f3363817b72
1 /** \file Parse.cpp - parse a string
2 **
3 ** Written: 1999-Feb-10 grymse@alhem.net
4 **/
6 /*
7 Copyright (C) 1999-2007 Anders Hedstrom
9 This library is made available under the terms of the GNU GPL.
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.
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.
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.
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.
31 #include <stdlib.h>
32 #include <string.h>
34 #include "Parse.h"
37 #ifdef SOCKETS_NAMESPACE
38 namespace SOCKETS_NAMESPACE {
39 #endif
42 /* implementation of class Parse */
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)
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)
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)
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)
97 Parse::~Parse()
101 #define C ((pa_the_ptr<pa_the_str.size()) ? pa_the_str[pa_the_ptr] : 0)
103 short Parse::issplit(const char c)
105 for (size_t i = 0; i < pa_splits.size(); i++)
106 if (pa_splits[i] == c)
107 return 1;
108 return 0;
111 void Parse::getsplit()
113 size_t x;
115 if (C == '=')
117 x = pa_the_ptr++;
118 } else
120 while (C && (issplit(C)))
121 pa_the_ptr++;
122 x = pa_the_ptr;
123 while (C && !issplit(C) && C != '=')
124 pa_the_ptr++;
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) : "";
131 std::string Parse::getword()
133 size_t x;
134 int disabled = 0;
135 int quote = 0;
136 int rem = 0;
138 if (pa_nospace)
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))
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 != '"')
154 pa_the_ptr++;
156 if (pa_quote && C == '"')
158 pa_the_ptr++;
160 quote = 0;
162 } else
164 if (C == pa_breakchar && pa_breakchar)
166 x = pa_the_ptr++;
167 rem = 1;
168 } else
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))
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 == '"')
182 quote = 1;
183 pa_the_ptr++;
184 while (quote && C && C != '"')
186 pa_the_ptr++;
188 if (pa_quote && C == '"')
190 pa_the_ptr++;
193 else
194 pa_the_ptr++;
195 quote = 0;
197 pa_the_ptr++;
198 rem = 1;
200 if (x == pa_the_ptr && C == pa_breakchar && pa_breakchar)
201 pa_the_ptr++;
203 if (x < pa_the_str.size())
205 pa_ord = pa_the_str.substr(x,pa_the_ptr - x - rem);
207 else
209 pa_ord = "";
211 return pa_ord;
214 void Parse::getword(std::string&s)
216 s = Parse::getword();
219 void Parse::getsplit(std::string&s)
221 Parse::getsplit();
222 s = pa_ord;
225 void Parse::getword(std::string&s,std::string&fill,int l)
227 Parse::getword();
228 s = "";
229 while (s.size() + pa_ord.size() < (size_t)l)
230 s += fill;
231 s += pa_ord;
234 std::string Parse::getrest()
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;
243 void Parse::getrest(std::string&s)
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) : "";
250 long Parse::getvalue()
252 Parse::getword();
253 return atol(pa_ord.c_str());
256 void Parse::setbreak(const char c)
258 pa_breakchar = c;
261 int Parse::getwordlen()
263 size_t x,y = pa_the_ptr,len;
265 if (C == pa_breakchar && pa_breakchar)
267 x = pa_the_ptr++;
268 } else
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++;
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;
283 int Parse::getrestlen()
285 size_t y = pa_the_ptr;
286 size_t len;
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;
295 void Parse::getline()
297 size_t x;
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++;
309 void Parse::getline(std::string&s)
311 getline();
312 s = pa_ord;
315 /* end of implementation of class Parse */
316 /***************************************************/
317 #ifdef SOCKETS_NAMESPACE
319 #endif