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

Side by Side Diff: core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp

Issue 1541703003: Use std::map as CPDF_Dictionary's underlying store. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: XFA still uses CFX_CMapByteStringToPtr :( Created 5 years 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 unified diff | Download patch
OLDNEW
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/fpdfapi/fpdf_parser.h" 7 #include "core/include/fpdfapi/fpdf_parser.h"
8 8
9 #include "core/include/fxcrt/fx_string.h" 9 #include "core/include/fxcrt/fx_string.h"
10 10
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 int n = pThis->GetCount(); 251 int n = pThis->GetCount();
252 for (int i = 0; i < n; i++) { 252 for (int i = 0; i < n; i++) {
253 CPDF_Object* value = pThis->m_Objects.GetAt(i); 253 CPDF_Object* value = pThis->m_Objects.GetAt(i);
254 pCopy->m_Objects.Add(value->CloneInternal(bDirect, visited)); 254 pCopy->m_Objects.Add(value->CloneInternal(bDirect, visited));
255 } 255 }
256 return pCopy; 256 return pCopy;
257 } 257 }
258 case PDFOBJ_DICTIONARY: { 258 case PDFOBJ_DICTIONARY: {
259 CPDF_Dictionary* pCopy = new CPDF_Dictionary(); 259 CPDF_Dictionary* pCopy = new CPDF_Dictionary();
260 const CPDF_Dictionary* pThis = AsDictionary(); 260 const CPDF_Dictionary* pThis = AsDictionary();
261 FX_POSITION pos = pThis->m_Map.GetStartPosition(); 261 for (const auto& it : pThis->m_Map) {
Tom Sepez 2016/01/05 19:41:56 nit: (const auto& it : *pThis) ?
Oliver Chang 2016/01/07 16:04:25 Done.
262 while (pos) { 262 pCopy->m_Map.insert(std::make_pair(
263 CFX_ByteString key; 263 it.first, it.second->CloneInternal(bDirect, visited)));
264 CPDF_Object* value;
265 pThis->m_Map.GetNextAssoc(pos, key, (void*&)value);
266 pCopy->m_Map.SetAt(key, value->CloneInternal(bDirect, visited));
267 } 264 }
268 return pCopy; 265 return pCopy;
269 } 266 }
270 case PDFOBJ_NULL: { 267 case PDFOBJ_NULL: {
271 return new CPDF_Null; 268 return new CPDF_Null;
272 } 269 }
273 case PDFOBJ_STREAM: { 270 case PDFOBJ_STREAM: {
274 const CPDF_Stream* pThis = AsStream(); 271 const CPDF_Stream* pThis = AsStream();
275 CPDF_StreamAcc acc; 272 CPDF_StreamAcc acc;
276 acc.LoadAllData(pThis, TRUE); 273 acc.LoadAllData(pThis, TRUE);
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 if (m_Objects.GetSize() != pOther->m_Objects.GetSize()) { 561 if (m_Objects.GetSize() != pOther->m_Objects.GetSize()) {
565 return FALSE; 562 return FALSE;
566 } 563 }
567 for (int i = 0; i < m_Objects.GetSize(); i++) { 564 for (int i = 0; i < m_Objects.GetSize(); i++) {
568 if (!m_Objects[i]->IsIdentical(pOther->m_Objects[i])) 565 if (!m_Objects[i]->IsIdentical(pOther->m_Objects[i]))
569 return FALSE; 566 return FALSE;
570 } 567 }
571 return TRUE; 568 return TRUE;
572 } 569 }
573 CPDF_Dictionary::~CPDF_Dictionary() { 570 CPDF_Dictionary::~CPDF_Dictionary() {
574 FX_POSITION pos = m_Map.GetStartPosition(); 571 for (const auto& it : m_Map) {
575 while (pos) { 572 it.second->Release();
576 if (CPDF_Object* value = static_cast<CPDF_Object*>(m_Map.GetNextValue(pos)))
577 value->Release();
578 } 573 }
579 } 574 }
580 FX_POSITION CPDF_Dictionary::GetStartPos() const {
581 return m_Map.GetStartPosition();
582 }
583 CPDF_Object* CPDF_Dictionary::GetNextElement(FX_POSITION& pos,
584 CFX_ByteString& key) const {
585 if (!pos) {
586 return NULL;
587 }
588 CPDF_Object* p;
589 m_Map.GetNextAssoc(pos, key, (void*&)p);
590 return p;
591 }
592 CPDF_Object* CPDF_Dictionary::GetElement(const CFX_ByteStringC& key) const { 575 CPDF_Object* CPDF_Dictionary::GetElement(const CFX_ByteStringC& key) const {
593 CPDF_Object* p = NULL; 576 auto it = m_Map.find(key);
594 m_Map.Lookup(key, (void*&)p); 577 if (it == m_Map.end())
595 return p; 578 return nullptr;
579 return it->second;
596 } 580 }
597 CPDF_Object* CPDF_Dictionary::GetElementValue( 581 CPDF_Object* CPDF_Dictionary::GetElementValue(
598 const CFX_ByteStringC& key) const { 582 const CFX_ByteStringC& key) const {
599 CPDF_Object* p = NULL; 583 CPDF_Object* p = GetElement(key);
600 m_Map.Lookup(key, (void*&)p); 584 return p ? p->GetDirect() : nullptr;
601 return p ? p->GetDirect() : NULL;
602 } 585 }
603 CFX_ByteString CPDF_Dictionary::GetString(const CFX_ByteStringC& key) const { 586 CFX_ByteString CPDF_Dictionary::GetString(const CFX_ByteStringC& key) const {
604 CPDF_Object* p = NULL; 587 CPDF_Object* p = GetElement(key);
605 m_Map.Lookup(key, (void*&)p);
606 if (p) { 588 if (p) {
607 return p->GetString(); 589 return p->GetString();
608 } 590 }
609 return CFX_ByteString(); 591 return CFX_ByteString();
610 } 592 }
611 CFX_ByteStringC CPDF_Dictionary::GetConstString( 593 CFX_ByteStringC CPDF_Dictionary::GetConstString(
612 const CFX_ByteStringC& key) const { 594 const CFX_ByteStringC& key) const {
613 CPDF_Object* p = NULL; 595 CPDF_Object* p = GetElement(key);
614 m_Map.Lookup(key, (void*&)p);
615 if (p) { 596 if (p) {
616 return p->GetConstString(); 597 return p->GetConstString();
617 } 598 }
618 return CFX_ByteStringC(); 599 return CFX_ByteStringC();
619 } 600 }
620 CFX_WideString CPDF_Dictionary::GetUnicodeText(const CFX_ByteStringC& key, 601 CFX_WideString CPDF_Dictionary::GetUnicodeText(const CFX_ByteStringC& key,
621 CFX_CharMap* pCharMap) const { 602 CFX_CharMap* pCharMap) const {
622 CPDF_Object* p = NULL; 603 CPDF_Object* p = GetElement(key);
623 m_Map.Lookup(key, (void*&)p);
624 if (CPDF_Reference* pRef = ToReference(p)) 604 if (CPDF_Reference* pRef = ToReference(p))
625 p = pRef->GetDirect(); 605 p = pRef->GetDirect();
626 return p ? p->GetUnicodeText(pCharMap) : CFX_WideString(); 606 return p ? p->GetUnicodeText(pCharMap) : CFX_WideString();
627 } 607 }
628 CFX_ByteString CPDF_Dictionary::GetString(const CFX_ByteStringC& key, 608 CFX_ByteString CPDF_Dictionary::GetString(const CFX_ByteStringC& key,
629 const CFX_ByteStringC& def) const { 609 const CFX_ByteStringC& def) const {
630 CPDF_Object* p = NULL; 610 CPDF_Object* p = GetElement(key);
631 m_Map.Lookup(key, (void*&)p);
632 if (p) { 611 if (p) {
633 return p->GetString(); 612 return p->GetString();
634 } 613 }
635 return CFX_ByteString(def); 614 return CFX_ByteString(def);
636 } 615 }
637 CFX_ByteStringC CPDF_Dictionary::GetConstString( 616 CFX_ByteStringC CPDF_Dictionary::GetConstString(
638 const CFX_ByteStringC& key, 617 const CFX_ByteStringC& key,
639 const CFX_ByteStringC& def) const { 618 const CFX_ByteStringC& def) const {
640 CPDF_Object* p = NULL; 619 CPDF_Object* p = GetElement(key);
641 m_Map.Lookup(key, (void*&)p);
642 if (p) { 620 if (p) {
643 return p->GetConstString(); 621 return p->GetConstString();
644 } 622 }
645 return CFX_ByteStringC(def); 623 return CFX_ByteStringC(def);
646 } 624 }
647 int CPDF_Dictionary::GetInteger(const CFX_ByteStringC& key) const { 625 int CPDF_Dictionary::GetInteger(const CFX_ByteStringC& key) const {
648 CPDF_Object* p = NULL; 626 CPDF_Object* p = GetElement(key);
649 m_Map.Lookup(key, (void*&)p);
650 if (p) { 627 if (p) {
651 return p->GetInteger(); 628 return p->GetInteger();
652 } 629 }
653 return 0; 630 return 0;
654 } 631 }
655 int CPDF_Dictionary::GetInteger(const CFX_ByteStringC& key, int def) const { 632 int CPDF_Dictionary::GetInteger(const CFX_ByteStringC& key, int def) const {
656 CPDF_Object* p = NULL; 633 CPDF_Object* p = GetElement(key);
657 m_Map.Lookup(key, (void*&)p);
658 if (p) { 634 if (p) {
659 return p->GetInteger(); 635 return p->GetInteger();
660 } 636 }
661 return def; 637 return def;
662 } 638 }
663 FX_FLOAT CPDF_Dictionary::GetNumber(const CFX_ByteStringC& key) const { 639 FX_FLOAT CPDF_Dictionary::GetNumber(const CFX_ByteStringC& key) const {
664 CPDF_Object* p = NULL; 640 CPDF_Object* p = GetElement(key);
665 m_Map.Lookup(key, (void*&)p);
666 if (p) { 641 if (p) {
667 return p->GetNumber(); 642 return p->GetNumber();
668 } 643 }
669 return 0; 644 return 0;
670 } 645 }
671 FX_BOOL CPDF_Dictionary::GetBoolean(const CFX_ByteStringC& key, 646 FX_BOOL CPDF_Dictionary::GetBoolean(const CFX_ByteStringC& key,
672 FX_BOOL bDefault) const { 647 FX_BOOL bDefault) const {
673 CPDF_Object* p = NULL; 648 CPDF_Object* p = GetElement(key);
674 m_Map.Lookup(key, (void*&)p);
675 if (ToBoolean(p)) 649 if (ToBoolean(p))
676 return p->GetInteger(); 650 return p->GetInteger();
677 return bDefault; 651 return bDefault;
678 } 652 }
679 CPDF_Dictionary* CPDF_Dictionary::GetDict(const CFX_ByteStringC& key) const { 653 CPDF_Dictionary* CPDF_Dictionary::GetDict(const CFX_ByteStringC& key) const {
680 CPDF_Object* p = GetElementValue(key); 654 CPDF_Object* p = GetElementValue(key);
681 if (!p) 655 if (!p)
682 return nullptr; 656 return nullptr;
683 if (CPDF_Dictionary* pDict = p->AsDictionary()) 657 if (CPDF_Dictionary* pDict = p->AsDictionary())
684 return pDict; 658 return pDict;
(...skipping 15 matching lines...) Expand all
700 return rect; 674 return rect;
701 } 675 }
702 CFX_Matrix CPDF_Dictionary::GetMatrix(const CFX_ByteStringC& key) const { 676 CFX_Matrix CPDF_Dictionary::GetMatrix(const CFX_ByteStringC& key) const {
703 CFX_Matrix matrix; 677 CFX_Matrix matrix;
704 CPDF_Array* pArray = GetArray(key); 678 CPDF_Array* pArray = GetArray(key);
705 if (pArray) 679 if (pArray)
706 matrix = pArray->GetMatrix(); 680 matrix = pArray->GetMatrix();
707 return matrix; 681 return matrix;
708 } 682 }
709 FX_BOOL CPDF_Dictionary::KeyExist(const CFX_ByteStringC& key) const { 683 FX_BOOL CPDF_Dictionary::KeyExist(const CFX_ByteStringC& key) const {
710 void* value; 684 return m_Map.find(key) != m_Map.end();
711 return m_Map.Lookup(key, value);
712 } 685 }
713 686
714 void CPDF_Dictionary::SetAt(const CFX_ByteStringC& key, CPDF_Object* pObj) { 687 void CPDF_Dictionary::SetAt(const CFX_ByteStringC& key, CPDF_Object* pObj) {
715 ASSERT(IsDictionary()); 688 ASSERT(IsDictionary());
716 void* pValue = nullptr; 689 // Avoid 2 constructions of CFX_ByteString.
717 m_Map.Lookup(key, pValue); 690 CFX_ByteString key_bytestring = key;
718 CPDF_Object* pExisting = static_cast<CPDF_Object*>(pValue); 691 auto it = m_Map.find(key_bytestring);
719 if (pExisting == pObj) 692 if (it == m_Map.end()) {
693 if (pObj) {
694 m_Map.insert(std::make_pair(key_bytestring, pObj));
695 }
720 return; 696 return;
697 }
721 698
722 if (pExisting) 699 if (it->second == pObj)
723 pExisting->Release(); 700 return;
701 it->second->Release();
724 702
725 if (pObj) 703 if (pObj)
726 m_Map.SetAt(key, pObj); 704 it->second = pObj;
727 else 705 else
728 m_Map.RemoveKey(key); 706 m_Map.erase(it);
729 }
730
731 void CPDF_Dictionary::AddValue(const CFX_ByteStringC& key, CPDF_Object* pObj) {
732 ASSERT(m_Type == PDFOBJ_DICTIONARY);
733 m_Map.AddValue(key, pObj);
734 } 707 }
735 void CPDF_Dictionary::RemoveAt(const CFX_ByteStringC& key) { 708 void CPDF_Dictionary::RemoveAt(const CFX_ByteStringC& key) {
736 ASSERT(m_Type == PDFOBJ_DICTIONARY); 709 ASSERT(m_Type == PDFOBJ_DICTIONARY);
737 CPDF_Object* p = NULL; 710 auto it = m_Map.find(key);
738 m_Map.Lookup(key, (void*&)p); 711 if (it == m_Map.end())
739 if (!p) {
740 return; 712 return;
741 } 713
742 p->Release(); 714 it->second->Release();
743 m_Map.RemoveKey(key); 715 m_Map.erase(it);
744 } 716 }
745 void CPDF_Dictionary::ReplaceKey(const CFX_ByteStringC& oldkey, 717 void CPDF_Dictionary::ReplaceKey(const CFX_ByteStringC& oldkey,
746 const CFX_ByteStringC& newkey) { 718 const CFX_ByteStringC& newkey) {
747 ASSERT(m_Type == PDFOBJ_DICTIONARY); 719 ASSERT(m_Type == PDFOBJ_DICTIONARY);
748 CPDF_Object* p = NULL; 720 auto old_it = m_Map.find(oldkey);
749 m_Map.Lookup(oldkey, (void*&)p); 721 if (old_it == m_Map.end())
750 if (!p) {
751 return; 722 return;
723
724 // Avoid 2 constructions of CFX_ByteString.
725 CFX_ByteString newkey_bytestring = newkey;
726 auto new_it = m_Map.find(newkey_bytestring);
727 if (new_it != m_Map.end()) {
728 new_it->second->Release();
729 new_it->second = old_it->second;
730 } else {
731 m_Map.insert(std::make_pair(newkey_bytestring, old_it->second));
752 } 732 }
753 m_Map.RemoveKey(oldkey); 733 m_Map.erase(old_it);
754 m_Map.SetAt(newkey, p);
755 } 734 }
756 FX_BOOL CPDF_Dictionary::Identical(CPDF_Dictionary* pOther) const { 735 FX_BOOL CPDF_Dictionary::Identical(CPDF_Dictionary* pOther) const {
757 if (!pOther) { 736 if (!pOther) {
758 return FALSE; 737 return FALSE;
759 } 738 }
760 if (m_Map.GetCount() != pOther->m_Map.GetCount()) { 739 if (m_Map.size() != pOther->m_Map.size()) {
761 return FALSE; 740 return FALSE;
762 } 741 }
763 FX_POSITION pos = m_Map.GetStartPosition(); 742 for (const auto& it : m_Map) {
764 while (pos) { 743 const CFX_ByteString& key = it.first;
765 CFX_ByteString key; 744 if (!it.second->IsIdentical(pOther->GetElement(key)))
766 void* value;
767 m_Map.GetNextAssoc(pos, key, value);
768 if (!value)
769 return FALSE;
770 if (!static_cast<CPDF_Object*>(value)->IsIdentical(pOther->GetElement(key)))
771 return FALSE; 745 return FALSE;
772 } 746 }
773 return TRUE; 747 return TRUE;
774 } 748 }
775 void CPDF_Dictionary::SetAtInteger(const CFX_ByteStringC& key, int i) { 749 void CPDF_Dictionary::SetAtInteger(const CFX_ByteStringC& key, int i) {
776 SetAt(key, new CPDF_Number(i)); 750 SetAt(key, new CPDF_Number(i));
777 } 751 }
778 void CPDF_Dictionary::SetAtName(const CFX_ByteStringC& key, 752 void CPDF_Dictionary::SetAtName(const CFX_ByteStringC& key,
779 const CFX_ByteString& name) { 753 const CFX_ByteString& name) {
780 SetAt(key, new CPDF_Name(name)); 754 SetAt(key, new CPDF_Name(name));
781 } 755 }
782 void CPDF_Dictionary::SetAtString(const CFX_ByteStringC& key, 756 void CPDF_Dictionary::SetAtString(const CFX_ByteStringC& key,
783 const CFX_ByteString& str) { 757 const CFX_ByteString& str) {
784 SetAt(key, new CPDF_String(str, FALSE)); 758 SetAt(key, new CPDF_String(str, FALSE));
785 } 759 }
786 void CPDF_Dictionary::SetAtReference(const CFX_ByteStringC& key, 760 void CPDF_Dictionary::SetAtReference(const CFX_ByteStringC& key,
787 CPDF_IndirectObjects* pDoc, 761 CPDF_IndirectObjects* pDoc,
788 FX_DWORD objnum) { 762 FX_DWORD objnum) {
789 SetAt(key, new CPDF_Reference(pDoc, objnum)); 763 SetAt(key, new CPDF_Reference(pDoc, objnum));
790 } 764 }
791 void CPDF_Dictionary::AddReference(const CFX_ByteStringC& key, 765 void CPDF_Dictionary::AddReference(const CFX_ByteStringC& key,
792 CPDF_IndirectObjects* pDoc, 766 CPDF_IndirectObjects* pDoc,
793 FX_DWORD objnum) { 767 FX_DWORD objnum) {
794 AddValue(key, new CPDF_Reference(pDoc, objnum)); 768 SetAt(key, new CPDF_Reference(pDoc, objnum));
795 } 769 }
796 void CPDF_Dictionary::SetAtNumber(const CFX_ByteStringC& key, FX_FLOAT f) { 770 void CPDF_Dictionary::SetAtNumber(const CFX_ByteStringC& key, FX_FLOAT f) {
797 CPDF_Number* pNumber = new CPDF_Number; 771 CPDF_Number* pNumber = new CPDF_Number;
798 pNumber->SetNumber(f); 772 pNumber->SetNumber(f);
799 SetAt(key, pNumber); 773 SetAt(key, pNumber);
800 } 774 }
801 void CPDF_Dictionary::SetAtBoolean(const CFX_ByteStringC& key, FX_BOOL bValue) { 775 void CPDF_Dictionary::SetAtBoolean(const CFX_ByteStringC& key, FX_BOOL bValue) {
802 SetAt(key, new CPDF_Boolean(bValue)); 776 SetAt(key, new CPDF_Boolean(bValue));
803 } 777 }
804 void CPDF_Dictionary::SetAtRect(const CFX_ByteStringC& key, 778 void CPDF_Dictionary::SetAtRect(const CFX_ByteStringC& key,
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
1170 } 1144 }
1171 pObj->m_ObjNum = objnum; 1145 pObj->m_ObjNum = objnum;
1172 m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj); 1146 m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj);
1173 if (m_LastObjNum < objnum) 1147 if (m_LastObjNum < objnum)
1174 m_LastObjNum = objnum; 1148 m_LastObjNum = objnum;
1175 return TRUE; 1149 return TRUE;
1176 } 1150 }
1177 FX_DWORD CPDF_IndirectObjects::GetLastObjNum() const { 1151 FX_DWORD CPDF_IndirectObjects::GetLastObjNum() const {
1178 return m_LastObjNum; 1152 return m_LastObjNum;
1179 } 1153 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698