Debian Codegraph @

CodeLite/tokenizer.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2008 by Eran Ifrah
5 // file name : tokenizer.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25 // StringTokenizer.cpp: implementation of the StringTokenizer class.
26 //
27 //////////////////////////////////////////////////////////////////////
28 #include "precompiled_header.h"
29 #include "tokenizer.h"
30
31 #ifdef __VISUALC__
32 #ifdef _DEBUG
33 #define new DEBUG_NEW
34 #endif
35 #endif
36
37
38 //////////////////////////////////////////////////////////////////////
39 // Construction/Destruction
40 //////////////////////////////////////////////////////////////////////
41
42 StringTokenizer::StringTokenizer(const wxString& str,
43 const wxString& strDelimiter,
44 const bool &bAllowEmptyTokens /* false */)
45 {
46 Initialize();
47
48 int nEnd = (int)str.find(strDelimiter, 0);
49 int nStart = 0;
50 wxString token;
51 while( nEnd != -1 )
52 {
53 if( nEnd != nStart)
54 token = str.substr(nStart, nEnd-nStart);
55 else
56 token.Empty();
57
58 if(!token.empty())
59 m_tokensArr.push_back(token);
60 else if( bAllowEmptyTokens )
61 m_tokensArr.push_back(token);
62
63 // next token
64 nStart = nEnd + (int)strDelimiter.length();
65 nEnd = (int)str.find(strDelimiter, nStart );
66 }
67 if( nStart != (int)str.length() )
68 {
69 //We have another token which is not delimited
70 wxString token = str.substr(nStart);
71 m_tokensArr.push_back(token);
72 }
73 }
74
75 StringTokenizer::StringTokenizer(const wxString& str, const wxArrayString& delimiterArr, const bool &allowEmptyTokens)
76 {
77 Initialize();
78 wxString tmpStr( str );
79
80 // Replace all delimiters to the first one
81 if(delimiterArr.GetCount() >= 2)
82 {
83 size_t i=1;
84 for(; i<delimiterArr.GetCount(); i++)
85 tmpStr.Replace(delimiterArr[i], delimiterArr[0]);
86 }
87 *this = StringTokenizer(tmpStr, delimiterArr[0], allowEmptyTokens);
88 }
89
90 // Default
91 StringTokenizer::StringTokenizer()
92 {
93 Initialize();
94 }
95
96 // Copy constructor
97 StringTokenizer::StringTokenizer(const StringTokenizer &src)
98 {
99 *this = src;
100 }
101
102 StringTokenizer& StringTokenizer::operator =(const StringTokenizer &src)
103 {
104 if( &src == this)
105 return *this;
106 Initialize();
107
108 // Copy the tokens
109 m_tokensArr.clear();
110 for( int i=0; i<(int)src.m_tokensArr.size() ; i++)
111 m_tokensArr.push_back(src.m_tokensArr[i]);
112 m_nCurr = src.m_nCurr;
113 return *this;
114 }
115
116 StringTokenizer::~StringTokenizer()
117 {
118 m_tokensArr.clear();
119 m_nCurr = 0;
120 }
121
122 // Return current token and advance to the next. If there are no more tokens, return empty string.
123 wxString StringTokenizer::Next()
124 {
125 if( m_nCurr == (int)m_tokensArr.size() )
126 {
127 // We are at the end of the tokens array
128 return wxEmptyString;
129 }
130 wxString strToken = m_tokensArr[m_nCurr];
131 m_nCurr++;
132 return strToken;
133 }
134
135 // We return the previous token, if we are already at the start, return empty string.
136 wxString StringTokenizer::Previous()
137 {
138 if(m_nCurr == 0)
139 {
140 return wxEmptyString;
141 }
142 if(m_tokensArr.size() == 0)
143 {
144 return wxEmptyString;
145 }
146 m_nCurr--;
147 return m_tokensArr[m_nCurr];
148 }
149
150 bool StringTokenizer::HasMore()
151 {
152 if(m_nCurr < (int)m_tokensArr.size())
153 {
154 return true;
155 }
156 return false;
157 }
158
159 wxString StringTokenizer::First()
160 {
161 if(m_tokensArr.size()>0)
162 {
163 m_nCurr = 1; //The next one
164 return m_tokensArr[0];
165 }
166 else
167 {
168 return wxEmptyString;
169 }
170 }
171
172 // This function is much similar to the 'next()' function with one difference,
173 // it does not advance the pointer to the next token.
174 wxString StringTokenizer::Current()
175 {
176 if( m_nCurr == (int)m_tokensArr.size() )
177 {
178 // We are at the end of the tokens array
179 return wxEmptyString;
180 }
181 return m_tokensArr[m_nCurr];
182 }
183
184 const int StringTokenizer::Count() const
185 {
186 return (int)m_tokensArr.size();
187 }
188
189 void StringTokenizer::Initialize()
190 {
191 m_tokensArr.clear();
192 m_nCurr = 0;
193 }
194
195 // Return last token
196 wxString StringTokenizer::Last()
197 {
198 if(m_tokensArr.size() == 0)
199 return wxEmptyString;
200 m_nCurr = (int)m_tokensArr.size()-1;
201 return m_tokensArr[m_tokensArr.size()-1];
202 }
203
204 wxString StringTokenizer::operator[](const int nIndex)
205 {
206 if(m_tokensArr.size() == 0)
207 return wxEmptyString;
208 if( nIndex>=(int)m_tokensArr.size() || nIndex<0)
209 return wxEmptyString;
210 return m_tokensArr[nIndex];
211 }
212
213