]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/tokenzr.cpp
chmod is not available in VisualAge
[wxWidgets.git] / src / common / tokenzr.cpp
... / ...
CommitLineData
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// Required for wxIs... functions
34#include <ctype.h>
35
36// ============================================================================
37// implementation
38// ============================================================================
39
40// ----------------------------------------------------------------------------
41// wxStringTokenizer construction
42// ----------------------------------------------------------------------------
43
44wxStringTokenizer::wxStringTokenizer(const wxString& str,
45 const wxString& delims,
46 wxStringTokenizerMode mode)
47{
48 SetString(str, delims, mode);
49}
50
51void wxStringTokenizer::SetString(const wxString& str,
52 const wxString& delims,
53 wxStringTokenizerMode mode)
54{
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
80 m_delims = delims;
81 m_mode = mode;
82
83 Reinit(str);
84}
85
86void wxStringTokenizer::Reinit(const wxString& str)
87{
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();
95}
96
97// ----------------------------------------------------------------------------
98// access to the tokens
99// ----------------------------------------------------------------------------
100
101// do we have more of them?
102bool wxStringTokenizer::HasMoreTokens() const
103{
104 wxCHECK_MSG( IsOk(), FALSE, _T("you should call SetString() first") );
105
106 if ( m_string.find_first_not_of(m_delims) == wxString::npos )
107 {
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}
120
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") );
125
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;
132
133 size_t count = 0;
134 while ( self->HasMoreTokens() )
135 {
136 count++;
137
138 (void)self->GetNextToken();
139 }
140
141 self->Reinit(stringInitial);
142
143 return count;
144}
145
146// ----------------------------------------------------------------------------
147// token extraction
148// ----------------------------------------------------------------------------
149
150wxString wxStringTokenizer::GetNextToken()
151{
152 // strtok() doesn't return empty tokens, all other modes do
153 bool allowEmpty = m_mode != wxTOKEN_STRTOK;
154
155 wxString token;
156 do
157 {
158 if ( !HasMoreTokens() )
159 {
160 break;
161 }
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 )
167 {
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();
174
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)
177 m_hasMore = FALSE;
178 }
179 else
180 {
181 size_t pos2 = pos + 1;
182
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);
187
188 // remove token with the following it delimiter from string
189 m_string.erase(0, pos2);
190
191 // keep track of the position in the original string too
192 m_pos += pos2;
193 }
194 }
195 while ( !allowEmpty && token.empty() );
196
197 return token;
198}