-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringTokenizer.cpp
167 lines (129 loc) · 3.2 KB
/
StringTokenizer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! \file StringTokenizer.cpp - implementation of StringTokenizer class
#include "StringTokenizer.h"
StringTokenizer::StringTokenizer(const std::string& _str, const std::string& _delim)
{
if ((_str.length() == 0) || (_delim.length() == 0)) return;
token_str = _str;
delim = _delim;
/*
Remove sequential delimiter
*/
std::string::size_type curr_pos = 0;
while(true)
{
if ((curr_pos = token_str.find(delim,curr_pos)) != std::string::npos)
{
curr_pos += delim.length();
while(token_str.find(delim,curr_pos) == curr_pos)
{
token_str.erase(curr_pos,delim.length());
}
}
else
break;
}
/*
Trim leading delimiter
*/
if (token_str.find(delim,0) == 0)
{
token_str.erase(0,delim.length());
}
/*
Trim ending delimiter
*/
curr_pos = 0;
if ((curr_pos = token_str.rfind(delim)) != std::string::npos)
{
if (curr_pos != (token_str.length() - delim.length())) return;
token_str.erase(token_str.length() - delim.length(),delim.length());
}
}
int StringTokenizer::countTokens()
{
std::string::size_type prev_pos = 0;
int num_tokens = 0;
if (token_str.length() > 0)
{
num_tokens = 0;
std::string::size_type curr_pos = 0;
while(true)
{
if ((curr_pos = token_str.find(delim,curr_pos)) != std::string::npos)
{
num_tokens++;
prev_pos = curr_pos;
curr_pos += delim.length();
}
else
break;
}
return ++num_tokens;
}
else
{
return 0;
}
}
bool StringTokenizer::hasMoreTokens()
{
return (token_str.length() > 0);
}
std::string StringTokenizer::nextToken()
{
if (token_str.length() == 0)
return "";
std::string tmp_str = "";
std::string::size_type pos = token_str.find(delim,0);
if (pos != std::string::npos)
{
tmp_str = token_str.substr(0,pos);
token_str = token_str.substr(pos+delim.length(),token_str.length()-pos);
}
else
{
tmp_str = token_str.substr(0,token_str.length());
token_str = "";
}
return tmp_str;
}
int StringTokenizer::nextIntToken()
{
return atoi(nextToken().c_str());
}
double StringTokenizer::nextFloatToken()
{
return atof(nextToken().c_str());
}
std::string StringTokenizer::nextToken(const std::string& delimiter)
{
if (token_str.length() == 0)
return "";
std::string tmp_str = "";
std::string::size_type pos = token_str.find(delimiter,0);
if (pos != std::string::npos)
{
tmp_str = token_str.substr(0,pos);
token_str = token_str.substr(pos + delimiter.length(),token_str.length() - pos);
}
else
{
tmp_str = token_str.substr(0,token_str.length());
token_str = "";
}
return tmp_str;
}
std::string StringTokenizer::remainingString()
{
return token_str;
}
std::string StringTokenizer::filterNextToken(const std::string& filterStr)
{
std::string tmp_str = nextToken();
unsigned int currentPos = 0;
while((currentPos = tmp_str.find(filterStr,currentPos)) != std::string::npos)
{
tmp_str.erase(currentPos,filterStr.length());
}
return tmp_str;
}