From d88a364c1c79b294b980c20ebc9356ea42635bff Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Tue, 10 Nov 2015 09:38:57 -0800 Subject: Merge to XFA: Remove CFX_PtrArray usage in fpdfsdk. TBR=tsepez@chromium.org Review URL: https://codereview.chromium.org/1430213002 . (cherry picked from commit 8deeacd23872ecc132a0a678e344a018b5114a3a) Review URL: https://codereview.chromium.org/1416113010 . --- core/src/fpdfdoc/doc_form.cpp | 173 +++++++++++++++++++----------------------- 1 file changed, 76 insertions(+), 97 deletions(-) (limited to 'core/src/fpdfdoc/doc_form.cpp') diff --git a/core/src/fpdfdoc/doc_form.cpp b/core/src/fpdfdoc/doc_form.cpp index 5cc2a66a89..8d8a836cab 100644 --- a/core/src/fpdfdoc/doc_form.cpp +++ b/core/src/fpdfdoc/doc_form.cpp @@ -9,9 +9,9 @@ const int nMaxRecursion = 32; -class _CFieldNameExtractor { +class CFieldNameExtractor { public: - _CFieldNameExtractor(const CFX_WideString& full_name) { + explicit CFieldNameExtractor(const CFX_WideString& full_name) { m_pStart = full_name.c_str(); m_pEnd = m_pStart + full_name.GetLength(); m_pCur = m_pStart; @@ -151,7 +151,7 @@ void CFieldTree::SetField(const CFX_WideString& full_name, if (full_name == L"") { return; } - _CFieldNameExtractor name_extractor(full_name); + CFieldNameExtractor name_extractor(full_name); const FX_WCHAR* pName; FX_STRSIZE nLength; name_extractor.GetNext(pName, nLength); @@ -173,7 +173,7 @@ CPDF_FormField* CFieldTree::GetField(const CFX_WideString& full_name) { if (full_name == L"") { return NULL; } - _CFieldNameExtractor name_extractor(full_name); + CFieldNameExtractor name_extractor(full_name); const FX_WCHAR* pName; FX_STRSIZE nLength; name_extractor.GetNext(pName, nLength); @@ -190,7 +190,7 @@ CPDF_FormField* CFieldTree::RemoveField(const CFX_WideString& full_name) { if (full_name == L"") { return NULL; } - _CFieldNameExtractor name_extractor(full_name); + CFieldNameExtractor name_extractor(full_name); const FX_WCHAR* pName; FX_STRSIZE nLength; name_extractor.GetNext(pName, nLength); @@ -219,7 +219,7 @@ CFieldTree::_Node* CFieldTree::FindNode(const CFX_WideString& full_name) { if (full_name == L"") { return NULL; } - _CFieldNameExtractor name_extractor(full_name); + CFieldNameExtractor name_extractor(full_name); const FX_WCHAR* pName; FX_STRSIZE nLength; name_extractor.GetNext(pName, nLength); @@ -765,19 +765,7 @@ void CPDF_InterForm::GetAllFieldNames(CFX_WideStringArray& allFieldNames) { } } } -FX_BOOL CPDF_InterForm::IsValidFormField(const void* pField) { - if (pField == NULL) { - return FALSE; - } - int nCount = m_pFieldTree->m_Root.CountFields(); - for (int i = 0; i < nCount; i++) { - CPDF_FormField* pFormField = m_pFieldTree->m_Root.GetField(i); - if (pField == pFormField) { - return TRUE; - } - } - return FALSE; -} + CPDF_FormField* CPDF_InterForm::GetFieldByDict( CPDF_Dictionary* pFieldDict) const { if (pFieldDict == NULL) { @@ -946,58 +934,45 @@ int CPDF_InterForm::GetFormAlignment() { } return m_pFormDict->GetInteger("Q", 0); } -FX_BOOL CPDF_InterForm::ResetForm(const CFX_PtrArray& fields, - FX_BOOL bIncludeOrExclude, - FX_BOOL bNotify) { - if (bNotify && m_pFormNotify != NULL) { - int iRet = m_pFormNotify->BeforeFormReset(this); - if (iRet < 0) { - return FALSE; - } - } + +bool CPDF_InterForm::ResetForm(const std::vector& fields, + bool bIncludeOrExclude, + bool bNotify) { + if (bNotify && m_pFormNotify && m_pFormNotify->BeforeFormReset(this) < 0) + return false; + int nCount = m_pFieldTree->m_Root.CountFields(); - for (int i = 0; i < nCount; i++) { + for (int i = 0; i < nCount; ++i) { CPDF_FormField* pField = m_pFieldTree->m_Root.GetField(i); - if (pField == NULL) { + if (!pField) continue; - } - FX_BOOL bFind = FALSE; - int iCount = fields.GetSize(); - for (int i = 0; i < iCount; i++) { - if (pField == (CPDF_FormField*)fields[i]) { - bFind = TRUE; - break; - } - } - if ((bIncludeOrExclude && bFind) || (!bIncludeOrExclude && !bFind)) { + + auto it = std::find(fields.begin(), fields.end(), pField); + if (bIncludeOrExclude == (it != fields.end())) pField->ResetField(bNotify); - } } - if (bNotify && m_pFormNotify != NULL) { + if (bNotify && m_pFormNotify) m_pFormNotify->AfterFormReset(this); - } - return TRUE; + return true; } -FX_BOOL CPDF_InterForm::ResetForm(FX_BOOL bNotify) { - if (bNotify && m_pFormNotify != NULL) { - int iRet = m_pFormNotify->BeforeFormReset(this); - if (iRet < 0) { - return FALSE; - } - } + +bool CPDF_InterForm::ResetForm(bool bNotify) { + if (bNotify && m_pFormNotify && m_pFormNotify->BeforeFormReset(this) < 0) + return false; + int nCount = m_pFieldTree->m_Root.CountFields(); - for (int i = 0; i < nCount; i++) { + for (int i = 0; i < nCount; ++i) { CPDF_FormField* pField = m_pFieldTree->m_Root.GetField(i); - if (pField == NULL) { + if (!pField) continue; - } + pField->ResetField(bNotify); } - if (bNotify && m_pFormNotify != NULL) { + if (bNotify && m_pFormNotify) m_pFormNotify->AfterFormReset(this); - } - return TRUE; + return true; } + void CPDF_InterForm::LoadField(CPDF_Dictionary* pFieldDict, int nLevel) { if (nLevel > nMaxRecursion) { return; @@ -1126,52 +1101,58 @@ CPDF_FormControl* CPDF_InterForm::AddControl(const CPDF_FormField* pField, ((CPDF_FormField*)pField)->m_ControlList.Add(pControl); return pControl; } + CPDF_FormField* CPDF_InterForm::CheckRequiredFields( - const CFX_PtrArray* fields, - FX_BOOL bIncludeOrExclude) const { + const std::vector* fields, + bool bIncludeOrExclude) const { int nCount = m_pFieldTree->m_Root.CountFields(); - for (int i = 0; i < nCount; i++) { + for (int i = 0; i < nCount; ++i) { CPDF_FormField* pField = m_pFieldTree->m_Root.GetField(i); - if (pField == NULL) { + if (!pField) continue; - } + int32_t iType = pField->GetType(); if (iType == CPDF_FormField::PushButton || iType == CPDF_FormField::CheckBox || iType == CPDF_FormField::ListBox) { continue; } FX_DWORD dwFlags = pField->GetFieldFlags(); - if (dwFlags & 0x04) { + // TODO(thestig): Look up these magic numbers and add constants for them. + if (dwFlags & 0x04) continue; + + bool bFind = true; + if (fields) { + auto it = std::find(fields->begin(), fields->end(), pField); + bFind = (it != fields->end()); } - FX_BOOL bFind = TRUE; - if (fields != NULL) { - bFind = fields->Find(pField, 0) >= 0; - } - if ((bIncludeOrExclude && bFind) || (!bIncludeOrExclude && !bFind)) { + if (bIncludeOrExclude == bFind) { CPDF_Dictionary* pFieldDict = pField->m_pDict; if ((dwFlags & 0x02) != 0 && pFieldDict->GetString("V").IsEmpty()) { return pField; } } } - return NULL; + return nullptr; } + CFDF_Document* CPDF_InterForm::ExportToFDF(const CFX_WideStringC& pdf_path, - FX_BOOL bSimpleFileSpec) const { - CFX_PtrArray fields; + bool bSimpleFileSpec) const { + std::vector fields; int nCount = m_pFieldTree->m_Root.CountFields(); - for (int i = 0; i < nCount; i++) { - CPDF_FormField* pField = m_pFieldTree->m_Root.GetField(i); - fields.Add(pField); - } - return ExportToFDF(pdf_path, fields, TRUE, bSimpleFileSpec); + for (int i = 0; i < nCount; ++i) + fields.push_back(m_pFieldTree->m_Root.GetField(i)); + return ExportToFDF(pdf_path, fields, true, bSimpleFileSpec); } + +// TODO(thestig): Fix this. CFX_WideString FILESPEC_EncodeFileName(const CFX_WideStringC& filepath); -CFDF_Document* CPDF_InterForm::ExportToFDF(const CFX_WideStringC& pdf_path, - CFX_PtrArray& fields, - FX_BOOL bIncludeOrExclude, - FX_BOOL bSimpleFileSpec) const { + +CFDF_Document* CPDF_InterForm::ExportToFDF( + const CFX_WideStringC& pdf_path, + const std::vector& fields, + bool bIncludeOrExclude, + bool bSimpleFileSpec) const { CFDF_Document* pDoc = CFDF_Document::CreateNewDoc(); if (pDoc == NULL) { return NULL; @@ -1201,23 +1182,23 @@ CFDF_Document* CPDF_InterForm::ExportToFDF(const CFX_WideStringC& pdf_path, continue; } FX_DWORD dwFlags = pField->GetFieldFlags(); - if (dwFlags & 0x04) { + if (dwFlags & 0x04) continue; - } - FX_BOOL bFind = fields.Find(pField, 0) >= 0; - if ((bIncludeOrExclude && bFind) || (!bIncludeOrExclude && !bFind)) { - if ((dwFlags & 0x02) != 0 && pField->m_pDict->GetString("V").IsEmpty()) { + + auto it = std::find(fields.begin(), fields.end(), pField); + if (bIncludeOrExclude == (it != fields.end())) { + if ((dwFlags & 0x02) != 0 && pField->m_pDict->GetString("V").IsEmpty()) continue; - } + CFX_WideString fullname = GetFullName(pField->GetFieldDict()); CPDF_Dictionary* pFieldDict = CPDF_Dictionary::Create(); - if (pFieldDict == NULL) { - return NULL; - } + if (!pFieldDict) + return nullptr; + CPDF_String* pString = CPDF_String::Create(fullname); - if (pString == NULL) { + if (!pString) { pFieldDict->Release(); - return NULL; + return nullptr; } pFieldDict->SetAt("T", pString); if (pField->GetType() == CPDF_FormField::CheckBox || @@ -1225,16 +1206,14 @@ CFDF_Document* CPDF_InterForm::ExportToFDF(const CFX_WideStringC& pdf_path, CFX_WideString csExport = pField->GetCheckValue(FALSE); CFX_ByteString csBExport = PDF_EncodeText(csExport); CPDF_Object* pOpt = FPDF_GetFieldAttr(pField->m_pDict, "Opt"); - if (pOpt == NULL) { - pFieldDict->SetAtName("V", csBExport); - } else { + if (pOpt) pFieldDict->SetAtString("V", csBExport); - } + else + pFieldDict->SetAtName("V", csBExport); } else { CPDF_Object* pV = FPDF_GetFieldAttr(pField->m_pDict, "V"); - if (pV != NULL) { + if (pV) pFieldDict->SetAt("V", pV->Clone(TRUE)); - } } pFields->Add(pFieldDict); } -- cgit v1.2.3