|
1 // bufferbase.cpp |
|
2 // |
|
3 // Copyright (c) 2009 - 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "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 // Accenture - Initial contribution |
|
11 // |
|
12 #include "bufferbase.h" |
|
13 #include "filebuffer.h" // for CFileBlock::CountNewLines |
|
14 |
|
15 CFedBufferBase::CFedBufferBase() |
|
16 : CActive(EPriorityStandard), iRefCount(1) |
|
17 { |
|
18 } |
|
19 |
|
20 TInt CFedBufferBase::RefCount() const |
|
21 { |
|
22 return iRefCount; |
|
23 } |
|
24 |
|
25 void CFedBufferBase::IncRef() |
|
26 { |
|
27 iRefCount++; |
|
28 } |
|
29 |
|
30 TInt CFedBufferBase::DecRef() |
|
31 { |
|
32 iRefCount--; |
|
33 TInt refCount = iRefCount; |
|
34 if (refCount == 0) delete this; |
|
35 return refCount; |
|
36 } |
|
37 |
|
38 void StaticDecRef(TAny* aPtr) |
|
39 { |
|
40 static_cast<CFedBufferBase*>(aPtr)->DecRef(); |
|
41 } |
|
42 |
|
43 void CFedBufferBase::PushL() |
|
44 { |
|
45 CleanupStack::PushL(TCleanupItem(&StaticDecRef, this)); |
|
46 } |
|
47 |
|
48 void CFedBufferBase::DoCancel() |
|
49 { |
|
50 ASSERT(EFalse); |
|
51 } |
|
52 |
|
53 void CFedBufferBase::RunL() |
|
54 { |
|
55 ASSERT(EFalse); |
|
56 } |
|
57 |
|
58 TInt CFedBufferBase::FindClientRequest(MSharedCacheClient& aClient) |
|
59 { |
|
60 TPtrC dummy; |
|
61 TRange dummyRange; |
|
62 TInt dummyLine; |
|
63 return iClientRequests.FindInUnsignedKeyOrder(TClientRequest(&aClient, dummy, dummyRange, dummyLine)); |
|
64 } |
|
65 |
|
66 TInt CFedBufferBase::RegisterClient(MSharedCacheClient& aClient, TPtrC& aDes, TRange& aRange, TInt& aRangeStartLineNumber) |
|
67 { |
|
68 TInt err = iClientRequests.InsertInUnsignedKeyOrder(TClientRequest(&aClient, aDes, aRange, aRangeStartLineNumber)); |
|
69 return err; |
|
70 } |
|
71 |
|
72 void CFedBufferBase::UnregisterClient(MSharedCacheClient& aClient) |
|
73 { |
|
74 TInt res = FindClientRequest(aClient); |
|
75 if (res >= 0) iClientRequests.Remove(res); |
|
76 } |
|
77 |
|
78 CFedBufferBase::~CFedBufferBase() |
|
79 { |
|
80 ASSERT(iRefCount == 0); // Otherwise someone has deleted us directly rather than going through the reference counting |
|
81 iClientRequests.Reset(); |
|
82 } |
|
83 |
|
84 void CFedBufferBase::InsertTextL(TInt /*aDocumentPosition*/, const TDesC& /*aText*/) |
|
85 { |
|
86 User::Leave(KErrNotSupported); |
|
87 } |
|
88 |
|
89 void CFedBufferBase::DeleteTextL(TRange /*aRange*/) |
|
90 { |
|
91 User::Leave(KErrNotSupported); |
|
92 } |
|
93 |
|
94 void CFedBufferBase::SaveL(const TDesC& /*aNewName*/, TBool /*aReplace*/) |
|
95 { |
|
96 User::Leave(KErrNotSupported); |
|
97 } |
|
98 |
|
99 TBool CFedBufferBase::Modified() const |
|
100 { |
|
101 return EFalse; |
|
102 } |
|
103 |
|
104 TBool CFedBufferBase::Editable() const |
|
105 { |
|
106 return EFalse; |
|
107 } |
|
108 |
|
109 TBool CFedBufferBase::IsOpen() const |
|
110 { |
|
111 return EFalse; |
|
112 } |
|
113 |
|
114 /////// |
|
115 |
|
116 CConstDataBuffer::CConstDataBuffer(const TDesC& aTitle, const TDesC& aText) |
|
117 : iTitle(aTitle), iText(aText) |
|
118 { |
|
119 } |
|
120 |
|
121 CConstDataBuffer::CConstDataBuffer(const TDesC& aTitle, HBufC* aTextPtr) |
|
122 : iTitle(aTitle), iText(*aTextPtr), iOwnedTextPtr(aTextPtr) |
|
123 { |
|
124 } |
|
125 |
|
126 CConstDataBuffer::~CConstDataBuffer() |
|
127 { |
|
128 delete iOwnedTextPtr; |
|
129 } |
|
130 |
|
131 const TDesC& CConstDataBuffer::Title() const |
|
132 { |
|
133 return iTitle; |
|
134 } |
|
135 |
|
136 TDelimiterType CConstDataBuffer::DelimiterType() const |
|
137 { |
|
138 return EDelimLF; |
|
139 } |
|
140 |
|
141 TEncodingType CConstDataBuffer::Encoding() const |
|
142 { |
|
143 return EEncodingUtf16Native; |
|
144 } |
|
145 |
|
146 TInt CConstDataBuffer::GetData(MSharedCacheClient& aClient, TInt aDocumentPosition) |
|
147 { |
|
148 TInt idx = FindClientRequest(aClient); |
|
149 if (idx < 0) |
|
150 { |
|
151 return idx; |
|
152 } |
|
153 |
|
154 TClientRequest& client = iClientRequests[idx]; |
|
155 ASSERT(aDocumentPosition >= 0); |
|
156 |
|
157 client.iRange = TRange(aDocumentPosition, iText.Length()-aDocumentPosition); |
|
158 client.iDes.Set(iText.Mid(aDocumentPosition)); |
|
159 TInt lines = KMaxTInt; |
|
160 CFileBlock::CountNewLines(iText.Left(aDocumentPosition), lines, KMaxTInt); |
|
161 client.iRangeStartLineNumber = lines; |
|
162 return KErrNone; |
|
163 } |
|
164 |
|
165 TInt CConstDataBuffer::SeekFromOffset(MSharedCacheClient& aClient, TInt aOffset, TInt aNumLinesFromOffset, TInt aLineLength) |
|
166 { |
|
167 TInt idx = FindClientRequest(aClient); |
|
168 if (idx < 0) |
|
169 { |
|
170 return idx; |
|
171 } |
|
172 TClientRequest& client = iClientRequests[idx]; |
|
173 |
|
174 TInt lineDelta = aNumLinesFromOffset; |
|
175 TInt bufPos = -1; |
|
176 if (lineDelta > 0) |
|
177 { |
|
178 TPtrC ptr = iText.Mid(aOffset); |
|
179 bufPos = CFileBlock::CountNewLines(ptr, lineDelta, aLineLength); |
|
180 if (bufPos == KErrNotFound) |
|
181 { |
|
182 aOffset = 0; |
|
183 ptr.Set(iText); |
|
184 lineDelta = -1; |
|
185 bufPos = CFileBlock::CountNewLines(ptr, lineDelta, aLineLength); |
|
186 } |
|
187 } |
|
188 else |
|
189 { |
|
190 lineDelta--; |
|
191 TPtrC ptr = iText.Left(aOffset); |
|
192 aOffset = 0; |
|
193 bufPos = CFileBlock::CountNewLines(ptr, lineDelta, aLineLength); |
|
194 if (bufPos == KErrNotFound) bufPos = 0; |
|
195 } |
|
196 |
|
197 |
|
198 client.iDes.Set(iText.Mid(bufPos+aOffset)); |
|
199 client.iRange = TRange(bufPos + aOffset, client.iDes.Length()); |
|
200 TInt lines = KMaxTInt; |
|
201 CFileBlock::CountNewLines(iText.Left(bufPos + aOffset), lines, KMaxTInt); |
|
202 client.iRangeStartLineNumber = lines; |
|
203 return KErrNone; |
|
204 } |
|
205 |
|
206 TBool CConstDataBuffer::DocumentPositionIsEof(TInt aDocumentPosition) const |
|
207 { |
|
208 return (aDocumentPosition == iText.Length()); |
|
209 } |
|
210 |
|
211 TInt CConstDataBuffer::Find(TInt aStartingPosition, const TDesC& aSearchString, TBool aBackwards) |
|
212 { |
|
213 ASSERT(!aBackwards); |
|
214 TInt res = iText.Mid(aStartingPosition).Find(aSearchString); |
|
215 if (res >= 0) res += aStartingPosition; |
|
216 return res; |
|
217 } |