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.

228 lines
4.6 KiB

  1. //+---------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1993 - 1997.
  5. //
  6. // File: cstrings.cpp
  7. //
  8. // Contents: Implements the class CStrings to manage a dynamically
  9. // expandable array of string pairs which may be enumerated
  10. //
  11. // Classes:
  12. //
  13. // Methods: CStrings::CStrings
  14. // CStrings::~CStrings
  15. // CStrings::PutItem
  16. // CStrings::FindItem
  17. // CStrings::FindAppid
  18. // CStrings::AddClsid
  19. // CStrings::InitGetNext
  20. // CStrings::GetNextItem
  21. // CStrings::GetItem
  22. // CStrings::GetNumItems
  23. // CStrings::RemoveItem
  24. // CStrings::RemoveAll
  25. //
  26. // History: 23-Apr-96 BruceMa Created.
  27. //
  28. //----------------------------------------------------------------------
  29. #include "stdafx.h"
  30. #include "types.h"
  31. #include "cstrings.h"
  32. #ifdef _DEBUG
  33. #define new DEBUG_NEW
  34. #undef THIS_FILE
  35. static char BASED_CODE THIS_FILE[] = __FILE__;
  36. #endif
  37. CStrings::CStrings(void)
  38. {
  39. m_nCount = 0;
  40. }
  41. CStrings::~CStrings(void)
  42. {
  43. RemoveAll();
  44. }
  45. // Store a string pair, expanding the array if necessary
  46. SItem *CStrings::PutItem(TCHAR *szString, TCHAR *szTitle, TCHAR *szAppid)
  47. {
  48. SItem *psTemp = new SItem(szString, szTitle, szAppid);
  49. if (psTemp )
  50. arrSItems.Add(psTemp);
  51. return psTemp;
  52. }
  53. SItem *CStrings::FindItem(TCHAR *szItem)
  54. {
  55. for (int wItem = 0; wItem < arrSItems.GetSize(); wItem++)
  56. {
  57. SItem* pTmp = (SItem*)arrSItems.GetAt(wItem);
  58. if (pTmp -> szItem .CompareNoCase(szItem) == 0)
  59. return pTmp;
  60. }
  61. return NULL;
  62. }
  63. SItem *CStrings::FindAppid(TCHAR *szAppid)
  64. {
  65. for (int wItem = 0; wItem < arrSItems.GetSize(); wItem++)
  66. {
  67. SItem* pTmp = (SItem*)arrSItems.GetAt(wItem);
  68. if (!(pTmp -> szItem.IsEmpty()) &&
  69. (pTmp -> szAppid.CompareNoCase(szAppid) == 0))
  70. {
  71. return pTmp;
  72. }
  73. }
  74. return NULL;
  75. }
  76. BOOL CStrings::AddClsid(SItem *pItem, TCHAR *szClsid)
  77. {
  78. // Create or expand the clsid table if necessary
  79. if (pItem->ulClsids == pItem->ulClsidTbl)
  80. {
  81. TCHAR **ppTmp = new TCHAR *[pItem->ulClsidTbl + 8];
  82. if (ppTmp == NULL)
  83. {
  84. return FALSE;
  85. }
  86. if (pItem->ppszClsids)
  87. {
  88. memcpy(ppTmp,
  89. pItem->ppszClsids,
  90. pItem->ulClsids * sizeof(TCHAR *));
  91. delete pItem->ppszClsids;
  92. }
  93. pItem->ppszClsids = ppTmp;
  94. pItem->ulClsidTbl += 8;
  95. }
  96. // Add the new clsid
  97. TCHAR *pszTmp = new TCHAR[GUIDSTR_MAX + 1];
  98. if (pszTmp == NULL)
  99. {
  100. return FALSE;
  101. }
  102. _tcscpy(pszTmp, szClsid);
  103. pItem->ppszClsids[pItem->ulClsids++] = pszTmp;
  104. return TRUE;
  105. }
  106. // Prepare to enumerate the array
  107. DWORD CStrings::InitGetNext(void)
  108. {
  109. m_nCount = 0;
  110. return (DWORD)arrSItems.GetSize();
  111. }
  112. // Return the first string in the next eumerated item
  113. SItem *CStrings::GetNextItem(void)
  114. {
  115. if (m_nCount < arrSItems.GetSize())
  116. {
  117. return (SItem*)(arrSItems[m_nCount++]);
  118. }
  119. else
  120. {
  121. m_nCount = 0;
  122. return NULL;
  123. }
  124. }
  125. // Return the first string in the next eumerated item
  126. SItem *CStrings::GetItem(DWORD dwItem)
  127. {
  128. if (((int)dwItem) < arrSItems.GetSize())
  129. {
  130. return (SItem*)(arrSItems[dwItem]);
  131. }
  132. else
  133. {
  134. m_nCount = 0;
  135. return NULL;
  136. }
  137. }
  138. // Return the total number of items
  139. DWORD CStrings::GetNumItems(void)
  140. {
  141. return (DWORD)arrSItems.GetSize();
  142. }
  143. // Given an item index, remove it
  144. BOOL CStrings::RemoveItem(DWORD dwItem)
  145. {
  146. if (((int)dwItem) < arrSItems.GetSize())
  147. {
  148. SItem* pTmp = (SItem*)arrSItems.GetAt(dwItem);
  149. if (pTmp)
  150. {
  151. arrSItems.RemoveAt(dwItem);
  152. delete pTmp;
  153. return TRUE;
  154. }
  155. }
  156. return FALSE;
  157. }
  158. // Remove the array of items
  159. BOOL CStrings::RemoveAll(void)
  160. {
  161. int nItems = (int)arrSItems.GetSize();
  162. for (int nItem = 0; nItem < nItems; nItem++)
  163. {
  164. SItem* pTmp = (SItem*)arrSItems.GetAt(nItem);
  165. delete pTmp;
  166. }
  167. arrSItems.RemoveAll();
  168. return TRUE;
  169. }
  170. SItem::SItem(LPCTSTR sItem, LPCTSTR sTitle, LPCTSTR sAppid)
  171. : szItem(sItem), szTitle(sTitle), szAppid(sAppid)
  172. {
  173. fMarked = FALSE;
  174. fChecked = FALSE;
  175. fHasAppid = FALSE;
  176. fDontDisplay = FALSE;
  177. ulClsids = 0;
  178. ulClsidTbl = 0;
  179. ppszClsids = 0;
  180. }
  181. SItem::~SItem()
  182. {
  183. for (UINT k = 0; k < ulClsids; k++)
  184. {
  185. delete ppszClsids[k];
  186. }
  187. ulClsids = 0;
  188. ulClsidTbl = 0;
  189. delete ppszClsids;
  190. }
  191.