ColPack
Utilities/StringTokenizer.cpp
Go to the documentation of this file.
00001 /************************************************************************************
00002     Copyright (C) 2005-2008 Assefaw H. Gebremedhin, Arijit Tarafdar, Duc Nguyen,
00003     Alex Pothen
00004 
00005     This file is part of ColPack.
00006 
00007     ColPack is free software: you can redistribute it and/or modify
00008     it under the terms of the GNU Lesser General Public License as published
00009     by the Free Software Foundation, either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     ColPack is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public License
00018     along with ColPack.  If not, see <http://www.gnu.org/licenses/>.
00019 ************************************************************************************/
00020 
00021 #include <vector>
00022 #include <string>
00023 
00024 using namespace std;
00025 
00026 #include "Definitions.h"
00027 
00028 #include "StringTokenizer.h"
00029 
00030 namespace ColPack
00031 {
00032         //Public Constructor 4151
00033         StringTokenizer::StringTokenizer()
00034         {
00035 
00036         }
00037 
00038         
00039         //Public Constructor 4152
00040         StringTokenizer::StringTokenizer(char * InputChar)
00041         {
00042           string TempInputString(InputChar);
00043           
00044           InputString = TempInputString;
00045           TokenString = InputString;
00046 
00047         }
00048 
00049         
00050         //Public Constructor 4153
00051         StringTokenizer::StringTokenizer(char * InputChar, char * DelimiterChar)
00052         {
00053           string TempInputString(InputChar);
00054           string TempDelimiterString(DelimiterChar);
00055 
00056           InputString = TempInputString;
00057           TokenString = InputString;
00058 
00059           DelimiterString = TempDelimiterString;
00060 
00061         }
00062 
00063         
00064         //Public Constructor 4154
00065         StringTokenizer::StringTokenizer(string InputChar, char * DelimiterChar)
00066         {
00067           string TempDelimiterString(DelimiterChar);
00068 
00069           InputString = InputChar;
00070           TokenString = InputString;
00071 
00072           DelimiterString = TempDelimiterString;
00073 
00074         }
00075 
00076         
00077         //Public Constructor 4155
00078         StringTokenizer::StringTokenizer(string InputChar, string DelimiterChar)
00079         {
00080           InputString = InputChar;
00081           TokenString = InputString;
00082 
00083           DelimiterString = DelimiterChar;
00084 
00085         }
00086 
00087         
00088         //Public Destructor 4156
00089         StringTokenizer::~StringTokenizer()
00090         {
00091 
00092 
00093         }
00094 
00095         
00096         //Public Function 4157
00097         int StringTokenizer::CountTokens()
00098         {
00099                 int TokenCounter = 1;
00100 
00101                 int DelimiterPosition;
00102           
00103                 int LastPosition;
00104 
00105                 int TokenStringLength = TokenString.size();
00106                 int DelimiterStringLength = DelimiterString.size();
00107 
00108                 string DelimiterSubString;
00109 
00110                 if(TokenStringLength == 0)
00111                 {
00112                         return(0);
00113                 }
00114 
00115                 if(DelimiterStringLength == 0)
00116                 {
00117                         return(1);
00118                 }
00119 
00120                 DelimiterPosition = 0;
00121                 LastPosition = 0;
00122 
00123                 for ( ; ; )
00124                 {
00125             
00126                         DelimiterPosition = TokenString.find(DelimiterString, DelimiterPosition);
00127 
00128                         if(DelimiterPosition == 0)
00129                         { 
00130                                 DelimiterPosition += DelimiterStringLength;
00131 
00132                                 continue;
00133                         }
00134 
00135                         if((DelimiterPosition < 0) || (DelimiterPosition == TokenStringLength))
00136                         { 
00137                                 return(TokenCounter);
00138                         }
00139             
00140                         if(DelimiterStringLength != (DelimiterPosition - LastPosition))
00141                         {
00142                                 //      cout<<"Delimiter Position = "<<DelimiterPosition<<endl;
00143 
00144                                 TokenCounter++;
00145                         }
00146 
00147                         LastPosition = DelimiterPosition;
00148             
00149                         DelimiterPosition += DelimiterStringLength;
00150 
00151                 }
00152         }
00153 
00154 
00155         
00156         //Public Function 4158
00157         int StringTokenizer::CountTokens(char * DelimiterChar)
00158         {
00159           SetDelimiterString(DelimiterChar);
00160 
00161           return(CountTokens());
00162         }
00163 
00164 
00165         
00166         //Public Function 4159
00167         string StringTokenizer::GetDelimiterString() const
00168         {
00169           return(DelimiterString);
00170         }
00171 
00172 
00173         
00174         //Public Function 4160
00175         string StringTokenizer::GetFirstToken()
00176         {
00177           int TokenCount = 0;
00178 
00179           string StringToken;
00180 
00181           TokenString = InputString;
00182 
00183           while(HasMoreTokens())
00184           {
00185                 if(TokenCount == 1)
00186                 {
00187                   break;
00188                 }
00189 
00190                 StringToken = GetNextToken();
00191 
00192                 TokenCount++;
00193 
00194           }
00195 
00196           return(StringToken);
00197         }
00198 
00199         
00200         //Public Function 4161
00201         string StringTokenizer::GetInputString() const
00202         {
00203           return(InputString);
00204         }
00205 
00206         
00207         //Public Function 4162
00208         string StringTokenizer::GetLastToken()
00209         {
00210           string StringToken;
00211 
00212           TokenString = InputString;
00213 
00214           while(HasMoreTokens())
00215           {
00216                 StringToken = GetNextToken();
00217           }
00218 
00219           return(StringToken);
00220           
00221         }
00222 
00223         
00224         //Public Function 4163
00225         string StringTokenizer::GetNextToken()
00226         {
00227           string Token;
00228           
00229           int DelimiterPosition;
00230           
00231           int TokenStringLength = TokenString.size();
00232           int DelimiterStringLength = DelimiterString.size();
00233 
00234           string DelimiterSubString;
00235 
00236           if((TokenStringLength == 0))
00237           {
00238                 return(NULL);
00239           }
00240 
00241           if(DelimiterStringLength == 0)
00242           {
00243                 return(InputString);
00244           }
00245           
00246           DelimiterPosition = TokenString.find(DelimiterString);
00247 
00248           if(DelimiterPosition == 0)
00249           {
00250                 for ( ; ; )
00251                 {
00252                   if(TokenString.substr(0, DelimiterStringLength) == DelimiterString)
00253                   {
00254                         TokenString.erase(0, DelimiterStringLength);
00255                   }
00256                   else
00257                   {
00258                         break;
00259                   }
00260                 }
00261 
00262                 DelimiterPosition = TokenString.find(DelimiterString);
00263           }
00264             
00265           if(DelimiterPosition < 0)
00266           {
00267                 Token = TokenString;
00268 
00269                 TokenString.erase();
00270           }
00271           else
00272           {
00273 
00274                 Token = TokenString.substr(0, DelimiterPosition);
00275 
00276                 TokenString.erase(0, DelimiterPosition+DelimiterStringLength);
00277           
00278 
00279                 DelimiterPosition = 0;
00280 
00281                 for ( ; ; )
00282                 {
00283                   if(TokenString.substr(0, DelimiterStringLength) == DelimiterString)
00284                   {
00285                         TokenString.erase(0, DelimiterStringLength);
00286                   }
00287                   else
00288                   {
00289                         break;
00290                   }
00291                 }
00292             
00293           }
00294 
00295           return(Token);
00296         }  
00297 
00298         
00299         //Public Function 4164
00300         string StringTokenizer::GetNextToken(char * DelimiterChar)
00301         {
00302           SetDelimiterString(DelimiterChar);
00303 
00304           return(GetNextToken());
00305         }
00306 
00307         
00308         //Public Function 4165
00309         string StringTokenizer::GetToken(int TokenPosition)
00310         {
00311           int TokenCount = 0;
00312 
00313           string StringToken;
00314 
00315           TokenString = InputString;
00316 
00317           while(HasMoreTokens())
00318           {
00319                 if(TokenCount == TokenPosition)
00320                 {
00321                   break;
00322                 }
00323 
00324                 StringToken = GetNextToken();
00325 
00326                 TokenCount++;
00327           }
00328 
00329           return(StringToken);
00330         }
00331 
00332         
00333         //Public Function 4166
00334         int StringTokenizer::HasMoreTokens()
00335         {
00336           return(CountTokens());
00337         }
00338 
00339         
00340         //Public Function 4167
00341         int StringTokenizer::HasMoreTokens(char * DelimiterChar)
00342         {
00343           SetDelimiterString(DelimiterChar);
00344 
00345           return(HasMoreTokens());
00346         }
00347 
00348         
00349         //Public Function 4168
00350         int StringTokenizer::SetInputString(char * InputChar)
00351         {
00352           string TempInputString(InputChar);
00353 
00354           InputString = TempInputString;
00355           TokenString = InputString;
00356 
00357           return(0);
00358         }
00359 
00360         
00361         //Public Function 4169
00362         int StringTokenizer::SetDelimiterString(char * DelimiterChar)
00363         {
00364           string TempDelimiterString(DelimiterChar);
00365 
00366           DelimiterString = TempDelimiterString;
00367 
00368           return(0);
00369         }
00370 
00371 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines