]> git.saurik.com Git - wxWidgets.git/blame - src/common/tokenzr.cpp
chmod is not available in VisualAge
[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
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 {
7c968cee
VZ
108 // no non empty tokens left, but in wxTOKEN_RET_EMPTY_ALL mode we
109 // still may return TRUE if GetNextToken() wasn't called yet for the
110 // last trailing empty token
111 return m_mode == wxTOKEN_RET_EMPTY_ALL ? m_hasMore : FALSE;
112 }
113 else
114 {
115 // there are non delimiter characters left, hence we do have more
116 // tokens
117 return TRUE;
118 }
119}
bbf8fc53 120
7c968cee
VZ
121// count the number of tokens in the string
122size_t wxStringTokenizer::CountTokens() const
123{
124 wxCHECK_MSG( IsOk(), 0, _T("you should call SetString() first") );
bbf8fc53 125
7c968cee
VZ
126 // VZ: this function is IMHO not very useful, so it's probably not very
127 // important if it's implementation here is not as efficient as it
128 // could be - but OTOH like this we're sure to get the correct answer
129 // in all modes
130 wxStringTokenizer *self = (wxStringTokenizer *)this; // const_cast
131 wxString stringInitial = m_string;
bbf8fc53 132
7c968cee
VZ
133 size_t count = 0;
134 while ( self->HasMoreTokens() )
bbf8fc53
VZ
135 {
136 count++;
7c968cee
VZ
137
138 (void)self->GetNextToken();
bbf8fc53
VZ
139 }
140
7c968cee
VZ
141 self->Reinit(stringInitial);
142
bbf8fc53
VZ
143 return count;
144}
145
146// ----------------------------------------------------------------------------
147// token extraction
148// ----------------------------------------------------------------------------
149
150wxString wxStringTokenizer::GetNextToken()
151{
7c968cee
VZ
152 // strtok() doesn't return empty tokens, all other modes do
153 bool allowEmpty = m_mode != wxTOKEN_STRTOK;
154
bbf8fc53 155 wxString token;
7c968cee 156 do
bbf8fc53 157 {
7c968cee 158 if ( !HasMoreTokens() )
85833f5c 159 {
7c968cee 160 break;
85833f5c 161 }
7c968cee
VZ
162 // find the end of this token
163 size_t pos = m_string.find_first_of(m_delims);
164
165 // and the start of the next one
166 if ( pos == wxString::npos )
85833f5c 167 {
7c968cee
VZ
168 // no more delimiters, the token is everything till the end of
169 // string
170 token = m_string;
171
172 m_pos += m_string.length();
173 m_string.clear();
bbf8fc53 174
7c968cee
VZ
175 // no more tokens in this string, even in wxTOKEN_RET_EMPTY_ALL
176 // mode (we will return the trailing one right now in this case)
bbf8fc53 177 m_hasMore = FALSE;
85833f5c 178 }
7c968cee
VZ
179 else
180 {
181 size_t pos2 = pos + 1;
f4ada568 182
7c968cee
VZ
183 // in wxTOKEN_RET_DELIMS mode we return the delimiter character
184 // with token
185 token = wxString(m_string, m_mode == wxTOKEN_RET_DELIMS ? pos2
186 : pos);
dab58492 187
7c968cee
VZ
188 // remove token with the following it delimiter from string
189 m_string.erase(0, pos2);
bbf8fc53 190
7c968cee
VZ
191 // keep track of the position in the original string too
192 m_pos += pos2;
193 }
85833f5c 194 }
7c968cee 195 while ( !allowEmpty && token.empty() );
bbf8fc53
VZ
196
197 return token;
f4ada568 198}