1998-09-06 18:28:00 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2000-02-04 18:31:26 +00:00
|
|
|
// Name: wx/tokenzr.h
|
|
|
|
// Purpose: String tokenizer - a C++ replacement for strtok(3)
|
1998-09-06 18:28:00 +00:00
|
|
|
// Author: Guilhem Lavaux
|
2001-06-26 20:59:19 +00:00
|
|
|
// Modified by: (or rather rewritten by) Vadim Zeitlin
|
1998-09-06 18:28:00 +00:00
|
|
|
// Created: 04/22/98
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) Guilhem Lavaux
|
2004-05-23 20:53:33 +00:00
|
|
|
// Licence: wxWindows licence
|
1998-09-06 18:28:00 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_TOKENZRH
|
|
|
|
#define _WX_TOKENZRH
|
|
|
|
|
|
|
|
#include "wx/object.h"
|
|
|
|
#include "wx/string.h"
|
Added --use-stl to cnfigure, wxUSE_STL to setup0.h
Moved wx/datetime.inl contents to wx/datetime.h and removed
inline redefinition hack.
Implemented STL-like interface on top of wxList/wxArray, when wxUSE_STL=0.
Implemented wxList-like and wxArray interfaces on top of std::list and
std::vector, when wxUSE_STL=1.
Added arrstr.h, moved wxArrayString declaration there; string.h
#includes arrstr.h only if WXWIN_COMPATIBILITY_2_4 is enabled.
Added WX_CLEAR_HASH_MAP, WX_CLEAR_HASH_TABLE, WX_CLEAR_LIST macros,
to clear a wxHashMap, wxHashTable, wxList containing pointers: deletes
pointers and makes container zero-sized.
When wxUSE_STL=1, wxStringList works like a std::list<wxString>.
Made wxBase compile when wxUSE_STL=1.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21768 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2003-07-08 19:52:35 +00:00
|
|
|
#include "wx/arrstr.h"
|
2000-01-31 20:46:49 +00:00
|
|
|
|
2000-02-04 18:31:26 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2000-01-31 20:46:49 +00:00
|
|
|
// default: delimiters are usual white space characters
|
|
|
|
#define wxDEFAULT_DELIMITERS (_T(" \t\r\n"))
|
1998-09-06 18:28:00 +00:00
|
|
|
|
2000-02-04 18:31:26 +00:00
|
|
|
// wxStringTokenizer mode flags which determine its behaviour
|
|
|
|
enum wxStringTokenizerMode
|
|
|
|
{
|
|
|
|
wxTOKEN_INVALID = -1, // set by def ctor until SetString() is called
|
|
|
|
wxTOKEN_DEFAULT, // strtok() for whitespace delims, RET_EMPTY else
|
|
|
|
wxTOKEN_RET_EMPTY, // return empty token in the middle of the string
|
|
|
|
wxTOKEN_RET_EMPTY_ALL, // return trailing empty tokens too
|
|
|
|
wxTOKEN_RET_DELIMS, // return the delim with token (implies RET_EMPTY)
|
|
|
|
wxTOKEN_STRTOK // behave exactly like strtok(3)
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxStringTokenizer: replaces infamous strtok() and has some other features
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2003-07-02 01:59:24 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxStringTokenizer : public wxObject
|
1999-06-10 21:16:58 +00:00
|
|
|
{
|
1998-09-06 18:28:00 +00:00
|
|
|
public:
|
2000-02-04 18:31:26 +00:00
|
|
|
// ctors and initializers
|
|
|
|
// default ctor, call SetString() later
|
|
|
|
wxStringTokenizer() { m_mode = wxTOKEN_INVALID; }
|
|
|
|
// ctor which gives us the string
|
|
|
|
wxStringTokenizer(const wxString& str,
|
2000-01-31 20:46:49 +00:00
|
|
|
const wxString& delims = wxDEFAULT_DELIMITERS,
|
2000-02-04 18:31:26 +00:00
|
|
|
wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
|
|
|
|
|
|
|
|
// args are same as for the non default ctor above
|
|
|
|
void SetString(const wxString& str,
|
2000-01-31 20:46:49 +00:00
|
|
|
const wxString& delims = wxDEFAULT_DELIMITERS,
|
2000-02-04 18:31:26 +00:00
|
|
|
wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
|
|
|
|
|
|
|
|
// reinitialize the tokenizer with the same delimiters/mode
|
|
|
|
void Reinit(const wxString& str);
|
1999-06-10 21:16:58 +00:00
|
|
|
|
2000-02-04 18:31:26 +00:00
|
|
|
// tokens access
|
2005-12-24 00:12:54 +00:00
|
|
|
// return the number of remaining tokens
|
2000-01-31 20:46:49 +00:00
|
|
|
size_t CountTokens() const;
|
2000-02-04 18:31:26 +00:00
|
|
|
// did we reach the end of the string?
|
|
|
|
bool HasMoreTokens() const;
|
|
|
|
// get the next token, will return empty string if !HasMoreTokens()
|
2000-01-31 20:46:49 +00:00
|
|
|
wxString GetNextToken();
|
2005-12-24 00:12:54 +00:00
|
|
|
// get the delimiter which terminated the token last retrieved by
|
|
|
|
// GetNextToken() or NUL if there had been no tokens yet or the last
|
|
|
|
// one wasn't terminated (but ran to the end of the string)
|
|
|
|
wxChar GetLastDelimiter() const { return m_lastDelim; }
|
1999-06-10 21:16:58 +00:00
|
|
|
|
2000-02-04 18:31:26 +00:00
|
|
|
// get current tokenizer state
|
|
|
|
// returns the part of the string which remains to tokenize (*not* the
|
|
|
|
// initial string)
|
2000-01-31 20:46:49 +00:00
|
|
|
wxString GetString() const { return m_string; }
|
1999-06-10 21:16:58 +00:00
|
|
|
|
2000-02-04 18:31:26 +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
|
2000-01-31 20:46:49 +00:00
|
|
|
size_t GetPosition() const { return m_pos; }
|
1999-02-16 20:17:02 +00:00
|
|
|
|
2000-02-04 18:31:26 +00:00
|
|
|
// misc
|
|
|
|
// get the current mode - can be different from the one passed to the
|
|
|
|
// ctor if it was wxTOKEN_DEFAULT
|
|
|
|
wxStringTokenizerMode GetMode() const { return m_mode; }
|
2005-12-24 00:12:54 +00:00
|
|
|
// do we return empty tokens?
|
|
|
|
bool AllowEmpty() const { return m_mode != wxTOKEN_STRTOK; }
|
|
|
|
|
2000-02-04 18:31:26 +00:00
|
|
|
|
|
|
|
// backwards compatibility section from now on
|
|
|
|
// -------------------------------------------
|
|
|
|
|
2000-01-31 20:46:49 +00:00
|
|
|
// for compatibility only, use GetNextToken() instead
|
|
|
|
wxString NextToken() { return GetNextToken(); }
|
1999-06-10 21:16:58 +00:00
|
|
|
|
2000-02-04 18:31:26 +00:00
|
|
|
// compatibility only, don't use
|
|
|
|
void SetString(const wxString& to_tokenize,
|
|
|
|
const wxString& delims,
|
2000-02-09 17:51:16 +00:00
|
|
|
bool WXUNUSED(ret_delim))
|
2000-02-04 18:31:26 +00:00
|
|
|
{
|
|
|
|
SetString(to_tokenize, delims, wxTOKEN_RET_DELIMS);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxStringTokenizer(const wxString& to_tokenize,
|
|
|
|
const wxString& delims,
|
|
|
|
bool ret_delim)
|
|
|
|
{
|
|
|
|
SetString(to_tokenize, delims, ret_delim);
|
|
|
|
}
|
|
|
|
|
2000-01-31 18:26:16 +00:00
|
|
|
protected:
|
2000-02-04 18:31:26 +00:00
|
|
|
bool IsOk() const { return m_mode != wxTOKEN_INVALID; }
|
|
|
|
|
2005-12-24 00:12:54 +00:00
|
|
|
wxString m_string, // the string we tokenize
|
|
|
|
m_delims; // all possible delimiters
|
2000-01-31 20:46:49 +00:00
|
|
|
|
2005-12-24 00:12:54 +00:00
|
|
|
size_t m_pos; // the current position in m_string
|
2000-01-31 20:46:49 +00:00
|
|
|
|
2000-02-04 18:31:26 +00:00
|
|
|
wxStringTokenizerMode m_mode; // see wxTOKEN_XXX values
|
|
|
|
|
2005-12-24 00:12:54 +00:00
|
|
|
wxChar m_lastDelim; // delimiter after last token or '\0'
|
1998-09-06 18:28:00 +00:00
|
|
|
};
|
|
|
|
|
2001-06-26 20:59:19 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// convenience function which returns all tokens at once
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// the function takes the same parameters as wxStringTokenizer ctor and returns
|
|
|
|
// the array containing all tokens
|
2003-07-02 01:59:24 +00:00
|
|
|
wxArrayString WXDLLIMPEXP_BASE
|
2001-06-26 20:59:19 +00:00
|
|
|
wxStringTokenize(const wxString& str,
|
|
|
|
const wxString& delims = wxDEFAULT_DELIMITERS,
|
|
|
|
wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
|
|
|
|
|
1999-06-10 21:16:58 +00:00
|
|
|
#endif // _WX_TOKENZRH
|