]> git.saurik.com Git - wxWidgets.git/blob - src/common/tokenzr.cpp
eb992377113bf19e711d9f2a2b8b9eb9af21398e
[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 #include "wx/object.h"
17 #include "wx/string.h"
18 #include "wx/tokenzr.h"
19
20 wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize,
21 const wxString& delims,
22 bool ret_delims)
23 : wxObject()
24 {
25 m_string = to_tokenize;
26 m_delims = delims;
27 m_retdelims = ret_delims;
28 }
29
30 wxStringTokenizer::~wxStringTokenizer()
31 {
32 }
33
34 off_t wxStringTokenizer::FindDelims(const wxString& str, const wxString& delims)
35 {
36 int i, j;
37 register int s_len = str.Length(),
38 len = delims.Length();
39
40 for (i=0;i<s_len;i++) {
41 register char c = str[i];
42
43 for (j=0;j<len;j++)
44 if (delims[j] == c)
45 return i;
46 }
47 return -1;
48 }
49
50 int wxStringTokenizer::CountTokens()
51 {
52 wxString p_string = m_string;
53 bool found = TRUE;
54 int pos, count = 1;
55
56 if (p_string.Length() == 0)
57 return 0;
58
59 while (found) {
60 pos = FindDelims(p_string, m_delims);
61 if (pos != -1) {
62 count++;
63 p_string = p_string(0, pos);
64 } else
65 found = FALSE;
66 }
67 return count;
68 }
69
70 bool wxStringTokenizer::HasMoreToken()
71 {
72 return (m_string.Length() != 0);
73 }
74
75 wxString wxStringTokenizer::NextToken()
76 {
77 register off_t pos, pos2;
78 wxString r_string;
79
80 if (m_string.IsNull())
81 return m_string;
82
83 pos = FindDelims(m_string, m_delims);
84 if (pos == -1) {
85 r_string = m_string;
86 m_string = (char *)NULL;
87
88 return r_string;
89 }
90
91 if (m_retdelims) {
92 if (!pos) {
93 pos++;
94 pos2 = 1;
95 } else
96 pos2 = pos;
97 } else
98 pos2 = pos + 1;
99
100 r_string = m_string.Left((size_t)pos);
101 m_string = m_string.Mid((size_t)pos2);
102
103 return r_string;
104 }