]> git.saurik.com Git - wxWidgets.git/blame - src/common/tokenzr.cpp
added --enable-filesystem
[wxWidgets.git] / src / common / tokenzr.cpp
CommitLineData
f4ada568
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: tokenzr.cpp
3// Purpose: String tokenizer
4// Author: Guilhem Lavaux
bbf8fc53 5// Modified by: Vadim Zeitlin
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
bbf8fc53
VZ
33// ============================================================================
34// implementation
35// ============================================================================
36
37// ----------------------------------------------------------------------------
38// wxStringTokenizer construction
39// ----------------------------------------------------------------------------
40
7c968cee 41wxStringTokenizer::wxStringTokenizer(const wxString& str,
f4ada568 42 const wxString& delims,
7c968cee 43 wxStringTokenizerMode mode)
bbf8fc53 44{
7c968cee 45 SetString(str, delims, mode);
bbf8fc53
VZ
46}
47
7c968cee 48void wxStringTokenizer::SetString(const wxString& str,
bbf8fc53 49 const wxString& delims,
7c968cee 50 wxStringTokenizerMode mode)
f4ada568 51{
7c968cee
VZ
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
85833f5c 77 m_delims = delims;
7c968cee 78 m_mode = mode;
bbf8fc53 79
7c968cee 80 Reinit(str);
f4ada568
GL
81}
82
7c968cee 83void wxStringTokenizer::Reinit(const wxString& str)
f4ada568 84{
7c968cee
VZ
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();
f4ada568
GL
92}
93
bbf8fc53 94// ----------------------------------------------------------------------------
7c968cee 95// access to the tokens
bbf8fc53
VZ
96// ----------------------------------------------------------------------------
97
7c968cee
VZ
98// do we have more of them?
99bool wxStringTokenizer::HasMoreTokens() const
f4ada568 100{
7c968cee
VZ
101 wxCHECK_MSG( IsOk(), FALSE, _T("you should call SetString() first") );
102
103 if ( m_string.find_first_not_of(m_delims) == wxString::npos )
bbf8fc53 104 {
7c968cee
VZ
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}
bbf8fc53 117
7c968cee
VZ
118// count the number of tokens in the string
119size_t wxStringTokenizer::CountTokens() const
120{
121 wxCHECK_MSG( IsOk(), 0, _T("you should call SetString() first") );
bbf8fc53 122
7c968cee
VZ
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;
bbf8fc53 129
7c968cee
VZ
130 size_t count = 0;
131 while ( self->HasMoreTokens() )
bbf8fc53
VZ
132 {
133 count++;
7c968cee
VZ
134
135 (void)self->GetNextToken();
bbf8fc53
VZ
136 }
137
7c968cee
VZ
138 self->Reinit(stringInitial);
139
bbf8fc53
VZ
140 return count;
141}
142
143// ----------------------------------------------------------------------------
144// token extraction
145// ----------------------------------------------------------------------------
146
147wxString wxStringTokenizer::GetNextToken()
148{
7c968cee
VZ
149 // strtok() doesn't return empty tokens, all other modes do
150 bool allowEmpty = m_mode != wxTOKEN_STRTOK;
151
bbf8fc53 152 wxString token;
7c968cee 153 do
bbf8fc53 154 {
7c968cee 155 if ( !HasMoreTokens() )
85833f5c 156 {
7c968cee 157 break;
85833f5c 158 }
7c968cee
VZ
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 )
85833f5c 164 {
7c968cee
VZ
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();
bbf8fc53 171
7c968cee
VZ
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)
bbf8fc53 174 m_hasMore = FALSE;
85833f5c 175 }
7c968cee
VZ
176 else
177 {
178 size_t pos2 = pos + 1;
f4ada568 179
7c968cee
VZ
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);
dab58492 184
7c968cee
VZ
185 // remove token with the following it delimiter from string
186 m_string.erase(0, pos2);
bbf8fc53 187
7c968cee
VZ
188 // keep track of the position in the original string too
189 m_pos += pos2;
190 }
85833f5c 191 }
7c968cee 192 while ( !allowEmpty && token.empty() );
bbf8fc53
VZ
193
194 return token;
f4ada568 195}