|
1 // Copyright (c) 1997-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 // |
|
15 |
|
16 #include "ByteList.h" |
|
17 |
|
18 // |
|
19 // class CByteList - FIFO for bytes |
|
20 // |
|
21 |
|
22 CByteList* CByteList::NewLC() |
|
23 { |
|
24 CByteList* self = CByteList::NewL(); |
|
25 CleanupStack::PushL(self); |
|
26 return self; |
|
27 } |
|
28 |
|
29 CByteList* CByteList::NewL() |
|
30 { |
|
31 CByteList* self = new (ELeave) CByteList; |
|
32 return self; |
|
33 } |
|
34 |
|
35 CByteList::CByteList() |
|
36 : iList(CItem::iOffset),iListIter(iList),iListCommit(iList) |
|
37 {} |
|
38 |
|
39 CByteList::~CByteList() |
|
40 { |
|
41 CItem* item; |
|
42 |
|
43 iListIter.SetToFirst(); |
|
44 while ((item = iListIter++) != NULL) |
|
45 { |
|
46 iList.Remove(*item); |
|
47 delete item; |
|
48 }; |
|
49 } |
|
50 |
|
51 void CByteList::AddToListL(HBufC8& aData) |
|
52 { |
|
53 // NULL buffers accepted, but not stored |
|
54 if (&aData == NULL) |
|
55 return; |
|
56 |
|
57 TBool first = FALSE; |
|
58 if (iList.IsEmpty()) |
|
59 { |
|
60 first = TRUE; |
|
61 } |
|
62 |
|
63 CItem* item = CItem::NewL(CONST_CAST(HBufC8&,aData)); |
|
64 iList.AddLast(*item); |
|
65 |
|
66 if (first) |
|
67 { |
|
68 iListIter.SetToFirst(); |
|
69 iByteOffset = 0; |
|
70 iListCommit.SetToFirst(); |
|
71 iByteCommit = 0; |
|
72 } |
|
73 } |
|
74 |
|
75 TInt CByteList::Byte(TUint8* aByte, TBool aIncrement) |
|
76 { |
|
77 if (iList.IsEmpty()) |
|
78 return KErrNotFound; |
|
79 |
|
80 // We have data, but it's all used |
|
81 if (iListIter == NULL) |
|
82 return KErrNotFound; |
|
83 |
|
84 if (iByteOffset >= ((*iListIter).Data())->Length()) |
|
85 return KErrNotFound; |
|
86 |
|
87 *aByte = (((*iListIter).Data())->Des())[iByteOffset]; |
|
88 Inc(aIncrement ? 1 : 0); |
|
89 |
|
90 return KErrNone; |
|
91 } |
|
92 |
|
93 TInt CByteList::Inc(TInt aCount) |
|
94 { |
|
95 // Note: Inc(0) is ok |
|
96 if (!aCount) |
|
97 return KErrNone; |
|
98 |
|
99 const HBufC8* data = (*iListIter).Data(); |
|
100 for (TInt i=0; i<aCount; i++) |
|
101 { |
|
102 iByteOffset++; |
|
103 if (iByteOffset >= data->Length()) |
|
104 { |
|
105 // Need next buffer |
|
106 iListIter++; |
|
107 if (iListIter == NULL) |
|
108 { |
|
109 // User should call RollBack() |
|
110 return KErrNotFound; |
|
111 } |
|
112 |
|
113 data = (*iListIter).Data(); |
|
114 iByteOffset = 0; |
|
115 } |
|
116 } |
|
117 |
|
118 return KErrNone; |
|
119 } |
|
120 |
|
121 void CByteList::Commit() |
|
122 { |
|
123 CItem* current = iListIter; |
|
124 iListIter.SetToFirst(); |
|
125 CItem* item = iListIter; |
|
126 |
|
127 // Remove used buffers |
|
128 if (item != current && item != NULL) |
|
129 { |
|
130 // Note: Increment iListIter to keep it on the list during loop |
|
131 while ((item = iListIter++) != NULL) |
|
132 { |
|
133 if (item == current) |
|
134 break; |
|
135 |
|
136 iList.Remove(*item); |
|
137 delete item; |
|
138 }; |
|
139 iListIter.SetToFirst(); |
|
140 current = iListIter; |
|
141 } |
|
142 |
|
143 if (current != NULL) |
|
144 { |
|
145 // Is current buffer fully used |
|
146 if (iByteOffset >= (*iListIter).Data()->Length()) |
|
147 { |
|
148 item = iListIter; |
|
149 iList.Remove(*item); |
|
150 delete item; |
|
151 iByteOffset = 0; |
|
152 // BUG: is iListIter NULL after Remove? Manual didn't say anything |
|
153 } |
|
154 } |
|
155 |
|
156 iListCommit = iListIter; |
|
157 iByteCommit = iByteOffset; |
|
158 } |
|
159 |
|
160 void CByteList::RollBack() |
|
161 { |
|
162 iListIter = iListCommit; |
|
163 iByteOffset = iByteCommit; |
|
164 } |
|
165 |
|
166 // |
|
167 // class CItem - to be used with CList |
|
168 // |
|
169 |
|
170 const TInt CItem::iOffset = _FOFF(CItem,iLink); |
|
171 |
|
172 CItem* CItem::NewLC (const HBufC8& aData) |
|
173 { |
|
174 CItem* self = new (ELeave) CItem(); |
|
175 CleanupStack::PushL(self); |
|
176 self->ConstructL(aData); |
|
177 return self; |
|
178 } |
|
179 |
|
180 CItem* CItem::NewL(const HBufC8& aData) |
|
181 { |
|
182 CItem* self = CItem::NewLC(aData); |
|
183 CleanupStack::Pop(); |
|
184 return self; |
|
185 } |
|
186 |
|
187 void CItem::ConstructL(const HBufC8& aData) |
|
188 { |
|
189 // NOTE: Make sure aData != NULL |
|
190 iData = HBufC8::NewL(aData.Length()); |
|
191 (*iData) = aData; |
|
192 } |
|
193 |
|
194 CItem::CItem() |
|
195 {} |
|
196 |
|
197 CItem::~CItem() |
|
198 { |
|
199 delete iData; |
|
200 } |
|
201 |
|
202 HBufC8* CItem::Data() const |
|
203 { |
|
204 return (iData); |
|
205 } |
|
206 |
|
207 // |
|
208 // End of file |
|
209 // |