2008-03-08 13:52:38 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tokenzr.h
|
2008-03-10 15:24:38 +00:00
|
|
|
// Purpose: interface of wxStringTokenizer
|
2008-03-08 13:52:38 +00:00
|
|
|
// Author: wxWidgets team
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
@class wxStringTokenizer
|
|
|
|
@wxheader{tokenzr.h}
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
wxStringTokenizer helps you to break a string up into a number of tokens. It
|
|
|
|
replaces the standard C function @c strtok() and also extends it in a
|
|
|
|
number of ways.
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
To use this class, you should create a wxStringTokenizer object, give it the
|
|
|
|
string to tokenize and also the delimiters which separate tokens in the string
|
|
|
|
(by default, white space characters will be used).
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
Then wxStringTokenizer::GetNextToken may be called
|
2008-03-08 14:43:31 +00:00
|
|
|
repeatedly until it wxStringTokenizer::HasMoreTokens
|
2008-03-08 13:52:38 +00:00
|
|
|
returns @false.
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
For example:
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
@code
|
|
|
|
wxStringTokenizer tkz(wxT("first:second:third:fourth"), wxT(":"));
|
|
|
|
while ( tkz.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
wxString token = tkz.GetNextToken();
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
// process token here
|
|
|
|
}
|
|
|
|
@endcode
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
By default, wxStringTokenizer will behave in the same way as @c strtok() if
|
|
|
|
the delimiters string only contains white space characters but, unlike the
|
|
|
|
standard function, it will return empty tokens if this is not the case. This
|
|
|
|
is helpful for parsing strictly formatted data where the number of fields is
|
|
|
|
fixed but some of them may be empty (i.e. @c TAB or comma delimited text
|
|
|
|
files).
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
The behaviour is governed by the last
|
2008-03-08 13:52:38 +00:00
|
|
|
@ref wxStringTokenizer::wxstringtokenizer
|
2008-03-08 14:43:31 +00:00
|
|
|
constructor/wxStringTokenizer::SetString
|
2008-03-08 13:52:38 +00:00
|
|
|
parameter @c mode which may be one of the following:
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
@c wxTOKEN_DEFAULT
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
Default behaviour (as described above):
|
|
|
|
same as @c wxTOKEN_STRTOK if the delimiter string contains only
|
|
|
|
whitespaces, same as @c wxTOKEN_RET_EMPTY otherwise
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
@c wxTOKEN_RET_EMPTY
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
In this mode, the empty tokens in the
|
|
|
|
middle of the string will be returned, i.e. @c "a::b:" will be tokenized in
|
|
|
|
three tokens 'a', '' and 'b'. Notice that all trailing delimiters are ignored
|
|
|
|
in this mode, not just the last one, i.e. a string @c "a::b::" would
|
|
|
|
still result in the same set of tokens.
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
@c wxTOKEN_RET_EMPTY_ALL
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
In this mode, empty trailing tokens
|
|
|
|
(including the one after the last delimiter character) will be returned as
|
|
|
|
well. The string @c "a::b:" will be tokenized in four tokens: the already
|
2008-03-08 14:43:31 +00:00
|
|
|
mentioned ones and another empty one as the last one and a string
|
2008-03-08 13:52:38 +00:00
|
|
|
@c "a::b::" will have five tokens.
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
@c wxTOKEN_RET_DELIMS
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
In this mode, the delimiter character
|
|
|
|
after the end of the current token (there may be none if this is the last
|
2008-03-08 14:43:31 +00:00
|
|
|
token) is returned appended to the token. Otherwise, it is the same mode as
|
2008-03-08 13:52:38 +00:00
|
|
|
@c wxTOKEN_RET_EMPTY. Notice that there is no mode like this one but
|
2008-03-08 14:43:31 +00:00
|
|
|
behaving like @c wxTOKEN_RET_EMPTY_ALL instead of
|
|
|
|
@c wxTOKEN_RET_EMPTY, use @c wxTOKEN_RET_EMPTY_ALL and
|
2008-03-08 13:52:38 +00:00
|
|
|
wxStringTokenizer::GetLastDelimiter to emulate it.
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
@c wxTOKEN_STRTOK
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
In this mode the class behaves exactly like
|
|
|
|
the standard @c strtok() function: the empty tokens are never returned.
|
2008-03-08 14:43:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
@library{wxbase}
|
|
|
|
@category{data}
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-10 15:24:38 +00:00
|
|
|
@see wxStringTokenize()
|
2008-03-08 13:52:38 +00:00
|
|
|
*/
|
|
|
|
class wxStringTokenizer : public wxObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
//@{
|
|
|
|
/**
|
|
|
|
Constructor. Pass the string to tokenize, a string containing delimiters
|
|
|
|
and the mode specifying how the string should be tokenized.
|
|
|
|
*/
|
|
|
|
wxStringTokenizer();
|
2008-03-08 14:43:31 +00:00
|
|
|
wxStringTokenizer(const wxString& str,
|
|
|
|
const wxString& delims = " \t\r\n",
|
|
|
|
wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
|
2008-03-08 13:52:38 +00:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the number of tokens remaining in the input string. The number of
|
2008-03-08 14:43:31 +00:00
|
|
|
tokens returned by this function is decremented each time
|
2008-03-08 13:52:38 +00:00
|
|
|
GetNextToken() is called and when it
|
|
|
|
reaches 0 HasMoreTokens() returns
|
|
|
|
@false.
|
|
|
|
*/
|
2008-03-09 16:24:26 +00:00
|
|
|
int CountTokens() const;
|
2008-03-08 13:52:38 +00:00
|
|
|
|
|
|
|
/**
|
2008-03-08 14:43:31 +00:00
|
|
|
Returns the delimiter which ended scan for the last token returned by
|
2008-03-08 13:52:38 +00:00
|
|
|
GetNextToken() or @c NUL if
|
|
|
|
there had been no calls to this function yet or if it returned the trailing
|
|
|
|
empty token in @c wxTOKEN_RET_EMPTY_ALL mode.
|
2008-03-10 15:24:38 +00:00
|
|
|
|
|
|
|
@wxsince{2.7.0}
|
2008-03-08 13:52:38 +00:00
|
|
|
*/
|
|
|
|
wxChar GetLastDelimiter();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the next token or empty string if the end of string was reached.
|
|
|
|
*/
|
2008-03-09 16:24:26 +00:00
|
|
|
wxString GetNextToken() const;
|
2008-03-08 13:52:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the current position (i.e. one index after the last returned
|
|
|
|
token or 0 if GetNextToken() has never been called) in the original
|
|
|
|
string.
|
|
|
|
*/
|
2008-03-09 16:24:26 +00:00
|
|
|
size_t GetPosition() const;
|
2008-03-08 13:52:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the part of the starting string without all token already extracted.
|
|
|
|
*/
|
2008-03-09 16:24:26 +00:00
|
|
|
wxString GetString() const;
|
2008-03-08 13:52:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns @true if the tokenizer has further tokens, @false if none are left.
|
|
|
|
*/
|
2008-03-09 16:24:26 +00:00
|
|
|
bool HasMoreTokens() const;
|
2008-03-08 13:52:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Initializes the tokenizer.
|
|
|
|
Pass the string to tokenize, a string containing delimiters,
|
|
|
|
and the mode specifying how the string should be tokenized.
|
|
|
|
*/
|
|
|
|
void SetString(const wxString& to_tokenize,
|
|
|
|
const wxString& delims = " \t\r\n",
|
|
|
|
wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
|
|
|
|
};
|
2008-03-10 15:24:38 +00:00
|
|
|
|