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
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_TOKENZRH
|
|
|
|
#define _WX_TOKENZRH
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
2000-01-31 13:26:16 -05:00
|
|
|
#pragma interface "tokenzr.h"
|
1998-09-06 14:28:00 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/object.h"
|
|
|
|
#include "wx/string.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
|
|
|
|
#define wxDEFAULT_DELIMITERS (_T(" \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
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-06-10 17:16:58 -04:00
|
|
|
class WXDLLEXPORT wxStringTokenizer : public wxObject
|
|
|
|
{
|
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
|
|
|
|
// count them
|
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();
|
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)
|
2000-01-31 15:46:49 -05:00
|
|
|
wxString GetString() const { return m_string; }
|
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
|
2000-01-31 15:46:49 -05:00
|
|
|
size_t GetPosition() const { return m_pos; }
|
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; }
|
|
|
|
|
|
|
|
// 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; }
|
|
|
|
|
2000-01-31 15:46:49 -05:00
|
|
|
wxString m_string, // the (rest of) string to tokenize
|
|
|
|
m_delims; // all delimiters
|
|
|
|
|
|
|
|
size_t m_pos; // the position in the original string
|
|
|
|
|
2000-02-04 13:31:26 -05:00
|
|
|
wxStringTokenizerMode m_mode; // see wxTOKEN_XXX values
|
|
|
|
|
|
|
|
bool m_hasMore; // do we have more (possible empty) tokens?
|
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
|
|
|
|
wxArrayString WXDLLEXPORT
|
|
|
|
wxStringTokenize(const wxString& str,
|
|
|
|
const wxString& delims = wxDEFAULT_DELIMITERS,
|
|
|
|
wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
|
|
|
|
|
1999-06-10 17:16:58 -04:00
|
|
|
#endif // _WX_TOKENZRH
|