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