|
1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Variable and Label Storage/Retrieval |
|
15 // |
|
16 // |
|
17 |
|
18 /** |
|
19 @file Svarlab.cpp |
|
20 */ |
|
21 |
|
22 #include "SVARLAB.H" |
|
23 #include "ND_STD.H" |
|
24 #include "SSCRREAD.H" |
|
25 |
|
26 _LIT(KIntegerVariableString,"%d"); |
|
27 |
|
28 const TInt KLabelArrayGranularity=10; |
|
29 const TInt KVarArrayGranularity=20; |
|
30 const TInt KVarValueLength=15; |
|
31 |
|
32 // CScriptLabelMan definitions |
|
33 |
|
34 CScriptLabelMan* CScriptLabelMan::NewL() |
|
35 |
|
36 /** |
|
37 2 phased constructor for CScriptLabelMan, first phase. |
|
38 |
|
39 @exception Leaves if ConstructL() leaves, or not enough memory is available. |
|
40 @return a new CScriptLabelMan object. |
|
41 */ |
|
42 { |
|
43 CScriptLabelMan* m=new(ELeave) CScriptLabelMan(); |
|
44 CleanupStack::PushL(m); |
|
45 m->ConstructL(); |
|
46 CleanupStack::Pop(); |
|
47 return m; |
|
48 } |
|
49 |
|
50 CScriptLabelMan::CScriptLabelMan() |
|
51 /** |
|
52 Constructor for CScriptReader, used in the first phase of construction. |
|
53 */ |
|
54 {} |
|
55 |
|
56 void CScriptLabelMan::ConstructL() |
|
57 /** |
|
58 Instantiate member variables. |
|
59 */ |
|
60 { |
|
61 iLabelArray=new(ELeave) CArrayFixFlat<TLabel>(KLabelArrayGranularity); |
|
62 } |
|
63 |
|
64 CScriptLabelMan::~CScriptLabelMan() |
|
65 /** |
|
66 Destructor. |
|
67 Delete members. |
|
68 */ |
|
69 { |
|
70 DeleteAll(); |
|
71 delete iLabelArray; |
|
72 } |
|
73 |
|
74 void CScriptLabelMan::AddLabelL(const TDesC& aLabelName,const TLinePosition& aPosition) |
|
75 /** |
|
76 Add a label with name aLabelName and position aPosition to the array if it is |
|
77 not already in the array. |
|
78 |
|
79 @param aLabelName a reference to label name. |
|
80 @param aPosition a reference to script position. |
|
81 */ |
|
82 { |
|
83 if (aLabelName.Length()>KMaxLabelLength) |
|
84 User::Leave(KErrLabelNameTooLong); |
|
85 |
|
86 TLinePosition dummyPos; |
|
87 if (FindLabel(aLabelName,dummyPos)==KErrNotFound) |
|
88 { |
|
89 TLabel label; |
|
90 label.iName.CopyF(aLabelName); |
|
91 label.iPosition=aPosition; |
|
92 iLabelArray->AppendL(label); |
|
93 } |
|
94 else |
|
95 { |
|
96 // Encountered a label name that already exists - If it's |
|
97 // in EXACTLY the same place in the script as the existing |
|
98 // instance then it's actually the SAME label re-visited as |
|
99 // a result of executing a loop/branch operation, if it's in |
|
100 // a different place then it's an error in the script... |
|
101 if ((aPosition.iLineCount != dummyPos.iLineCount) || |
|
102 (aPosition.iOffset != dummyPos.iOffset)) |
|
103 { |
|
104 User::Leave(KErrAlreadyExists); |
|
105 } |
|
106 } |
|
107 } |
|
108 |
|
109 TInt CScriptLabelMan::FindLabel(const TDesC& aLabelName,TLinePosition& aPosition) |
|
110 /** |
|
111 Find label with name aLabelName and set aPos to position and aLine to line. |
|
112 |
|
113 @param aLabelName a reference to label name. |
|
114 @param aPosition a reference to script position. |
|
115 @return a KErrNone or KErrNotFound. |
|
116 */ |
|
117 { |
|
118 TInt index; |
|
119 for (index=0; index<iLabelArray->Count(); index++) |
|
120 { |
|
121 if((*iLabelArray)[index].iName.CompareF(aLabelName)==0) |
|
122 { |
|
123 aPosition=(*iLabelArray)[index].iPosition; |
|
124 return KErrNone; |
|
125 } |
|
126 } |
|
127 return KErrNotFound; |
|
128 } |
|
129 |
|
130 void CScriptLabelMan::DeleteAll() |
|
131 /** |
|
132 Delete all labels from the array |
|
133 */ |
|
134 { |
|
135 if (!iLabelArray) |
|
136 return; |
|
137 |
|
138 TInt count=iLabelArray->Count(); |
|
139 iLabelArray->Delete(0,count); |
|
140 } |
|
141 |
|
142 // CScriptVarMan definitions |
|
143 |
|
144 CScriptVarMan* CScriptVarMan::NewL() |
|
145 /** |
|
146 2 phased constructor for CScriptVarMan, first phase. |
|
147 |
|
148 @exception Leaves if ConstructL() leaves, or not enough memory is available. |
|
149 @return a new CScriptVarMan object. |
|
150 */ |
|
151 { |
|
152 CScriptVarMan* m=new(ELeave) CScriptVarMan(); |
|
153 CleanupStack::PushL(m); |
|
154 m->ConstructL(); |
|
155 CleanupStack::Pop(); |
|
156 return m; |
|
157 } |
|
158 |
|
159 CScriptVarMan::CScriptVarMan() |
|
160 /** |
|
161 Constructor for CScriptReader, used in the first phase of construction. |
|
162 */ |
|
163 {} |
|
164 |
|
165 void CScriptVarMan::ConstructL() |
|
166 /** |
|
167 Instantiate member variables. |
|
168 */ |
|
169 { |
|
170 iVarArray=new(ELeave) CArrayFixFlat<TVar>(KVarArrayGranularity); |
|
171 } |
|
172 |
|
173 CScriptVarMan::~CScriptVarMan() |
|
174 /** |
|
175 Destructor. |
|
176 Delete members. |
|
177 */ |
|
178 { |
|
179 DeleteAll(); |
|
180 delete iVarArray; |
|
181 } |
|
182 |
|
183 void CScriptVarMan::AddVariableL(const TDesC& aName,const TDesC& aContent) |
|
184 /** |
|
185 Add variable with name aName and aContent to the array |
|
186 |
|
187 @param aName a reference to variable name. |
|
188 @param aContent a reference to content. |
|
189 */ |
|
190 { |
|
191 if (aName.Length()>KMaxVarNameLength) |
|
192 User::Leave(KErrVariableNameTooLong); |
|
193 |
|
194 HBufC* contentCopy=HBufC::NewLC(aContent.Length()); |
|
195 contentCopy->Des().Copy(aContent); |
|
196 |
|
197 TPtrC dummy; |
|
198 if (FindVariable(aName,dummy)==KErrNotFound) |
|
199 { |
|
200 TVar var; |
|
201 var.iContent=contentCopy; |
|
202 var.iName.CopyF(aName); |
|
203 iVarArray->AppendL(var); |
|
204 } |
|
205 else |
|
206 { |
|
207 delete (*iVarArray)[iIndex].iContent; // FindVariable() will have set iIndex correctly |
|
208 (*iVarArray)[iIndex].iContent=contentCopy; |
|
209 } |
|
210 CleanupStack::Pop(); |
|
211 } |
|
212 |
|
213 void CScriptVarMan::AddVariableL(const TDesC& aName,TInt aVal) |
|
214 /** |
|
215 Add variable with name aName and value aVal to the array |
|
216 |
|
217 @param aName a reference to variable name. |
|
218 @param aVal is variable value. |
|
219 */ |
|
220 { |
|
221 TBuf<KVarValueLength> buf; |
|
222 buf.Format(KIntegerVariableString,aVal); |
|
223 AddVariableL(aName,buf); |
|
224 } |
|
225 |
|
226 TInt CScriptVarMan::FindVariable(const TDesC& aName,TPtrC& aContent) |
|
227 /** |
|
228 Find variable with name aName and put its value in aContent |
|
229 |
|
230 @param aName a reference to variable name. |
|
231 @param aContent a reference to content. |
|
232 @return a KErrNone or KErrNotFound. |
|
233 */ |
|
234 { |
|
235 for (iIndex=0; iIndex<iVarArray->Count(); iIndex++) |
|
236 { |
|
237 if ((*iVarArray)[iIndex].iName.CompareF(aName)==0) |
|
238 { |
|
239 aContent.Set((*iVarArray)[iIndex].iContent->Des()); |
|
240 return KErrNone; |
|
241 } |
|
242 } |
|
243 return KErrNotFound; |
|
244 } |
|
245 |
|
246 void CScriptVarMan::DeleteAll() |
|
247 /** |
|
248 Delete all variables from the array |
|
249 */ |
|
250 { |
|
251 if (!iVarArray) |
|
252 return; |
|
253 |
|
254 TInt i; |
|
255 for (i=0; i<iVarArray->Count(); i++) |
|
256 delete (*iVarArray)[i].iContent; |
|
257 iVarArray->Reset(); |
|
258 } |