Logo
~Sockets~
~Examples~
~Contact~


Parse.cpp

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 1999-2007  Anders Hedstrom
00008 
00009 This library is made available under the terms of the GNU GPL.
00010 
00011 If you would like to use this library in a closed-source application,
00012 a separate license agreement is available. For information about 
00013 the closed-source license agreement for the C++ sockets library,
00014 please visit http://www.alhem.net/Sockets/license.html and/or
00015 email license@alhem.net.
00016 
00017 This program is free software; you can redistribute it and/or
00018 modify it under the terms of the GNU General Public License
00019 as published by the Free Software Foundation; either version 2
00020 of the License, or (at your option) any later version.
00021 
00022 This program is distributed in the hope that it will be useful,
00023 but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 GNU General Public License for more details.
00026 
00027 You should have received a copy of the GNU General Public License
00028 along with this program; if not, write to the Free Software
00029 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00030 */
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00034 #include "Parse.h"
00035 
00036 
00037 #ifdef SOCKETS_NAMESPACE
00038 namespace SOCKETS_NAMESPACE {
00039 #endif
00040 
00041 
00042 /* implementation of class Parse */
00043 
00044 Parse::Parse()
00045 :pa_the_str("")
00046 ,pa_splits("")
00047 ,pa_ord("")
00048 ,pa_the_ptr(0)
00049 ,pa_breakchar(0)
00050 ,pa_enable(0)
00051 ,pa_disable(0)
00052 ,pa_nospace(0)
00053 ,pa_quote(false)
00054 {
00055 }
00056 
00057 Parse::Parse(const std::string&s)
00058 :pa_the_str(s)
00059 ,pa_splits("")
00060 ,pa_ord("")
00061 ,pa_the_ptr(0)
00062 ,pa_breakchar(0)
00063 ,pa_enable(0)
00064 ,pa_disable(0)
00065 ,pa_nospace(0)
00066 ,pa_quote(false)
00067 {
00068 }
00069 
00070 Parse::Parse(const std::string&s,const std::string&sp)
00071 :pa_the_str(s)
00072 ,pa_splits(sp)
00073 ,pa_ord("")
00074 ,pa_the_ptr(0)
00075 ,pa_breakchar(0)
00076 ,pa_enable(0)
00077 ,pa_disable(0)
00078 ,pa_nospace(0)
00079 ,pa_quote(false)
00080 {
00081 }
00082 
00083 Parse::Parse(const std::string&s,const std::string&sp,short nospace)
00084 :pa_the_str(s)
00085 ,pa_splits(sp)
00086 ,pa_ord("")
00087 ,pa_the_ptr(0)
00088 ,pa_breakchar(0)
00089 ,pa_enable(0)
00090 ,pa_disable(0)
00091 ,pa_nospace(1)
00092 ,pa_quote(false)
00093 {
00094 }
00095 
00096 
00097 Parse::~Parse()
00098 {
00099 }
00100 
00101 #define C ((pa_the_ptr<pa_the_str.size()) ? pa_the_str[pa_the_ptr] : 0)
00102 
00103 short Parse::issplit(const char c)
00104 {
00105         for (size_t i = 0; i < pa_splits.size(); i++)
00106                 if (pa_splits[i] == c)
00107                         return 1;
00108         return 0;
00109 }
00110 
00111 void Parse::getsplit()
00112 {
00113         size_t x;
00114 
00115         if (C == '=')
00116         {
00117                 x = pa_the_ptr++;
00118         } else
00119         {
00120                 while (C && (issplit(C)))
00121                         pa_the_ptr++;
00122                 x = pa_the_ptr;
00123                 while (C && !issplit(C) && C != '=')
00124                         pa_the_ptr++;
00125         }
00126         if (x == pa_the_ptr && C == '=')
00127                 pa_the_ptr++;
00128         pa_ord = (x < pa_the_str.size()) ? pa_the_str.substr(x,pa_the_ptr - x) : "";
00129 }
00130 
00131 std::string Parse::getword()
00132 {
00133         size_t x;
00134         int disabled = 0;
00135         int quote = 0;
00136         int rem = 0;
00137 
00138         if (pa_nospace)
00139         {
00140                 while (C && issplit(C))
00141                         pa_the_ptr++;
00142                 x = pa_the_ptr;
00143                 while (C && !issplit(C) && (C != pa_breakchar || !pa_breakchar || disabled))
00144                 {
00145                         if (pa_breakchar && C == pa_disable)
00146                                 disabled = 1;
00147                         if (pa_breakchar && C == pa_enable)
00148                                 disabled = 0;
00149                         if (pa_quote && C == '"')
00150                                 quote = 1;
00151                         pa_the_ptr++;
00152                         while (quote && C && C != '"')
00153                         {
00154                                 pa_the_ptr++;
00155                         }
00156                         if (pa_quote && C == '"')
00157                         {
00158                                 pa_the_ptr++;
00159                         }
00160                         quote = 0;
00161                 }
00162         } else
00163         {
00164                 if (C == pa_breakchar && pa_breakchar)
00165                 {
00166                         x = pa_the_ptr++;
00167                         rem = 1;
00168                 } else
00169                 {
00170                         while (C && (C == ' ' || C == 9 || C == 13 || C == 10 || issplit(C)))
00171                                 pa_the_ptr++;
00172                         x = pa_the_ptr;
00173                         while (C && C != ' ' && C != 9 && C != 13 && C != 10 && !issplit(C) &&
00174                          (C != pa_breakchar || !pa_breakchar || disabled))
00175                         {
00176                                 if (pa_breakchar && C == pa_disable)
00177                                         disabled = 1;
00178                                 if (pa_breakchar && C == pa_enable)
00179                                         disabled = 0;
00180                                 if (pa_quote && C == '"')
00181                                 {
00182                                         quote = 1;
00183                                 pa_the_ptr++;
00184                                 while (quote && C && C != '"')
00185                                 {
00186                                         pa_the_ptr++;
00187                                 }
00188                                 if (pa_quote && C == '"')
00189                                 {
00190                                         pa_the_ptr++;
00191                                 }
00192                                 }
00193                                 else
00194                                         pa_the_ptr++;
00195                                 quote = 0;
00196                         }
00197                         pa_the_ptr++;
00198                         rem = 1;
00199                 }
00200                 if (x == pa_the_ptr && C == pa_breakchar && pa_breakchar)
00201                         pa_the_ptr++;
00202         }
00203         if (x < pa_the_str.size())
00204         {
00205                 pa_ord = pa_the_str.substr(x,pa_the_ptr - x - rem);
00206         }
00207         else
00208         {
00209                 pa_ord = "";
00210         }
00211         return pa_ord;
00212 }
00213 
00214 void Parse::getword(std::string&s)
00215 {
00216         s = Parse::getword();
00217 }
00218 
00219 void Parse::getsplit(std::string&s)
00220 {
00221         Parse::getsplit();
00222         s = pa_ord;
00223 }
00224 
00225 void Parse::getword(std::string&s,std::string&fill,int l)
00226 {
00227         Parse::getword();
00228         s = "";
00229         while (s.size() + pa_ord.size() < (size_t)l)
00230                 s += fill;
00231         s += pa_ord;
00232 }
00233 
00234 std::string Parse::getrest()
00235 {
00236         std::string s;
00237         while (C && (C == ' ' || C == 9 || issplit(C)))
00238                 pa_the_ptr++;
00239         s = (pa_the_ptr < pa_the_str.size()) ? pa_the_str.substr(pa_the_ptr) : "";
00240         return s;
00241 }
00242 
00243 void Parse::getrest(std::string&s)
00244 {
00245         while (C && (C == ' ' || C == 9 || issplit(C)))
00246                 pa_the_ptr++;
00247         s = (pa_the_ptr < pa_the_str.size()) ? pa_the_str.substr(pa_the_ptr) : "";
00248 }
00249 
00250 long Parse::getvalue()
00251 {
00252         Parse::getword();
00253         return atol(pa_ord.c_str());
00254 }
00255 
00256 void Parse::setbreak(const char c)
00257 {
00258         pa_breakchar = c;
00259 }
00260 
00261 int Parse::getwordlen()
00262 {
00263         size_t x,y = pa_the_ptr,len;
00264 
00265         if (C == pa_breakchar && pa_breakchar)
00266         {
00267                 x = pa_the_ptr++;
00268         } else
00269         {
00270                 while (C && (C == ' ' || C == 9 || C == 13 || C == 10 || issplit(C)))
00271                         pa_the_ptr++;
00272                 x = pa_the_ptr;
00273                 while (C && C != ' ' && C != 9 && C != 13 && C != 10 && !issplit(C) && (C != pa_breakchar || !pa_breakchar))
00274                         pa_the_ptr++;
00275         }
00276         if (x == pa_the_ptr && C == pa_breakchar && pa_breakchar)
00277                 pa_the_ptr++;
00278         len = pa_the_ptr - x;
00279         pa_the_ptr = y;
00280         return (int)len;
00281 }
00282 
00283 int Parse::getrestlen()
00284 {
00285         size_t y = pa_the_ptr;
00286         size_t len;
00287 
00288         while (C && (C == ' ' || C == 9 || issplit(C)))
00289                 pa_the_ptr++;
00290         len = strlen(pa_the_str.c_str() + pa_the_ptr);
00291         pa_the_ptr = y;
00292         return (int)len;
00293 }
00294 
00295 void Parse::getline()
00296 {
00297         size_t x;
00298 
00299         x = pa_the_ptr;
00300         while (C && C != 13 && C != 10)
00301                 pa_the_ptr++;
00302         pa_ord = (x < pa_the_str.size()) ? pa_the_str.substr(x,pa_the_ptr - x) : "";
00303         if (C == 13)
00304                 pa_the_ptr++;
00305         if (C == 10)
00306                 pa_the_ptr++;
00307 }
00308 
00309 void Parse::getline(std::string&s)
00310 {
00311         getline();
00312         s = pa_ord;
00313 }
00314 
00315 /* end of implementation of class Parse */
00316 /***************************************************/
00317 #ifdef SOCKETS_NAMESPACE
00318 }
00319 #endif
00320 
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4