]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: tokenzr.cpp | |
3 | // Purpose: String tokenizer | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 04/22/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
85833f5c | 13 | #pragma implementation "tokenzr.h" |
f4ada568 GL |
14 | #endif |
15 | ||
fcc6dddd JS |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
85833f5c | 20 | #pragma hdrstop |
fcc6dddd JS |
21 | #endif |
22 | ||
f4ada568 GL |
23 | #include "wx/tokenzr.h" |
24 | ||
25 | wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize, | |
26 | const wxString& delims, | |
85833f5c | 27 | bool ret_delims) |
f4ada568 | 28 | { |
85833f5c VZ |
29 | m_string = to_tokenize; |
30 | m_delims = delims; | |
31 | m_retdelims = ret_delims; | |
f4ada568 GL |
32 | } |
33 | ||
34 | wxStringTokenizer::~wxStringTokenizer() | |
35 | { | |
36 | } | |
37 | ||
e0272d05 | 38 | off_t wxStringTokenizer::FindDelims(const wxString& str, const wxString& delims) const |
f4ada568 | 39 | { |
aef82f12 | 40 | for ( size_t i = 0; i < str.Length(); i++ ) |
85833f5c | 41 | { |
c17bcb84 | 42 | wxChar c = str[i]; |
85833f5c | 43 | |
aef82f12 | 44 | for ( size_t j = 0; j < delims.Length() ; j++ ) |
85833f5c VZ |
45 | { |
46 | if ( delims[j] == c ) | |
47 | return i; | |
48 | } | |
49 | } | |
50 | ||
51 | return -1; | |
f4ada568 GL |
52 | } |
53 | ||
85833f5c | 54 | int wxStringTokenizer::CountTokens() const |
f4ada568 | 55 | { |
85833f5c VZ |
56 | wxString p_string = m_string; |
57 | bool found = TRUE; | |
e0272d05 | 58 | int pos, count = 1; |
85833f5c VZ |
59 | |
60 | if (p_string.Length() == 0) | |
61 | return 0; | |
62 | ||
63 | while (found) | |
64 | { | |
65 | pos = FindDelims(p_string, m_delims); | |
66 | if (pos != -1) | |
67 | { | |
68 | count++; | |
44d6d191 | 69 | p_string = p_string(pos+1, p_string.Length()); |
85833f5c VZ |
70 | } |
71 | else | |
72 | { | |
73 | found = FALSE; | |
74 | } | |
75 | } | |
76 | ||
77 | return count; | |
f4ada568 GL |
78 | } |
79 | ||
e0272d05 | 80 | bool wxStringTokenizer::HasMoreTokens() |
f4ada568 | 81 | { |
85833f5c | 82 | return !m_string.IsEmpty(); |
f4ada568 GL |
83 | } |
84 | ||
85833f5c | 85 | // needed to fix leading whitespace / mult. delims bugs |
e0272d05 | 86 | void wxStringTokenizer::EatLeadingDelims() |
dab58492 | 87 | { |
85833f5c | 88 | int pos; |
dab58492 | 89 | |
85833f5c VZ |
90 | // while leading delims trim 'em from the left |
91 | while ( ( pos = FindDelims(m_string, m_delims)) == 0 ) | |
92 | { | |
93 | m_string = m_string.Mid((size_t)1); | |
94 | } | |
dab58492 GL |
95 | } |
96 | ||
f4ada568 GL |
97 | wxString wxStringTokenizer::NextToken() |
98 | { | |
85833f5c VZ |
99 | off_t pos, pos2; |
100 | wxString r_string; | |
101 | ||
102 | if ( m_string.IsEmpty() ) | |
103 | return m_string; | |
104 | ||
105 | if ( !m_retdelims ) | |
106 | EatLeadingDelims(); | |
107 | ||
108 | pos = FindDelims(m_string, m_delims); | |
109 | if (pos == -1) | |
110 | { | |
111 | r_string = m_string; | |
112 | m_string = wxEmptyString; | |
113 | ||
114 | return r_string; | |
115 | } | |
116 | ||
117 | if (m_retdelims) | |
118 | { | |
119 | if (!pos) | |
120 | { | |
121 | pos++; | |
122 | pos2 = 1; | |
123 | } | |
124 | else | |
125 | { | |
126 | pos2 = pos; | |
127 | } | |
128 | } | |
129 | else | |
130 | { | |
131 | pos2 = pos + 1; | |
132 | } | |
133 | ||
134 | r_string = m_string.Left((size_t)pos); | |
135 | m_string = m_string.Mid((size_t)pos2); | |
dab58492 | 136 | |
f4ada568 | 137 | return r_string; |
f4ada568 | 138 | } |