OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "xfa/fde/cfde_txtedtbuf.h" | 7 #include "xfa/fde/cfde_txtedtbuf.h" |
8 | 8 |
9 #include "xfa/fgas/crt/fgas_memory.h" | 9 #include "xfa/fgas/crt/fgas_memory.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 const int kDefaultChunkSize = 1024; | 13 const int kDefaultChunkSize = 1024; |
14 const int kDefaultChunkCount = 2; | 14 const int kDefaultChunkCount = 2; |
15 | 15 |
16 } // namespace | 16 } // namespace |
17 | 17 |
18 CFDE_TxtEdtBuf::CFDE_TxtEdtBuf() | 18 CFDE_TxtEdtBuf::CFDE_TxtEdtBuf() |
19 : m_nChunkSize(kDefaultChunkSize), m_nTotal(0), m_bChanged(FALSE) { | 19 : m_nChunkSize(kDefaultChunkSize), m_nTotal(0), m_bChanged(false) { |
20 ResetChunkBuffer(kDefaultChunkCount, m_nChunkSize); | 20 ResetChunkBuffer(kDefaultChunkCount, m_nChunkSize); |
21 } | 21 } |
22 | 22 |
23 CFDE_TxtEdtBuf::~CFDE_TxtEdtBuf() { | 23 CFDE_TxtEdtBuf::~CFDE_TxtEdtBuf() { |
24 Clear(TRUE); | 24 Clear(true); |
25 m_Chunks.RemoveAll(); | 25 m_Chunks.RemoveAll(); |
26 } | 26 } |
27 | 27 |
28 int32_t CFDE_TxtEdtBuf::GetChunkSize() const { | 28 int32_t CFDE_TxtEdtBuf::GetChunkSize() const { |
29 return m_nChunkSize; | 29 return m_nChunkSize; |
30 } | 30 } |
31 | 31 |
32 int32_t CFDE_TxtEdtBuf::GetTextLength() const { | 32 int32_t CFDE_TxtEdtBuf::GetTextLength() const { |
33 return m_nTotal; | 33 return m_nTotal; |
34 } | 34 } |
35 | 35 |
36 void CFDE_TxtEdtBuf::SetText(const CFX_WideString& wsText) { | 36 void CFDE_TxtEdtBuf::SetText(const CFX_WideString& wsText) { |
37 ASSERT(!wsText.IsEmpty()); | 37 ASSERT(!wsText.IsEmpty()); |
38 Clear(FALSE); | 38 Clear(false); |
39 int32_t nTextLength = wsText.GetLength(); | 39 int32_t nTextLength = wsText.GetLength(); |
40 int32_t nNeedCount = | 40 int32_t nNeedCount = |
41 ((nTextLength - 1) / m_nChunkSize + 1) - m_Chunks.GetSize(); | 41 ((nTextLength - 1) / m_nChunkSize + 1) - m_Chunks.GetSize(); |
42 int32_t i = 0; | 42 int32_t i = 0; |
43 for (i = 0; i < nNeedCount; i++) { | 43 for (i = 0; i < nNeedCount; i++) { |
44 FDE_CHUNKHEADER* lpChunk = | 44 FDE_CHUNKHEADER* lpChunk = |
45 static_cast<FDE_CHUNKHEADER*>(m_pAllocator->Alloc( | 45 static_cast<FDE_CHUNKHEADER*>(m_pAllocator->Alloc( |
46 sizeof(FDE_CHUNKHEADER) + (m_nChunkSize - 1) * sizeof(FX_WCHAR))); | 46 sizeof(FDE_CHUNKHEADER) + (m_nChunkSize - 1) * sizeof(FX_WCHAR))); |
47 lpChunk->nUsed = 0; | 47 lpChunk->nUsed = 0; |
48 m_Chunks.Add(lpChunk); | 48 m_Chunks.Add(lpChunk); |
49 } | 49 } |
50 int32_t nTotalCount = m_Chunks.GetSize(); | 50 int32_t nTotalCount = m_Chunks.GetSize(); |
51 const FX_WCHAR* lpSrcBuf = wsText.c_str(); | 51 const FX_WCHAR* lpSrcBuf = wsText.c_str(); |
52 int32_t nLeave = nTextLength; | 52 int32_t nLeave = nTextLength; |
53 int32_t nCopyedLength = m_nChunkSize; | 53 int32_t nCopyedLength = m_nChunkSize; |
54 for (i = 0; i < nTotalCount && nLeave > 0; i++) { | 54 for (i = 0; i < nTotalCount && nLeave > 0; i++) { |
55 if (nLeave < nCopyedLength) { | 55 if (nLeave < nCopyedLength) { |
56 nCopyedLength = nLeave; | 56 nCopyedLength = nLeave; |
57 } | 57 } |
58 FDE_CHUNKHEADER* lpChunk = m_Chunks[i]; | 58 FDE_CHUNKHEADER* lpChunk = m_Chunks[i]; |
59 FXSYS_memcpy(lpChunk->wChars, lpSrcBuf, nCopyedLength * sizeof(FX_WCHAR)); | 59 FXSYS_memcpy(lpChunk->wChars, lpSrcBuf, nCopyedLength * sizeof(FX_WCHAR)); |
60 nLeave -= nCopyedLength; | 60 nLeave -= nCopyedLength; |
61 lpSrcBuf += nCopyedLength; | 61 lpSrcBuf += nCopyedLength; |
62 lpChunk->nUsed = nCopyedLength; | 62 lpChunk->nUsed = nCopyedLength; |
63 } | 63 } |
64 m_nTotal = nTextLength; | 64 m_nTotal = nTextLength; |
65 m_bChanged = TRUE; | 65 m_bChanged = true; |
66 } | 66 } |
67 | 67 |
68 void CFDE_TxtEdtBuf::GetText(CFX_WideString& wsText) const { | 68 void CFDE_TxtEdtBuf::GetText(CFX_WideString& wsText) const { |
69 GetRange(wsText, 0, m_nTotal); | 69 GetRange(wsText, 0, m_nTotal); |
70 } | 70 } |
71 | 71 |
72 FX_WCHAR CFDE_TxtEdtBuf::GetCharByIndex(int32_t nIndex) const { | 72 FX_WCHAR CFDE_TxtEdtBuf::GetCharByIndex(int32_t nIndex) const { |
73 ASSERT(nIndex >= 0 && nIndex < GetTextLength()); | 73 ASSERT(nIndex >= 0 && nIndex < GetTextLength()); |
74 FDE_CHUNKHEADER* pChunkHeader = nullptr; | 74 FDE_CHUNKHEADER* pChunkHeader = nullptr; |
75 int32_t nTotal = 0; | 75 int32_t nTotal = 0; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 ASSERT(lpChunk); | 158 ASSERT(lpChunk); |
159 int32_t nCopy = std::min(nLengthTemp, m_nChunkSize); | 159 int32_t nCopy = std::min(nLengthTemp, m_nChunkSize); |
160 FXSYS_memcpy(lpChunk->wChars, lpText, nCopy * sizeof(FX_WCHAR)); | 160 FXSYS_memcpy(lpChunk->wChars, lpText, nCopy * sizeof(FX_WCHAR)); |
161 lpText += nCopy; | 161 lpText += nCopy; |
162 nLengthTemp -= nCopy; | 162 nLengthTemp -= nCopy; |
163 lpChunk->nUsed = nCopy; | 163 lpChunk->nUsed = nCopy; |
164 m_Chunks.InsertAt(cp.nChunkIndex, lpChunk); | 164 m_Chunks.InsertAt(cp.nChunkIndex, lpChunk); |
165 cp.nChunkIndex++; | 165 cp.nChunkIndex++; |
166 } | 166 } |
167 m_nTotal += nLength; | 167 m_nTotal += nLength; |
168 m_bChanged = TRUE; | 168 m_bChanged = true; |
169 } | 169 } |
170 | 170 |
171 void CFDE_TxtEdtBuf::Delete(int32_t nIndex, int32_t nLength) { | 171 void CFDE_TxtEdtBuf::Delete(int32_t nIndex, int32_t nLength) { |
172 ASSERT(nLength > 0 && nIndex >= 0 && nIndex + nLength <= m_nTotal); | 172 ASSERT(nLength > 0 && nIndex >= 0 && nIndex + nLength <= m_nTotal); |
173 FDE_CHUNKPLACE cpEnd; | 173 FDE_CHUNKPLACE cpEnd; |
174 Index2CP(nIndex + nLength - 1, cpEnd); | 174 Index2CP(nIndex + nLength - 1, cpEnd); |
175 m_nTotal -= nLength; | 175 m_nTotal -= nLength; |
176 FDE_CHUNKHEADER* lpChunk = m_Chunks[cpEnd.nChunkIndex]; | 176 FDE_CHUNKHEADER* lpChunk = m_Chunks[cpEnd.nChunkIndex]; |
177 int32_t nFirstPart = cpEnd.nCharIndex + 1; | 177 int32_t nFirstPart = cpEnd.nCharIndex + 1; |
178 int32_t nMovePart = lpChunk->nUsed - nFirstPart; | 178 int32_t nMovePart = lpChunk->nUsed - nFirstPart; |
(...skipping 10 matching lines...) Expand all Loading... |
189 int32_t nDeleted = std::min(lpChunk->nUsed, nLength); | 189 int32_t nDeleted = std::min(lpChunk->nUsed, nLength); |
190 lpChunk->nUsed -= nDeleted; | 190 lpChunk->nUsed -= nDeleted; |
191 if (lpChunk->nUsed == 0) { | 191 if (lpChunk->nUsed == 0) { |
192 m_pAllocator->Free(lpChunk); | 192 m_pAllocator->Free(lpChunk); |
193 m_Chunks.RemoveAt(cpEnd.nChunkIndex); | 193 m_Chunks.RemoveAt(cpEnd.nChunkIndex); |
194 lpChunk = nullptr; | 194 lpChunk = nullptr; |
195 } | 195 } |
196 nLength -= nDeleted; | 196 nLength -= nDeleted; |
197 cpEnd.nChunkIndex--; | 197 cpEnd.nChunkIndex--; |
198 } | 198 } |
199 m_bChanged = TRUE; | 199 m_bChanged = true; |
200 } | 200 } |
201 | 201 |
202 void CFDE_TxtEdtBuf::Clear(FX_BOOL bRelease) { | 202 void CFDE_TxtEdtBuf::Clear(bool bRelease) { |
203 int32_t i = 0; | 203 int32_t i = 0; |
204 int32_t nCount = m_Chunks.GetSize(); | 204 int32_t nCount = m_Chunks.GetSize(); |
205 if (bRelease) { | 205 if (bRelease) { |
206 while (i < nCount) { | 206 while (i < nCount) { |
207 m_pAllocator->Free(m_Chunks[i++]); | 207 m_pAllocator->Free(m_Chunks[i++]); |
208 } | 208 } |
209 m_Chunks.RemoveAll(); | 209 m_Chunks.RemoveAll(); |
210 } else { | 210 } else { |
211 while (i < nCount) { | 211 while (i < nCount) { |
212 m_Chunks[i++]->nUsed = 0; | 212 m_Chunks[i++]->nUsed = 0; |
213 } | 213 } |
214 } | 214 } |
215 m_nTotal = 0; | 215 m_nTotal = 0; |
216 m_bChanged = TRUE; | 216 m_bChanged = true; |
217 } | 217 } |
218 | 218 |
219 FX_BOOL CFDE_TxtEdtBuf::Optimize(IFX_Pause* pPause) { | 219 bool CFDE_TxtEdtBuf::Optimize(IFX_Pause* pPause) { |
220 if (m_bChanged == FALSE) { | 220 if (m_bChanged == false) { |
221 return TRUE; | 221 return true; |
222 } | 222 } |
223 if (m_nTotal == 0) { | 223 if (m_nTotal == 0) { |
224 return TRUE; | 224 return true; |
225 } | 225 } |
226 int32_t nCount = m_Chunks.GetSize(); | 226 int32_t nCount = m_Chunks.GetSize(); |
227 if (nCount == 0) { | 227 if (nCount == 0) { |
228 return TRUE; | 228 return true; |
229 } | 229 } |
230 int32_t i = 0; | 230 int32_t i = 0; |
231 for (; i < nCount; i++) { | 231 for (; i < nCount; i++) { |
232 FDE_CHUNKHEADER* lpChunk = m_Chunks[i]; | 232 FDE_CHUNKHEADER* lpChunk = m_Chunks[i]; |
233 if (lpChunk->nUsed == 0) { | 233 if (lpChunk->nUsed == 0) { |
234 m_pAllocator->Free(lpChunk); | 234 m_pAllocator->Free(lpChunk); |
235 m_Chunks.RemoveAt(i); | 235 m_Chunks.RemoveAt(i); |
236 --i; | 236 --i; |
237 --nCount; | 237 --nCount; |
238 } | 238 } |
239 } | 239 } |
240 if (pPause && pPause->NeedToPauseNow()) | 240 if (pPause && pPause->NeedToPauseNow()) |
241 return FALSE; | 241 return false; |
242 | 242 |
243 FDE_CHUNKHEADER* lpPreChunk = m_Chunks[0]; | 243 FDE_CHUNKHEADER* lpPreChunk = m_Chunks[0]; |
244 FDE_CHUNKHEADER* lpCurChunk = nullptr; | 244 FDE_CHUNKHEADER* lpCurChunk = nullptr; |
245 for (i = 1; i < nCount; i++) { | 245 for (i = 1; i < nCount; i++) { |
246 lpCurChunk = m_Chunks[i]; | 246 lpCurChunk = m_Chunks[i]; |
247 if (lpPreChunk->nUsed + lpCurChunk->nUsed <= m_nChunkSize) { | 247 if (lpPreChunk->nUsed + lpCurChunk->nUsed <= m_nChunkSize) { |
248 FXSYS_memcpy(lpPreChunk->wChars + lpPreChunk->nUsed, lpCurChunk->wChars, | 248 FXSYS_memcpy(lpPreChunk->wChars + lpPreChunk->nUsed, lpCurChunk->wChars, |
249 lpCurChunk->nUsed * sizeof(FX_WCHAR)); | 249 lpCurChunk->nUsed * sizeof(FX_WCHAR)); |
250 lpPreChunk->nUsed += lpCurChunk->nUsed; | 250 lpPreChunk->nUsed += lpCurChunk->nUsed; |
251 m_pAllocator->Free(lpCurChunk); | 251 m_pAllocator->Free(lpCurChunk); |
252 m_Chunks.RemoveAt(i); | 252 m_Chunks.RemoveAt(i); |
253 --i; | 253 --i; |
254 --nCount; | 254 --nCount; |
255 } else { | 255 } else { |
256 lpPreChunk = lpCurChunk; | 256 lpPreChunk = lpCurChunk; |
257 } | 257 } |
258 if (pPause && pPause->NeedToPauseNow()) | 258 if (pPause && pPause->NeedToPauseNow()) |
259 return FALSE; | 259 return false; |
260 } | 260 } |
261 m_bChanged = FALSE; | 261 m_bChanged = false; |
262 return TRUE; | 262 return true; |
263 } | 263 } |
264 | 264 |
265 void CFDE_TxtEdtBuf::ResetChunkBuffer(int32_t nDefChunkCount, | 265 void CFDE_TxtEdtBuf::ResetChunkBuffer(int32_t nDefChunkCount, |
266 int32_t nChunkSize) { | 266 int32_t nChunkSize) { |
267 ASSERT(nChunkSize); | 267 ASSERT(nChunkSize); |
268 ASSERT(nDefChunkCount); | 268 ASSERT(nDefChunkCount); |
269 m_Chunks.RemoveAll(); | 269 m_Chunks.RemoveAll(); |
270 m_nChunkSize = nChunkSize; | 270 m_nChunkSize = nChunkSize; |
271 int32_t nChunkLength = | 271 int32_t nChunkLength = |
272 sizeof(FDE_CHUNKHEADER) + (m_nChunkSize - 1) * sizeof(FX_WCHAR); | 272 sizeof(FDE_CHUNKHEADER) + (m_nChunkSize - 1) * sizeof(FX_WCHAR); |
(...skipping 28 matching lines...) Expand all Loading... |
301 int32_t nCount = m_Chunks.GetSize(); | 301 int32_t nCount = m_Chunks.GetSize(); |
302 for (; i < nCount; i++) { | 302 for (; i < nCount; i++) { |
303 nTotal += m_Chunks[i]->nUsed; | 303 nTotal += m_Chunks[i]->nUsed; |
304 if (nTotal > nIndex) { | 304 if (nTotal > nIndex) { |
305 break; | 305 break; |
306 } | 306 } |
307 } | 307 } |
308 cp.nChunkIndex = i; | 308 cp.nChunkIndex = i; |
309 cp.nCharIndex = m_Chunks[i]->nUsed - (nTotal - nIndex); | 309 cp.nCharIndex = m_Chunks[i]->nUsed - (nTotal - nIndex); |
310 } | 310 } |
OLD | NEW |