Google
Web alhem.net

Parse.cpp

Go to the documentation of this file.
00001 
00007 /*
00008 Copyright (C) 1999  Anders Hedstrom
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 */
00024 
00025 #ifdef _WIN32
00026 #pragma warning(disable:4786)
00027 #endif
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 
00032 #include "Parse.h"
00033 
00034 namespace Cgi
00035 {
00036 
00037 /* implementation of class Parse */
00038 
00039 Parse::Parse()
00040 {
00041         pa_the_str = NULL;
00042         pa_splits = NULL;
00043         pa_ord = NULL;
00044         pa_the_ptr = 0;
00045         pa_breakchar = 0;
00046         pa_enable = pa_disable = 0;
00047         pa_nospace = 0;
00048         pa_quote = false;
00049 }
00050 
00051 Parse::Parse(const char *s)
00052 {
00053         pa_the_str = NULL;
00054         pa_splits = NULL;
00055         pa_ord = NULL;
00056         pa_the_ptr = 0;
00057         pa_breakchar = 0;
00058         pa_enable = pa_disable = 0;
00059         pa_nospace = 0;
00060         pa_quote = false;
00061 
00062         pa_the_str = new char[strlen(s) + 1];
00063         strcpy(pa_the_str,s);
00064         pa_ord = new char[strlen(s) + 1];
00065         *pa_ord = 0;
00066 }
00067 
00068 Parse::Parse(const char *s,const char *sp)
00069 {
00070         pa_the_str = NULL;
00071         pa_splits = NULL;
00072         pa_ord = NULL;
00073         pa_the_ptr = 0;
00074         pa_breakchar = 0;
00075         pa_enable = pa_disable = 0;
00076         pa_nospace = 0;
00077         pa_quote = false;
00078 
00079         pa_the_str = new char[strlen(s) + 1];
00080         strcpy(pa_the_str,s);
00081         pa_splits = new char[strlen(sp) + 1];
00082         strcpy(pa_splits,sp);
00083         pa_ord = new char[strlen(s) + 1];
00084         *pa_ord = 0;
00085 }
00086 
00087 Parse::Parse(const char *s,const char *sp,short ) //nospace)
00088 {
00089         pa_the_str = NULL;
00090         pa_splits = NULL;
00091         pa_ord = NULL;
00092         pa_the_ptr = 0;
00093         pa_breakchar = 0;
00094         pa_enable = pa_disable = 0;
00095         pa_nospace = 0;
00096         pa_quote = false;
00097 
00098         pa_the_str = new char[strlen(s) + 1];
00099         strcpy(pa_the_str,s);
00100         pa_splits = new char[strlen(sp) + 1];
00101         strcpy(pa_splits,sp);
00102         pa_ord = new char[strlen(s) + 1];
00103         *pa_ord = 0;
00104         pa_nospace = 1;
00105 }
00106 
00107 Parse::~Parse()
00108 {
00109   if (pa_the_str)
00110     delete[] pa_the_str;
00111   if (pa_splits)
00112     delete[] pa_splits;
00113   if (pa_ord)
00114     delete[] pa_ord;
00115 }
00116 
00117 
00118 #define C pa_the_str[pa_the_ptr]
00119 
00120 short Parse::issplit(char c)
00121 {
00122         short i;
00123 
00124         if (pa_splits)
00125                 for (i = 0; (size_t)i < strlen(pa_splits); i++)
00126                         if (pa_splits[i] == c)
00127                                 return 1;
00128         return 0;
00129 }
00130 
00131 void Parse::getsplit()
00132 {
00133         int x;
00134 
00135         if (C == '=')
00136         {
00137                 x = pa_the_ptr++;
00138         } else
00139         {
00140                 while (C && (issplit(C)))
00141                         pa_the_ptr++;
00142                 x = pa_the_ptr;
00143                 while (C && !issplit(C) && C != '=')
00144                         pa_the_ptr++;
00145         }
00146         if (x == pa_the_ptr && C == '=')
00147                 pa_the_ptr++;
00148         strncpy(pa_ord,pa_the_str + x,pa_the_ptr - x);
00149         pa_ord[pa_the_ptr - x] = 0;
00150 }
00151 
00152 std::string Parse::getword()
00153 {
00154         int x;
00155         int disabled = 0;
00156         int quote = 0;
00157 
00158         if (pa_nospace)
00159         {
00160                 while (C && issplit(C))
00161                         pa_the_ptr++;
00162                 x = pa_the_ptr;
00163                 while (C && !issplit(C) && (C != pa_breakchar || !pa_breakchar || disabled))
00164                 {
00165                         if (pa_breakchar && C == pa_disable)
00166                                 disabled = 1;
00167                         if (pa_breakchar && C == pa_enable)
00168                                 disabled = 0;
00169                         if (pa_quote && C == '"')
00170                                 quote = 1;
00171                         pa_the_ptr++;
00172                         while (quote && C && C != '"')
00173                         {
00174                                 pa_the_ptr++;
00175                         }
00176                         if (pa_quote && C == '"')
00177                         {
00178                                 pa_the_ptr++;
00179                         }
00180                         quote = 0;
00181                 }
00182         } else
00183         {
00184                 if (C == pa_breakchar && pa_breakchar)
00185                 {
00186                         x = pa_the_ptr++;
00187                 } else
00188                 {
00189                         while (C && (C == ' ' || C == 9 || C == 13 || C == 10 || issplit(C)))
00190                                 pa_the_ptr++;
00191                         x = pa_the_ptr;
00192 #define DEB(x) 
00193 DEB(printf("Split starts at %d\n",x);)
00194                         while (C && C != ' ' && C != 9 && C != 13 && C != 10 && !issplit(C) &&
00195                          (C != pa_breakchar || !pa_breakchar || disabled))
00196                         {
00197 DEB(printf("Char '%c'\n",C);)
00198                                 if (pa_breakchar && C == pa_disable)
00199                                         disabled = 1;
00200                                 if (pa_breakchar && C == pa_enable)
00201                                         disabled = 0;
00202                                 if (pa_quote && C == '"')
00203                                 {
00204                                         quote = 1;
00205                                 pa_the_ptr++;
00206                                 while (quote && C && C != '"')
00207                                 {
00208                                         pa_the_ptr++;
00209                                 }
00210                                 if (pa_quote && C == '"')
00211                                 {
00212                                         pa_the_ptr++;
00213                                 }
00214                                 }
00215                                 else
00216                                         pa_the_ptr++;
00217                                 quote = 0;
00218                         }
00219                 }
00220                 if (x == pa_the_ptr && C == pa_breakchar && pa_breakchar)
00221                         pa_the_ptr++;
00222         }
00223         strncpy(pa_ord,pa_the_str + x,pa_the_ptr - x);
00224         pa_ord[pa_the_ptr - x] = 0;
00225         return pa_ord;
00226 }
00227 
00228 void Parse::getword(char *s)
00229 {
00230         Parse::getword();
00231         strcpy(s,pa_ord);
00232 }
00233 
00234 void Parse::getword(char *s,int l)
00235 {
00236         Parse::getword();
00237         strncpy(s,pa_ord,l - 1);
00238         s[l - 1] = 0;
00239 }
00240 
00241 void Parse::getsplit(char *s)
00242 {
00243         Parse::getsplit();
00244         strcpy(s,pa_ord);
00245 }
00246 
00247 void Parse::getword(char *s,char *fill,int l)
00248 {
00249         Parse::getword();
00250         *s = 0;
00251         while (strlen(s) + strlen(pa_ord) < (size_t)l)
00252                 strcat(s,fill);
00253         strcat(s,pa_ord);
00254 }
00255 
00256 void Parse::getrest(char *s)
00257 {
00258         while (C && (C == ' ' || C == 9 || issplit(C)))
00259                 pa_the_ptr++;
00260         strcpy(s,pa_the_str + pa_the_ptr);
00261 }
00262 
00263 long Parse::getvalue()
00264 {
00265         Parse::getword();
00266         return atol(pa_ord);
00267 }
00268 
00269 void Parse::setbreak(char c)
00270 {
00271         pa_breakchar = c;
00272 }
00273 
00274 int Parse::getwordlen()
00275 {
00276         int x,y = pa_the_ptr,len;
00277 
00278         if (C == pa_breakchar && pa_breakchar)
00279         {
00280                 x = pa_the_ptr++;
00281         } else
00282         {
00283                 while (C && (C == ' ' || C == 9 || C == 13 || C == 10 || issplit(C)))
00284                         pa_the_ptr++;
00285                 x = pa_the_ptr;
00286                 while (C && C != ' ' && C != 9 && C != 13 && C != 10 && !issplit(C) && (C != pa_breakchar || !pa_breakchar))
00287                         pa_the_ptr++;
00288         }
00289         if (x == pa_the_ptr && C == pa_breakchar && pa_breakchar)
00290                 pa_the_ptr++;
00291         len = pa_the_ptr - x;
00292         pa_the_ptr = y;
00293         return len;
00294 }
00295 
00296 int Parse::getrestlen()
00297 {
00298         int y = pa_the_ptr,len;
00299 
00300         while (C && (C == ' ' || C == 9 || issplit(C)))
00301                 pa_the_ptr++;
00302         len = strlen(pa_the_str + pa_the_ptr);
00303         pa_the_ptr = y;
00304         return len;
00305 }
00306 
00307 void Parse::getline()
00308 {
00309         int x;
00310 
00311 //      while (C && (C == 13 || C == 10))
00312 //              pa_the_ptr++;
00313         x = pa_the_ptr;
00314         while (C && C != 13 && C != 10)
00315                 pa_the_ptr++;
00316         strncpy(pa_ord,pa_the_str + x,pa_the_ptr - x);
00317         pa_ord[pa_the_ptr - x] = 0;
00318         if (C == 13)
00319                 pa_the_ptr++;
00320         if (C == 10)
00321                 pa_the_ptr++;
00322 }
00323 
00324 void Parse::getline(char *s)
00325 {
00326         getline();
00327         strcpy(s,pa_ord);
00328 }
00329 
00330 void Parse::getline(char *s,int l)
00331 {
00332         getline();
00333         strncpy(s,pa_ord,l - 1);
00334         s[l - 1] = 0;
00335 }
00336 
00337 void Parse::enablebreak(char c) 
00338 {
00339         pa_enable = c;
00340 }
00341 
00342 void Parse::disablebreak(char c) 
00343 {
00344         pa_disable = c;
00345 }
00346 
00347 int Parse::getptr() 
00348 { 
00349   return pa_the_ptr; 
00350 }
00351 
00352 void Parse::EnableQuote(bool b) 
00353 { 
00354   pa_quote = b; 
00355 }
00356 
00357 Parse::Parse(const Parse& ) 
00358 {
00359 }
00360 
00361 Parse& Parse::operator=(const Parse& ) 
00362 { 
00363   return *this; 
00364 }
00365 
00366 /* end of implementation of class Parse */
00367 /***************************************************/
00368 
00369 } // namespace Cgi
00370 

Generated for cgi++ by doxygen 1.3.7

Page, code, and content Copyright (C) 2004 by Anders Hedström