Clean up CPDF_AnnotList.
[pdfium.git] / core / src / fpdfdoc / doc_form.cpp
index 970b4b9..5cc2a66 100644 (file)
@@ -233,21 +233,25 @@ CFieldTree::_Node* CFieldTree::FindNode(const CFX_WideString& full_name) {
   return pNode;
 }
 CPDF_InterForm::CPDF_InterForm(CPDF_Document* pDocument, FX_BOOL bGenerateAP)
-    : CFX_PrivateData() {
-  m_pDocument = pDocument;
-  m_bGenerateAP = bGenerateAP;
-  m_pFormNotify = NULL;
-  m_bUpdated = FALSE;
-  m_pFieldTree = new CFieldTree;
+    : CFX_PrivateData(),
+      m_pDocument(pDocument),
+      m_bGenerateAP(bGenerateAP),
+      m_pFormDict(nullptr),
+      m_pFieldTree(new CFieldTree),
+      m_pFormNotify(nullptr),
+      m_bUpdated(FALSE) {
   CPDF_Dictionary* pRoot = m_pDocument->GetRoot();
+  if (!pRoot)
+    return;
+
   m_pFormDict = pRoot->GetDict("AcroForm");
-  if (m_pFormDict == NULL) {
+  if (!m_pFormDict)
     return;
-  }
+
   CPDF_Array* pFields = m_pFormDict->GetArray("Fields");
-  if (pFields == NULL) {
+  if (!pFields)
     return;
-  }
+
   int count = pFields->GetCount();
   for (int i = 0; i < count; i++) {
     LoadField(pFields->GetDict(i));
@@ -257,12 +261,10 @@ CPDF_InterForm::CPDF_InterForm(CPDF_Document* pDocument, FX_BOOL bGenerateAP)
 CPDF_InterForm::~CPDF_InterForm() {
   for (auto it : m_ControlMap)
     delete it.second;
-  if (m_pFieldTree) {
-    int nCount = m_pFieldTree->m_Root.CountFields();
-    for (int i = 0; i < nCount; ++i) {
-      delete m_pFieldTree->m_Root.GetField(i);
-    }
-    delete m_pFieldTree;
+
+  int nCount = m_pFieldTree->m_Root.CountFields();
+  for (int i = 0; i < nCount; ++i) {
+    delete m_pFieldTree->m_Root.GetField(i);
   }
 }
 
@@ -816,7 +818,7 @@ CPDF_FormControl* CPDF_InterForm::GetControlAtPoint(CPDF_Page* pPage,
 }
 
 CPDF_FormControl* CPDF_InterForm::GetControlByDict(
-    CPDF_Dictionary* pWidgetDict) const {
+    const CPDF_Dictionary* pWidgetDict) const {
   const auto it = m_ControlMap.find(pWidgetDict);
   return it != m_ControlMap.end() ? it->second : nullptr;
 }
@@ -852,9 +854,9 @@ CPDF_FormField* CPDF_InterForm::GetFieldInCalculationOrder(int index) {
   if (pArray == NULL) {
     return NULL;
   }
-  CPDF_Object* pElement = pArray->GetElementValue(index);
-  if (pElement != NULL && pElement->GetType() == PDFOBJ_DICTIONARY) {
-    return GetFieldByDict((CPDF_Dictionary*)pElement);
+  if (CPDF_Dictionary* pElement =
+          ToDictionary(pArray->GetElementValue(index))) {
+    return GetFieldByDict(pElement);
   }
   return NULL;
 }
@@ -1047,25 +1049,24 @@ void CPDF_InterForm::FixPageFields(const CPDF_Page* pPage) {
     }
   }
 }
-CPDF_FormField* CPDF_InterForm::AddTerminalField(
-    const CPDF_Dictionary* pFieldDict) {
+CPDF_FormField* CPDF_InterForm::AddTerminalField(CPDF_Dictionary* pFieldDict) {
   if (!pFieldDict->KeyExist(FX_BSTRC("T"))) {
     return NULL;
   }
-  CPDF_Dictionary* pDict = (CPDF_Dictionary*)pFieldDict;
-  CFX_WideString csWName = GetFullName(pDict);
+  CPDF_Dictionary* pDict = pFieldDict;
+  CFX_WideString csWName = GetFullName(pFieldDict);
   if (csWName.IsEmpty()) {
     return NULL;
   }
   CPDF_FormField* pField = NULL;
   pField = m_pFieldTree->GetField(csWName);
   if (pField == NULL) {
-    CPDF_Dictionary* pParent = (CPDF_Dictionary*)pFieldDict;
+    CPDF_Dictionary* pParent = pFieldDict;
     if (!pFieldDict->KeyExist(FX_BSTRC("T")) &&
         pFieldDict->GetString(FX_BSTRC("Subtype")) == FX_BSTRC("Widget")) {
       pParent = pFieldDict->GetDict(FX_BSTRC("Parent"));
       if (!pParent) {
-        pParent = (CPDF_Dictionary*)pFieldDict;
+        pParent = pFieldDict;
       }
     }
     if (pParent && pParent != pFieldDict &&
@@ -1085,13 +1086,12 @@ CPDF_FormField* CPDF_InterForm::AddTerminalField(
     }
     pField = new CPDF_FormField(this, pParent);
     CPDF_Object* pTObj = pDict->GetElement("T");
-    if (pTObj && pTObj->GetType() == PDFOBJ_REFERENCE) {
+    if (ToReference(pTObj)) {
       CPDF_Object* pClone = pTObj->Clone(TRUE);
-      if (pClone) {
+      if (pClone)
         pDict->SetAt("T", pClone);
-      } else {
+      else
         pDict->SetAtName("T", "");
-      }
     }
     m_pFieldTree->SetField(csWName, pField);
   }
@@ -1114,15 +1114,14 @@ CPDF_FormField* CPDF_InterForm::AddTerminalField(
   }
   return pField;
 }
-CPDF_FormControl* CPDF_InterForm::AddControl(
-    const CPDF_FormField* pField,
-    const CPDF_Dictionary* pWidgetDict) {
+CPDF_FormControl* CPDF_InterForm::AddControl(const CPDF_FormField* pField,
+                                             CPDF_Dictionary* pWidgetDict) {
   const auto it = m_ControlMap.find(pWidgetDict);
   if (it != m_ControlMap.end())
     return it->second;
 
-  CPDF_FormControl* pControl = new CPDF_FormControl(
-      (CPDF_FormField*)pField, (CPDF_Dictionary*)pWidgetDict);
+  CPDF_FormControl* pControl =
+      new CPDF_FormControl((CPDF_FormField*)pField, pWidgetDict);
   m_ControlMap[pWidgetDict] = pControl;
   ((CPDF_FormField*)pField)->m_ControlList.Add(pControl);
   return pControl;
@@ -1187,7 +1186,7 @@ CFDF_Document* CPDF_InterForm::ExportToFDF(const CFX_WideStringC& pdf_path,
     } else {
       CPDF_FileSpec filespec;
       filespec.SetFileName(pdf_path);
-      pMainDict->SetAt("F", (CPDF_Object*)filespec);
+      pMainDict->SetAt("F", static_cast<CPDF_Object*>(filespec));
     }
   }
   CPDF_Array* pFields = CPDF_Array::Create();