ECFBBlockList.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include "ECFBBlockList.h"
  19. namespace KC {
  20. ECFBBlockList::ECFBBlockList(void)
  21. {
  22. m_bInitIter = false;
  23. m_FBIter = m_FBMap.end();
  24. m_tmRestictStart = 0;
  25. m_tmRestictEnd = 0;
  26. }
  27. void ECFBBlockList::Copy(ECFBBlockList *lpfbBlkList)
  28. {
  29. this->m_FBMap = lpfbBlkList->m_FBMap;
  30. this->Restrict(lpfbBlkList->m_tmRestictStart, lpfbBlkList->m_tmRestictEnd);
  31. }
  32. HRESULT ECFBBlockList::Add(FBBlock_1* lpFBBlock)
  33. {
  34. if (lpFBBlock == NULL)
  35. return MAPI_E_INVALID_PARAMETER;
  36. m_FBMap.insert(mapFB::value_type(lpFBBlock->m_tmStart, *lpFBBlock));
  37. return hrSuccess;
  38. }
  39. HRESULT ECFBBlockList::Merge(FBBlock_1* lpFBBlock)
  40. {
  41. if (lpFBBlock == NULL)
  42. return MAPI_E_INVALID_PARAMETER;
  43. auto FBIter = m_FBMap.begin();
  44. for (; FBIter != m_FBMap.cend(); ++FBIter)
  45. if(FBIter->second.m_tmEnd == lpFBBlock->m_tmStart)
  46. {
  47. FBIter->second.m_tmEnd = lpFBBlock->m_tmEnd;
  48. break;
  49. }
  50. if (FBIter == m_FBMap.cend())
  51. return MAPI_E_NOT_FOUND;
  52. return hrSuccess;
  53. }
  54. HRESULT ECFBBlockList::Next(FBBlock_1* pblk)
  55. {
  56. if (pblk == NULL)
  57. return MAPI_E_INVALID_PARAMETER;
  58. // Set iter on the begin of the list
  59. if (!m_bInitIter)
  60. Restrict(m_tmRestictStart, m_tmRestictEnd);
  61. // Check if you are at the end of the list or the item doesn't matched with the restriction
  62. if (m_FBIter == m_FBMap.cend() || (m_tmRestictEnd != 0 && static_cast<ULONG>(m_FBIter->second.m_tmStart) > static_cast<ULONG>(m_tmRestictEnd)))
  63. return MAPI_E_NOT_FOUND;
  64. *pblk = (*m_FBIter).second;
  65. // blocks before the start time get capped on the start time
  66. if (pblk->m_tmStart < m_tmRestictStart)
  67. pblk->m_tmStart = m_tmRestictStart;
  68. ++m_FBIter;
  69. return hrSuccess;
  70. }
  71. HRESULT ECFBBlockList::Reset()
  72. {
  73. m_bInitIter = false;
  74. return hrSuccess;
  75. }
  76. HRESULT ECFBBlockList::Skip(LONG items)
  77. {
  78. if (!m_bInitIter)
  79. Restrict(m_tmRestictStart, m_tmRestictEnd);
  80. for (LONG i = 0; i < items; ++i) {
  81. // Check if you are at the end of the list or the item doesn't matched with the restriction
  82. if (m_FBIter == m_FBMap.cend() || (m_tmRestictEnd != 0 && (ULONG)m_FBIter->second.m_tmStart > (ULONG)m_tmRestictEnd) )
  83. break; //FIXME: gives a error or always oke?
  84. ++m_FBIter;
  85. }
  86. return hrSuccess;
  87. }
  88. HRESULT ECFBBlockList::Restrict(LONG tmStart, LONG tmEnd)
  89. {
  90. m_tmRestictStart = tmStart;
  91. m_tmRestictEnd = tmEnd;
  92. m_FBIter = m_FBMap.begin();
  93. m_bInitIter = true;
  94. // seek to the first matched item
  95. while (m_tmRestictStart != 0 && m_FBIter != m_FBMap.cend()) {
  96. if( (ULONG)m_FBIter->second.m_tmEnd > (ULONG)m_tmRestictStart )
  97. break;
  98. ++m_FBIter;
  99. }
  100. return S_OK;
  101. }
  102. void ECFBBlockList::Clear()
  103. {
  104. m_FBMap.clear();
  105. m_FBIter = m_FBMap.end();
  106. m_bInitIter = false;
  107. m_tmRestictStart = 0;
  108. m_tmRestictEnd = 0;
  109. }
  110. /*
  111. Get the size of fbBlocks, restriction proof
  112. */
  113. ULONG ECFBBlockList::Size()
  114. {
  115. ULONG size = 0;
  116. auto FBIter = m_FBMap.cbegin();
  117. // seek to the first matched item
  118. while (m_tmRestictStart != 0 && FBIter != m_FBMap.cend()) {
  119. if( (ULONG)FBIter->second.m_tmEnd > (ULONG)m_tmRestictStart )
  120. break;
  121. ++FBIter;
  122. }
  123. // loop while you reached end of list or doesn't mached with the restriction
  124. while (FBIter != m_FBMap.cend() && (m_tmRestictEnd == 0 || static_cast<ULONG>(FBIter->second.m_tmStart) <= static_cast<ULONG>(m_tmRestictEnd))) {
  125. ++size;
  126. ++FBIter;
  127. }
  128. return size;
  129. }
  130. HRESULT ECFBBlockList::GetEndTime(LONG *lprtmEnd)
  131. {
  132. LONG ulEnd = 0;
  133. bool bFound = false;
  134. if (lprtmEnd == NULL)
  135. return MAPI_E_INVALID_PARAMETER;
  136. auto FBIter = m_FBMap.cbegin();
  137. while (FBIter != m_FBMap.cend() && (m_tmRestictEnd == 0 || static_cast<ULONG>(FBIter->second.m_tmStart) <= static_cast<ULONG>(m_tmRestictEnd))) {
  138. ulEnd = FBIter->second.m_tmEnd;
  139. ++FBIter;
  140. bFound = true;
  141. }
  142. if (!bFound)
  143. return MAPI_E_NOT_FOUND;
  144. *lprtmEnd = ulEnd;
  145. return hrSuccess;
  146. }
  147. } /* namespace */