]> git.saurik.com Git - wxWidgets.git/blob - src/common/tokenzr.cpp
19ae5e0f29a44ab49cfe32d98f3adb5deef8048b
[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 #ifndef WX_PRECOMP
24 #endif
25
26 #include "wx/object.h"
27 #include "wx/string.h"
28 #include "wx/tokenzr.h"
29
30 wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize,
31 const wxString& delims,
32 bool ret_delims)
33 : wxObject()
34 {
35 m_string = to_tokenize;
36 m_delims = delims;
37 m_retdelims = ret_delims;
38 }
39
40 wxStringTokenizer::~wxStringTokenizer()
41 {
42 }
43
44 off_t wxStringTokenizer::FindDelims(const wxString& str, const wxString& delims)
45 {
46 int i, j;
47 register int s_len = str.Length(),
48 len = delims.Length();
49
50 for (i=0;i<s_len;i++) {
51 register char c = str[i];
52
53 for (j=0;j<len;j++)
54 if (delims[j] == c)
55 return i;
56 }
57 return -1;
58 }
59
60 int wxStringTokenizer::CountTokens()
61 {
62 wxString p_string = m_string;
63 bool found = TRUE;
64 int pos, count = 1;
65
66 if (p_string.Length() == 0)
67 return 0;
68
69 while (found) {
70 pos = FindDelims(p_string, m_delims);
71 if (pos != -1) {
72 count++;
73 p_string = p_string(0, pos);
74 } else
75 found = FALSE;
76 }
77 return count;
78 }
79
80 bool wxStringTokenizer::HasMoreToken()
81 {
82 return (m_string.Length() != 0);
83 }
84
85 // AVS - added to fix leading whitespace / mult. delims bugs
86 void wxStringTokenizer::EatLeadingDelims()
87 {
88 int pos;
89
90 while ((pos=FindDelims(m_string, m_delims))==0) { // while leading delims
91 m_string = m_string.Mid((size_t)1); // trim 'em from the left
92 }
93 }
94
95 wxString wxStringTokenizer::NextToken()
96 {
97 register off_t pos, pos2;
98 wxString r_string;
99
100 if (m_string.IsNull())
101 return m_string;
102
103 if (!m_retdelims)
104 EatLeadingDelims(); // AVS - added to fix leading whitespace /
105 // mult. delims bugs
106
107 pos = FindDelims(m_string, m_delims);
108 if (pos == -1) {
109 r_string = m_string;
110 m_string = wxEmptyString;
111
112 return r_string;
113 }
114
115 if (m_retdelims) {
116 if (!pos) {
117 pos++;
118 pos2 = 1;
119 } else
120 pos2 = pos;
121 } else
122 pos2 = pos + 1;
123
124 r_string = m_string.Left((size_t)pos);
125 m_string = m_string.Mid((size_t)pos2);
126
127 return r_string;
128 }