aboutsummaryrefslogtreecommitdiff
path: root/src/old/tokenstring.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/old/tokenstring.cpp')
-rw-r--r--src/old/tokenstring.cpp163
1 files changed, 163 insertions, 0 deletions
diff --git a/src/old/tokenstring.cpp b/src/old/tokenstring.cpp
new file mode 100644
index 0000000..e57ba69
--- /dev/null
+++ b/src/old/tokenstring.cpp
@@ -0,0 +1,163 @@
1#include "tokenstring.h"
2#include <string.h>
3
4TokenString::TokenString( const char *lpNewTokenString )
5{
6 lpTokenString = NULL;
7 if( lpNewTokenString )
8 {
9 parseLine( lpNewTokenString );
10 }
11}
12
13TokenString::~TokenString()
14{
15 delete[] lpTokenString;
16 for( int j = 0; j < lToken.getSize(); j++ )
17 {
18 delete[] (((Token *)lToken[j])->lpToken);
19 delete ((Token *)lToken[j]);
20 }
21}
22
23void TokenString::parseLine( const char *lpNewTokenString )
24{
25 if( lpTokenString != NULL )
26 {
27 delete[] lpTokenString;
28 lpTokenString = NULL;
29 for( int j = 0; j < lToken.getSize(); j++ )
30 {
31 delete[] (((Token *)lToken[j])->lpToken);
32 delete ((Token *)lToken[j]);
33 }
34 lToken.empty();
35 }
36 if( lpNewTokenString == NULL )
37 {
38 lpTokenString = new char[1];
39 lpTokenString[0] = '\0';
40 lToken.setSize(0);
41 return;
42 }
43 // First order of business, make an internal copy so someone can get it
44 // if they want to.
45 int nLen = strlen(lpNewTokenString);
46 lpTokenString = new char[nLen+1];
47 strcpy( lpTokenString, lpNewTokenString );
48
49 // Now we do a preliminary parse. This could be effected by later
50 // editing and aliasing, but we'll see...
51 int nTkStart, nTkEnd;
52 int mode=0; // 0 = startSearch, 1=endSearch
53 for( int j = 0; j <= nLen; j++ )
54 {
55 if( mode == 0 )
56 {
57 if( lpTokenString[j] != ' ' &&
58 lpTokenString[j] != '\t' )
59 {
60 nTkStart = j;
61 mode = 1;
62 }
63 }
64 else
65 {
66 if( lpTokenString[j] == ' ' ||
67 lpTokenString[j] == '\t' ||
68 lpTokenString[j] == '\0' )
69 {
70 nTkEnd = j-1;
71 mode = 0;
72
73 appendToken( nTkStart, nTkEnd );
74 }
75 }
76 }
77}
78
79void TokenString::appendToken( int nStart, int nEnd )
80{
81 Token *pToken = new Token;
82 pToken->lpOrig = &lpTokenString[nStart];
83
84 // nStart and nEnd are inclusive, we must add two for the end, and the null
85 pToken->lpToken = new char[nEnd-nStart+2];
86 memcpy( pToken->lpToken, &lpTokenString[nStart], nEnd-nStart+1 );
87 pToken->lpToken[nEnd-nStart+1] = '\0';
88
89// printf("%s\n", pToken->lpToken );
90 lToken.append( pToken );
91}
92
93void TokenString::insertToken( int nStart, int nEnd, char *lpOldOrig, const char *lpNewToken, int nIndex )
94{
95 Token *pToken = new Token;
96 pToken->lpOrig = lpOldOrig;
97
98 // nStart and nEnd are inclusive, we must add two for the end, and the null
99 pToken->lpToken = new char[nEnd-nStart+2];
100 memcpy( pToken->lpToken, &lpNewToken[nStart], nEnd-nStart+1 );
101 pToken->lpToken[nEnd-nStart+1] = '\0';
102
103 lToken.insertBefore( pToken, nIndex );
104}
105
106int TokenString::getNumTokens()
107{
108 return lToken.getSize();
109}
110
111char *TokenString::getToken( int nIndex )
112{
113 if( nIndex >= lToken.getSize() ) return NULL;
114 return (char *)(((Token *)lToken[nIndex])->lpToken);
115}
116
117char *TokenString::getTokenString( int nIndex )
118{
119 if( nIndex >= lToken.getSize() ) return NULL;
120 return (char *)(((Token *)lToken[nIndex])->lpOrig);
121}
122
123void TokenString::expandTokenTo( int nIndex, char *lpNewToken )
124{
125 // First, we delete the token at nIndex, then we keep inserting
126 // at that position...
127 // We also have to remember the index to the original string,
128 // since most of what we're expanding to won't be in the origingal
129 // we need to keep these indexes updated in order to make other parts
130 // of the system happy.
131 char *lpOldOrig = ((Token *)lToken[nIndex])->lpOrig;
132 delete[] ((Token *)lToken[nIndex])->lpToken;
133 delete ((Token *)lToken[nIndex]);
134 lToken.deleteAt( nIndex );
135
136 // We'll do this just like we did above, but instead we'll
137 // do tricky things when we find tokens...
138 int nLen = strlen(lpNewToken);
139 int nTkStart, nTkEnd, nNewIndex=nIndex;
140 int mode=0; // 0 = startSearch, 1=endSearch
141 for( int j = 0; j <= nLen; j++ )
142 {
143 if( mode == 0 )
144 {
145 if( lpNewToken[j] != ' ' && lpNewToken[j] != '\t' )
146 {
147 nTkStart = j;
148 mode = 1;
149 }
150 }
151 else
152 {
153 if( lpNewToken[j] == ' ' || lpNewToken[j] == '\t' || lpNewToken[j] == '\0' )
154 {
155 nTkEnd = j-1;
156 mode = 0;
157
158 insertToken( nTkStart, nTkEnd, lpOldOrig, lpNewToken, nNewIndex );
159 nNewIndex++;
160 }
161 }
162 }
163}