Rev 47 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 47 | Rev 55 | ||
---|---|---|---|
Line 24... | Line 24... | ||
24 | #define _STRTOK_H_
|
24 | #define _STRTOK_H_
|
25 | 25 | ||
26 | 26 | ||
27 | #include <string>
|
27 | #include <string>
|
28 | 28 | ||
- | 29 | typedef std::string::size_type SIZE; |
|
- | 30 | ||
29 | /// Simple string tokenizer class.
|
31 | /// Simple string tokenizer class.
|
30 | /**
|
32 | /**
|
31 | * This class implements a string tokenizer. It splits a string
|
33 | * This class implements a string tokenizer. It splits a string
|
32 | * by a character to a number of elements (tokens) which are
|
34 | * by a character to a number of elements (tokens) which are
|
33 | * provided sequentially.
|
35 | * provided sequentially.
|
34 | *
|
36 | *
|
35 | * All operations are made on the original string itself.
|
37 | * All operations are made on a copy of the original string
|
36 | * The implementation is not ready to handle any changes of the
|
38 | * (which may be in fact a copy-on-write instance).
|
37 | * string.
|
- | |
38 | *
|
39 | *
|
39 | * The original string is left unchanged. All tokens are returned
|
40 | * The original string is left unchanged. All tokens are returned
|
40 | * as newly created strings.
|
41 | * as newly created strings.
|
- | 42 | *
|
|
- | 43 | * There is possibility to specify a prefix character which
|
|
- | 44 | * causes the consecutive character is not considered as
|
|
- | 45 | * a delimiter. If you don't specify this character (or specify
|
|
- | 46 | * the NUL character, 0x00) this feature is disabled. The mostly
|
|
- | 47 | * used prefix is a backslash ('\').
|
|
- | 48 | *
|
|
- | 49 | * This class is not thread-safe.
|
|
- | 50 | *
|
|
- | 51 | * Performance note: This class is currently not intended
|
|
- | 52 | * to be very fast. Speed optimizations will be done later.
|
|
41 | */
|
53 | */
|
42 | class StringTokenizer
|
54 | class StringTokenizer
|
43 | {
|
55 | {
|
44 | public: |
56 | public: |
45 | /// Constructor.
|
57 | /// Constructor.
|
46 | /**
|
58 | /**
|
47 | * Creates a ready-to-use tokenizer.
|
59 | * Creates a ready-to-use tokenizer.
|
48 | *
|
60 | *
|
49 | * \param[in] rStr string for tokenizing
|
61 | * \param[in] rStr string for tokenizing
|
50 | * \param[in] cDelim delimiter (separator) character
|
62 | * \param[in] cDelim delimiter (separator) character
|
- | 63 | * \param[in] cPrefix character which is prepended if a
|
|
- | 64 | * character must not separate tokens
|
|
51 | */
|
65 | */
|
52 | StringTokenizer(const std::string& rStr, char cDelim = ','); |
66 | StringTokenizer(const std::string& rStr, char cDelim = ',', char cPrefix = '\0'); |
53 | 67 | ||
54 | /// Destructor.
|
68 | /// Destructor.
|
55 | ~StringTokenizer() {} |
69 | ~StringTokenizer() {} |
56 | 70 | ||
57 | /// Checks whether the tokenizer can provide more tokens.
|
71 | /// Checks whether the tokenizer can provide more tokens.
|
Line 63... | Line 77... | ||
63 | return m_pos < m_len; |
77 | return m_pos < m_len; |
64 | }
|
78 | }
|
65 | 79 | ||
66 | /// Returns the next token.
|
80 | /// Returns the next token.
|
67 | /**
|
81 | /**
|
- | 82 | * If a prefix is defined it is stripped from the returned
|
|
- | 83 | * string (e.g. 'abc\ def' is transformed to 'abc def'
|
|
- | 84 | * while the prefix is '\').
|
|
- | 85 | *
|
|
- | 86 | * \param[in] fSkipEmpty skip empty strings (more consecutive delimiters)
|
|
- | 87 | * \return next token or "" if no more tokens available
|
|
- | 88 | *
|
|
- | 89 | * \sa GetNextTokenRaw()
|
|
- | 90 | */
|
|
- | 91 | std::string GetNextToken(bool fSkipEmpty = false); |
|
- | 92 | ||
- | 93 | /// Returns the next token.
|
|
- | 94 | /**
|
|
- | 95 | * This method always returns an unmodified string even
|
|
- | 96 | * if it contains prefix characters.
|
|
- | 97 | *
|
|
- | 98 | * \param[in] fSkipEmpty skip empty strings (more consecutive delimiters)
|
|
68 | * \return next token or "" if no more tokens available
|
99 | * \return next token or "" if no more tokens available
|
- | 100 | *
|
|
- | 101 | * \sa GetNextToken()
|
|
- | 102 | */
|
|
- | 103 | std::string GetNextTokenRaw(bool fSkipEmpty = false); |
|
- | 104 | ||
- | 105 | /// Returns the remainder of the source string.
|
|
- | 106 | /**
|
|
- | 107 | * This method returns everything what has not been
|
|
- | 108 | * processed (tokenized) yet and moves the current
|
|
- | 109 | * position to the end of the string.
|
|
- | 110 | *
|
|
- | 111 | * If a prefix is defined it is stripped from
|
|
- | 112 | * the returned string.
|
|
- | 113 | *
|
|
- | 114 | * \return remainder string
|
|
69 | */
|
115 | */
|
70 | std::string GetNextToken(); |
116 | std::string GetRemainder(); |
71 | 117 | ||
72 | /// Sets a delimiter (separator) character.
|
118 | /// Sets a delimiter (separator) character.
|
73 | /**
|
119 | /**
|
74 | * The new delimiter has effect only to tokens returned later;
|
120 | * The new delimiter has effect only to tokens returned later;
|
75 | * the position in the string is not affected.
|
121 | * the position in the string is not affected.
|
76 | *
|
122 | *
|
- | 123 | * If you specify a NUL character (0x00) here the prefix
|
|
- | 124 | * will not be used.
|
|
- | 125 | *
|
|
77 | * \param[in] cDelim delimiter character
|
126 | * \param[in] cDelim delimiter character
|
78 | */
|
127 | */
|
79 | inline void SetDelimiter(char cDelim) |
128 | inline void SetDelimiter(char cDelim) |
80 | {
|
129 | {
|
81 | m_cDelim = cDelim; |
130 | m_cDelim = cDelim; |
Line 88... | Line 137... | ||
88 | inline char GetDelimiter() const |
137 | inline char GetDelimiter() const |
89 | {
|
138 | {
|
90 | return m_cDelim; |
139 | return m_cDelim; |
91 | }
|
140 | }
|
92 | 141 | ||
- | 142 | /// Sets a prefix character.
|
|
- | 143 | /**
|
|
- | 144 | * The new prefix has effect only to tokens returned later;
|
|
- | 145 | * the position in the string is not affected.
|
|
- | 146 | *
|
|
- | 147 | * \param[in] cPrefix prefix character
|
|
- | 148 | *
|
|
- | 149 | * \sa SetNoPrefix()
|
|
- | 150 | */
|
|
- | 151 | inline void SetPrefix(char cPrefix) |
|
- | 152 | {
|
|
- | 153 | m_cPrefix = cPrefix; |
|
- | 154 | }
|
|
- | 155 | ||
- | 156 | /// Returns the prefix character.
|
|
- | 157 | /**
|
|
- | 158 | * \return prefix character
|
|
- | 159 | */
|
|
- | 160 | inline char GetPrefix() const |
|
- | 161 | {
|
|
- | 162 | return m_cPrefix; |
|
- | 163 | }
|
|
- | 164 | ||
- | 165 | /// Sets the prefix to 'no prefix'.
|
|
- | 166 | /**
|
|
- | 167 | * Calling this method is equivalent to SetPrefix((char) 0).
|
|
- | 168 | *
|
|
- | 169 | * \sa SetPrefix()
|
|
- | 170 | */
|
|
- | 171 | inline void SetNoPrefix() |
|
- | 172 | {
|
|
- | 173 | SetPrefix('\0'); |
|
- | 174 | }
|
|
- | 175 | ||
93 | /// Resets the tokenizer.
|
176 | /// Resets the tokenizer.
|
94 | /**
|
177 | /**
|
95 | * Re-initializes tokenizing to the start of the string.
|
178 | * Re-initializes tokenizing to the start of the string.
|
96 | */
|
179 | */
|
97 | inline void Reset() |
180 | inline void Reset() |
Line 100... | Line 183... | ||
100 | }
|
183 | }
|
101 | 184 | ||
102 | private: |
185 | private: |
103 | std::string m_str; ///< tokenized string |
186 | std::string m_str; ///< tokenized string |
104 | char m_cDelim; ///< delimiter character |
187 | char m_cDelim; ///< delimiter character |
- | 188 | char m_cPrefix; ///< prefix character |
|
105 | std::string::size_type m_pos; ///< current position |
189 | std::string::size_type m_pos; ///< current position |
106 | std::string::size_type m_len; ///< string length |
190 | std::string::size_type m_len; ///< string length |
- | 191 | ||
- | 192 | /// Strips all prefix characters.
|
|
- | 193 | /**
|
|
- | 194 | * \param[in] s source string
|
|
- | 195 | * \param[in] cnt string length
|
|
- | 196 | * \return modified string
|
|
- | 197 | */
|
|
- | 198 | std::string StripPrefix(const char* s, SIZE cnt); |
|
- | 199 | ||
- | 200 | /// Extracts the next token (internal method).
|
|
- | 201 | /**
|
|
- | 202 | * The extracted token may be empty.
|
|
- | 203 | *
|
|
- | 204 | * \param[out] rToken extracted token
|
|
- | 205 | * \param[in] fStripPrefix strip prefix characters yes/no
|
|
- | 206 | */
|
|
- | 207 | void _GetNextToken(std::string& rToken, bool fStripPrefix); |
|
- | 208 | ||
- | 209 | /// Extracts the next token (internal method).
|
|
- | 210 | /**
|
|
- | 211 | * This method does no checking about the prefix character.
|
|
- | 212 | *
|
|
- | 213 | * The extracted token may be empty.
|
|
- | 214 | *
|
|
- | 215 | * \param[out] rToken extracted token
|
|
- | 216 | */
|
|
- | 217 | void _GetNextTokenNoPrefix(std::string& rToken); |
|
- | 218 | ||
- | 219 | /// Extracts the next token (internal method).
|
|
- | 220 | /**
|
|
- | 221 | * This method does checking about the prefix character.
|
|
- | 222 | *
|
|
- | 223 | * The extracted token may be empty.
|
|
- | 224 | *
|
|
- | 225 | * \param[out] rToken extracted token
|
|
- | 226 | */
|
|
- | 227 | void _GetNextTokenWithPrefix(std::string& rToken); |
|
107 | }; |
228 | }; |
108 | 229 | ||
109 | 230 | ||
110 | #endif //_STRTOK_H_
|
231 | #endif //_STRTOK_H_
|