| 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 | wxString wxStringTokenizer::NextToken() |
| 86 | { |
| 87 | register off_t pos, pos2; |
| 88 | wxString r_string; |
| 89 | |
| 90 | if (m_string.IsNull()) |
| 91 | return m_string; |
| 92 | |
| 93 | pos = FindDelims(m_string, m_delims); |
| 94 | if (pos == -1) { |
| 95 | r_string = m_string; |
| 96 | m_string = wxEmptyString; |
| 97 | |
| 98 | return r_string; |
| 99 | } |
| 100 | |
| 101 | if (m_retdelims) { |
| 102 | if (!pos) { |
| 103 | pos++; |
| 104 | pos2 = 1; |
| 105 | } else |
| 106 | pos2 = pos; |
| 107 | } else |
| 108 | pos2 = pos + 1; |
| 109 | |
| 110 | r_string = m_string.Left((size_t)pos); |
| 111 | m_string = m_string.Mid((size_t)pos2); |
| 112 | |
| 113 | return r_string; |
| 114 | } |