Google
Web alhem.net
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

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

Generated for My SDL C++ Gui by doxygen 1.3.6