|
1 /* |
|
2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "StateMachine.h" |
|
20 |
|
21 // CStateMachine |
|
22 CStateMachine::CStateMachine(TInt aMaxNumberChars, TInt aMaxNumberStates) : iMaxNumberChars(static_cast<TInt>(aMaxNumberChars)), |
|
23 iMaxNumberStates(static_cast<TInt>(aMaxNumberStates)) |
|
24 { |
|
25 } |
|
26 |
|
27 CStateMachine* CStateMachine::NewL(TInt aMaxNumberChars, TInt aMaxNumberStates) |
|
28 { |
|
29 CStateMachine* s = NewLC(aMaxNumberChars, aMaxNumberStates); |
|
30 CleanupStack::Pop(); |
|
31 return s; |
|
32 } |
|
33 |
|
34 CStateMachine* CStateMachine::NewLC(TInt aMaxNumberChars, TInt aMaxNumberStates) |
|
35 { |
|
36 CStateMachine* s = new(ELeave)CStateMachine(aMaxNumberChars, aMaxNumberStates); |
|
37 CleanupStack::PushL(s); |
|
38 s->ConstructL(); |
|
39 return s; |
|
40 } |
|
41 |
|
42 CStateMachine::~CStateMachine() |
|
43 { |
|
44 for(TInt i = 0; i < iMaxNumberChars; ++i) |
|
45 delete iStateTable[i]; |
|
46 |
|
47 delete iStateTable; |
|
48 } |
|
49 |
|
50 void CStateMachine::ConstructL() |
|
51 { |
|
52 iStateTable = new TInt*[iMaxNumberChars]; |
|
53 |
|
54 for(TInt i = 0; i < iMaxNumberChars; ++i) |
|
55 { |
|
56 iStateTable[i] = new TInt[iMaxNumberStates]; |
|
57 for(TInt j = 0; j < iMaxNumberStates; j++) |
|
58 iStateTable[i][j] = 0; |
|
59 } |
|
60 } |
|
61 |
|
62 void CStateMachine::AddStateTransistionL(TChar aChar, TInt aState, TInt aNextState) |
|
63 { |
|
64 RArray<TInt> Dummy; |
|
65 TInt CharIndex = MapIndex(aChar, Dummy); |
|
66 Dummy.Close(); |
|
67 _LIT(KBufParsingError,"Parsing syntax error"); |
|
68 __ASSERT_ALWAYS( CharIndex >= 0 , User::Panic(KBufParsingError, KErrSyntaxError) ); |
|
69 AddStateTransistionL(CharIndex, aState, aNextState); |
|
70 } |
|
71 |
|
72 void CStateMachine::AddStateTransistionL(TInt aIndex, TInt aState, TInt aNextState) |
|
73 { |
|
74 if(aIndex > iMaxNumberChars || aState > iMaxNumberStates || aNextState > iMaxNumberStates) |
|
75 User::Leave(KErrGeneral); |
|
76 |
|
77 iStateTable[static_cast<TInt>(aIndex)][static_cast<TInt>(aState)] = static_cast<TInt>(aNextState); |
|
78 } |
|
79 |
|
80 TBool CStateMachine::Matches(const TDesC& aString) |
|
81 { |
|
82 // Walk the state table and return true if the string matches the pattern. |
|
83 |
|
84 TInt nTableState = KStateNoMatch; |
|
85 RArray<TInt> arrIndices; |
|
86 TBool bFound = EFalse; |
|
87 |
|
88 for(TInt nStringIndex = 0; nStringIndex < aString.Length(); ++nStringIndex) |
|
89 { |
|
90 arrIndices.Reset(); |
|
91 |
|
92 TChar charChar = aString[nStringIndex]; |
|
93 TInt nTableIndex = MapIndex(charChar, arrIndices); |
|
94 |
|
95 if(nTableIndex == -1) |
|
96 return EFalse; |
|
97 |
|
98 TInt nCount = arrIndices.Count(); |
|
99 |
|
100 if(nCount) |
|
101 { |
|
102 for(TInt i = 0; i < nCount; i++) |
|
103 { |
|
104 nTableIndex = arrIndices[i]; |
|
105 TInt NewTableState = iStateTable[nTableIndex][nTableState]; |
|
106 |
|
107 if( NewTableState != KStateNoMatch || |
|
108 (NewTableState == KStateNoMatch && i == (arrIndices.Count() - 1)) || |
|
109 NewTableState == KStateMatched) |
|
110 { |
|
111 nTableState = NewTableState; |
|
112 break; |
|
113 } |
|
114 } |
|
115 } |
|
116 else |
|
117 { |
|
118 nTableState = iStateTable[nTableIndex][nTableState]; |
|
119 } |
|
120 |
|
121 if(nTableState == KStateNoMatch) |
|
122 break; |
|
123 |
|
124 if(nTableState == KStateMatched) |
|
125 { |
|
126 bFound = ETrue; |
|
127 break; |
|
128 } |
|
129 } |
|
130 |
|
131 arrIndices.Close(); |
|
132 |
|
133 return bFound; |
|
134 } |
|
135 |
|
136 TInt CStateMachine::MapIndex(TChar aChar, RArray<TInt>& aIndices) |
|
137 { |
|
138 // Check the type of aChar and return the relevant index or indicies |
|
139 if(aChar.IsDigit()) |
|
140 { |
|
141 TInt nIndex = static_cast<TInt>(aChar.GetNumericValue()); |
|
142 |
|
143 aIndices.Append(nIndex); |
|
144 aIndices.Append(KCharacterDot); |
|
145 |
|
146 return nIndex; |
|
147 } |
|
148 |
|
149 if(aChar == '+') return KCharacterPlus; |
|
150 if(aChar == '.') return KCharacterDot; |
|
151 |
|
152 return -1; // TO DO : define |
|
153 } |
|
154 |
|
155 void CStateMachine::GetWildcardVersionOfPattern( |
|
156 TText aWildcardChar, |
|
157 TDes& aWildcardedPattern ) const |
|
158 { |
|
159 aWildcardedPattern.SetLength(0); |
|
160 TInt maxLength = aWildcardedPattern.MaxLength(); |
|
161 // There is a column in the StateTable for each character in the regular expression. The first character |
|
162 // of the regexp in column [0], last in column [Length()-1] |
|
163 // The non-zero values found within a column of the StateTable represent the characters |
|
164 // that are valid at that position in a candidate match. |
|
165 // An example pattern is calculated by examining the StateTable. |
|
166 // For each column, count the number of matching ( i.e. not KStateNoMatch) states |
|
167 // If only one, then place that character in the example pattern. |
|
168 // If more than one, put the wildcard in. |
|
169 for(TInt stateIndex = 0; stateIndex < iMaxNumberStates && stateIndex < maxLength; stateIndex++) |
|
170 { |
|
171 TInt matches = 0; |
|
172 TInt matchedIndex = -1; |
|
173 for (TInt charIndex = 0; charIndex < iMaxNumberChars; charIndex++ ) |
|
174 { |
|
175 TInt nextState = iStateTable[charIndex][stateIndex]; |
|
176 |
|
177 if ( nextState != KStateNoMatch ) |
|
178 { |
|
179 matches++; |
|
180 matchedIndex = charIndex; |
|
181 if (matches > 1 ) |
|
182 break; |
|
183 } |
|
184 } |
|
185 if ( matches == 0 ) // Have found an empty column. Unused part of state machine. Stop filling |
|
186 { |
|
187 break; |
|
188 } |
|
189 else if ( matches > 1 ) |
|
190 aWildcardedPattern.Append(aWildcardChar); |
|
191 else |
|
192 { |
|
193 if ( 0 <= matchedIndex && matchedIndex <= 9 ) // Must be a numeric digit |
|
194 { |
|
195 aWildcardedPattern.Append((TText)(matchedIndex+'0')); |
|
196 } |
|
197 else if ( matchedIndex == KCharacterDot ) |
|
198 { |
|
199 aWildcardedPattern.Append(aWildcardChar); |
|
200 } |
|
201 else if ( matchedIndex == KCharacterPlus ) |
|
202 { |
|
203 aWildcardedPattern.Append('+'); |
|
204 } |
|
205 else |
|
206 { |
|
207 aWildcardedPattern.Append(aWildcardChar); |
|
208 } |
|
209 } |
|
210 } |
|
211 |
|
212 } |
|
213 // End of File |
|
214 |