00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <stdlib.h>
00034 #include <cstring>
00035
00036 #include "Parse.h"
00037
00038
00039 #ifdef SOCKETS_NAMESPACE
00040 namespace SOCKETS_NAMESPACE {
00041 #endif
00042
00043
00044
00045
00046 Parse::Parse()
00047 :pa_the_str("")
00048 ,pa_splits("")
00049 ,pa_ord("")
00050 ,pa_the_ptr(0)
00051 ,pa_breakchar(0)
00052 ,pa_enable(0)
00053 ,pa_disable(0)
00054 ,pa_nospace(0)
00055 ,pa_quote(false)
00056 {
00057 }
00058
00059 Parse::Parse(const std::string&s)
00060 :pa_the_str(s)
00061 ,pa_splits("")
00062 ,pa_ord("")
00063 ,pa_the_ptr(0)
00064 ,pa_breakchar(0)
00065 ,pa_enable(0)
00066 ,pa_disable(0)
00067 ,pa_nospace(0)
00068 ,pa_quote(false)
00069 {
00070 }
00071
00072 Parse::Parse(const std::string&s,const std::string&sp)
00073 :pa_the_str(s)
00074 ,pa_splits(sp)
00075 ,pa_ord("")
00076 ,pa_the_ptr(0)
00077 ,pa_breakchar(0)
00078 ,pa_enable(0)
00079 ,pa_disable(0)
00080 ,pa_nospace(0)
00081 ,pa_quote(false)
00082 {
00083 }
00084
00085 Parse::Parse(const std::string&s,const std::string&sp,short nospace)
00086 :pa_the_str(s)
00087 ,pa_splits(sp)
00088 ,pa_ord("")
00089 ,pa_the_ptr(0)
00090 ,pa_breakchar(0)
00091 ,pa_enable(0)
00092 ,pa_disable(0)
00093 ,pa_nospace(1)
00094 ,pa_quote(false)
00095 {
00096 }
00097
00098
00099 Parse::~Parse()
00100 {
00101 }
00102
00103 #define C ((pa_the_ptr<pa_the_str.size()) ? pa_the_str[pa_the_ptr] : 0)
00104
00105 short Parse::issplit(const char c)
00106 {
00107 for (size_t i = 0; i < pa_splits.size(); i++)
00108 if (pa_splits[i] == c)
00109 return 1;
00110 return 0;
00111 }
00112
00113 void Parse::getsplit()
00114 {
00115 size_t x;
00116
00117 if (C == '=')
00118 {
00119 x = pa_the_ptr++;
00120 } else
00121 {
00122 while (C && (issplit(C)))
00123 pa_the_ptr++;
00124 x = pa_the_ptr;
00125 while (C && !issplit(C) && C != '=')
00126 pa_the_ptr++;
00127 }
00128 if (x == pa_the_ptr && C == '=')
00129 pa_the_ptr++;
00130 pa_ord = (x < pa_the_str.size()) ? pa_the_str.substr(x,pa_the_ptr - x) : "";
00131 }
00132
00133 std::string Parse::getword()
00134 {
00135 size_t x;
00136 int disabled = 0;
00137 int quote = 0;
00138 int rem = 0;
00139
00140 if (pa_nospace)
00141 {
00142 while (C && issplit(C))
00143 pa_the_ptr++;
00144 x = pa_the_ptr;
00145 while (C && !issplit(C) && (C != pa_breakchar || !pa_breakchar || disabled))
00146 {
00147 if (pa_breakchar && C == pa_disable)
00148 disabled = 1;
00149 if (pa_breakchar && C == pa_enable)
00150 disabled = 0;
00151 if (pa_quote && C == '"')
00152 quote = 1;
00153 pa_the_ptr++;
00154 while (quote && C && C != '"')
00155 {
00156 pa_the_ptr++;
00157 }
00158 if (pa_quote && C == '"')
00159 {
00160 pa_the_ptr++;
00161 }
00162 quote = 0;
00163 }
00164 } else
00165 {
00166 if (C == pa_breakchar && pa_breakchar)
00167 {
00168 x = pa_the_ptr++;
00169 rem = 1;
00170 } else
00171 {
00172 while (C && (C == ' ' || C == 9 || C == 13 || C == 10 || issplit(C)))
00173 pa_the_ptr++;
00174 x = pa_the_ptr;
00175 while (C && C != ' ' && C != 9 && C != 13 && C != 10 && !issplit(C) &&
00176 (C != pa_breakchar || !pa_breakchar || disabled))
00177 {
00178 if (pa_breakchar && C == pa_disable)
00179 disabled = 1;
00180 if (pa_breakchar && C == pa_enable)
00181 disabled = 0;
00182 if (pa_quote && C == '"')
00183 {
00184 quote = 1;
00185 pa_the_ptr++;
00186 while (quote && C && C != '"')
00187 {
00188 pa_the_ptr++;
00189 }
00190 if (pa_quote && C == '"')
00191 {
00192 pa_the_ptr++;
00193 }
00194 }
00195 else
00196 pa_the_ptr++;
00197 quote = 0;
00198 }
00199 pa_the_ptr++;
00200 rem = 1;
00201 }
00202 if (x == pa_the_ptr && C == pa_breakchar && pa_breakchar)
00203 pa_the_ptr++;
00204 }
00205 if (x < pa_the_str.size())
00206 {
00207 pa_ord = pa_the_str.substr(x,pa_the_ptr - x - rem);
00208 }
00209 else
00210 {
00211 pa_ord = "";
00212 }
00213 return pa_ord;
00214 }
00215
00216 void Parse::getword(std::string&s)
00217 {
00218 s = Parse::getword();
00219 }
00220
00221 void Parse::getsplit(std::string&s)
00222 {
00223 Parse::getsplit();
00224 s = pa_ord;
00225 }
00226
00227 void Parse::getword(std::string&s,std::string&fill,int l)
00228 {
00229 Parse::getword();
00230 s = "";
00231 while (s.size() + pa_ord.size() < (size_t)l)
00232 s += fill;
00233 s += pa_ord;
00234 }
00235
00236 std::string Parse::getrest()
00237 {
00238 std::string s;
00239 while (C && (C == ' ' || C == 9 || issplit(C)))
00240 pa_the_ptr++;
00241 s = (pa_the_ptr < pa_the_str.size()) ? pa_the_str.substr(pa_the_ptr) : "";
00242 return s;
00243 }
00244
00245 void Parse::getrest(std::string&s)
00246 {
00247 while (C && (C == ' ' || C == 9 || issplit(C)))
00248 pa_the_ptr++;
00249 s = (pa_the_ptr < pa_the_str.size()) ? pa_the_str.substr(pa_the_ptr) : "";
00250 }
00251
00252 long Parse::getvalue()
00253 {
00254 Parse::getword();
00255 return atol(pa_ord.c_str());
00256 }
00257
00258 void Parse::setbreak(const char c)
00259 {
00260 pa_breakchar = c;
00261 }
00262
00263 int Parse::getwordlen()
00264 {
00265 size_t x,y = pa_the_ptr,len;
00266
00267 if (C == pa_breakchar && pa_breakchar)
00268 {
00269 x = pa_the_ptr++;
00270 } else
00271 {
00272 while (C && (C == ' ' || C == 9 || C == 13 || C == 10 || issplit(C)))
00273 pa_the_ptr++;
00274 x = pa_the_ptr;
00275 while (C && C != ' ' && C != 9 && C != 13 && C != 10 && !issplit(C) && (C != pa_breakchar || !pa_breakchar))
00276 pa_the_ptr++;
00277 }
00278 if (x == pa_the_ptr && C == pa_breakchar && pa_breakchar)
00279 pa_the_ptr++;
00280 len = pa_the_ptr - x;
00281 pa_the_ptr = y;
00282 return (int)len;
00283 }
00284
00285 int Parse::getrestlen()
00286 {
00287 size_t y = pa_the_ptr;
00288 size_t len;
00289
00290 while (C && (C == ' ' || C == 9 || issplit(C)))
00291 pa_the_ptr++;
00292 len = strlen(pa_the_str.c_str() + pa_the_ptr);
00293 pa_the_ptr = y;
00294 return (int)len;
00295 }
00296
00297 void Parse::getline()
00298 {
00299 size_t x;
00300
00301 x = pa_the_ptr;
00302 while (C && C != 13 && C != 10)
00303 pa_the_ptr++;
00304 pa_ord = (x < pa_the_str.size()) ? pa_the_str.substr(x,pa_the_ptr - x) : "";
00305 if (C == 13)
00306 pa_the_ptr++;
00307 if (C == 10)
00308 pa_the_ptr++;
00309 }
00310
00311 void Parse::getline(std::string&s)
00312 {
00313 getline();
00314 s = pa_ord;
00315 }
00316
00317
00318
00319 #ifdef SOCKETS_NAMESPACE
00320 }
00321 #endif
00322
00323