1 |
astrand |
930 |
///////////////////////////////////////////////////////////////////////////// |
2 |
|
|
// Tokenizer.cpp |
3 |
|
|
// |
4 |
|
|
// Date: Thursday, November 18, 1999 |
5 |
|
|
// Autor: Eduardo Velasquez |
6 |
|
|
// Description: Tokenizer class for CStrings. Works like strtok(). |
7 |
|
|
/////////////// |
8 |
|
|
|
9 |
|
|
//#include "atlstr.h" |
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
#include "Tokenizer.h" |
14 |
|
|
|
15 |
|
|
#ifdef _DEBUG |
16 |
|
|
#define new DEBUG_NEW |
17 |
|
|
#undef THIS_FILE |
18 |
|
|
static char THIS_FILE[] = __FILE__; |
19 |
|
|
#endif |
20 |
|
|
|
21 |
|
|
CTokenizer::CTokenizer(const CStdString& cs, const CStdString& csDelim): |
22 |
|
|
m_cs(cs), |
23 |
|
|
m_nCurPos(0) |
24 |
|
|
{ |
25 |
|
|
SetDelimiters(csDelim); |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
void CTokenizer::SetDelimiters(const CStdString& csDelim) |
29 |
|
|
{ |
30 |
|
|
for(int i = 0; i < csDelim.GetLength(); ++i) |
31 |
|
|
m_delim.set(static_cast<BYTE>(csDelim[i])); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
bool CTokenizer::Next(CStdString& cs) |
35 |
|
|
{ |
36 |
|
|
cs.Empty(); |
37 |
|
|
|
38 |
|
|
while(m_nCurPos < m_cs.GetLength() && m_delim[static_cast<BYTE>(m_cs[m_nCurPos])]) |
39 |
|
|
++m_nCurPos; |
40 |
|
|
|
41 |
|
|
if(m_nCurPos >= m_cs.GetLength()) |
42 |
|
|
return false; |
43 |
|
|
|
44 |
|
|
int nStartPos = m_nCurPos; |
45 |
|
|
while(m_nCurPos < m_cs.GetLength() && !m_delim[static_cast<BYTE>(m_cs[m_nCurPos])]) |
46 |
|
|
++m_nCurPos; |
47 |
|
|
|
48 |
|
|
cs = m_cs.Mid(nStartPos, m_nCurPos - nStartPos); |
49 |
|
|
|
50 |
|
|
return true; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
CStdString CTokenizer::Tail() const |
54 |
|
|
{ |
55 |
|
|
int nCurPos = m_nCurPos; |
56 |
|
|
|
57 |
|
|
while(nCurPos < m_cs.GetLength() && m_delim[static_cast<BYTE>(m_cs[nCurPos])]) |
58 |
|
|
++nCurPos; |
59 |
|
|
|
60 |
|
|
CStdString csResult; |
61 |
|
|
|
62 |
|
|
if(nCurPos < m_cs.GetLength()) |
63 |
|
|
csResult = m_cs.Mid(nCurPos); |
64 |
|
|
|
65 |
|
|
return csResult; |
66 |
|
|
} |