]> git.saurik.com Git - wxWidgets.git/blob - src/common/tokenzr.cpp
BC++ fixes.
[wxWidgets.git] / src / common / tokenzr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tokenzr.cpp
3 // Purpose: String tokenizer
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 04/22/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "tokenzr.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/tokenzr.h"
24
25 wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize,
26 const wxString& delims,
27 bool ret_delims)
28 {
29 m_string = to_tokenize;
30 m_delims = delims;
31 m_retdelims = ret_delims;
32 }
33
34 wxStringTokenizer::~wxStringTokenizer()
35 {
36 }
37
38 off_t wxStringTokenizer::FindDelims(const wxString& str, const wxString& delims) const
39 {
40 for ( size_t i = 0; i < str.Length(); i++ )
41 {
42 wxChar c = str[i];
43
44 for ( size_t j = 0; j < delims.Length() ; j++ )
45 {
46 if ( delims[j] == c )
47 return i;
48 }
49 }
50
51 return -1;
52 }
53
54 int wxStringTokenizer::CountTokens() const
55 {
56 wxString p_string = m_string;
57 bool found = TRUE;
58 int pos, count = 1;
59
60 if (p_string.Length() == 0)
61 return 0;
62
63 while (found)
64 {
65 pos = FindDelims(p_string, m_delims);
66 if (pos != -1)
67 {
68 count++;
69 p_string = p_string(pos+1, p_string.Length());
70 }
71 else
72 {
73 found = FALSE;
74 }
75 }
76
77 return count;
78 }
79
80 bool wxStringTokenizer::HasMoreTokens()
81 {
82 return !m_string.IsEmpty();
83 }
84
85 // needed to fix leading whitespace / mult. delims bugs
86 void wxStringTokenizer::EatLeadingDelims()
87 {
88 int pos;
89
90 // while leading delims trim 'em from the left
91 while ( ( pos = FindDelims(m_string, m_delims)) == 0 )
92 {
93 m_string = m_string.Mid((size_t)1);
94 }
95 }
96
97 wxString wxStringTokenizer::NextToken()
98 {
99 off_t pos, pos2;
100 wxString r_string;
101
102 if ( m_string.IsEmpty() )
103 return m_string;
104
105 if ( !m_retdelims )
106 EatLeadingDelims();
107
108 pos = FindDelims(m_string, m_delims);
109 if (pos == -1)
110 {
111 r_string = m_string;
112 m_string = wxEmptyString;
113
114 return r_string;
115 }
116
117 if (m_retdelims)
118 {
119 if (!pos)
120 {
121 pos++;
122 pos2 = 1;
123 }
124 else
125 {
126 pos2 = pos;
127 }
128 }
129 else
130 {
131 pos2 = pos + 1;
132 }
133
134 r_string = m_string.Left((size_t)pos);
135 m_string = m_string.Mid((size_t)pos2);
136
137 return r_string;
138 }