]> git.saurik.com Git - wxWidgets.git/blame - src/common/tokenzr.cpp
fixed memory leaks
[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
f4ada568
GL
41wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize,
42 const wxString& delims,
85833f5c 43 bool ret_delims)
bbf8fc53
VZ
44{
45 SetString(to_tokenize, delims, ret_delims);
46}
47
48void wxStringTokenizer::SetString(const wxString& to_tokenize,
49 const wxString& delims,
50 bool ret_delim)
f4ada568 51{
85833f5c
VZ
52 m_string = to_tokenize;
53 m_delims = delims;
bbf8fc53 54 m_retdelims = ret_delim;
53faea43 55 m_pos = 0;
bbf8fc53
VZ
56
57 // empty string doesn't have any tokens
58 m_hasMore = !m_string.empty();
f4ada568
GL
59}
60
61wxStringTokenizer::~wxStringTokenizer()
62{
63}
64
bbf8fc53
VZ
65// ----------------------------------------------------------------------------
66// count the number of tokens in the string
67// ----------------------------------------------------------------------------
68
69size_t wxStringTokenizer::CountTokens() const
f4ada568 70{
53faea43 71 size_t pos = 0;
bbf8fc53
VZ
72 size_t count = 0;
73 for ( ;; )
74 {
75 pos = m_string.find_first_of(m_delims, pos);
76 if ( pos == wxString::npos )
77 break;
78
79 count++; // one more token found
80
81 pos++; // skip delimiter
82 }
83
84 // normally, we didn't count the last token in the loop above - so add it
85 // unless the string was empty from the very beginning, in which case it
86 // still has 0 (and not 1) tokens
87 if ( !m_string.empty() )
88 {
89 count++;
90 }
91
92 return count;
93}
94
95// ----------------------------------------------------------------------------
96// token extraction
97// ----------------------------------------------------------------------------
98
99wxString wxStringTokenizer::GetNextToken()
100{
101 wxString token;
102 if ( HasMoreTokens() )
103 {
104 size_t pos = m_string.find_first_of(m_delims); // end of token
105 size_t pos2; // start of the next one
106 if ( pos != wxString::npos )
85833f5c 107 {
bbf8fc53
VZ
108 // return the delimiter too
109 pos2 = pos + 1;
85833f5c
VZ
110 }
111 else
112 {
bbf8fc53
VZ
113 pos2 = m_string.length();
114
115 // no more tokens in this string
116 m_hasMore = FALSE;
85833f5c 117 }
f4ada568 118
bbf8fc53 119 token = wxString(m_string, m_retdelims ? pos2 : pos);
dab58492 120
bbf8fc53
VZ
121 // remove token with the following it delimiter from string
122 m_string.erase(0, pos2);
123
124 // keep track of the position in the original string too
125 m_pos += pos2;
85833f5c 126 }
bbf8fc53
VZ
127 //else: no more tokens, return empty token
128
129 return token;
f4ada568 130}