|
1 /* |
|
2 * Copyright (c) 2005-2009 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: CStringList implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 |
|
21 #include "stringlist.h" |
|
22 #include "capability.h" |
|
23 #include "debug.h" |
|
24 |
|
25 const TInt KMaxStringlistSize( 1000 ); |
|
26 |
|
27 // ============================= MEMBER FUNCTIONS =============================== |
|
28 |
|
29 // ----------------------------------------------------------------------------- |
|
30 // CStringList::NewL() |
|
31 // Two-phase constructor. |
|
32 // ----------------------------------------------------------------------------- |
|
33 // |
|
34 CStringList* CStringList::NewL() |
|
35 { |
|
36 TRACE_FUNC_ENTRY; |
|
37 CStringList* self = CStringList::NewLC(); |
|
38 CleanupStack::Pop( self ); |
|
39 TRACE_FUNC_EXIT; |
|
40 return self; |
|
41 } |
|
42 |
|
43 // ----------------------------------------------------------------------------- |
|
44 // CStringList::NewLC() |
|
45 // Two-phase constructor. The created instance is placed to cleanup stack |
|
46 // ----------------------------------------------------------------------------- |
|
47 // |
|
48 CStringList* CStringList::NewLC() |
|
49 { |
|
50 TRACE_FUNC_ENTRY; |
|
51 CStringList* self = new ( ELeave ) CStringList(); |
|
52 CleanupStack::PushL( self ); |
|
53 self->ConstructL(); |
|
54 TRACE_FUNC_EXIT; |
|
55 return self; |
|
56 } |
|
57 |
|
58 // ----------------------------------------------------------------------------- |
|
59 // CStringList::CStringList() |
|
60 // Default constuctor |
|
61 // ----------------------------------------------------------------------------- |
|
62 // |
|
63 CStringList::CStringList() |
|
64 { |
|
65 TRACE_FUNC; |
|
66 } |
|
67 |
|
68 // ----------------------------------------------------------------------------- |
|
69 // CStringList::~CStringList() |
|
70 // Destructor |
|
71 // ----------------------------------------------------------------------------- |
|
72 // |
|
73 CStringList::~CStringList() |
|
74 { |
|
75 TRACE_FUNC_ENTRY; |
|
76 delete iLines; |
|
77 iLines = NULL; |
|
78 TRACE_FUNC_EXIT; |
|
79 } |
|
80 |
|
81 // ----------------------------------------------------------------------------- |
|
82 // CStringList::ConstructL() |
|
83 // Initializes member data |
|
84 // ----------------------------------------------------------------------------- |
|
85 // |
|
86 void CStringList::ConstructL() |
|
87 { |
|
88 TRACE_FUNC_ENTRY; |
|
89 const TInt KDefaultArrayGranularity = 10; |
|
90 iLines = new (ELeave) CDesCArrayFlat(KDefaultArrayGranularity); |
|
91 SetMark(0); |
|
92 TRACE_FUNC_EXIT; |
|
93 } |
|
94 |
|
95 // ----------------------------------------------------------------------------- |
|
96 // CStringList::Count() const |
|
97 // Returns the count of lines |
|
98 // ----------------------------------------------------------------------------- |
|
99 // |
|
100 TInt CStringList::Count() const |
|
101 { |
|
102 return iLines->Count(); |
|
103 } |
|
104 |
|
105 // ----------------------------------------------------------------------------- |
|
106 // CStringList::Panic(TInt aPanic) const |
|
107 // Creates a panic |
|
108 // ----------------------------------------------------------------------------- |
|
109 // |
|
110 #ifdef _DEBUG |
|
111 void CStringList::Panic(TInt aPanic) const |
|
112 #else |
|
113 void CStringList::Panic(TInt /*aPanic*/) const |
|
114 #endif |
|
115 { |
|
116 TRACE_FUNC_ENTRY; |
|
117 #ifdef _DEBUG |
|
118 _LIT(KPanicCategory,"CStringList"); |
|
119 |
|
120 User::Panic(KPanicCategory, aPanic); |
|
121 #endif |
|
122 TRACE_FUNC_EXIT; |
|
123 } |
|
124 |
|
125 // ----------------------------------------------------------------------------- |
|
126 // CStringList::Reset() |
|
127 // Resets iLines |
|
128 // ----------------------------------------------------------------------------- |
|
129 // |
|
130 void CStringList::Reset() |
|
131 { |
|
132 iLines->Reset(); |
|
133 } |
|
134 |
|
135 // ----------------------------------------------------------------------------- |
|
136 // CStringList::WriteL(const TDesC& aText) |
|
137 // Writes a string |
|
138 // ----------------------------------------------------------------------------- |
|
139 // |
|
140 void CStringList::WriteL(const TDesC& aText) |
|
141 { |
|
142 iLines->AppendL(aText); |
|
143 } |
|
144 |
|
145 // ----------------------------------------------------------------------------- |
|
146 // CStringList::ReadPtr(TInt aIndex) |
|
147 // Returns pointer to the string |
|
148 // ----------------------------------------------------------------------------- |
|
149 // |
|
150 TPtrC16 CStringList::ReadPtr(TInt aIndex) |
|
151 { |
|
152 if (aIndex<0 || aIndex>=Count()) |
|
153 { |
|
154 Panic(KErrArgument); |
|
155 } |
|
156 |
|
157 return iLines->MdcaPoint(aIndex); |
|
158 } |
|
159 |
|
160 // ----------------------------------------------------------------------------- |
|
161 // CStringList::CopyL(CStringList* aSource, TInt aStart, TInt aStop) |
|
162 // Copies a string / strings |
|
163 // ----------------------------------------------------------------------------- |
|
164 // |
|
165 void CStringList::CopyL(CStringList* aSource, TInt aStart, TInt aStop) |
|
166 { |
|
167 if (aStart<0 || aStop>=aSource->Count() || aStart>aStop) |
|
168 { |
|
169 Panic(KErrArgument); |
|
170 } |
|
171 |
|
172 for (TInt i=aStart; i<=aStop; i++) |
|
173 { |
|
174 WriteL(aSource->ReadPtr(i)); |
|
175 } |
|
176 } |
|
177 |
|
178 // ----------------------------------------------------------------------------- |
|
179 // CStringList::StrCopy(TDes& aTarget, const TDesC& aSource) const |
|
180 // Copies the string |
|
181 // ----------------------------------------------------------------------------- |
|
182 // |
|
183 TBool CStringList::StrCopy(TDes& aTarget, const TDesC& aSource) const |
|
184 { |
|
185 TInt len=aTarget.MaxLength(); |
|
186 if(len<aSource.Length()) |
|
187 { |
|
188 aTarget.Copy(aSource.Left(len)); |
|
189 return EFalse; |
|
190 } |
|
191 aTarget.Copy(aSource); |
|
192 return ETrue; |
|
193 } |
|
194 |
|
195 // ----------------------------------------------------------------------------- |
|
196 // CStringList::ReadFromFileL(const TDesC& aFileName) |
|
197 // Read strings from file. |
|
198 // ----------------------------------------------------------------------------- |
|
199 // |
|
200 |
|
201 void CStringList::ReadFromFileL( RFs& aFs, const TDesC& aFileName ) |
|
202 { |
|
203 InternalizeL( aFs, aFileName ); |
|
204 } |
|
205 |
|
206 // ----------------------------------------------------------------------------- |
|
207 // CStringList::InternalizeL(const TDesC& aFileName) |
|
208 // Internalizes from file |
|
209 // ----------------------------------------------------------------------------- |
|
210 // |
|
211 void CStringList::InternalizeL( RFs& aFs, const TDesC& aFileName ) |
|
212 { |
|
213 |
|
214 RFile file; |
|
215 User::LeaveIfError( file.Open( aFs, aFileName, EFileRead ) ); |
|
216 CleanupClosePushL( file ); |
|
217 |
|
218 |
|
219 TFileText textFile; |
|
220 textFile.Set( file ); |
|
221 textFile.Seek( ESeekStart ); |
|
222 TBuf<KMaxSize> buffer; |
|
223 TInt count(0); |
|
224 for (;;) |
|
225 { |
|
226 count++; |
|
227 if ( count > KMaxStringlistSize ) |
|
228 { |
|
229 break; |
|
230 } |
|
231 |
|
232 buffer = KNullDesC; |
|
233 |
|
234 // |
|
235 // Read seems to read chars until newline is reached or |
|
236 // the descriptor is full. In case descriptor becomes full, |
|
237 // err is KErrTooBig and next read starts from the next line. |
|
238 // |
|
239 TInt err = textFile.Read( buffer ); |
|
240 if ( err == KErrEof ) |
|
241 { |
|
242 break; |
|
243 } |
|
244 |
|
245 if ( err != KErrNone ) |
|
246 { |
|
247 User::Leave( err ); |
|
248 } |
|
249 |
|
250 iLines->AppendL( buffer ); |
|
251 } |
|
252 |
|
253 CleanupStack::PopAndDestroy( &file ); // file |
|
254 } |
|
255 |
|
256 |
|
257 // ----------------------------------------------------------------------------- |
|
258 // CStringList::Mark() const |
|
259 // Returns the mark |
|
260 // ----------------------------------------------------------------------------- |
|
261 // |
|
262 TInt CStringList::Mark() const |
|
263 { |
|
264 return iMark; |
|
265 } |
|
266 |
|
267 // ----------------------------------------------------------------------------- |
|
268 // CStringList::SetMark(TInt aMark) |
|
269 // Sets mark |
|
270 // ----------------------------------------------------------------------------- |
|
271 // |
|
272 void CStringList::SetMark(TInt aMark) |
|
273 { |
|
274 iMark=aMark; |
|
275 } |
|
276 |
|
277 // End of files |