Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
3.8 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: pbmap.cpp
  4. //
  5. // Module: CMDIAL32.DLL
  6. //
  7. // Synopsis: Implementation of CPBMap. Phonebook mapping object
  8. //
  9. // Copyright (c) 1998-1999 Microsoft Corporation
  10. //
  11. // Author: nickball Created 03/12/98
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include "cmmaster.h"
  15. //
  16. // Definitions
  17. //
  18. #define CPBMAP_BITS_FOR_PB 10
  19. #define CPBMAP_BITS_FOR_IDX ((sizeof(DWORD)*8)-CPBMAP_BITS_FOR_PB)
  20. #define CPBMAP_PB_CNT (1L<<CPBMAP_BITS_FOR_PB)
  21. #define CPBMAP_IDX_CNT (1L<<CPBMAP_BITS_FOR_IDX)
  22. #define CPBMAP_ENCODE_PB(pb) ((pb)<<CPBMAP_BITS_FOR_IDX)
  23. #define CPBMAP_ENCODE_IDX(idx) (idx)
  24. #define CPBMAP_DECODE_PB(cookie) ((cookie)>>CPBMAP_BITS_FOR_IDX)
  25. #define CPBMAP_DECODE_IDX(cookie) ((cookie)&(CPBMAP_IDX_CNT-1))
  26. extern "C" HRESULT WINAPI PhoneBookLoad(LPCSTR pszISP, DWORD_PTR *pdwPB);
  27. extern "C" HRESULT WINAPI PhoneBookUnload(DWORD_PTR dwPB);
  28. //
  29. // Types
  30. //
  31. typedef struct tagCPBData
  32. {
  33. DWORD_PTR dwPB;
  34. DWORD dwParam;
  35. } CPBD, *PCPBD;
  36. //
  37. // Implementation
  38. //
  39. CPBMap::CPBMap()
  40. {
  41. m_nCnt = 0;
  42. m_pvData = NULL;
  43. }
  44. CPBMap::~CPBMap()
  45. {
  46. UINT nIdx;
  47. for (nIdx=0;nIdx<m_nCnt;nIdx++)
  48. {
  49. PhoneBookUnload(((PCPBD) m_pvData)[nIdx].dwPB);
  50. }
  51. m_nCnt = 0;
  52. CmFree(m_pvData);
  53. m_pvData = NULL;
  54. }
  55. DWORD CPBMap::Open(LPCSTR pszISP, DWORD dwParam)
  56. {
  57. PCPBD pData;
  58. HRESULT hr;
  59. if (m_nCnt == CPBMAP_PB_CNT)
  60. {
  61. SetLastError(ERROR_INVALID_PARAMETER);
  62. return (CPBMAP_ERROR);
  63. }
  64. pData = (PCPBD) CmMalloc((m_nCnt+1)*sizeof(*pData));
  65. if (pData)
  66. {
  67. hr = PhoneBookLoad(pszISP, &pData[m_nCnt].dwPB);
  68. if (hr == ERROR_SUCCESS)
  69. {
  70. pData[m_nCnt].dwParam = dwParam;
  71. CopyMemory(pData, m_pvData, m_nCnt*sizeof(*pData));
  72. CmFree(m_pvData);
  73. m_pvData = pData;
  74. m_nCnt++;
  75. }
  76. else
  77. {
  78. CmFree(pData);
  79. SetLastError(hr);
  80. }
  81. }
  82. else
  83. {
  84. CMASSERTMSG(FALSE, TEXT("CPBMap::Open -- unable to allocate pData"));
  85. hr = ERROR_NOT_ENOUGH_MEMORY;
  86. SetLastError(hr);
  87. }
  88. return ((hr == ERROR_SUCCESS) ? (m_nCnt-1) : CPBMAP_ERROR);
  89. }
  90. DWORD CPBMap::ToCookie(DWORD_PTR dwPB, DWORD dwIdx, DWORD *pdwParam)
  91. {
  92. DWORD dwPBIdx;
  93. #ifdef DEBUG
  94. DWORD dwTmp = CPBMAP_IDX_CNT;
  95. #endif
  96. if (dwIdx >= CPBMAP_IDX_CNT)
  97. {
  98. SetLastError(ERROR_INVALID_PARAMETER);
  99. return (CPBMAP_ERROR);
  100. }
  101. for (dwPBIdx=0; dwPBIdx < m_nCnt; dwPBIdx++)
  102. {
  103. if (dwPB == ((PCPBD) m_pvData)[dwPBIdx].dwPB)
  104. {
  105. break;
  106. }
  107. }
  108. if (dwPBIdx == m_nCnt)
  109. {
  110. SetLastError(ERROR_INVALID_PARAMETER);
  111. return (CPBMAP_ERROR);
  112. }
  113. if (pdwParam)
  114. {
  115. *pdwParam = ((PCPBD) m_pvData)[dwPBIdx].dwParam;
  116. }
  117. return (CPBMAP_ENCODE_PB(dwPBIdx)|CPBMAP_ENCODE_IDX(dwIdx));
  118. }
  119. DWORD_PTR CPBMap::PBFromCookie(DWORD dwCookie, DWORD *pdwParam)
  120. {
  121. DWORD dwPBIdx;
  122. dwPBIdx = CPBMAP_DECODE_PB(dwCookie);
  123. if (dwPBIdx >= m_nCnt)
  124. {
  125. SetLastError(ERROR_INVALID_PARAMETER);
  126. return (CPBMAP_ERROR);
  127. }
  128. if (pdwParam)
  129. {
  130. *pdwParam = ((PCPBD) m_pvData)[dwPBIdx].dwParam;
  131. }
  132. return (((PCPBD) m_pvData)[dwPBIdx].dwPB);
  133. }
  134. DWORD CPBMap::IdxFromCookie(DWORD dwCookie, DWORD *pdwParam)
  135. {
  136. DWORD dwPBIdx;
  137. dwPBIdx = CPBMAP_DECODE_PB(dwCookie);
  138. if (dwPBIdx >= m_nCnt)
  139. {
  140. SetLastError(ERROR_INVALID_PARAMETER);
  141. return (CPBMAP_ERROR);
  142. }
  143. if (pdwParam)
  144. {
  145. *pdwParam = ((PCPBD) m_pvData)[dwPBIdx].dwParam;
  146. }
  147. return (CPBMAP_DECODE_IDX(dwCookie));
  148. }
  149. DWORD_PTR CPBMap::GetPBByIdx(DWORD_PTR dwIdx, DWORD *pdwParam)
  150. {
  151. if (dwIdx >= m_nCnt)
  152. {
  153. SetLastError(ERROR_INVALID_PARAMETER);
  154. return (CPBMAP_ERROR);
  155. }
  156. if (pdwParam)
  157. {
  158. *pdwParam = ((PCPBD) m_pvData)[dwIdx].dwParam;
  159. }
  160. return (((PCPBD) m_pvData)[dwIdx].dwPB);
  161. }
  162. DWORD CPBMap::GetCnt()
  163. {
  164. return (m_nCnt);
  165. }