Project

General

Profile

Statistics
| Branch: | Revision:

root / include / cstringtokenizer.h @ fbe00e73

History | View | Annotate | Download (3.77 KB)

1
//==========================================================================
2
//  CSTRINGTOKENIZER.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//
7
//  Declaration of the following classes:
8
//    cStringTokenizer  : string tokenizer utility class
9
//
10
//==========================================================================
11

    
12
/*--------------------------------------------------------------*
13
  Copyright (C) 1992-2008 Andras Varga
14
  Copyright (C) 2006-2008 OpenSim Ltd.
15

16
  This file is distributed WITHOUT ANY WARRANTY. See the file
17
  `license' for details on this and other legal matters.
18
*--------------------------------------------------------------*/
19

    
20
#ifndef __CSTRINGTOKENIZER_H
21
#define __CSTRINGTOKENIZER_H
22

    
23
#include <string>
24
#include <vector>
25
#include "simkerneldefs.h"
26

    
27
NAMESPACE_BEGIN
28

    
29
/**
30
 * String tokenizer class, modelled after strtok(). It considers the input
31
 * string to consist of tokens, separated by one or more delimiter characters.
32
 * Repeated calls to nextToken() will enumerate the tokens in the string,
33
 * returning NULL after the last token. The function hasMoreTokens() can be
34
 * used to find out whether there are more tokens without consuming one.
35
 *
36
 * Limitations: this class does not honor quotes, apostrophes or backslash
37
 * quoting; nor does it return empty tokens if it encounters multiple
38
 * delimiter characters in a row (so setting the delimiter to "," does not
39
 * produce the desired results). This behaviour is consistent with strtok().
40
 *
41
 * Example 1:
42
 *
43
 * <pre>
44
 * const char *str = "one two three four";
45
 * cStringTokenizer tokenizer(str);
46
 * while (tokenizer.hasMoreTokens())
47
 *     ev << " [" << tokenizer.nextToken() << "]";
48
 * </pre>
49
 *
50
 * Example 2:
51
 *
52
 * <pre>
53
 * const char *str = "42 13 46 72 41";
54
 * std::vector\<int\> array = cStringTokenizer(str).asIntVector();
55
 * </pre>
56
 *
57
 * @ingroup SimSupport
58
 */
59
class SIM_API cStringTokenizer
60
{
61
  private:
62
    char *str; // copy of full string
63
    char *rest; // rest of string (to be tokenized)
64
    std::string delimiter;
65

    
66
  public:
67
    /**
68
     * Constructor. The class will make its own copy of the input string
69
     * and of the delimiters string. The delimiters default to all whitespace
70
     * characters (space, tab, CR, LF, FF).
71
     */
72
    cStringTokenizer(const char *str, const char *delimiters=NULL);
73

    
74
    /**
75
     * Copy constructor. It copies the current state of the tokenizer
76
     * (i.e. does not re-position it to the first token.)
77
     */
78
    cStringTokenizer(const cStringTokenizer& other);
79

    
80
    /**
81
     * Destructor.
82
     */
83
    ~cStringTokenizer();
84

    
85
    /**
86
     * Assignment operator. It copies the current state of the tokenizer
87
     * (i.e. does not re-position it to the first token.)
88
     */
89
    cStringTokenizer& operator=(const cStringTokenizer& other);
90

    
91
    /**
92
     * Change delimiters. This allows for switching delimiters during
93
     * tokenization.
94
     */
95
    void setDelimiter(const char *s);
96

    
97
    /**
98
     * Returns true if there're more tokens (i.e. the next nextToken()
99
     * call won't return NULL).
100
     */
101
    bool hasMoreTokens();
102

    
103
    /**
104
     * Returns the next token. The returned pointers will stay valid as long
105
     * as the tokenizer object exists. If there're no more tokens,
106
     * a NULL pointer will be returned.
107
     */
108
    const char *nextToken();
109

    
110
    /**
111
     * Utility function: tokenizes the full input string at once, and
112
     * returns the tokens in a string vector.
113
     */
114
    std::vector<std::string> asVector();
115

    
116
    /**
117
     * Utility function: converts all tokens to int, and returns them
118
     * in a vector.
119
     */
120
    std::vector<int> asIntVector();
121

    
122
    /**
123
     * Utility function: converts all tokens to double, and returns them
124
     * in a vector.
125
     */
126
    std::vector<double> asDoubleVector();
127
};
128

    
129
NAMESPACE_END
130

    
131

    
132
#endif
133