1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
//========= Copyright Valve Corporation, All rights reserved. ============//
/*
** The copyright to the contents herein is the property of Valve, L.L.C.
** The contents may be used and/or copied only with the written permission of
** Valve, L.L.C., or in accordance with the terms and conditions stipulated in
** the agreement/contract under which the contents have been supplied.
**
*******************************************************************************
**
** Contents:
**
** TokenLine.cpp: implementation of the TokenLine class.
**
******************************************************************************/
#include "TokenLine.h"
#include <string.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
TokenLine::~TokenLine()
{
}
bool TokenLine::SetLine(const char * newLine)
{
m_tokenNumber = 0;
if (!newLine || ( strlen(newLine) >= (MAX_LINE_CHARS-1) ) )
{
memset( m_fullLine, 0, MAX_LINE_CHARS );
memset( m_tokenBuffer, 0, MAX_LINE_CHARS );
return false;
}
strncpy( m_fullLine, newLine, MAX_LINE_CHARS-1 );
m_fullLine[ MAX_LINE_CHARS-1 ] = '\0';
strncpy( m_tokenBuffer, newLine, MAX_LINE_CHARS-1 );
m_tokenBuffer[ MAX_LINE_CHARS-1 ] = '\0';
// parse tokens
char * charPointer = m_tokenBuffer;
while (*charPointer && (m_tokenNumber < MAX_LINE_TOKENS))
{
while (*charPointer && ((*charPointer <= 32) || (*charPointer > 126)))
charPointer++;
if (*charPointer)
{
m_token[m_tokenNumber] = charPointer;
// special treatment for quotes
if (*charPointer == '\"')
{
charPointer++;
m_token[m_tokenNumber] = charPointer;
while (*charPointer && (*charPointer != '\"') )
charPointer++;
}
else
{
m_token[m_tokenNumber] = charPointer;
while (*charPointer && ((*charPointer > 32) && (*charPointer <= 126)))
charPointer++;
}
m_tokenNumber++;
if (*charPointer)
{
*charPointer=0;
charPointer++;
}
}
}
return (m_tokenNumber != MAX_LINE_TOKENS);
}
char * TokenLine::GetLine()
{
return m_fullLine;
}
char * TokenLine::GetToken(int i)
{
if (i >= m_tokenNumber)
return NULL;
return m_token[i];
}
// if the given parm is not present return NULL
// otherwise return the address of the following token, or an empty string
char* TokenLine::CheckToken(char * parm)
{
for (int i = 0 ; i < m_tokenNumber; i ++)
{
if (!m_token[i])
continue;
if ( !strcmp (parm,m_token[i]) )
{
char * ret = m_token[i+1];
// if this token doesn't exist, since index i was the last
// return an empty string
if ( (i+1) == m_tokenNumber ) ret = "";
return ret;
}
}
return NULL;
}
int TokenLine::CountToken()
{
int c = 0;
for (int i = 0 ; i < m_tokenNumber; i ++)
{
if (m_token[i])
c++;
}
return c;
}
char* TokenLine::GetRestOfLine(int i)
{
if (i >= m_tokenNumber)
return NULL;
return m_fullLine + (m_token[i] - m_tokenBuffer);
}
TokenLine::TokenLine(char * string)
{
SetLine(string);
}
TokenLine::TokenLine()
{
}
|