1 #include "StringTokenizer.h"
3 StringTokenizer::StringTokenizer(const std::string
& _str
, const std::string
& _delim
)
6 if ((_str
.length() == 0) || (_delim
.length() == 0)) return;
12 Remove sequential delimiter
14 unsigned int curr_pos
= 0;
18 if ((curr_pos
= token_str
.find(delim
,curr_pos
)) != std::string::npos
)
20 curr_pos
+= delim
.length();
22 while(token_str
.find(delim
,curr_pos
) == curr_pos
)
24 token_str
.erase(curr_pos
,delim
.length());
32 Trim leading delimiter
34 if (token_str
.find(delim
,0) == 0)
36 token_str
.erase(0,delim
.length());
43 if ((curr_pos
= token_str
.rfind(delim
)) != std::string::npos
)
45 if (curr_pos
!= (token_str
.length() - delim
.length())) return;
46 token_str
.erase(token_str
.length() - delim
.length(),delim
.length());
52 int StringTokenizer::countTokens()
55 unsigned int prev_pos
= 0;
58 if (token_str
.length() > 0)
62 unsigned int curr_pos
= 0;
65 if ((curr_pos
= token_str
.find(delim
,curr_pos
)) != std::string::npos
)
69 curr_pos
+= delim
.length();
84 bool StringTokenizer::hasMoreTokens()
86 return (token_str
.length() > 0);
90 std::string
StringTokenizer::nextToken()
93 if (token_str
.length() == 0)
96 std::string tmp_str
= "";
97 unsigned int pos
= token_str
.find(delim
,0);
99 if (pos
!= std::string::npos
)
101 tmp_str
= token_str
.substr(0,pos
);
102 token_str
= token_str
.substr(pos
+delim
.length(),token_str
.length()-pos
);
106 tmp_str
= token_str
.substr(0,token_str
.length());
114 int StringTokenizer::nextIntToken()
116 return atoi(nextToken().c_str());
120 double StringTokenizer::nextFloatToken()
122 return atof(nextToken().c_str());
126 std::string
StringTokenizer::nextToken(const std::string
& delimiter
)
128 if (token_str
.length() == 0)
131 std::string tmp_str
= "";
132 unsigned int pos
= token_str
.find(delimiter
,0);
134 if (pos
!= std::string::npos
)
136 tmp_str
= token_str
.substr(0,pos
);
137 token_str
= token_str
.substr(pos
+ delimiter
.length(),token_str
.length() - pos
);
141 tmp_str
= token_str
.substr(0,token_str
.length());
149 std::string
StringTokenizer::remainingString()
155 std::string
StringTokenizer::filterNextToken(const std::string
& filterStr
)
157 std::string tmp_str
= nextToken();
158 unsigned int currentPos
= 0;
160 while((currentPos
= tmp_str
.find(filterStr
,currentPos
)) != std::string::npos
)
162 tmp_str
.erase(currentPos
,filterStr
.length());