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