Tokenizer.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _TOKENIZER_H
00023 #define _TOKENIZER_H
00024
00025 #include <string>
00026
00027
00029
00030 class Tokenizer{
00031
00032 private:
00033 std::string arg;
00034 std::string delim;
00035 std::string _nextToken();
00036
00037 public:
00038
00040
00043 Tokenizer( const std::string& s, const std::string& d );
00044
00046 std::string nextToken();
00047
00049 int hasMoreTokens();
00050
00051 };
00052
00053
00054
00055 inline Tokenizer::Tokenizer( const std::string& s, const std::string& t )
00056 {
00057 arg = s;
00058 delim = t;
00059 }
00060
00061
00062
00063 inline std::string Tokenizer::_nextToken()
00064 {
00065 int n;
00066 std::string result;
00067
00068 n = arg.find( delim );
00069
00070
00071 if( n < 0 ){
00072 result = arg;
00073 arg = std::string();
00074 }
00075 else{
00076 result = arg.substr( 0, n );
00077 arg = arg.substr( n + delim.length(), arg.length() );
00078 }
00079
00080 return result;
00081 }
00082
00083
00084
00085 inline std::string Tokenizer::nextToken()
00086 {
00087 std::string result;
00088 do{
00089 result = _nextToken();
00090 }
00091 while( result.empty() && !arg.empty() );
00092
00093 return result;
00094
00095 }
00096
00097
00098 inline int Tokenizer::hasMoreTokens()
00099 {
00100 int n = arg.find_first_not_of( delim, 0 );
00101 if( n >= 0 ) return 1;
00102 else return 0;
00103 }
00104
00105
00106
00107 #endif