Logo
~Apps~
~Projects~
~Contact~


ParseBT.cpp

Go to the documentation of this file.
00001 
00007 /*
00008 Copyright (C) 1999,2005  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 #ifdef _WIN32
00025 #pragma warning(disable:4786)
00026 #endif
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 #include "ParseBT.h"
00032 
00033 /* implementation of class ParseBT */
00034 
00035 ParseBT::ParseBT()
00036 {
00037         pa_the_str = NULL;
00038         pa_splits = NULL;
00039         pa_ord = NULL;
00040         pa_the_ptr = 0;
00041         pa_breakchar = 0;
00042         pa_enable = pa_disable = 0;
00043         pa_nospace = 0;
00044         pa_quote = false;
00045 }
00046 
00047 ParseBT::ParseBT(char *s)
00048 {
00049         pa_the_str = NULL;
00050         pa_splits = NULL;
00051         pa_ord = NULL;
00052         pa_the_ptr = 0;
00053         pa_breakchar = 0;
00054         pa_enable = pa_disable = 0;
00055         pa_nospace = 0;
00056         pa_quote = false;
00057 
00058         pa_the_str = new char[strlen(s) + 1];
00059         strcpy(pa_the_str,s);
00060         pa_ord = new char[strlen(s) + 1];
00061         *pa_ord = 0;
00062 }
00063 
00064 ParseBT::ParseBT(char *s,char *sp)
00065 {
00066         pa_the_str = NULL;
00067         pa_splits = NULL;
00068         pa_ord = NULL;
00069         pa_the_ptr = 0;
00070         pa_breakchar = 0;
00071         pa_enable = pa_disable = 0;
00072         pa_nospace = 0;
00073         pa_quote = false;
00074 
00075         pa_the_str = new char[strlen(s) + 1];
00076         strcpy(pa_the_str,s);
00077         pa_splits = new char[strlen(sp) + 1];
00078         strcpy(pa_splits,sp);
00079         pa_ord = new char[strlen(s) + 1];
00080         *pa_ord = 0;
00081 }
00082 
00083 ParseBT::ParseBT(char *s,char *sp,short nospace)
00084 {
00085         pa_the_str = NULL;
00086         pa_splits = NULL;
00087         pa_ord = NULL;
00088         pa_the_ptr = 0;
00089         pa_breakchar = 0;
00090         pa_enable = pa_disable = 0;
00091         pa_nospace = 0;
00092         pa_quote = false;
00093 
00094         pa_the_str = new char[strlen(s) + 1];
00095         strcpy(pa_the_str,s);
00096         pa_splits = new char[strlen(sp) + 1];
00097         strcpy(pa_splits,sp);
00098         pa_ord = new char[strlen(s) + 1];
00099         *pa_ord = 0;
00100         pa_nospace = 1;
00101 }
00102 
00103 #define FREE(x) if (x) delete x;
00104 
00105 ParseBT::~ParseBT()
00106 {
00107         FREE(pa_the_str);
00108         FREE(pa_splits);
00109         FREE(pa_ord);
00110 }
00111 
00112 #define C pa_the_str[pa_the_ptr]
00113 
00114 short ParseBT::issplit(char c)
00115 {
00116         short i;
00117 
00118         if (pa_splits)
00119                 for (i = 0; (size_t)i < strlen(pa_splits); i++)
00120                         if (pa_splits[i] == c)
00121                                 return 1;
00122         return 0;
00123 }
00124 
00125 void ParseBT::getsplit(void)
00126 {
00127         int x;
00128 
00129         if (C == '=')
00130         {
00131                 x = pa_the_ptr++;
00132         } else
00133         {
00134                 while (C && (issplit(C)))
00135                         pa_the_ptr++;
00136                 x = pa_the_ptr;
00137                 while (C && !issplit(C) && C != '=')
00138                         pa_the_ptr++;
00139         }
00140         if (x == pa_the_ptr && C == '=')
00141                 pa_the_ptr++;
00142         strncpy(pa_ord,pa_the_str + x,pa_the_ptr - x);
00143         pa_ord[pa_the_ptr - x] = 0;
00144 }
00145 
00146 void ParseBT::getword(void)
00147 {
00148         int x;
00149         int disabled = 0;
00150         int quote = 0;
00151 
00152         if (pa_nospace)
00153         {
00154                 while (C && issplit(C))
00155                         pa_the_ptr++;
00156                 x = pa_the_ptr;
00157                 while (C && !issplit(C) && (C != pa_breakchar || !pa_breakchar || disabled))
00158                 {
00159                         if (pa_breakchar && C == pa_disable)
00160                                 disabled = 1;
00161                         if (pa_breakchar && C == pa_enable)
00162                                 disabled = 0;
00163                         if (pa_quote && C == '"')
00164                                 quote = 1;
00165                         pa_the_ptr++;
00166                         while (quote && C && C != '"')
00167                         {
00168                                 pa_the_ptr++;
00169                         }
00170                         if (pa_quote && C == '"')
00171                         {
00172                                 pa_the_ptr++;
00173                         }
00174                         quote = 0;
00175                 }
00176         } else
00177         {
00178                 if (C == pa_breakchar && pa_breakchar)
00179                 {
00180                         x = pa_the_ptr++;
00181                 } else
00182                 {
00183                         while (C && (C == ' ' || C == 9 || C == 13 || C == 10 || issplit(C)))
00184                                 pa_the_ptr++;
00185                         x = pa_the_ptr;
00186 #define DEB(x) 
00187 DEB(printf("Split starts at %d\n",x);)
00188                         while (C && C != ' ' && C != 9 && C != 13 && C != 10 && !issplit(C) &&
00189                          (C != pa_breakchar || !pa_breakchar || disabled))
00190                         {
00191 DEB(printf("Char '%c'\n",C);)
00192                                 if (pa_breakchar && C == pa_disable)
00193                                         disabled = 1;
00194                                 if (pa_breakchar && C == pa_enable)
00195                                         disabled = 0;
00196                                 if (pa_quote && C == '"')
00197                                 {
00198                                         quote = 1;
00199                                 pa_the_ptr++;
00200                                 while (quote && C && C != '"')
00201                                 {
00202                                         pa_the_ptr++;
00203                                 }
00204                                 if (pa_quote && C == '"')
00205                                 {
00206                                         pa_the_ptr++;
00207                                 }
00208                                 }
00209                                 else
00210                                         pa_the_ptr++;
00211                                 quote = 0;
00212                         }
00213                 }
00214                 if (x == pa_the_ptr && C == pa_breakchar && pa_breakchar)
00215                         pa_the_ptr++;
00216         }
00217         strncpy(pa_ord,pa_the_str + x,pa_the_ptr - x);
00218         pa_ord[pa_the_ptr - x] = 0;
00219 }
00220 
00221 void ParseBT::getword(char *s)
00222 {
00223         ParseBT::getword();
00224         strcpy(s,pa_ord);
00225 }
00226 
00227 void ParseBT::getword(char *s,int l)
00228 {
00229         ParseBT::getword();
00230         strncpy(s,pa_ord,l - 1);
00231         s[l - 1] = 0;
00232 }
00233 
00234 void ParseBT::getsplit(char *s)
00235 {
00236         ParseBT::getsplit();
00237         strcpy(s,pa_ord);
00238 }
00239 
00240 void ParseBT::getword(char *s,char *fill,int l)
00241 {
00242         ParseBT::getword();
00243         *s = 0;
00244         while (strlen(s) + strlen(pa_ord) < (size_t)l)
00245                 strcat(s,fill);
00246         strcat(s,pa_ord);
00247 }
00248 
00249 void ParseBT::getrest(char *s)
00250 {
00251         while (C && (C == ' ' || C == 9 || issplit(C)))
00252                 pa_the_ptr++;
00253         strcpy(s,pa_the_str + pa_the_ptr);
00254 }
00255 
00256 long ParseBT::getvalue(void)
00257 {
00258         ParseBT::getword();
00259         return atol(pa_ord);
00260 }
00261 
00262 void ParseBT::setbreak(char c)
00263 {
00264         pa_breakchar = c;
00265 }
00266 
00267 int ParseBT::getwordlen(void)
00268 {
00269         int x,y = pa_the_ptr,len;
00270 
00271         if (C == pa_breakchar && pa_breakchar)
00272         {
00273                 x = pa_the_ptr++;
00274         } else
00275         {
00276                 while (C && (C == ' ' || C == 9 || C == 13 || C == 10 || issplit(C)))
00277                         pa_the_ptr++;
00278                 x = pa_the_ptr;
00279                 while (C && C != ' ' && C != 9 && C != 13 && C != 10 && !issplit(C) && (C != pa_breakchar || !pa_breakchar))
00280                         pa_the_ptr++;
00281         }
00282         if (x == pa_the_ptr && C == pa_breakchar && pa_breakchar)
00283                 pa_the_ptr++;
00284         len = pa_the_ptr - x;
00285         pa_the_ptr = y;
00286         return len;
00287 }
00288 
00289 int ParseBT::getrestlen(void)
00290 {
00291         int y = pa_the_ptr,len;
00292 
00293         while (C && (C == ' ' || C == 9 || issplit(C)))
00294                 pa_the_ptr++;
00295         len = strlen(pa_the_str + pa_the_ptr);
00296         pa_the_ptr = y;
00297         return len;
00298 }
00299 
00300 void ParseBT::getline(void)
00301 {
00302         int x;
00303 
00304 //      while (C && (C == 13 || C == 10))
00305 //              pa_the_ptr++;
00306         x = pa_the_ptr;
00307         while (C && C != 13 && C != 10)
00308                 pa_the_ptr++;
00309         strncpy(pa_ord,pa_the_str + x,pa_the_ptr - x);
00310         pa_ord[pa_the_ptr - x] = 0;
00311         if (C == 13)
00312                 pa_the_ptr++;
00313         if (C == 10)
00314                 pa_the_ptr++;
00315 }
00316 
00317 void ParseBT::getline(char *s)
00318 {
00319         getline();
00320         strcpy(s,pa_ord);
00321 }
00322 
00323 void ParseBT::getline(char *s,int l)
00324 {
00325         getline();
00326         strncpy(s,pa_ord,l - 1);
00327         s[l - 1] = 0;
00328 }
00329 
00330 /* end of implementation of class ParseBT */
00331 /***************************************************/
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4