tokenzr.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: tokenzr.h
  3. // Purpose: interface of wxStringTokenizer
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. The behaviour of wxStringTokenizer is governed by the
  9. wxStringTokenizer::wxStringTokenizer() or wxStringTokenizer::SetString()
  10. with the parameter @e mode, which may be one of the following:
  11. */
  12. enum wxStringTokenizerMode
  13. {
  14. wxTOKEN_INVALID = -1, ///< Invalid tokenizer mode.
  15. /**
  16. Default behaviour: wxStringTokenizer will behave in the same way as
  17. @c strtok() (::wxTOKEN_STRTOK) if the delimiters string only contains
  18. white space characters but, unlike the standard function, it will
  19. behave like ::wxTOKEN_RET_EMPTY, returning empty tokens if this is not
  20. the case. This is helpful for parsing strictly formatted data where
  21. the number of fields is fixed but some of them may be empty (i.e.
  22. @c TAB or comma delimited text files).
  23. */
  24. wxTOKEN_DEFAULT,
  25. /**
  26. In this mode, the empty tokens in the middle of the string will be returned,
  27. i.e. @c "a::b:" will be tokenized in three tokens @c 'a', @c '' and @c 'b'.
  28. Notice that all trailing delimiters are ignored in this mode, not just the last one,
  29. i.e. a string @c "a::b::" would still result in the same set of tokens.
  30. */
  31. wxTOKEN_RET_EMPTY,
  32. /**
  33. In this mode, empty trailing tokens (including the one after the last delimiter
  34. character) will be returned as well. The string @c "a::b:" will be tokenized in
  35. four tokens: the already mentioned ones and another empty one as the last one
  36. and a string @c "a::b::" will have five tokens.
  37. */
  38. wxTOKEN_RET_EMPTY_ALL,
  39. /**
  40. In this mode, the delimiter character after the end of the current token (there
  41. may be none if this is the last token) is returned appended to the token.
  42. Otherwise, it is the same mode as ::wxTOKEN_RET_EMPTY. Notice that there is no
  43. mode like this one but behaving like ::wxTOKEN_RET_EMPTY_ALL instead of
  44. ::wxTOKEN_RET_EMPTY, use ::wxTOKEN_RET_EMPTY_ALL and
  45. wxStringTokenizer::GetLastDelimiter() to emulate it.
  46. */
  47. wxTOKEN_RET_DELIMS,
  48. /**
  49. In this mode the class behaves exactly like the standard @c strtok() function:
  50. the empty tokens are never returned.
  51. */
  52. wxTOKEN_STRTOK
  53. };
  54. /// Default wxStringTokenizer delimiters are the usual white space characters.
  55. #define wxDEFAULT_DELIMITERS " \t\r\n"
  56. /**
  57. @class wxStringTokenizer
  58. wxStringTokenizer helps you to break a string up into a number of tokens.
  59. It replaces the standard C function @c strtok() and also extends it in a
  60. number of ways.
  61. To use this class, you should create a wxStringTokenizer object, give it the
  62. string to tokenize and also the delimiters which separate tokens in the string
  63. (by default, white space characters will be used).
  64. Then wxStringTokenizer::GetNextToken() may be called repeatedly until
  65. wxStringTokenizer::HasMoreTokens() returns @false.
  66. For example:
  67. @code
  68. wxStringTokenizer tokenizer("first:second:third:fourth", ":");
  69. while ( tokenizer.HasMoreTokens() )
  70. {
  71. wxString token = tokenizer.GetNextToken();
  72. // process token here
  73. }
  74. @endcode
  75. @library{wxbase}
  76. @category{data}
  77. @see ::wxStringTokenize()
  78. */
  79. class wxStringTokenizer : public wxObject
  80. {
  81. public:
  82. /**
  83. Default constructor. You must call SetString() before calling any other
  84. methods.
  85. */
  86. wxStringTokenizer();
  87. /**
  88. Constructor. Pass the string to tokenize, a string containing
  89. delimiters, and the @a mode specifying how the string should be
  90. tokenized.
  91. @see SetString()
  92. */
  93. wxStringTokenizer(const wxString& str,
  94. const wxString& delims = wxDEFAULT_DELIMITERS,
  95. wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
  96. /**
  97. Returns the number of tokens remaining in the input string. The number
  98. of tokens returned by this function is decremented each time
  99. GetNextToken() is called and when it reaches 0, HasMoreTokens()
  100. returns @false.
  101. */
  102. size_t CountTokens() const;
  103. /**
  104. Returns the delimiter which ended scan for the last token returned by
  105. GetNextToken() or @c NUL if there had been no calls to this function
  106. yet or if it returned the trailing empty token in
  107. ::wxTOKEN_RET_EMPTY_ALL mode.
  108. @since 2.7.0
  109. */
  110. wxChar GetLastDelimiter() const;
  111. /**
  112. Returns the next token or empty string if the end of string was reached.
  113. */
  114. wxString GetNextToken();
  115. /**
  116. Returns the current position (i.e.\ one index after the last returned
  117. token or 0 if GetNextToken() has never been called) in the original
  118. string.
  119. */
  120. size_t GetPosition() const;
  121. /**
  122. Returns the part of the starting string without all token already extracted.
  123. */
  124. wxString GetString() const;
  125. /**
  126. Returns @true if the tokenizer has further tokens, @false if none are left.
  127. */
  128. bool HasMoreTokens() const;
  129. /**
  130. Initializes the tokenizer. Pass the string to tokenize, a string
  131. containing delimiters, and the @a mode specifying how the string
  132. should be tokenized.
  133. */
  134. void SetString(const wxString& str,
  135. const wxString& delims = wxDEFAULT_DELIMITERS,
  136. wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
  137. };
  138. /** @addtogroup group_funcmacro_string */
  139. //@{
  140. /**
  141. This is a convenience function wrapping wxStringTokenizer which simply
  142. returns all tokens found in the given @a str as an array.
  143. Please see wxStringTokenizer::wxStringTokenizer for the description
  144. of the other parameters.
  145. @return The array with the parsed tokens.
  146. @header{wx/tokenzr.h}
  147. */
  148. wxArrayString
  149. wxStringTokenize(const wxString& str,
  150. const wxString& delims = wxDEFAULT_DELIMITERS,
  151. wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
  152. //@}