]> git.saurik.com Git - wxWidgets.git/blob - src/common/tokenzr.cpp
added compression ratio argument to wxZlibOutputStream ctor
[wxWidgets.git] / src / common / tokenzr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tokenzr.cpp
3 // Purpose: String tokenizer
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin
6 // Created: 04/22/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "tokenzr.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #include "wx/tokenzr.h"
32
33 // ============================================================================
34 // implementation
35 // ============================================================================
36
37 // ----------------------------------------------------------------------------
38 // wxStringTokenizer construction
39 // ----------------------------------------------------------------------------
40
41 wxStringTokenizer::wxStringTokenizer(const wxString& str,
42 const wxString& delims,
43 wxStringTokenizerMode mode)
44 {
45 SetString(str, delims, mode);
46 }
47
48 void wxStringTokenizer::SetString(const wxString& str,
49 const wxString& delims,
50 wxStringTokenizerMode mode)
51 {
52 if ( mode == wxTOKEN_DEFAULT )
53 {
54 // by default, we behave like strtok() if the delimiters are only
55 // whitespace characters and as wxTOKEN_RET_EMPTY otherwise (for
56 // whitespace delimiters, strtok() behaviour is better because we want
57 // to count consecutive spaces as one delimiter)
58 const wxChar *p;
59 for ( p = delims.c_str(); *p; p++ )
60 {
61 if ( !wxIsspace(*p) )
62 break;
63 }
64
65 if ( *p )
66 {
67 // not whitespace char in delims
68 mode = wxTOKEN_RET_EMPTY;
69 }
70 else
71 {
72 // only whitespaces
73 mode = wxTOKEN_STRTOK;
74 }
75 }
76
77 m_delims = delims;
78 m_mode = mode;
79
80 Reinit(str);
81 }
82
83 void wxStringTokenizer::Reinit(const wxString& str)
84 {
85 wxASSERT_MSG( IsOk(), _T("you should call SetString() first") );
86
87 m_string = str;
88 m_pos = 0;
89
90 // empty string doesn't have any tokens
91 m_hasMore = !m_string.empty();
92 }
93
94 // ----------------------------------------------------------------------------
95 // access to the tokens
96 // ----------------------------------------------------------------------------
97
98 // do we have more of them?
99 bool wxStringTokenizer::HasMoreTokens() const
100 {
101 wxCHECK_MSG( IsOk(), FALSE, _T("you should call SetString() first") );
102
103 if ( m_string.find_first_not_of(m_delims) == wxString::npos )
104 {
105 // no non empty tokens left, but in wxTOKEN_RET_EMPTY_ALL mode we
106 // still may return TRUE if GetNextToken() wasn't called yet for the
107 // last trailing empty token
108 return m_mode == wxTOKEN_RET_EMPTY_ALL ? m_hasMore : FALSE;
109 }
110 else
111 {
112 // there are non delimiter characters left, hence we do have more
113 // tokens
114 return TRUE;
115 }
116 }
117
118 // count the number of tokens in the string
119 size_t wxStringTokenizer::CountTokens() const
120 {
121 wxCHECK_MSG( IsOk(), 0, _T("you should call SetString() first") );
122
123 // VZ: this function is IMHO not very useful, so it's probably not very
124 // important if it's implementation here is not as efficient as it
125 // could be - but OTOH like this we're sure to get the correct answer
126 // in all modes
127 wxStringTokenizer *self = (wxStringTokenizer *)this; // const_cast
128 wxString stringInitial = m_string;
129
130 size_t count = 0;
131 while ( self->HasMoreTokens() )
132 {
133 count++;
134
135 (void)self->GetNextToken();
136 }
137
138 self->Reinit(stringInitial);
139
140 return count;
141 }
142
143 // ----------------------------------------------------------------------------
144 // token extraction
145 // ----------------------------------------------------------------------------
146
147 wxString wxStringTokenizer::GetNextToken()
148 {
149 // strtok() doesn't return empty tokens, all other modes do
150 bool allowEmpty = m_mode != wxTOKEN_STRTOK;
151
152 wxString token;
153 do
154 {
155 if ( !HasMoreTokens() )
156 {
157 break;
158 }
159 // find the end of this token
160 size_t pos = m_string.find_first_of(m_delims);
161
162 // and the start of the next one
163 if ( pos == wxString::npos )
164 {
165 // no more delimiters, the token is everything till the end of
166 // string
167 token = m_string;
168
169 m_pos += m_string.length();
170 m_string.clear();
171
172 // no more tokens in this string, even in wxTOKEN_RET_EMPTY_ALL
173 // mode (we will return the trailing one right now in this case)
174 m_hasMore = FALSE;
175 }
176 else
177 {
178 size_t pos2 = pos + 1;
179
180 // in wxTOKEN_RET_DELIMS mode we return the delimiter character
181 // with token
182 token = wxString(m_string, m_mode == wxTOKEN_RET_DELIMS ? pos2
183 : pos);
184
185 // remove token with the following it delimiter from string
186 m_string.erase(0, pos2);
187
188 // keep track of the position in the original string too
189 m_pos += pos2;
190 }
191 }
192 while ( !allowEmpty && token.empty() );
193
194 return token;
195 }