00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __TOKENIZER_H__
00021 #define __TOKENIZER_H__
00022
00023 #include <functional>
00024 #include <string>
00025 #include <algorithm>
00026 #include <vector>
00027
00028
00029 class CSpace : public std::unary_function<char, bool>
00030 {
00031 public :
00032 bool operator () ( char C ) const;
00033 };
00034
00035 class CNewLine : public std::unary_function<char, bool>
00036 {
00037 public :
00038 bool operator () ( char C ) const;
00039 };
00040
00041 class CComma : public std::unary_function<char, bool>
00042 {
00043 public :
00044 bool operator () ( char C ) const;
00045 };
00046
00047 class CSeparator : public std::unary_function<char, bool>
00048 {
00049 private :
00050 std::string m_String;
00051
00052 public :
00053 CSeparator( const std::string & String );
00054 bool operator () ( char C ) const;
00055 };
00056
00057 template <typename Predicate> class CTokenizer
00058 {
00059 public :
00060 static std::vector<std::string> Tokenize( const std::string & String, const Predicate & P = Predicate() )
00061 {
00062 std::vector<std::string> Tokens;
00063 std::string::const_iterator It(String.begin());
00064 std::string::const_iterator ItTokenEnd(String.begin());
00065
00066
00067 while( It != String.end() )
00068 {
00069 while( P(*It) )
00070 It++;
00071
00072 ItTokenEnd = find_if(It, String.end(), P);
00073
00074 if( It < ItTokenEnd )
00075 Tokens.push_back(std::string(It, ItTokenEnd));
00076
00077 It = ItTokenEnd;
00078 }
00079
00080 return Tokens;
00081 }
00082 };
00083
00084
00085 typedef CTokenizer<CSpace> CSpaceTokenizer;
00086 typedef CTokenizer<CComma> CCommaTokenizer;
00087 typedef CTokenizer<CNewLine> CNewLineTokenizer;
00088
00089 #endif //__TOKENIZER_H__