Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(572)

Unified Diff: core/fpdfdoc/cpdf_interform.cpp

Issue 2372423002: Made CFieldTree::Node a class. (Closed)
Patch Set: std::vector Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: core/fpdfdoc/cpdf_interform.cpp
diff --git a/core/fpdfdoc/cpdf_interform.cpp b/core/fpdfdoc/cpdf_interform.cpp
index 924084d4e2b3b5c01ce7f6fb481211291022e0a0..eea5bbaa88ace843cc92093070358709c6643b40 100644
--- a/core/fpdfdoc/cpdf_interform.cpp
+++ b/core/fpdfdoc/cpdf_interform.cpp
@@ -537,33 +537,41 @@ FX_BOOL RetrieveSpecificFont(uint8_t charSet,
class CFieldTree {
public:
- struct Node {
- Node* parent;
- CFX_ArrayTemplate<Node*> children;
- CFX_WideString short_name;
- CPDF_FormField* field_ptr;
+ class Node {
+ public:
+ Node() : field_ptr_(nullptr) {}
+ Node(const CFX_WideString& short_name, CPDF_FormField* field_ptr)
+ : short_name_(short_name), field_ptr_(field_ptr) {}
+ ~Node() {}
+
+ void AddChildNode(Node* pNode) { children_.push_back(pNode); }
+
+ size_t GetChildrenCount() const { return children_.size(); }
+
+ Node* GetChildAt(size_t i) { return children_[i]; }
Tom Sepez 2016/09/28 00:10:01 nit: is this a const method?
Lei Zhang 2016/09/28 00:22:54 Added a const version.
+
int CountFields(int nLevel = 0) {
if (nLevel > nMaxRecursion)
return 0;
- if (field_ptr)
+ if (field_ptr_)
return 1;
int count = 0;
- for (int i = 0; i < children.GetSize(); i++)
- count += children.GetAt(i)->CountFields(nLevel + 1);
+ for (size_t i = 0; i < GetChildrenCount(); ++i)
+ count += GetChildAt(i)->CountFields(nLevel + 1);
return count;
}
CPDF_FormField* GetField(int* fields_to_go) {
- if (field_ptr) {
+ if (field_ptr_) {
if (*fields_to_go == 0)
- return field_ptr;
+ return field_ptr_;
--*fields_to_go;
return nullptr;
}
- for (int i = 0; i < children.GetSize(); i++) {
- if (CPDF_FormField* pField = children.GetAt(i)->GetField(fields_to_go))
+ for (size_t i = 0; i < GetChildrenCount(); ++i) {
+ if (CPDF_FormField* pField = GetChildAt(i)->GetField(fields_to_go))
return pField;
}
return nullptr;
@@ -573,6 +581,15 @@ class CFieldTree {
int fields_to_go = index;
return GetField(&fields_to_go);
}
+
+ void set_field_ptr(CPDF_FormField* field) { field_ptr_ = field; }
Tom Sepez 2016/09/28 00:10:01 nit: not sure time to go full chromium naming. Se
Lei Zhang 2016/09/28 00:22:54 Done.
+ CPDF_FormField* field() { return field_ptr_; }
Tom Sepez 2016/09/28 00:10:01 nit: const method?
Lei Zhang 2016/09/28 00:22:54 Added const version.
+ const CFX_WideString& short_name() const { return short_name_; }
+
+ private:
+ std::vector<Node*> children_;
Tom Sepez 2016/09/28 00:10:01 nit: not sure now is the time to go full chromium
Lei Zhang 2016/09/28 00:22:55 Oh, they started out looking like chromium naming
+ CFX_WideString short_name_;
+ CPDF_FormField* field_ptr_;
};
CFieldTree();
@@ -580,7 +597,6 @@ class CFieldTree {
void SetField(const CFX_WideString& full_name, CPDF_FormField* field_ptr);
CPDF_FormField* GetField(const CFX_WideString& full_name);
- CPDF_FormField* RemoveField(const CFX_WideString& full_name);
void RemoveAll();
Node* FindNode(const CFX_WideString& full_name);
@@ -594,10 +610,7 @@ class CFieldTree {
Node m_Root;
};
-CFieldTree::CFieldTree() {
- m_Root.parent = nullptr;
- m_Root.field_ptr = nullptr;
-}
+CFieldTree::CFieldTree() {}
CFieldTree::~CFieldTree() {
RemoveAll();
@@ -609,20 +622,18 @@ CFieldTree::Node* CFieldTree::AddChild(Node* pParent,
if (!pParent)
return nullptr;
- Node* pNode = new Node;
- pNode->parent = pParent;
- pNode->short_name = short_name;
- pNode->field_ptr = field_ptr;
- pParent->children.Add(pNode);
+ Node* pNode = new Node(short_name, field_ptr);
+ pParent->AddChildNode(pNode);
return pNode;
}
void CFieldTree::RemoveNode(Node* pNode, int nLevel) {
if (!pNode)
return;
+
if (nLevel <= nMaxRecursion) {
- for (int i = 0; i < pNode->children.GetSize(); i++)
- RemoveNode(pNode->children[i], nLevel + 1);
+ for (size_t i = 0; i < pNode->GetChildrenCount(); ++i)
+ RemoveNode(pNode->GetChildAt(i), nLevel + 1);
}
delete pNode;
}
@@ -632,17 +643,17 @@ CFieldTree::Node* CFieldTree::Lookup(Node* pParent,
if (!pParent)
return nullptr;
- for (int i = 0; i < pParent->children.GetSize(); i++) {
- Node* pNode = pParent->children[i];
- if (pNode->short_name == short_name)
+ for (size_t i = 0; i < pParent->GetChildrenCount(); ++i) {
+ Node* pNode = pParent->GetChildAt(i);
+ if (pNode->short_name() == short_name)
return pNode;
}
return nullptr;
}
void CFieldTree::RemoveAll() {
- for (int i = 0; i < m_Root.children.GetSize(); i++)
- RemoveNode(m_Root.children[i]);
+ for (size_t i = 0; i < m_Root.GetChildrenCount(); ++i)
+ RemoveNode(m_Root.GetChildAt(i));
}
void CFieldTree::SetField(const CFX_WideString& full_name,
@@ -666,7 +677,7 @@ void CFieldTree::SetField(const CFX_WideString& full_name,
name_extractor.GetNext(pName, nLength);
}
if (pNode != &m_Root)
- pNode->field_ptr = field_ptr;
+ pNode->set_field_ptr(field_ptr);
}
CPDF_FormField* CFieldTree::GetField(const CFX_WideString& full_name) {
@@ -685,38 +696,7 @@ CPDF_FormField* CFieldTree::GetField(const CFX_WideString& full_name) {
pNode = Lookup(pLast, name);
name_extractor.GetNext(pName, nLength);
}
- return pNode ? pNode->field_ptr : nullptr;
-}
-
-CPDF_FormField* CFieldTree::RemoveField(const CFX_WideString& full_name) {
- if (full_name == L"")
- return nullptr;
-
- CFieldNameExtractor name_extractor(full_name);
- const FX_WCHAR* pName;
- FX_STRSIZE nLength;
- name_extractor.GetNext(pName, nLength);
- Node* pNode = &m_Root;
- Node* pLast = nullptr;
- while (nLength > 0 && pNode) {
- pLast = pNode;
- CFX_WideString name = CFX_WideString(pName, nLength);
- pNode = Lookup(pLast, name);
- name_extractor.GetNext(pName, nLength);
- }
-
- if (pNode && pNode != &m_Root) {
- for (int i = 0; i < pLast->children.GetSize(); i++) {
- if (pNode == pLast->children[i]) {
- pLast->children.RemoveAt(i);
- break;
- }
- }
- CPDF_FormField* pField = pNode->field_ptr;
- RemoveNode(pNode);
- return pField;
- }
- return nullptr;
+ return pNode ? pNode->field() : nullptr;
}
CFieldTree::Node* CFieldTree::FindNode(const CFX_WideString& full_name) {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698