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