]> git.saurik.com Git - wxWidgets.git/blame - include/wx/tokenzr.h
moved XML classes to the core
[wxWidgets.git] / include / wx / tokenzr.h
CommitLineData
f4ada568 1/////////////////////////////////////////////////////////////////////////////
7c968cee
VZ
2// Name: wx/tokenzr.h
3// Purpose: String tokenizer - a C++ replacement for strtok(3)
f4ada568 4// Author: Guilhem Lavaux
1e6feb95 5// Modified by: (or rather rewritten by) Vadim Zeitlin
f4ada568
GL
6// Created: 04/22/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_TOKENZRH
13#define _WX_TOKENZRH
14
af49c4b8 15#if defined(__GNUG__) && !defined(__APPLE__)
2224580a 16 #pragma interface "tokenzr.h"
f4ada568
GL
17#endif
18
19#include "wx/object.h"
20#include "wx/string.h"
df5168c4 21#include "wx/arrstr.h"
bbf8fc53 22
7c968cee
VZ
23// ----------------------------------------------------------------------------
24// constants
25// ----------------------------------------------------------------------------
26
bbf8fc53
VZ
27// default: delimiters are usual white space characters
28#define wxDEFAULT_DELIMITERS (_T(" \t\r\n"))
f4ada568 29
7c968cee
VZ
30// wxStringTokenizer mode flags which determine its behaviour
31enum wxStringTokenizerMode
32{
33 wxTOKEN_INVALID = -1, // set by def ctor until SetString() is called
34 wxTOKEN_DEFAULT, // strtok() for whitespace delims, RET_EMPTY else
35 wxTOKEN_RET_EMPTY, // return empty token in the middle of the string
36 wxTOKEN_RET_EMPTY_ALL, // return trailing empty tokens too
37 wxTOKEN_RET_DELIMS, // return the delim with token (implies RET_EMPTY)
38 wxTOKEN_STRTOK // behave exactly like strtok(3)
39};
40
41// ----------------------------------------------------------------------------
42// wxStringTokenizer: replaces infamous strtok() and has some other features
43// ----------------------------------------------------------------------------
44
bddd7a8d 45class WXDLLIMPEXP_BASE wxStringTokenizer : public wxObject
85833f5c 46{
f4ada568 47public:
7c968cee
VZ
48 // ctors and initializers
49 // default ctor, call SetString() later
50 wxStringTokenizer() { m_mode = wxTOKEN_INVALID; }
51 // ctor which gives us the string
52 wxStringTokenizer(const wxString& str,
bbf8fc53 53 const wxString& delims = wxDEFAULT_DELIMITERS,
7c968cee
VZ
54 wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
55
56 // args are same as for the non default ctor above
57 void SetString(const wxString& str,
bbf8fc53 58 const wxString& delims = wxDEFAULT_DELIMITERS,
7c968cee
VZ
59 wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
60
61 // reinitialize the tokenizer with the same delimiters/mode
62 void Reinit(const wxString& str);
85833f5c 63
7c968cee
VZ
64 // tokens access
65 // count them
bbf8fc53 66 size_t CountTokens() const;
7c968cee
VZ
67 // did we reach the end of the string?
68 bool HasMoreTokens() const;
69 // get the next token, will return empty string if !HasMoreTokens()
bbf8fc53 70 wxString GetNextToken();
85833f5c 71
7c968cee
VZ
72 // get current tokenizer state
73 // returns the part of the string which remains to tokenize (*not* the
74 // initial string)
bbf8fc53 75 wxString GetString() const { return m_string; }
85833f5c 76
7c968cee
VZ
77 // returns the current position (i.e. one index after the last
78 // returned token or 0 if GetNextToken() has never been called) in the
79 // original string
bbf8fc53 80 size_t GetPosition() const { return m_pos; }
dbdb39b2 81
7c968cee
VZ
82 // misc
83 // get the current mode - can be different from the one passed to the
84 // ctor if it was wxTOKEN_DEFAULT
85 wxStringTokenizerMode GetMode() const { return m_mode; }
86
87 // backwards compatibility section from now on
88 // -------------------------------------------
89
bbf8fc53
VZ
90 // for compatibility only, use GetNextToken() instead
91 wxString NextToken() { return GetNextToken(); }
85833f5c 92
7c968cee
VZ
93 // compatibility only, don't use
94 void SetString(const wxString& to_tokenize,
95 const wxString& delims,
06b466c7 96 bool WXUNUSED(ret_delim))
7c968cee
VZ
97 {
98 SetString(to_tokenize, delims, wxTOKEN_RET_DELIMS);
99 }
100
101 wxStringTokenizer(const wxString& to_tokenize,
102 const wxString& delims,
103 bool ret_delim)
104 {
105 SetString(to_tokenize, delims, ret_delim);
106 }
107
2224580a 108protected:
7c968cee
VZ
109 bool IsOk() const { return m_mode != wxTOKEN_INVALID; }
110
bbf8fc53
VZ
111 wxString m_string, // the (rest of) string to tokenize
112 m_delims; // all delimiters
113
114 size_t m_pos; // the position in the original string
115
7c968cee
VZ
116 wxStringTokenizerMode m_mode; // see wxTOKEN_XXX values
117
118 bool m_hasMore; // do we have more (possible empty) tokens?
f4ada568
GL
119};
120
1e6feb95
VZ
121// ----------------------------------------------------------------------------
122// convenience function which returns all tokens at once
123// ----------------------------------------------------------------------------
124
125// the function takes the same parameters as wxStringTokenizer ctor and returns
126// the array containing all tokens
bddd7a8d 127wxArrayString WXDLLIMPEXP_BASE
1e6feb95
VZ
128wxStringTokenize(const wxString& str,
129 const wxString& delims = wxDEFAULT_DELIMITERS,
130 wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
131
85833f5c 132#endif // _WX_TOKENZRH