1998-09-06 14:28:00 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2000-02-04 13:31:26 -05:00
|
|
|
// Name: wx/tokenzr.h
|
|
|
|
// Purpose: String tokenizer - a C++ replacement for strtok(3)
|
1998-09-06 14:28:00 -04:00
|
|
|
// Author: Guilhem Lavaux
|
2001-06-26 16:59:19 -04:00
|
|
|
// Modified by: (or rather rewritten by) Vadim Zeitlin
|
1998-09-06 14:28:00 -04:00
|
|
|
// Created: 04/22/98
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) Guilhem Lavaux
|
2004-05-23 16:53:33 -04:00
|
|
|
// Licence: wxWindows licence
|
1998-09-06 14:28:00 -04: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 15:52:35 -04:00
|
|
|
#include "wx/arrstr.h"
|
2000-01-31 15:46:49 -05:00
|
|
|
|
2000-02-04 13:31:26 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2000-01-31 15:46:49 -05:00
|
|
|
// default: delimiters are usual white space characters
|
2009-07-23 16:30:22 -04:00
|
|
|
#define wxDEFAULT_DELIMITERS (wxT(" \t\r\n"))
|
1998-09-06 14:28:00 -04:00
|
|
|
|
2000-02-04 13:31:26 -05: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-01 21:59:24 -04:00
|
|
|
class WXDLLIMPEXP_BASE wxStringTokenizer : public wxObject
|
1999-06-10 17:16:58 -04:00
|
|
|
{
|
1998-09-06 14:28:00 -04:00
|
|
|
public:
|
2000-02-04 13:31:26 -05: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 15:46:49 -05:00
|
|
|
const wxString& delims = wxDEFAULT_DELIMITERS,
|
2000-02-04 13:31:26 -05:00
|
|
|
wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
|
|
|
|
|
|
|
|
// args are same as for the non default ctor above
|
|
|
|
void SetString(const wxString& str,
|
2000-01-31 15:46:49 -05:00
|
|
|
const wxString& delims = wxDEFAULT_DELIMITERS,
|
2000-02-04 13:31:26 -05:00
|
|
|
wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
|
|
|
|
|
|
|
|
// reinitialize the tokenizer with the same delimiters/mode
|
|
|
|
void Reinit(const wxString& str);
|
1999-06-10 17:16:58 -04:00
|
|
|
|
2000-02-04 13:31:26 -05:00
|
|
|
// tokens access
|
2005-12-23 19:12:54 -05:00
|
|
|
// return the number of remaining tokens
|
2000-01-31 15:46:49 -05:00
|
|
|
size_t CountTokens() const;
|
2000-02-04 13:31:26 -05: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 15:46:49 -05:00
|
|
|
wxString GetNextToken();
|
2005-12-23 19:12:54 -05: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 17:16:58 -04:00
|
|
|
|
2000-02-04 13:31:26 -05:00
|
|
|
// get current tokenizer state
|
|
|
|
// returns the part of the string which remains to tokenize (*not* the
|
|
|
|
// initial string)
|
2007-07-24 16:00:12 -04:00
|
|
|
wxString GetString() const { return wxString(m_pos, m_string.end()); }
|
1999-06-10 17:16:58 -04:00
|
|
|
|
2000-02-04 13:31:26 -05: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
|
2007-07-24 16:00:12 -04:00
|
|
|
size_t GetPosition() const { return m_pos - m_string.begin(); }
|
1999-02-16 15:17:02 -05:00
|
|
|
|
2000-02-04 13:31:26 -05: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-23 19:12:54 -05:00
|
|
|
// do we return empty tokens?
|
|
|
|
bool AllowEmpty() const { return m_mode != wxTOKEN_STRTOK; }
|
|
|
|
|
2000-02-04 13:31:26 -05:00
|
|
|
|
|
|
|
// backwards compatibility section from now on
|
|
|
|
// -------------------------------------------
|
|
|
|
|
2000-01-31 15:46:49 -05:00
|
|
|
// for compatibility only, use GetNextToken() instead
|
|
|
|
wxString NextToken() { return GetNextToken(); }
|
1999-06-10 17:16:58 -04:00
|
|
|
|
2000-02-04 13:31:26 -05:00
|
|
|
// compatibility only, don't use
|
|
|
|
void SetString(const wxString& to_tokenize,
|
|
|
|
const wxString& delims,
|
2000-02-09 12:51:16 -05:00
|
|
|
bool WXUNUSED(ret_delim))
|
2000-02-04 13:31:26 -05: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 13:26:16 -05:00
|
|
|
protected:
|
2000-02-04 13:31:26 -05:00
|
|
|
bool IsOk() const { return m_mode != wxTOKEN_INVALID; }
|
|
|
|
|
2007-07-24 16:00:12 -04:00
|
|
|
bool DoHasMoreTokens() const;
|
2000-01-31 15:46:49 -05:00
|
|
|
|
2007-07-24 16:00:12 -04:00
|
|
|
enum MoreTokensState
|
|
|
|
{
|
|
|
|
MoreTokens_Unknown,
|
|
|
|
MoreTokens_Yes,
|
|
|
|
MoreTokens_No
|
|
|
|
};
|
|
|
|
|
|
|
|
MoreTokensState m_hasMoreTokens;
|
|
|
|
|
|
|
|
wxString m_string; // the string we tokenize
|
|
|
|
wxString::const_iterator m_stringEnd;
|
|
|
|
// FIXME-UTF8: use wxWcharBuffer
|
|
|
|
wxWxCharBuffer m_delims; // all possible delimiters
|
|
|
|
size_t m_delimsLen;
|
|
|
|
|
|
|
|
wxString::const_iterator m_pos; // the current position in m_string
|
2000-01-31 15:46:49 -05:00
|
|
|
|
2000-02-04 13:31:26 -05:00
|
|
|
wxStringTokenizerMode m_mode; // see wxTOKEN_XXX values
|
|
|
|
|
2005-12-23 19:12:54 -05:00
|
|
|
wxChar m_lastDelim; // delimiter after last token or '\0'
|
1998-09-06 14:28:00 -04:00
|
|
|
};
|
|
|
|
|
2001-06-26 16:59:19 -04: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-01 21:59:24 -04:00
|
|
|
wxArrayString WXDLLIMPEXP_BASE
|
2001-06-26 16:59:19 -04:00
|
|
|
wxStringTokenize(const wxString& str,
|
|
|
|
const wxString& delims = wxDEFAULT_DELIMITERS,
|
|
|
|
wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
|
|
|
|
|
1999-06-10 17:16:58 -04:00
|
|
|
#endif // _WX_TOKENZRH
|