Subversion Repositories public

Rev

Rev 55 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 55 Rev 67
1
1
2
/// string tokenizer implementation
2
/// string tokenizer implementation
3
/**
3
/**
4
 * \file strtok.cpp
4
 * \file strtok.cpp
5
 *
5
 *
6
 * string tokenizer
6
 * string tokenizer
7
 *
7
 *
8
 * Copyright (C) 2006 Lukas Jelinek, <lukas@aiken.cz>
8
 * Copyright (C) 2006, 2007 Lukas Jelinek, <lukas@aiken.cz>
9
 *
9
 *
10
 * This program is free software; you can redistribute it and/or
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of one of the following licenses:
11
 * modify it under the terms of one of the following licenses:
12
 *
12
 *
13
 * \li 1. X11-style license (see LICENSE-X11)
13
 * \li 1. X11-style license (see LICENSE-X11)
14
 * \li 2. GNU Lesser General Public License, version 2.1 (see LICENSE-LGPL)
14
 * \li 2. GNU Lesser General Public License, version 2.1 (see LICENSE-LGPL)
15
 * \li 3. GNU General Public License, version 2  (see LICENSE-GPL)
15
 * \li 3. GNU General Public License, version 2  (see LICENSE-GPL)
16
 *
16
 *
17
 * If you want to help with choosing the best license for you,
17
 * If you want to help with choosing the best license for you,
18
 * please visit http://www.gnu.org/licenses/license-list.html.
18
 * please visit http://www.gnu.org/licenses/license-list.html.
19
 *
19
 *
20
 */
20
 */
21
21
22
22
23
#include <sstream>
23
#include <sstream>
24
24
25
#include "strtok.h"
25
#include "strtok.h"
26
26
27
StringTokenizer::StringTokenizer(const std::string& rStr, char cDelim, char cPrefix)
27
StringTokenizer::StringTokenizer(const std::string& rStr, char cDelim, char cPrefix)
28
{
28
{
29
  m_str = rStr;
29
  m_str = rStr;
30
  m_cDelim = cDelim;
30
  m_cDelim = cDelim;
31
  m_cPrefix = cPrefix;
31
  m_cPrefix = cPrefix;
32
  m_pos = 0;
32
  m_pos = 0;
33
  m_len = rStr.length();
33
  m_len = rStr.length();
34
}
34
}
35
   
35
   
