1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: String tokenizer - a C++ replacement for strtok(3)
4 // Author: Guilhem Lavaux
5 // Modified by: (or rather rewritten by) Vadim Zeitlin
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "tokenzr.h"
19 #include "wx/object.h"
20 #include "wx/string.h"
21 #include "wx/arrstr.h"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
27 // default: delimiters are usual white space characters
28 #define wxDEFAULT_DELIMITERS (_T(" \t\r\n"))
30 // wxStringTokenizer mode flags which determine its behaviour
31 enum wxStringTokenizerMode
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)
41 // ----------------------------------------------------------------------------
42 // wxStringTokenizer: replaces infamous strtok() and has some other features
43 // ----------------------------------------------------------------------------
45 class WXDLLIMPEXP_BASE wxStringTokenizer
: public wxObject
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
,
53 const wxString
& delims
= wxDEFAULT_DELIMITERS
,
54 wxStringTokenizerMode mode
= wxTOKEN_DEFAULT
);
56 // args are same as for the non default ctor above
57 void SetString(const wxString
& str
,
58 const wxString
& delims
= wxDEFAULT_DELIMITERS
,
59 wxStringTokenizerMode mode
= wxTOKEN_DEFAULT
);
61 // reinitialize the tokenizer with the same delimiters/mode
62 void Reinit(const wxString
& str
);
66 size_t CountTokens() const;
67 // did we reach the end of the string?
68 bool HasMoreTokens() const;
69 // get the next token, will return empty string if !HasMoreTokens()
70 wxString
GetNextToken();
72 // get current tokenizer state
73 // returns the part of the string which remains to tokenize (*not* the
75 wxString
GetString() const { return m_string
; }
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
80 size_t GetPosition() const { return m_pos
; }
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
; }
87 // backwards compatibility section from now on
88 // -------------------------------------------
90 // for compatibility only, use GetNextToken() instead
91 wxString
NextToken() { return GetNextToken(); }
93 // compatibility only, don't use
94 void SetString(const wxString
& to_tokenize
,
95 const wxString
& delims
,
96 bool WXUNUSED(ret_delim
))
98 SetString(to_tokenize
, delims
, wxTOKEN_RET_DELIMS
);
101 wxStringTokenizer(const wxString
& to_tokenize
,
102 const wxString
& delims
,
105 SetString(to_tokenize
, delims
, ret_delim
);
109 bool IsOk() const { return m_mode
!= wxTOKEN_INVALID
; }
111 wxString m_string
, // the (rest of) string to tokenize
112 m_delims
; // all delimiters
114 size_t m_pos
; // the position in the original string
116 wxStringTokenizerMode m_mode
; // see wxTOKEN_XXX values
118 bool m_hasMore
; // do we have more (possible empty) tokens?
121 // ----------------------------------------------------------------------------
122 // convenience function which returns all tokens at once
123 // ----------------------------------------------------------------------------
125 // the function takes the same parameters as wxStringTokenizer ctor and returns
126 // the array containing all tokens
127 wxArrayString WXDLLIMPEXP_BASE
128 wxStringTokenize(const wxString
& str
,
129 const wxString
& delims
= wxDEFAULT_DELIMITERS
,
130 wxStringTokenizerMode mode
= wxTOKEN_DEFAULT
);
132 #endif // _WX_TOKENZRH