Statistics
| Branch: | Revision:

root / src / common / linetokenizer.h @ e1750c09

History | View | Annotate | Download (1.96 KB)

1
//=========================================================================
2
//  LINETOKENIZER.H - part of
3
//                  OMNeT++/OMNEST
4
//           Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//=========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

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

    
17
#ifndef __LINETOKENIZER_H_
18
#define __LINETOKENIZER_H_
19

    
20
#include <vector>
21
#include <string>
22
#include "commondefs.h"
23

    
24
NAMESPACE_BEGIN
25

    
26
/**
27
 * Tokenizes a line. Uses space as default separator, and also honors quoted substrings
28
 * (that is, `one "two or three" four' is returned as 3 tokens `one',
29
 * `two or three', `four'). If a quote appears within a token, it needs to
30
 * be preceded by backslash. A backslash also needs to be quoted (ie.
31
 * duplicated) if it appears within a token.
32
 */
33
class COMMON_API LineTokenizer
34
{
35
  private:
36
    // separators
37
    char sep1;
38
    char sep2;
39

    
40
    int lineBufferSize;
41
    char *lineBuffer;
42

    
43
    // storage of tokens
44
    char **vec;
45
    int vecsize;
46
    int numtokens;
47

    
48
  public:
49
    /**
50
     * Constructor.
51
     */
52
    LineTokenizer(int lineBufferSize = 1000, int maxTokenNum=-1 /*=lineBufferSize/4*/, char sep1=' ', char sep2='\t');
53

    
54
    /**
55
     * Destructor.
56
     */
57
    ~LineTokenizer();
58

    
59
    /**
60
     * Tokenizes the line. Returns the number of tokens on the line.
61
     * Throws exception on error.
62
     */
63
    int tokenize(const char *line, int length);
64

    
65
    /**
66
     * Number of tokens read by the last tokenize() call.
67
     */
68
    int numTokens() {return numtokens;}
69

    
70
    /**
71
     * Array that holds the tokens read by the last tokenize() call;
72
     * contents will be overwritten with the next call.
73
     */
74
    char **tokens() {return vec;}
75
};
76

    
77
NAMESPACE_END
78

    
79

    
80
#endif
81