Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
212 dhylands 1
/****************************************************************************
2
*
3
*     Copyright (c) 2003 Dave Hylands
4
*           All Rights Reserved
5
*
6
*   Permission is granted to any individual or institution to use, copy, or
7
*   redistribute this software so long as it is not sold for profit, and that
8
*   this copyright notice is retained.
9
*
10
****************************************************************************/
11
/**
12
*
13
*   @file   StrToken.cpp
14
*
15
*   @brief  Implements the string tokenizer
16
*
17
****************************************************************************/
18
 
19
// ---- Include Files -------------------------------------------------------
20
 
21
#include "StrToken.h"
22
 
23
#include <string.h>
24
 
25
// ---- Public Variables ----------------------------------------------------
26
// ---- Private Constants and Types -----------------------------------------
27
// ---- Private Variables ---------------------------------------------------
28
// ---- Private Function Prototypes -----------------------------------------
29
 
30
// ---- Functions -----------------------------------------------------------
31
 
32
/**
33
 * @addtogroup Str
34
 * @{
35
 */
36
 
37
//***************************************************************************
38
/**
39
*   Constructor
40
*/
41
 
42
StrTokenizer::StrTokenizer()
43
    : m_str( NULL ), m_outToken( NULL ), m_maxLen( 0 )
44
 
45
{
46
}
47
 
48
//***************************************************************************
49
/**
50
*   Constructor which specifies string to be tokenized, along with location
51
*   to store tokens as they're parsed.
52
*/
53
 
54
StrTokenizer::StrTokenizer
55
(
56
    const char *str,    ///< String to be parsed.
57
    char *outToken,     ///< Place to store the result.
58
    size_t maxLen       ///< Maximum lenght of @a outToken.
59
)
60
    : m_str( str ), m_outToken( outToken ), m_maxLen( maxLen )
61
{
62
}
63
 
64
//***************************************************************************
65
/**
66
*   Destructor
67
*/
68
 
69
StrTokenizer::~StrTokenizer()
70
{
71
    // Nothing to do.
72
}
73
 
74
//***************************************************************************
75
/**
76
*  Initializes the string tokenizer object.
77
*/
78
 
79
void StrTokenizer::Init
80
(
81
    const char *str,    ///< String to be parsed.
82
    char *outToken,     ///< Place to store the result.
83
    size_t maxLen       ///< Maximum lenght of @a outToken.
84
)
85
{
86
    m_str       = str;
87
    m_outToken  = outToken;
88
    m_maxLen    = maxLen;
89
 
90
} // Init
91
 
92
//***************************************************************************
93
/**
94
*   Returns the next token from the previously stored string.
95
*
96
*   @returns    A pointer to the next parsed token, or NULL if not more
97
*               tokens could be found.
98
*/
99
char *StrTokenizer::NextToken
100
(
101
    const char *delim   ///< Set of delimiter characters which can terminate the token.
102
)
103
{
104
    // Skip over delimiter characters
105
 
106
    while (( *m_str != '\0' ) && ( strchr( delim, *m_str ) != NULL ))
107
    {
108
        m_str++;
109
    }
110
 
111
    if ( *m_str == '\0' )
112
    {
113
        // We've run out of data - no more tokens
114
 
115
        return NULL;
116
    }
117
 
118
    // Copy characters until we fill up the token, we run out, or hit a 
119
    // delimiter.
120
 
121
    size_t i;
122
 
123
    for ( i = 0; i < ( m_maxLen - 1 ); i++ )
124
    {
125
        if ( *m_str == '\0' )
126
        {
127
            break;
128
        }
129
        if ( strchr( delim, *m_str ) == NULL )
130
        {
131
            m_outToken[ i ] = *m_str++;
132
        }
133
        else
134
        {
135
            // Delimiter found
136
 
137
            m_str++;
138
            break;
139
        }
140
    }
141
    m_outToken[ i ] = '\0';
142
 
143
    return m_outToken;
144
 
145
} // NextToken
146
 
147
/** @} */
148