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 "core/include/fpdfdoc/fpdf_doc.h" | 7 #include "core/include/fpdfdoc/fpdf_doc.h" |
8 #include "doc_utils.h" | 8 #include "doc_utils.h" |
9 | 9 |
10 const int nMaxRecursion = 32; | 10 const int nMaxRecursion = 32; |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 protected: | 30 protected: |
31 const FX_WCHAR* m_pStart; | 31 const FX_WCHAR* m_pStart; |
32 const FX_WCHAR* m_pEnd; | 32 const FX_WCHAR* m_pEnd; |
33 const FX_WCHAR* m_pCur; | 33 const FX_WCHAR* m_pCur; |
34 }; | 34 }; |
35 class CFieldTree { | 35 class CFieldTree { |
36 public: | 36 public: |
37 struct _Node { | 37 struct _Node { |
38 _Node* parent; | 38 _Node* parent; |
39 CFX_PtrArray children; | 39 CFX_ArrayTemplate<_Node*> children; |
40 CFX_WideString short_name; | 40 CFX_WideString short_name; |
41 CPDF_FormField* field_ptr; | 41 CPDF_FormField* field_ptr; |
42 int CountFields(int nLevel = 0) { | 42 int CountFields(int nLevel = 0) { |
43 if (nLevel > nMaxRecursion) { | 43 if (nLevel > nMaxRecursion) { |
44 return 0; | 44 return 0; |
45 } | 45 } |
46 if (field_ptr) { | 46 if (field_ptr) { |
47 return 1; | 47 return 1; |
48 } | 48 } |
49 int count = 0; | 49 int count = 0; |
50 for (int i = 0; i < children.GetSize(); i++) { | 50 for (int i = 0; i < children.GetSize(); i++) { |
51 count += ((_Node*)children.GetAt(i))->CountFields(nLevel + 1); | 51 count += children.GetAt(i)->CountFields(nLevel + 1); |
52 } | 52 } |
53 return count; | 53 return count; |
54 } | 54 } |
55 CPDF_FormField* GetField(int* fields_to_go) { | 55 CPDF_FormField* GetField(int* fields_to_go) { |
56 if (field_ptr) { | 56 if (field_ptr) { |
57 if (*fields_to_go == 0) { | 57 if (*fields_to_go == 0) { |
58 return field_ptr; | 58 return field_ptr; |
59 } | 59 } |
60 --*fields_to_go; | 60 --*fields_to_go; |
61 return NULL; | 61 return NULL; |
62 } | 62 } |
63 for (int i = 0; i < children.GetSize(); i++) { | 63 for (int i = 0; i < children.GetSize(); i++) { |
64 _Node* pNode = (_Node*)children.GetAt(i); | 64 if (CPDF_FormField* pField = children.GetAt(i)->GetField(fields_to_go)) |
65 CPDF_FormField* pField = pNode->GetField(fields_to_go); | |
66 if (pField) { | |
67 return pField; | 65 return pField; |
68 } | |
69 } | 66 } |
70 return NULL; | 67 return NULL; |
71 } | 68 } |
72 CPDF_FormField* GetField(int index) { | 69 CPDF_FormField* GetField(int index) { |
73 int fields_to_go = index; | 70 int fields_to_go = index; |
74 return GetField(&fields_to_go); | 71 return GetField(&fields_to_go); |
75 } | 72 } |
76 }; | 73 }; |
77 CFieldTree(); | 74 CFieldTree(); |
78 ~CFieldTree(); | 75 ~CFieldTree(); |
(...skipping 23 matching lines...) Expand all Loading... |
102 return NULL; | 99 return NULL; |
103 } | 100 } |
104 _Node* pNode = new _Node; | 101 _Node* pNode = new _Node; |
105 pNode->parent = pParent; | 102 pNode->parent = pParent; |
106 pNode->short_name = short_name; | 103 pNode->short_name = short_name; |
107 pNode->field_ptr = field_ptr; | 104 pNode->field_ptr = field_ptr; |
108 pParent->children.Add(pNode); | 105 pParent->children.Add(pNode); |
109 return pNode; | 106 return pNode; |
110 } | 107 } |
111 void CFieldTree::RemoveNode(_Node* pNode, int nLevel) { | 108 void CFieldTree::RemoveNode(_Node* pNode, int nLevel) { |
112 if (pNode == NULL) { | 109 if (!pNode) { |
113 return; | 110 return; |
114 } | 111 } |
115 if (nLevel > nMaxRecursion) { | 112 if (nLevel <= nMaxRecursion) { |
116 delete pNode; | 113 for (int i = 0; i < pNode->children.GetSize(); i++) { |
117 return; | 114 RemoveNode(pNode->children[i], nLevel + 1); |
118 } | 115 } |
119 CFX_PtrArray& ptr_array = pNode->children; | |
120 for (int i = 0; i < ptr_array.GetSize(); i++) { | |
121 _Node* pChild = (_Node*)ptr_array[i]; | |
122 RemoveNode(pChild, nLevel + 1); | |
123 } | 116 } |
124 delete pNode; | 117 delete pNode; |
125 } | 118 } |
126 CFieldTree::_Node* CFieldTree::_Lookup(_Node* pParent, | 119 CFieldTree::_Node* CFieldTree::_Lookup(_Node* pParent, |
127 const CFX_WideString& short_name) { | 120 const CFX_WideString& short_name) { |
128 if (pParent == NULL) { | 121 if (pParent == NULL) { |
129 return NULL; | 122 return NULL; |
130 } | 123 } |
131 CFX_PtrArray& ptr_array = pParent->children; | 124 for (int i = 0; i < pParent->children.GetSize(); i++) { |
132 for (int i = 0; i < ptr_array.GetSize(); i++) { | 125 _Node* pNode = pParent->children[i]; |
133 _Node* pNode = (_Node*)ptr_array[i]; | |
134 if (pNode->short_name.GetLength() == short_name.GetLength() && | 126 if (pNode->short_name.GetLength() == short_name.GetLength() && |
135 FXSYS_memcmp(pNode->short_name.c_str(), short_name.c_str(), | 127 FXSYS_memcmp(pNode->short_name.c_str(), short_name.c_str(), |
136 short_name.GetLength() * sizeof(FX_WCHAR)) == 0) { | 128 short_name.GetLength() * sizeof(FX_WCHAR)) == 0) { |
137 return pNode; | 129 return pNode; |
138 } | 130 } |
139 } | 131 } |
140 return NULL; | 132 return NULL; |
141 } | 133 } |
142 void CFieldTree::RemoveAll() { | 134 void CFieldTree::RemoveAll() { |
143 CFX_PtrArray& ptr_array = m_Root.children; | 135 for (int i = 0; i < m_Root.children.GetSize(); i++) { |
144 for (int i = 0; i < ptr_array.GetSize(); i++) { | 136 RemoveNode(m_Root.children[i]); |
145 _Node* pNode = (_Node*)ptr_array[i]; | |
146 RemoveNode(pNode); | |
147 } | 137 } |
148 } | 138 } |
149 void CFieldTree::SetField(const CFX_WideString& full_name, | 139 void CFieldTree::SetField(const CFX_WideString& full_name, |
150 CPDF_FormField* field_ptr) { | 140 CPDF_FormField* field_ptr) { |
151 if (full_name == L"") { | 141 if (full_name == L"") { |
152 return; | 142 return; |
153 } | 143 } |
154 CFieldNameExtractor name_extractor(full_name); | 144 CFieldNameExtractor name_extractor(full_name); |
155 const FX_WCHAR* pName; | 145 const FX_WCHAR* pName; |
156 FX_STRSIZE nLength; | 146 FX_STRSIZE nLength; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 FX_STRSIZE nLength; | 185 FX_STRSIZE nLength; |
196 name_extractor.GetNext(pName, nLength); | 186 name_extractor.GetNext(pName, nLength); |
197 _Node *pNode = &m_Root, *pLast = NULL; | 187 _Node *pNode = &m_Root, *pLast = NULL; |
198 while (nLength > 0 && pNode) { | 188 while (nLength > 0 && pNode) { |
199 pLast = pNode; | 189 pLast = pNode; |
200 CFX_WideString name = CFX_WideString(pName, nLength); | 190 CFX_WideString name = CFX_WideString(pName, nLength); |
201 pNode = _Lookup(pLast, name); | 191 pNode = _Lookup(pLast, name); |
202 name_extractor.GetNext(pName, nLength); | 192 name_extractor.GetNext(pName, nLength); |
203 } | 193 } |
204 if (pNode && pNode != &m_Root) { | 194 if (pNode && pNode != &m_Root) { |
205 CFX_PtrArray& ptr_array = pLast->children; | 195 for (int i = 0; i < pLast->children.GetSize(); i++) { |
206 for (int i = 0; i < ptr_array.GetSize(); i++) { | 196 if (pNode == pLast->children[i]) { |
207 if (pNode == (_Node*)ptr_array[i]) { | 197 pLast->children.RemoveAt(i); |
208 ptr_array.RemoveAt(i); | |
209 break; | 198 break; |
210 } | 199 } |
211 } | 200 } |
212 CPDF_FormField* pField = pNode->field_ptr; | 201 CPDF_FormField* pField = pNode->field_ptr; |
213 RemoveNode(pNode); | 202 RemoveNode(pNode); |
214 return pField; | 203 return pField; |
215 } | 204 } |
216 return NULL; | 205 return NULL; |
217 } | 206 } |
218 CFieldTree::_Node* CFieldTree::FindNode(const CFX_WideString& full_name) { | 207 CFieldTree::_Node* CFieldTree::FindNode(const CFX_WideString& full_name) { |
(...skipping 1038 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1257 FDF_ImportField(pField, L"", bNotify); | 1246 FDF_ImportField(pField, L"", bNotify); |
1258 } | 1247 } |
1259 if (bNotify && m_pFormNotify != NULL) { | 1248 if (bNotify && m_pFormNotify != NULL) { |
1260 m_pFormNotify->AfterFormImportData(this); | 1249 m_pFormNotify->AfterFormImportData(this); |
1261 } | 1250 } |
1262 return TRUE; | 1251 return TRUE; |
1263 } | 1252 } |
1264 void CPDF_InterForm::SetFormNotify(const CPDF_FormNotify* pNotify) { | 1253 void CPDF_InterForm::SetFormNotify(const CPDF_FormNotify* pNotify) { |
1265 m_pFormNotify = (CPDF_FormNotify*)pNotify; | 1254 m_pFormNotify = (CPDF_FormNotify*)pNotify; |
1266 } | 1255 } |
OLD | NEW |