31
int SimpleParser::tokenize (const string &line, vector<string> &words,
32
const string &delimiters) {
34
// Skip delimiters at beginning.
35
string::size_type lastPos = line.find_first_not_of (delimiters, 0);
36
// Find first "delimiter".
37
string::size_type pos = line.find_first_of (delimiters, lastPos);
39
string::size_type hpos = line.find_first_of ('"', lastPos);
41
if (hpos != string::npos && hpos < pos)
42
// Find next "delimiter" after closing '"'
43
pos = line.find_first_of (delimiters, line.find_first_of ('"', hpos + 1) + 1);
45
while ((string::npos != pos) || (string::npos != lastPos)) {
46
string token = line.substr (lastPos, pos - lastPos);
48
//if (token[0] == '"' && token[token.length () - 1] == '"')
49
// token = token.substr (1, token.length () - 2);
50
// Found a token, add it to the vector.
31
string::size_type SimpleParser::findBeginOfNextToken (const string& line, const string& delimiters, string::size_type pos) {
33
string::size_type lastPos = string::npos;
34
if (pos != string::npos) {
35
for (string::size_type i = pos; i < line.length() && lastPos == string::npos; i++) {
37
if (delimiters.find(c) == string::npos) {
38
lastPos = escaped ? i-1 : i;
51
string::size_type SimpleParser::findEndOfNextToken (const string& line, const string& delimiters, string::size_type lastPos) {
52
bool in_string = false, escaped = false;
53
string::size_type endpos = line.length();
54
if (lastPos != string::npos) {
55
for (string::size_type i = lastPos; i < line.length(); i++) {
57
if (! in_string && delimiters.find(c) != string::npos) {
61
if (! escaped && c == '"') {
62
in_string = ! in_string;
75
string::size_type SimpleParser::next (const string& line, const string& delimiters, string::size_type pos, string::size_type& lastPos) {
76
// find begin of next token
77
lastPos = findBeginOfNextToken(line, delimiters, pos);
78
// find end of the token
79
return findEndOfNextToken(line, delimiters, lastPos);
83
int SimpleParser::tokenize (const std::string& line, std::vector<std::string>& words, const std::string& delimiters) {
85
string::size_type pos = 0, lastPos = 0;
87
// get first token position
88
pos = next (line, delimiters, 0, lastPos);
90
// while not at the end of the line
91
while (pos != string::npos && lastPos != string::npos) {
93
string token = line.substr (lastPos, pos-lastPos);
94
// add current token to vector
51
95
words.push_back (token);
53
// Skip delimiters. Note the "not_of"
54
lastPos = line.find_first_not_of (delimiters, pos);
55
// Find next "non-delimiter"
56
pos = line.find_first_of (delimiters, lastPos);
58
hpos = line.find_first_of ('"', lastPos);
59
if (hpos != string::npos && hpos < pos)
60
// Find next "delimiter" after closing '"'
61
pos = line.find_first_of (delimiters, line.find_first_of ('"', hpos + 1) + 1);
97
// get next token position
98
pos = next (line, delimiters, pos, lastPos);