1 /** \file Parse.cpp - parse a string
3 ** Written: 1999-Feb-10 grymse@alhem.net
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.
37 #ifdef SOCKETS_NAMESPACE
38 namespace SOCKETS_NAMESPACE
{
42 /* implementation of class Parse */
57 Parse::Parse(const std::string
&s
)
70 Parse::Parse(const std::string
&s
,const std::string
&sp
)
83 Parse::Parse(const std::string
&s
,const std::string
&sp
,short nospace
)
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
)
111 void Parse::getsplit()
120 while (C
&& (issplit(C
)))
123 while (C
&& !issplit(C
) && C
!= '=')
126 if (x
== pa_the_ptr
&& C
== '=')
128 pa_ord
= (x
< pa_the_str
.size()) ? pa_the_str
.substr(x
,pa_the_ptr
- x
) : "";
131 std::string
Parse::getword()
140 while (C
&& issplit(C
))
143 while (C
&& !issplit(C
) && (C
!= pa_breakchar
|| !pa_breakchar
|| disabled
))
145 if (pa_breakchar
&& C
== pa_disable
)
147 if (pa_breakchar
&& C
== pa_enable
)
149 if (pa_quote
&& C
== '"')
152 while (quote
&& C
&& C
!= '"')
156 if (pa_quote
&& C
== '"')
164 if (C
== pa_breakchar
&& pa_breakchar
)
170 while (C
&& (C
== ' ' || C
== 9 || C
== 13 || C
== 10 || issplit(C
)))
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
)
178 if (pa_breakchar
&& C
== pa_enable
)
180 if (pa_quote
&& C
== '"')
184 while (quote
&& C
&& C
!= '"')
188 if (pa_quote
&& C
== '"')
200 if (x
== pa_the_ptr
&& C
== pa_breakchar
&& pa_breakchar
)
203 if (x
< pa_the_str
.size())
205 pa_ord
= pa_the_str
.substr(x
,pa_the_ptr
- x
- rem
);
214 void Parse::getword(std::string
&s
)
216 s
= Parse::getword();
219 void Parse::getsplit(std::string
&s
)
225 void Parse::getword(std::string
&s
,std::string
&fill
,int l
)
229 while (s
.size() + pa_ord
.size() < (size_t)l
)
234 std::string
Parse::getrest()
237 while (C
&& (C
== ' ' || C
== 9 || issplit(C
)))
239 s
= (pa_the_ptr
< pa_the_str
.size()) ? pa_the_str
.substr(pa_the_ptr
) : "";
243 void Parse::getrest(std::string
&s
)
245 while (C
&& (C
== ' ' || C
== 9 || issplit(C
)))
247 s
= (pa_the_ptr
< pa_the_str
.size()) ? pa_the_str
.substr(pa_the_ptr
) : "";
250 long Parse::getvalue()
253 return atol(pa_ord
.c_str());
256 void Parse::setbreak(const char c
)
261 int Parse::getwordlen()
263 size_t x
,y
= pa_the_ptr
,len
;
265 if (C
== pa_breakchar
&& pa_breakchar
)
270 while (C
&& (C
== ' ' || C
== 9 || C
== 13 || C
== 10 || issplit(C
)))
273 while (C
&& C
!= ' ' && C
!= 9 && C
!= 13 && C
!= 10 && !issplit(C
) && (C
!= pa_breakchar
|| !pa_breakchar
))
276 if (x
== pa_the_ptr
&& C
== pa_breakchar
&& pa_breakchar
)
278 len
= pa_the_ptr
- x
;
283 int Parse::getrestlen()
285 size_t y
= pa_the_ptr
;
288 while (C
&& (C
== ' ' || C
== 9 || issplit(C
)))
290 len
= strlen(pa_the_str
.c_str() + pa_the_ptr
);
295 void Parse::getline()
300 while (C
&& C
!= 13 && C
!= 10)
302 pa_ord
= (x
< pa_the_str
.size()) ? pa_the_str
.substr(x
,pa_the_ptr
- x
) : "";
309 void Parse::getline(std::string
&s
)
315 /* end of implementation of class Parse */
316 /***************************************************/
317 #ifdef SOCKETS_NAMESPACE