cxfa_occur.cpp revision 4d3acf4ec42bf6e838f9060103aff98fbf170794
1// Copyright 2016 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "xfa/fxfa/parser/cxfa_occur.h"
8
9#include "xfa/fxfa/parser/xfa_object.h"
10
11CXFA_Occur::CXFA_Occur(CXFA_Node* pNode) : CXFA_Data(pNode) {}
12
13int32_t CXFA_Occur::GetMax() {
14  int32_t iMax = 1;
15  if (m_pNode) {
16    if (!m_pNode->TryInteger(XFA_ATTRIBUTE_Max, iMax, true))
17      iMax = GetMin();
18  }
19  return iMax;
20}
21
22int32_t CXFA_Occur::GetMin() {
23  int32_t iMin = 1;
24  if (m_pNode) {
25    if (!m_pNode->TryInteger(XFA_ATTRIBUTE_Min, iMin, true) || iMin < 0)
26      iMin = 1;
27  }
28  return iMin;
29}
30
31bool CXFA_Occur::GetOccurInfo(int32_t& iMin, int32_t& iMax, int32_t& iInit) {
32  if (!m_pNode)
33    return false;
34  if (!m_pNode->TryInteger(XFA_ATTRIBUTE_Min, iMin, false) || iMin < 0)
35    iMin = 1;
36  if (!m_pNode->TryInteger(XFA_ATTRIBUTE_Max, iMax, false)) {
37    if (iMin == 0)
38      iMax = 1;
39    else
40      iMax = iMin;
41  }
42  if (!m_pNode->TryInteger(XFA_ATTRIBUTE_Initial, iInit, false) ||
43      iInit < iMin) {
44    iInit = iMin;
45  }
46  return true;
47}
48
49void CXFA_Occur::SetMax(int32_t iMax) {
50  iMax = (iMax != -1 && iMax < 1) ? 1 : iMax;
51  m_pNode->SetInteger(XFA_ATTRIBUTE_Max, iMax, false);
52  int32_t iMin = GetMin();
53  if (iMax != -1 && iMax < iMin) {
54    iMin = iMax;
55    m_pNode->SetInteger(XFA_ATTRIBUTE_Min, iMin, false);
56  }
57}
58
59void CXFA_Occur::SetMin(int32_t iMin) {
60  iMin = (iMin < 0) ? 1 : iMin;
61  m_pNode->SetInteger(XFA_ATTRIBUTE_Min, iMin, false);
62  int32_t iMax = GetMax();
63  if (iMax > 0 && iMax < iMin) {
64    iMax = iMin;
65    m_pNode->SetInteger(XFA_ATTRIBUTE_Max, iMax, false);
66  }
67}
68