Rev 45 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
45 | luk | 1 | |
2 | /// string tokenizer header |
||
3 | /** |
||
4 | * \file strtok.h |
||
5 | * |
||
6 | * string tokenizer |
||
7 | * |
||
8 | * Copyright (C) 2006 Lukas Jelinek, <lukas@aiken.cz> |
||
9 | * |
||
10 | * This program is free software; you can redistribute it and/or |
||
11 | * modify it under the terms of one of the following licenses: |
||
12 | * |
||
13 | * \li 1. X11-style license (see LICENSE-X11) |
||
14 | * \li 2. GNU Lesser General Public License, version 2.1 (see LICENSE-LGPL) |
||
15 | * \li 3. GNU General Public License, version 2 (see LICENSE-GPL) |
||
16 | * |
||
17 | * If you want to help with choosing the best license for you, |
||
18 | * please visit http://www.gnu.org/licenses/license-list.html. |
||
19 | * |
||
20 | */ |
||
21 | |||
22 | |||
23 | #ifndef _STRTOK_H_ |
||
24 | #define _STRTOK_H_ |
||
25 | |||
26 | |||
27 | #include <string> |
||
28 | |||
47 | luk | 29 | /// Simple string tokenizer class. |
30 | /** |
||
31 | * This class implements a string tokenizer. It splits a string |
||
32 | * by a character to a number of elements (tokens) which are |
||
33 | * provided sequentially. |
||
34 | * |
||
35 | * All operations are made on the original string itself. |
||
36 | * The implementation is not ready to handle any changes of the |
||
37 | * string. |
||
38 | * |
||
39 | * The original string is left unchanged. All tokens are returned |
||
40 | * as newly created strings. |
||
41 | */ |
||
45 | luk | 42 | class StringTokenizer |
43 | { |
||
44 | public: |
||
47 | luk | 45 | /// Constructor. |
46 | /** |
||
47 | * Creates a ready-to-use tokenizer. |
||
48 | * |
||
49 | * \param[in] rStr string for tokenizing |
||
50 | * \param[in] cDelim delimiter (separator) character |
||
51 | */ |
||
45 | luk | 52 | StringTokenizer(const std::string& rStr, char cDelim = ','); |
53 | |||
47 | luk | 54 | /// Destructor. |
45 | luk | 55 | ~StringTokenizer() {} |
56 | |||
47 | luk | 57 | /// Checks whether the tokenizer can provide more tokens. |
58 | /** |
||
59 | * \return true = more tokens available, false = otherwise |
||
60 | */ |
||
45 | luk | 61 | inline bool HasMoreTokens() const |
62 | { |
||
63 | return m_pos < m_len; |
||
64 | } |
||
65 | |||
47 | luk | 66 | /// Returns the next token. |
67 | /** |
||
68 | * \return next token or "" if no more tokens available |
||
69 | */ |
||
45 | luk | 70 | std::string GetNextToken(); |
71 | |||
47 | luk | 72 | /// Sets a delimiter (separator) character. |
73 | /** |
||
74 | * The new delimiter has effect only to tokens returned later; |
||
75 | * the position in the string is not affected. |
||
76 | * |
||
77 | * \param[in] cDelim delimiter character |
||
78 | */ |
||
45 | luk | 79 | inline void SetDelimiter(char cDelim) |
80 | { |
||
81 | m_cDelim = cDelim; |
||
82 | } |
||
83 | |||
47 | luk | 84 | /// Returns the delimiter (separator) character. |
85 | /** |
||
86 | * \return delimiter character |
||
87 | */ |
||
45 | luk | 88 | inline char GetDelimiter() const |
89 | { |
||
90 | return m_cDelim; |
||
91 | } |
||
92 | |||
47 | luk | 93 | /// Resets the tokenizer. |
94 | /** |
||
95 | * Re-initializes tokenizing to the start of the string. |
||
96 | */ |
||
45 | luk | 97 | inline void Reset() |
98 | { |
||
99 | m_pos = 0; |
||
100 | } |
||
101 | |||
102 | private: |
||
47 | luk | 103 | std::string m_str; ///< tokenized string |
104 | char m_cDelim; ///< delimiter character |
||
105 | std::string::size_type m_pos; ///< current position |
||
106 | std::string::size_type m_len; ///< string length |
||
45 | luk | 107 | }; |
108 | |||
109 | |||
110 | #endif //_STRTOK_H_ |