# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

algo.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000-2002 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "stdmisc.h"
00027 
00028 #include "nel/misc/algo.h"
00029 #include <string>
00030 
00031 
00032 using   namespace std;
00033 
00034 
00035 namespace NLMISC 
00036 {
00037 
00038 
00039 // ***************************************************************************
00040 bool            testWildCard(const char *strIn, const char *wildCard)
00041 {
00042         // run the 2 string in //el
00043         while(*wildCard!=0 && *strIn!=0)
00044         {
00045                 // if same char, continue.
00046                 if(*wildCard==*strIn)
00047                 {
00048                         wildCard++;
00049                         strIn++;
00050                 }
00051                 // if wildCard is ?, continue
00052                 else if(*wildCard=='?')
00053                 {
00054                         wildCard++;
00055                         strIn++;
00056                 }
00057                 // if wildcard is *, recurs check.
00058                 else if(*wildCard=='*')
00059                 {
00060                         wildCard++;
00061                         // if last *, its OK.
00062                         if(*wildCard==0)
00063                                 return true;
00064                         // else must check next strings.
00065                         else
00066                         {
00067                                 // build the wilcard token. eg from "*pipo?", take "pipo"
00068                                 string  token;
00069                                 while(*wildCard!='*' && *wildCard!='?' && *wildCard!=0)
00070                                 {
00071                                         token+= *wildCard;
00072                                         wildCard++;
00073                                 }
00074                                 // if token size is empty, error
00075                                 if(token.empty())
00076                                         return false;
00077 
00078                                 // in strIn, search all the occurence of token. For each solution, recurs test.
00079                                 string  sCopy= strIn;
00080                                 uint    pos= sCopy.find(token, 0);
00081                                 while(pos!=string::npos)
00082                                 {
00083                                         // do a testWildCard test on the remaining string/wildCard
00084                                         if( testWildCard(strIn+pos+token.size(), wildCard) )
00085                                                 // if succeed, end
00086                                                 return true;
00087                                         // fails=> test with an other occurence of token in the string.
00088                                         pos= sCopy.find(token, pos+1);
00089                                 }
00090                                 
00091                                 // if all failed, fail
00092                                 return false;
00093                         }
00094                 }
00095                 // else fail
00096                 else
00097                         return false;
00098         }
00099 
00100         // If quit here because end Of 2 strs, OK.
00101         if(*wildCard==0 && *strIn==0)
00102                 return true;
00103         // if quit here because wildCard=="*" and s="", OK too.
00104         if(*strIn==0 && wildCard[0]=='*' && wildCard[1]==0)
00105                 return true;
00106 
00107         /*
00108                 Else false:
00109                         It may be wildCard="?aez" and s="" => error
00110                         It may be wildCard="" and s="aer" => error
00111         */
00112         return false;
00113 }
00114 
00115 
00116 // ***************************************************************************
00117 void            splitString(const std::string &str, const std::string &separator, std::vector<std::string> &retList)
00118 {
00119         sint    pos=0;
00120         sint    newPos=0;
00121         retList.clear();
00122         while( (newPos= str.find(separator,pos)) != string::npos)
00123         {
00124                 // if not empty sub str. (skip repetition of separator )
00125                 if(newPos-pos>0)
00126                         retList.push_back(str.substr(pos, newPos-pos));
00127                 // skip token
00128                 pos= newPos+separator.size();
00129         }
00130         // copy the last substr
00131         if( pos<(sint)str.size() )
00132                 retList.push_back(str.substr(pos, str.size()-pos));
00133 }
00134 
00135 
00136 } // NLMISC