]> git.saurik.com Git - wxWidgets.git/blame - include/wx/tokenzr.h
added wxCRITICAL_SECTION macro
[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
15#ifdef __GNUG__
2224580a 16 #pragma interface "tokenzr.h"
f4ada568
GL
17#endif
18
19#include "wx/object.h"
20#include "wx/string.h"
bbf8fc53 21
7c968cee
VZ
22// ----------------------------------------------------------------------------
23// constants
24// ----------------------------------------------------------------------------
25
bbf8fc53
VZ
26// default: delimiters are usual white space characters
27#define wxDEFAULT_DELIMITERS (_T(" \t\r\n"))
f4ada568 28
7c968cee
VZ
29// wxStringTokenizer mode flags which determine its behaviour
30enum wxStringTokenizerMode
31{
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)
38};
39
40// ----------------------------------------------------------------------------
41// wxStringTokenizer: replaces infamous strtok() and has some other features
42// ----------------------------------------------------------------------------
43
85833f5c
VZ
44class WXDLLEXPORT wxStringTokenizer : public wxObject
45{
f4ada568 46public:
7c968cee
VZ
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,
bbf8fc53 52 const wxString& delims = wxDEFAULT_DELIMITERS,
7c968cee
VZ
53 wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
54
55 // args are same as for the non default ctor above
56 void SetString(const wxString& str,
bbf8fc53 57 const wxString& delims = wxDEFAULT_DELIMITERS,
7c968cee
VZ
58 wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
59
60 // reinitialize the tokenizer with the same delimiters/mode
61 void Reinit(const wxString& str);
85833f5c 62
7c968cee
VZ
63 // tokens access
64 // count them
bbf8fc53 65 size_t CountTokens() const;
7c968cee
VZ
66 // did we reach the end of the string?
67 bool HasMoreTokens() const;
68 // get the next token, will return empty string if !HasMoreTokens()
bbf8fc53 69 wxString GetNextToken();
85833f5c 70
7c968cee
VZ
71 // get current tokenizer state
72 // returns the part of the string which remains to tokenize (*not* the
73 // initial string)
bbf8fc53 74 wxString GetString() const { return m_string; }
85833f5c 75
7c968cee
VZ
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
78 // original string
bbf8fc53 79 size_t GetPosition() const { return m_pos; }
dbdb39b2 80
7c968cee
VZ
81 // misc
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; }
85
86 // backwards compatibility section from now on
87 // -------------------------------------------
88
bbf8fc53
VZ
89 // for compatibility only, use GetNextToken() instead
90 wxString NextToken() { return GetNextToken(); }
85833f5c 91
7c968cee
VZ
92 // compatibility only, don't use
93 void SetString(const wxString& to_tokenize,
94 const wxString& delims,
06b466c7 95 bool WXUNUSED(ret_delim))
7c968cee
VZ
96 {
97 SetString(to_tokenize, delims, wxTOKEN_RET_DELIMS);
98 }
99
100 wxStringTokenizer(const wxString& to_tokenize,
101 const wxString& delims,
102 bool ret_delim)
103 {
104 SetString(to_tokenize, delims, ret_delim);
105 }
106
2224580a 107protected:
7c968cee
VZ
108 bool IsOk() const { return m_mode != wxTOKEN_INVALID; }
109
bbf8fc53
VZ
110 wxString m_string, // the (rest of) string to tokenize
111 m_delims; // all delimiters
112
113 size_t m_pos; // the position in the original string
114
7c968cee
VZ
115 wxStringTokenizerMode m_mode; // see wxTOKEN_XXX values
116
117 bool m_hasMore; // do we have more (possible empty) tokens?
f4ada568
GL
118};
119
1e6feb95
VZ
120// ----------------------------------------------------------------------------
121// convenience function which returns all tokens at once
122// ----------------------------------------------------------------------------
123
124// the function takes the same parameters as wxStringTokenizer ctor and returns
125// the array containing all tokens
126wxArrayString WXDLLEXPORT
127wxStringTokenize(const wxString& str,
128 const wxString& delims = wxDEFAULT_DELIMITERS,
129 wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
130
85833f5c 131#endif // _WX_TOKENZRH