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

Side by Side Diff: core/fpdfdoc/cpdf_interform.cpp

Issue 2372423002: Made CFieldTree::Node a class. (Closed)
Patch Set: std::vector Created 4 years, 2 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 PDFium Authors. All rights reserved. 1 // Copyright 2016 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 <vector> 7 #include <vector>
8 8
9 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h" 9 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h"
10 #include "core/fpdfapi/fpdf_font/include/cpdf_fontencoding.h" 10 #include "core/fpdfapi/fpdf_font/include/cpdf_fontencoding.h"
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 strcpy(lf.lfFaceName, pcsFontName); 530 strcpy(lf.lfFaceName, pcsFontName);
531 } 531 }
532 return RetrieveSpecificFont(lf); 532 return RetrieveSpecificFont(lf);
533 } 533 }
534 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 534 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
535 535
536 } // namespace 536 } // namespace
537 537
538 class CFieldTree { 538 class CFieldTree {
539 public: 539 public:
540 struct Node { 540 class Node {
541 Node* parent; 541 public:
542 CFX_ArrayTemplate<Node*> children; 542 Node() : field_ptr_(nullptr) {}
543 CFX_WideString short_name; 543 Node(const CFX_WideString& short_name, CPDF_FormField* field_ptr)
544 CPDF_FormField* field_ptr; 544 : short_name_(short_name), field_ptr_(field_ptr) {}
545 ~Node() {}
546
547 void AddChildNode(Node* pNode) { children_.push_back(pNode); }
548
549 size_t GetChildrenCount() const { return children_.size(); }
550
551 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.
552
545 int CountFields(int nLevel = 0) { 553 int CountFields(int nLevel = 0) {
546 if (nLevel > nMaxRecursion) 554 if (nLevel > nMaxRecursion)
547 return 0; 555 return 0;
548 if (field_ptr) 556 if (field_ptr_)
549 return 1; 557 return 1;
550 558
551 int count = 0; 559 int count = 0;
552 for (int i = 0; i < children.GetSize(); i++) 560 for (size_t i = 0; i < GetChildrenCount(); ++i)
553 count += children.GetAt(i)->CountFields(nLevel + 1); 561 count += GetChildAt(i)->CountFields(nLevel + 1);
554 return count; 562 return count;
555 } 563 }
556 564
557 CPDF_FormField* GetField(int* fields_to_go) { 565 CPDF_FormField* GetField(int* fields_to_go) {
558 if (field_ptr) { 566 if (field_ptr_) {
559 if (*fields_to_go == 0) 567 if (*fields_to_go == 0)
560 return field_ptr; 568 return field_ptr_;
561 569
562 --*fields_to_go; 570 --*fields_to_go;
563 return nullptr; 571 return nullptr;
564 } 572 }
565 for (int i = 0; i < children.GetSize(); i++) { 573 for (size_t i = 0; i < GetChildrenCount(); ++i) {
566 if (CPDF_FormField* pField = children.GetAt(i)->GetField(fields_to_go)) 574 if (CPDF_FormField* pField = GetChildAt(i)->GetField(fields_to_go))
567 return pField; 575 return pField;
568 } 576 }
569 return nullptr; 577 return nullptr;
570 } 578 }
571 579
572 CPDF_FormField* GetField(int index) { 580 CPDF_FormField* GetField(int index) {
573 int fields_to_go = index; 581 int fields_to_go = index;
574 return GetField(&fields_to_go); 582 return GetField(&fields_to_go);
575 } 583 }
584
585 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.
586 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.
587 const CFX_WideString& short_name() const { return short_name_; }
588
589 private:
590 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
591 CFX_WideString short_name_;
592 CPDF_FormField* field_ptr_;
576 }; 593 };
577 594
578 CFieldTree(); 595 CFieldTree();
579 ~CFieldTree(); 596 ~CFieldTree();
580 597
581 void SetField(const CFX_WideString& full_name, CPDF_FormField* field_ptr); 598 void SetField(const CFX_WideString& full_name, CPDF_FormField* field_ptr);
582 CPDF_FormField* GetField(const CFX_WideString& full_name); 599 CPDF_FormField* GetField(const CFX_WideString& full_name);
583 CPDF_FormField* RemoveField(const CFX_WideString& full_name);
584 void RemoveAll(); 600 void RemoveAll();
585 601
586 Node* FindNode(const CFX_WideString& full_name); 602 Node* FindNode(const CFX_WideString& full_name);
587 Node* AddChild(Node* pParent, 603 Node* AddChild(Node* pParent,
588 const CFX_WideString& short_name, 604 const CFX_WideString& short_name,
589 CPDF_FormField* field_ptr); 605 CPDF_FormField* field_ptr);
590 void RemoveNode(Node* pNode, int nLevel = 0); 606 void RemoveNode(Node* pNode, int nLevel = 0);
591 607
592 Node* Lookup(Node* pParent, const CFX_WideString& short_name); 608 Node* Lookup(Node* pParent, const CFX_WideString& short_name);
593 609
594 Node m_Root; 610 Node m_Root;
595 }; 611 };
596 612
597 CFieldTree::CFieldTree() { 613 CFieldTree::CFieldTree() {}
598 m_Root.parent = nullptr;
599 m_Root.field_ptr = nullptr;
600 }
601 614
602 CFieldTree::~CFieldTree() { 615 CFieldTree::~CFieldTree() {
603 RemoveAll(); 616 RemoveAll();
604 } 617 }
605 618
606 CFieldTree::Node* CFieldTree::AddChild(Node* pParent, 619 CFieldTree::Node* CFieldTree::AddChild(Node* pParent,
607 const CFX_WideString& short_name, 620 const CFX_WideString& short_name,
608 CPDF_FormField* field_ptr) { 621 CPDF_FormField* field_ptr) {
609 if (!pParent) 622 if (!pParent)
610 return nullptr; 623 return nullptr;
611 624
612 Node* pNode = new Node; 625 Node* pNode = new Node(short_name, field_ptr);
613 pNode->parent = pParent; 626 pParent->AddChildNode(pNode);
614 pNode->short_name = short_name;
615 pNode->field_ptr = field_ptr;
616 pParent->children.Add(pNode);
617 return pNode; 627 return pNode;
618 } 628 }
619 629
620 void CFieldTree::RemoveNode(Node* pNode, int nLevel) { 630 void CFieldTree::RemoveNode(Node* pNode, int nLevel) {
621 if (!pNode) 631 if (!pNode)
622 return; 632 return;
633
623 if (nLevel <= nMaxRecursion) { 634 if (nLevel <= nMaxRecursion) {
624 for (int i = 0; i < pNode->children.GetSize(); i++) 635 for (size_t i = 0; i < pNode->GetChildrenCount(); ++i)
625 RemoveNode(pNode->children[i], nLevel + 1); 636 RemoveNode(pNode->GetChildAt(i), nLevel + 1);
626 } 637 }
627 delete pNode; 638 delete pNode;
628 } 639 }
629 640
630 CFieldTree::Node* CFieldTree::Lookup(Node* pParent, 641 CFieldTree::Node* CFieldTree::Lookup(Node* pParent,
631 const CFX_WideString& short_name) { 642 const CFX_WideString& short_name) {
632 if (!pParent) 643 if (!pParent)
633 return nullptr; 644 return nullptr;
634 645
635 for (int i = 0; i < pParent->children.GetSize(); i++) { 646 for (size_t i = 0; i < pParent->GetChildrenCount(); ++i) {
636 Node* pNode = pParent->children[i]; 647 Node* pNode = pParent->GetChildAt(i);
637 if (pNode->short_name == short_name) 648 if (pNode->short_name() == short_name)
638 return pNode; 649 return pNode;
639 } 650 }
640 return nullptr; 651 return nullptr;
641 } 652 }
642 653
643 void CFieldTree::RemoveAll() { 654 void CFieldTree::RemoveAll() {
644 for (int i = 0; i < m_Root.children.GetSize(); i++) 655 for (size_t i = 0; i < m_Root.GetChildrenCount(); ++i)
645 RemoveNode(m_Root.children[i]); 656 RemoveNode(m_Root.GetChildAt(i));
646 } 657 }
647 658
648 void CFieldTree::SetField(const CFX_WideString& full_name, 659 void CFieldTree::SetField(const CFX_WideString& full_name,
649 CPDF_FormField* field_ptr) { 660 CPDF_FormField* field_ptr) {
650 if (full_name == L"") 661 if (full_name == L"")
651 return; 662 return;
652 663
653 CFieldNameExtractor name_extractor(full_name); 664 CFieldNameExtractor name_extractor(full_name);
654 const FX_WCHAR* pName; 665 const FX_WCHAR* pName;
655 FX_STRSIZE nLength; 666 FX_STRSIZE nLength;
656 name_extractor.GetNext(pName, nLength); 667 name_extractor.GetNext(pName, nLength);
657 Node* pNode = &m_Root; 668 Node* pNode = &m_Root;
658 Node* pLast = nullptr; 669 Node* pLast = nullptr;
659 while (nLength > 0) { 670 while (nLength > 0) {
660 pLast = pNode; 671 pLast = pNode;
661 CFX_WideString name = CFX_WideString(pName, nLength); 672 CFX_WideString name = CFX_WideString(pName, nLength);
662 pNode = Lookup(pLast, name); 673 pNode = Lookup(pLast, name);
663 if (!pNode) 674 if (!pNode)
664 pNode = AddChild(pLast, name, nullptr); 675 pNode = AddChild(pLast, name, nullptr);
665 676
666 name_extractor.GetNext(pName, nLength); 677 name_extractor.GetNext(pName, nLength);
667 } 678 }
668 if (pNode != &m_Root) 679 if (pNode != &m_Root)
669 pNode->field_ptr = field_ptr; 680 pNode->set_field_ptr(field_ptr);
670 } 681 }
671 682
672 CPDF_FormField* CFieldTree::GetField(const CFX_WideString& full_name) { 683 CPDF_FormField* CFieldTree::GetField(const CFX_WideString& full_name) {
673 if (full_name == L"") 684 if (full_name == L"")
674 return nullptr; 685 return nullptr;
675 686
676 CFieldNameExtractor name_extractor(full_name); 687 CFieldNameExtractor name_extractor(full_name);
677 const FX_WCHAR* pName; 688 const FX_WCHAR* pName;
678 FX_STRSIZE nLength; 689 FX_STRSIZE nLength;
679 name_extractor.GetNext(pName, nLength); 690 name_extractor.GetNext(pName, nLength);
680 Node* pNode = &m_Root; 691 Node* pNode = &m_Root;
681 Node* pLast = nullptr; 692 Node* pLast = nullptr;
682 while (nLength > 0 && pNode) { 693 while (nLength > 0 && pNode) {
683 pLast = pNode; 694 pLast = pNode;
684 CFX_WideString name = CFX_WideString(pName, nLength); 695 CFX_WideString name = CFX_WideString(pName, nLength);
685 pNode = Lookup(pLast, name); 696 pNode = Lookup(pLast, name);
686 name_extractor.GetNext(pName, nLength); 697 name_extractor.GetNext(pName, nLength);
687 } 698 }
688 return pNode ? pNode->field_ptr : nullptr; 699 return pNode ? pNode->field() : nullptr;
689 }
690
691 CPDF_FormField* CFieldTree::RemoveField(const CFX_WideString& full_name) {
692 if (full_name == L"")
693 return nullptr;
694
695 CFieldNameExtractor name_extractor(full_name);
696 const FX_WCHAR* pName;
697 FX_STRSIZE nLength;
698 name_extractor.GetNext(pName, nLength);
699 Node* pNode = &m_Root;
700 Node* pLast = nullptr;
701 while (nLength > 0 && pNode) {
702 pLast = pNode;
703 CFX_WideString name = CFX_WideString(pName, nLength);
704 pNode = Lookup(pLast, name);
705 name_extractor.GetNext(pName, nLength);
706 }
707
708 if (pNode && pNode != &m_Root) {
709 for (int i = 0; i < pLast->children.GetSize(); i++) {
710 if (pNode == pLast->children[i]) {
711 pLast->children.RemoveAt(i);
712 break;
713 }
714 }
715 CPDF_FormField* pField = pNode->field_ptr;
716 RemoveNode(pNode);
717 return pField;
718 }
719 return nullptr;
720 } 700 }
721 701
722 CFieldTree::Node* CFieldTree::FindNode(const CFX_WideString& full_name) { 702 CFieldTree::Node* CFieldTree::FindNode(const CFX_WideString& full_name) {
723 if (full_name == L"") 703 if (full_name == L"")
724 return nullptr; 704 return nullptr;
725 705
726 CFieldNameExtractor name_extractor(full_name); 706 CFieldNameExtractor name_extractor(full_name);
727 const FX_WCHAR* pName; 707 const FX_WCHAR* pName;
728 FX_STRSIZE nLength; 708 FX_STRSIZE nLength;
729 name_extractor.GetNext(pName, nLength); 709 name_extractor.GetNext(pName, nLength);
(...skipping 894 matching lines...) Expand 10 before | Expand all | Expand 10 after
1624 FDF_ImportField(pField, L"", bNotify); 1604 FDF_ImportField(pField, L"", bNotify);
1625 } 1605 }
1626 if (bNotify && m_pFormNotify) 1606 if (bNotify && m_pFormNotify)
1627 m_pFormNotify->AfterFormImportData(this); 1607 m_pFormNotify->AfterFormImportData(this);
1628 return TRUE; 1608 return TRUE;
1629 } 1609 }
1630 1610
1631 void CPDF_InterForm::SetFormNotify(IPDF_FormNotify* pNotify) { 1611 void CPDF_InterForm::SetFormNotify(IPDF_FormNotify* pNotify) {
1632 m_pFormNotify = pNotify; 1612 m_pFormNotify = pNotify;
1633 } 1613 }
OLDNEW
« 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