source: trunk/FACT++/dim/src/tokenstring.cxx@ 18275

Last change on this file since 18275 was 15282, checked in by tbretz, 12 years ago
Updated to v20r7.
File size: 3.2 KB
Line 
1#define DIMLIB
2#include "tokenstring.hxx"
3
4TokenString::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
14TokenString::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
25TokenString::~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
39void TokenString::store_str(char *str)
40{
41int i, in_tok = 0;
42int 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
145int 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
162void TokenString::pushToken()
163{
164 push_token_ptr = token_ptr;
165}
166
167void TokenString::popToken()
168{
169 token_ptr = push_token_ptr;
170}
171
172int TokenString::cmpToken(char *str)
173{
174 if(!strcmp(curr_token_ptr, str))
175 return(1);
176 return(0);
177}
178
179int TokenString::firstToken()
180{
181 if(curr_token_ptr == token_buff)
182 return(1);
183 return(0);
184}
185
186int TokenString::getNTokens()
187{
188 return n_tokens;
189}
190
191int 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/*
204main(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*/
Note: See TracBrowser for help on using the repository browser.