1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: String tokenizer - a C++ replacement for strtok(3)
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "tokenzr.h"
19 #include "wx/object.h"
20 #include "wx/string.h"
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 // default: delimiters are usual white space characters
27 #define wxDEFAULT_DELIMITERS (_T(" \t\r\n"))
29 // wxStringTokenizer mode flags which determine its behaviour
30 enum wxStringTokenizerMode
32 wxTOKEN_INVALID
= -1, // set by def ctor until SetString() is called
33 wxTOKEN_DEFAULT
, // strtok() for whitespace delims, RET_EMPTY else
34 wxTOKEN_RET_EMPTY
, // return empty token in the middle of the string
35 wxTOKEN_RET_EMPTY_ALL
, // return trailing empty tokens too
36 wxTOKEN_RET_DELIMS
, // return the delim with token (implies RET_EMPTY)
37 wxTOKEN_STRTOK
// behave exactly like strtok(3)
40 // ----------------------------------------------------------------------------
41 // wxStringTokenizer: replaces infamous strtok() and has some other features
42 // ----------------------------------------------------------------------------
44 class WXDLLEXPORT wxStringTokenizer
: public wxObject
47 // ctors and initializers
48 // default ctor, call SetString() later
49 wxStringTokenizer() { m_mode
= wxTOKEN_INVALID
; }
50 // ctor which gives us the string
51 wxStringTokenizer(const wxString
& str
,
52 const wxString
& delims
= wxDEFAULT_DELIMITERS
,
53 wxStringTokenizerMode mode
= wxTOKEN_DEFAULT
);
55 // args are same as for the non default ctor above
56 void SetString(const wxString
& str
,
57 const wxString
& delims
= wxDEFAULT_DELIMITERS
,
58 wxStringTokenizerMode mode
= wxTOKEN_DEFAULT
);
60 // reinitialize the tokenizer with the same delimiters/mode
61 void Reinit(const wxString
& str
);
65 size_t CountTokens() const;
66 // did we reach the end of the string?
67 bool HasMoreTokens() const;
68 // get the next token, will return empty string if !HasMoreTokens()
69 wxString
GetNextToken();
71 // get current tokenizer state
72 // returns the part of the string which remains to tokenize (*not* the
74 wxString
GetString() const { return m_string
; }
76 // returns the current position (i.e. one index after the last
77 // returned token or 0 if GetNextToken() has never been called) in the
79 size_t GetPosition() const { return m_pos
; }
82 // get the current mode - can be different from the one passed to the
83 // ctor if it was wxTOKEN_DEFAULT
84 wxStringTokenizerMode
GetMode() const { return m_mode
; }
86 // backwards compatibility section from now on
87 // -------------------------------------------
89 // for compatibility only, use GetNextToken() instead
90 wxString
NextToken() { return GetNextToken(); }
92 // compatibility only, don't use
93 void SetString(const wxString
& to_tokenize
,
94 const wxString
& delims
,
95 bool WXUNUSED(ret_delim
))
97 SetString(to_tokenize
, delims
, wxTOKEN_RET_DELIMS
);
100 wxStringTokenizer(const wxString
& to_tokenize
,
101 const wxString
& delims
,
104 SetString(to_tokenize
, delims
, ret_delim
);
108 bool IsOk() const { return m_mode
!= wxTOKEN_INVALID
; }
110 wxString m_string
, // the (rest of) string to tokenize
111 m_delims
; // all delimiters
113 size_t m_pos
; // the position in the original string
115 wxStringTokenizerMode m_mode
; // see wxTOKEN_XXX values
117 bool m_hasMore
; // do we have more (possible empty) tokens?
120 #endif // _WX_TOKENZRH