Subversion Repositories public

Rev

Rev 47 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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