36
std::string StringTokenizer::GetNextToken(bool fSkipEmpty)
36
std::string StringTokenizer::GetNextToken(bool fSkipEmpty)
37
{
37
{
38
  std::string s;
38
  std::string s;
39
 
39
 
40
  do {
40
  do {
41
    _GetNextToken(s, true);
41
    _GetNextToken(s, true);
42
  } while (fSkipEmpty && s.empty() && m_pos < m_len);
42
  } while (fSkipEmpty && s.empty() && m_pos < m_len);
43
 
43
 
44
  return s;
44
  return s;
45
}
45
}
46
46
47
std::string StringTokenizer::GetNextTokenRaw(bool fSkipEmpty)
47
std::string StringTokenizer::GetNextTokenRaw(bool fSkipEmpty)
48
{
48
{
49
  std::string s;
49
  std::string s;
50
 
50
 
51
  do {
51
  do {
52
    _GetNextToken(s, false);
52
    _GetNextToken(s, false);
53
  } while (fSkipEmpty && s.empty() && m_pos < m_len);
53
  } while (fSkipEmpty && s.empty() && m_pos < m_len);
54
 
54
 
55
  return s;
55
  return s;
56
}
56
}
57
57
58
std::string StringTokenizer::GetRemainder()
58
std::string StringTokenizer::GetRemainder()
59
{
59
{
60
  return  m_cPrefix == '\0'
60
  return  m_cPrefix == '\0'
61
      ?   m_str.substr(m_pos)
61
      ?   m_str.substr(m_pos)
62
      :   StripPrefix(m_str.c_str() + m_pos, m_len - m_pos);
62
      :   StripPrefix(m_str.c_str() + m_pos, m_len - m_pos);
63
}
63
}
64
64
65
std::string StringTokenizer::StripPrefix(const char* s, SIZE cnt)
65
std::string StringTokenizer::StripPrefix(const char* s, SIZE cnt)
66
{
66
{
67
  std::ostringstream stream;
67
  std::ostringstream stream;
68
  SIZE pos = 0;
68
  SIZE pos = 0;
69
  while (pos < cnt) {
69
  while (pos < cnt) {
70
    if (s[pos] == m_cPrefix) {
70
    if (s[pos] == m_cPrefix) {
71
      if ((pos < cnt - 1) && s[pos+1] == m_cPrefix) {
71
      if ((pos < cnt - 1) && s[pos+1] == m_cPrefix) {
72
        stream << m_cPrefix;
72
        stream << m_cPrefix;
73
        pos++;
73
        pos++;
74
      }
74
      }
75
    }
75
    }
76
    else {
76
    else {
77
      stream << s[pos];
77
      stream << s[pos];
78
    }
78
    }
79
   
79
   
80
    pos++;
80
    pos++;
81
  }
81
  }
82
 
82
 
83
  return stream.str();
83
  return stream.str();
84
}
84
}
85
85
86
void StringTokenizer::_GetNextToken(std::string& rToken, bool fStripPrefix)
86
void StringTokenizer::_GetNextToken(std::string& rToken, bool fStripPrefix)
87
{
87
{
88
  if (m_cPrefix == '\0') {
88
  if (m_cPrefix == '\0') {
89
    _GetNextTokenNoPrefix(rToken);
89
    _GetNextTokenNoPrefix(rToken);
90
  }
90
  }
91
  else {
91
  else {
92
    _GetNextTokenWithPrefix(rToken);
92
    _GetNextTokenWithPrefix(rToken);
93
    if (fStripPrefix)
93
    if (fStripPrefix)
94
      rToken = StripPrefix(rToken.c_str(), rToken.length());
94
      rToken = StripPrefix(rToken.c_str(), rToken.length());
95
  }
95
  }
96
}
96
}
97
97
98
void StringTokenizer::_GetNextTokenNoPrefix(std::string& rToken)
98
void StringTokenizer::_GetNextTokenNoPrefix(std::string& rToken)
99
{
99
{
-
 
100
  const char* s = m_str.c_str();
100
  for (SIZE i=m_pos; i<m_len; i++) {
101
  for (SIZE i=m_pos; i<m_len; i++) {
101
    if (m_str[i] == m_cDelim) {
102
    if (s[i] == m_cDelim) {
102
      rToken = m_str.substr(m_pos, i - m_pos);
103
      rToken = m_str.substr(m_pos, i - m_pos);
103
      m_pos = i + 1;
104
      m_pos = i + 1;
104
      return;
105
      return;
105
    }    
106
    }    
106
  }
107
  }
107
 
108
 
108
  rToken = m_str.substr(m_pos);
109
  rToken = m_str.substr(m_pos);
109
  m_pos = m_len;
110
  m_pos = m_len;
110
}
111
}
111
 
112
 
112
void StringTokenizer::_GetNextTokenWithPrefix(std::string& rToken)
113
void StringTokenizer::_GetNextTokenWithPrefix(std::string& rToken)
113
{
114
{
114
  int pref = 0;
115
  int pref = 0;
-
 
116
  const char* s = m_str.c_str();
115
  for (SIZE i=m_pos; i<m_len; i++) {
117
  for (SIZE i=m_pos; i<m_len; i++) {
116
    if (m_str[i] == m_cDelim) {
118
    if (s[i] == m_cDelim) {
117
      if (pref == 0) {
119
      if (pref == 0) {
118
        rToken = m_str.substr(m_pos, i - m_pos);
120
        rToken = m_str.substr(m_pos, i - m_pos);
119
        m_pos = i + 1;
121
        m_pos = i + 1;
120
        return;
122
        return;
121
      }
123
      }
122
      else {
124
      else {
123
        pref = 0;
125
        pref = 0;
124
      }
126
      }
125
    }
127
    }
126
    else if (m_str[i] == m_cPrefix) {
128
    else if (s[i] == m_cPrefix) {
127
      if (pref == 1)
129
      if (pref == 1)
128
        pref = 0;
130
        pref = 0;
129
      else
131
      else
130
        pref = 1;
132
        pref = 1;
131
    }
133
    }
132
    else {
134
    else {
133
      pref = 0;
135
      pref = 0;
134
    }
136
    }
135
  }
137
  }
136
 
138
 
137
  rToken = m_str.substr(m_pos);
139
  rToken = m_str.substr(m_pos);
138
  m_pos = m_len;
140
  m_pos = m_len;
139
}
141
}
140
142
141
 
143