00001 #include "StringTokenizer.h"
00002
00003 StringTokenizer::StringTokenizer(const std::string& _str, const std::string& _delim)
00004 {
00005
00006 if ((_str.length() == 0) || (_delim.length() == 0)) return;
00007
00008 token_str = _str;
00009 delim = _delim;
00010
00011
00012
00013
00014 unsigned int curr_pos = 0;
00015
00016 while(true)
00017 {
00018 if ((curr_pos = token_str.find(delim,curr_pos)) != std::string::npos)
00019 {
00020 curr_pos += delim.length();
00021
00022 while(token_str.find(delim,curr_pos) == curr_pos)
00023 {
00024 token_str.erase(curr_pos,delim.length());
00025 }
00026 }
00027 else
00028 break;
00029 }
00030
00031
00032
00033
00034 if (token_str.find(delim,0) == 0)
00035 {
00036 token_str.erase(0,delim.length());
00037 }
00038
00039
00040
00041
00042 curr_pos = 0;
00043 if ((curr_pos = token_str.rfind(delim)) != std::string::npos)
00044 {
00045 if (curr_pos != (token_str.length() - delim.length())) return;
00046 token_str.erase(token_str.length() - delim.length(),delim.length());
00047 }
00048
00049 }
00050
00051
00052 int StringTokenizer::countTokens()
00053 {
00054
00055 unsigned int prev_pos = 0;
00056 int num_tokens = 0;
00057
00058 if (token_str.length() > 0)
00059 {
00060 num_tokens = 0;
00061
00062 unsigned int curr_pos = 0;
00063 while(true)
00064 {
00065 if ((curr_pos = token_str.find(delim,curr_pos)) != std::string::npos)
00066 {
00067 num_tokens++;
00068 prev_pos = curr_pos;
00069 curr_pos += delim.length();
00070 }
00071 else
00072 break;
00073 }
00074 return ++num_tokens;
00075 }
00076 else
00077 {
00078 return 0;
00079 }
00080
00081 }
00082
00083
00084 bool StringTokenizer::hasMoreTokens()
00085 {
00086 return (token_str.length() > 0);
00087 }
00088
00089
00090 std::string StringTokenizer::nextToken()
00091 {
00092
00093 if (token_str.length() == 0)
00094 return "";
00095
00096 std::string tmp_str = "";
00097 unsigned int pos = token_str.find(delim,0);
00098
00099 if (pos != std::string::npos)
00100 {
00101 tmp_str = token_str.substr(0,pos);
00102 token_str = token_str.substr(pos+delim.length(),token_str.length()-pos);
00103 }
00104 else
00105 {
00106 tmp_str = token_str.substr(0,token_str.length());
00107 token_str = "";
00108 }
00109
00110 return tmp_str;
00111 }
00112
00113
00114 int StringTokenizer::nextIntToken()
00115 {
00116 return atoi(nextToken().c_str());
00117 }
00118
00119
00120 double StringTokenizer::nextFloatToken()
00121 {
00122 return atof(nextToken().c_str());
00123 }
00124
00125
00126 std::string StringTokenizer::nextToken(const std::string& delimiter)
00127 {
00128 if (token_str.length() == 0)
00129 return "";
00130
00131 std::string tmp_str = "";
00132 unsigned int pos = token_str.find(delimiter,0);
00133
00134 if (pos != std::string::npos)
00135 {
00136 tmp_str = token_str.substr(0,pos);
00137 token_str = token_str.substr(pos + delimiter.length(),token_str.length() - pos);
00138 }
00139 else
00140 {
00141 tmp_str = token_str.substr(0,token_str.length());
00142 token_str = "";
00143 }
00144
00145 return tmp_str;
00146 }
00147
00148
00149 std::string StringTokenizer::remainingString()
00150 {
00151 return token_str;
00152 }
00153
00154
00155 std::string StringTokenizer::filterNextToken(const std::string& filterStr)
00156 {
00157 std::string tmp_str = nextToken();
00158 unsigned int currentPos = 0;
00159
00160 while((currentPos = tmp_str.find(filterStr,currentPos)) != std::string::npos)
00161 {
00162 tmp_str.erase(currentPos,filterStr.length());
00163 }
00164
00165 return tmp_str;
00166 }