FACT++
1.0
Main Page
Related Pages
Namespaces
Classes
Files
Examples
File List
File Members
tokenstring.cxx
Go to the documentation of this file.
1
#define DIMLIB
2
#include "
tokenstring.hxx
"
3
4
TokenString::TokenString
(
char
*
str
)
5
{
6
token_buff
=
new
char
[((int)strlen(str)+1)*2];
7
token_ptr
=
token_buff
;
8
token_seps
= 0;
9
store_str
(str);
10
token_ptr =
token_buff
;
11
curr_token_ptr
=
token_ptr
;
12
}
13
14
TokenString::TokenString
(
char
*
str
,
char
*seps)
15
{
16
token_buff
=
new
char
[((int)strlen(str)+1)*2];
17
token_ptr
=
token_buff
;
18
token_seps
=
new
char
[((int)strlen(seps)+1)];
19
strcpy(
token_seps
,seps);
20
store_str
(str);
21
token_ptr =
token_buff
;
22
curr_token_ptr
=
token_ptr
;
23
}
24
25
TokenString::~TokenString
()
26
{
27
if
(
token_buff
)
28
{
29
delete
[]
token_buff
;
30
token_buff
= 0;
31
}
32
if
(
token_seps
)
33
{
34
delete
[]
token_seps
;
35
token_seps
= 0;
36
}
37
}
38
39
void
TokenString::store_str
(
char
*
str
)
40
{
41
int
i
, in_tok = 0;
42
int
sep = 0;
43
44
n_tokens
= 0;
45
if
(!
token_seps
)
46
{
47
while
(*str)
48
{
49
if
( (*str ==
'@'
) || (*str ==
'|'
) || (*str ==
'/'
) ||
50
(*str ==
'='
) || (*str ==
'('
) || (*str ==
')'
) ||
51
(*str ==
'.'
) || (*str ==
'\n'
))
52
{
53
if
(in_tok)
54
{
55
*
token_ptr
++ =
'\0'
;
56
n_tokens
++;
57
}
58
*
token_ptr
++ = *str++;
59
*
token_ptr
++ =
'\0'
;
60
n_tokens
++;
61
in_tok = 0;
62
}
63
else
if
(*str ==
'"'
)
64
{
65
if
(in_tok)
66
{
67
*
token_ptr
++ =
'\0'
;
68
n_tokens
++;
69
}
70
*
token_ptr
++ = *str++;
71
while
(*str !=
'"'
)
72
{
73
*
token_ptr
++ = *str++;
74
}
75
*
token_ptr
++ = *str++;
76
*
token_ptr
++ =
'\0'
;
77
n_tokens
++;
78
in_tok = 0;
79
}
80
else
if
(*str ==
':'
)
81
{
82
if
(*(str+1) ==
':'
)
83
{
84
if
(in_tok)
85
{
86
*
token_ptr
++ =
'\0'
;
87
n_tokens
++;
88
}
89
*
token_ptr
++ = *str++;
90
*
token_ptr
++ = *str++;
91
*
token_ptr
++ =
'\0'
;
92
n_tokens
++;
93
in_tok = 0;
94
}
95
else
96
{
97
*
token_ptr
++ = *str++;
98
in_tok = 1;
99
}
100
}
101
else
102
{
103
*
token_ptr
++ = *str++;
104
in_tok = 1;
105
}
106
}
107
}
108
else
109
{
110
while
(*str)
111
{
112
sep = 0;
113
for
(i = 0; i < (int)strlen(
token_seps
); i++)
114
{
115
if
(*str ==
token_seps
[i])
116
{
117
if
(in_tok)
118
{
119
*
token_ptr
++ =
'\0'
;
120
n_tokens
++;
121
}
122
*
token_ptr
++ = *str++;
123
*
token_ptr
++ =
'\0'
;
124
sep = 1;
125
in_tok = 0;
126
n_tokens
++;
127
break
;
128
}
129
}
130
if
(!sep)
131
{
132
*
token_ptr
++ = *str++;
133
in_tok = 1;
134
}
135
}
136
}
137
if
(in_tok)
138
{
139
*
token_ptr
++ =
'\0'
;
140
n_tokens
++;
141
}
142
*
token_ptr
++ =
'\0'
;
143
}
144
145
int
TokenString::getToken
(
char
*&token)
146
{
147
148
if
(!*
token_ptr
)
149
{
150
token_ptr
=
token_buff
;
151
curr_token_ptr
=
token_ptr
;
152
return
(0);
153
}
154
155
curr_token_ptr
=
token_ptr
;
156
token_ptr
+= (int)strlen(
curr_token_ptr
)+1;
157
token =
curr_token_ptr
;
158
159
return
(1);
160
}
161
162
void
TokenString::pushToken
()
163
{
164
push_token_ptr
=
token_ptr
;
165
}
166
167
void
TokenString::popToken
()
168
{
169
token_ptr
=
push_token_ptr
;
170
}
171
172
int
TokenString::cmpToken
(
char
*
str
)
173
{
174
if
(!strcmp(
curr_token_ptr
, str))
175
return
(1);
176
return
(0);
177
}
178
179
int
TokenString::firstToken
()
180
{
181
if
(
curr_token_ptr
==
token_buff
)
182
return
(1);
183
return
(0);
184
}
185
186
int
TokenString::getNTokens
()
187
{
188
return
n_tokens
;
189
}
190
191
int
TokenString::getNTokens
(
char
*
str
)
192
{
193
int
n = 0;
194
char
*token;
195
196
while
(
getToken
(token))
197
{
198
if
(!strcmp(token,str))
199
n++;
200
}
201
return
n;
202
}
203
/*
204
main(int argc, char *argv[])
205
{
206
TokenString *ts;
207
char *token;
208
209
ts = new TokenString(argv[1],"/)");
210
cout << "n = " << ts->getNTokens() << "\n";
211
cout.flush();
212
while(ts->getToken(token))
213
{
214
cout << token << "\n";
215
cout.flush();
216
}
217
}
218
*/
TokenString::curr_token_ptr
char * curr_token_ptr
Definition:
tokenstring.hxx:25
tokenstring.hxx
TokenString::getNTokens
int getNTokens()
Definition:
tokenstring.cxx:186
TokenString::push_token_ptr
char * push_token_ptr
Definition:
tokenstring.hxx:26
TokenString::cmpToken
int cmpToken(char *str)
Definition:
tokenstring.cxx:172
i
int i
Definition:
db_dim_client.c:21
str
char str[80]
Definition:
test_client.c:7
TokenString::token_ptr
char * token_ptr
Definition:
tokenstring.hxx:24
TokenString::getToken
int getToken(char *&token)
Definition:
tokenstring.cxx:145
TokenString::~TokenString
~TokenString()
Definition:
tokenstring.cxx:25
TokenString::TokenString
TokenString(char *str)
Definition:
tokenstring.cxx:4
TokenString::firstToken
int firstToken()
Definition:
tokenstring.cxx:179
TokenString::store_str
void store_str(char *str)
Definition:
tokenstring.cxx:39
TokenString::popToken
void popToken()
Definition:
tokenstring.cxx:167
TokenString::n_tokens
int n_tokens
Definition:
tokenstring.hxx:28
TokenString::pushToken
void pushToken()
Definition:
tokenstring.cxx:162
TokenString::token_seps
char * token_seps
Definition:
tokenstring.hxx:27
TokenString::token_buff
char * token_buff
Definition:
tokenstring.hxx:23
dim
src
tokenstring.cxx
Generated on Sun Sep 18 2016 20:50:18 for FACT++ by
1.8.11