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.

299 lines
6.2 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. INDEXCAC.CPP
  5. Abstract:
  6. Caches string/integer combinations.
  7. History:
  8. a-davj 04-Mar-97 Created.
  9. --*/
  10. #include "precomp.h"
  11. #include "indexcac.h"
  12. //***************************************************************************
  13. //
  14. // CCacheEntry::CCacheEntry
  15. //
  16. // DESCRIPTION:
  17. //
  18. // Constructor.
  19. //
  20. // PARAMETERS:
  21. //
  22. // pValue string value to save
  23. // iIndex integer value to save
  24. //***************************************************************************
  25. CCacheEntry::CCacheEntry(
  26. TCHAR * pValue,
  27. int iIndex)
  28. : CObject()
  29. {
  30. m_iIndex = iIndex;
  31. m_ptcValue = pValue;
  32. m_pwcValue = NULL;
  33. }
  34. //***************************************************************************
  35. //
  36. // CCacheEntry::CCacheEntry
  37. //
  38. // DESCRIPTION:
  39. //
  40. // Constructor.
  41. //
  42. // PARAMETERS:
  43. //
  44. // pValue string value to save
  45. //
  46. //***************************************************************************
  47. CCacheEntry::CCacheEntry(
  48. WCHAR * pValue)
  49. : CObject()
  50. {
  51. m_iIndex = -1;
  52. m_ptcValue = NULL;
  53. m_pwcValue = pValue;
  54. }
  55. //***************************************************************************
  56. //
  57. // CCacheEntry::~CCacheEntry
  58. //
  59. // DESCRIPTION:
  60. //
  61. // Destructor.
  62. //
  63. //***************************************************************************
  64. CCacheEntry::~CCacheEntry()
  65. {
  66. if(m_ptcValue)
  67. delete m_ptcValue;
  68. if(m_pwcValue)
  69. delete m_pwcValue;
  70. }
  71. //***************************************************************************
  72. //
  73. // CIndexCache::CIndexCache
  74. //
  75. // DESCRIPTION:
  76. //
  77. // Constructor.
  78. //
  79. //***************************************************************************
  80. CIndexCache::CIndexCache()
  81. {
  82. }
  83. //***************************************************************************
  84. //
  85. // void CIndexCache::Empty
  86. //
  87. // DESCRIPTION:
  88. //
  89. // Frees up the storage.
  90. //
  91. //***************************************************************************
  92. void CIndexCache::Empty()
  93. {
  94. CCacheEntry * pEntry;
  95. int iCnt, iSize;
  96. iSize = m_Array.Size();
  97. for(iCnt = 0; iCnt < iSize; iCnt++)
  98. {
  99. pEntry = (CCacheEntry *)m_Array.GetAt(iCnt);
  100. if(pEntry)
  101. delete pEntry;
  102. }
  103. if(iSize > 0)
  104. m_Array.Empty();
  105. }
  106. //***************************************************************************
  107. //
  108. // int CIndexCache::Find
  109. //
  110. // DESCRIPTION:
  111. //
  112. // Finds an entry in the cache.
  113. //
  114. // PARAMETERS:
  115. //
  116. // pFind string value used to locate the entry
  117. // dwWhichEntry a non zero value would return subsequent
  118. // matching entries.
  119. //
  120. // RETURN VALUE:
  121. //
  122. // index in cache. -1 if the entry cant be found
  123. //
  124. //***************************************************************************
  125. int CIndexCache::Find(
  126. IN const TCHAR * pFind, DWORD dwWhichEntry)
  127. {
  128. CCacheEntry * pEntry;
  129. int iCnt, iSize;
  130. DWORD dwFound = 0;
  131. iSize = m_Array.Size();
  132. for(iCnt = 0; iCnt < iSize; iCnt++)
  133. {
  134. pEntry = (CCacheEntry *)m_Array.GetAt(iCnt);
  135. if(!lstrcmpi(pEntry->m_ptcValue, pFind))
  136. if(dwFound == dwWhichEntry)
  137. return pEntry->m_iIndex;
  138. else
  139. dwFound++;
  140. }
  141. return -1; // never found it
  142. }
  143. //***************************************************************************
  144. //
  145. // BOOL CIndexCache::Add
  146. //
  147. // DESCRIPTION:
  148. //
  149. // Adds an entry to the cache.
  150. //
  151. // PARAMETERS:
  152. //
  153. // pAdd string to add to cache
  154. // iIndex associated number
  155. //
  156. // RETURN VALUE:
  157. //
  158. //
  159. //***************************************************************************
  160. BOOL CIndexCache::Add(
  161. IN TCHAR * pAdd,
  162. IN int iIndex)
  163. {
  164. TCHAR * pValue = new TCHAR[lstrlen(pAdd)+1];
  165. if(pValue == NULL)
  166. return FALSE;
  167. lstrcpy(pValue, pAdd);
  168. // Note that if created, the CCacheEntry object owns the string and
  169. // will take care of freeing it
  170. CCacheEntry * pNew = new CCacheEntry(pValue, iIndex);
  171. if(pNew == NULL)
  172. {
  173. delete pValue;
  174. return FALSE;
  175. }
  176. int iRet = m_Array.Add(pNew);
  177. if(iRet == CFlexArray::no_error)
  178. return TRUE;
  179. {
  180. delete pNew;
  181. return FALSE;
  182. }
  183. }
  184. //***************************************************************************
  185. //
  186. // WCHAR * CIndexCache::GetWString
  187. //
  188. // DESCRIPTION:
  189. //
  190. // Gets a string from the cache.
  191. //
  192. // PARAMETERS:
  193. //
  194. // iIndex cache index
  195. //
  196. // RETURN VALUE:
  197. //
  198. // pointer to string, does not need to be freed. NULL if the index
  199. // is invalid.
  200. //***************************************************************************
  201. WCHAR * CIndexCache::GetWString(
  202. IN int iIndex)
  203. {
  204. if(iIndex >= m_Array.Size())
  205. return NULL;
  206. CCacheEntry * pEntry = (CCacheEntry *)m_Array.GetAt(iIndex);
  207. if(pEntry == NULL)
  208. return NULL;
  209. WCHAR * pRet = new WCHAR[wcslen(pEntry->m_pwcValue)+1];
  210. if(pRet)
  211. wcscpy(pRet,pEntry->m_pwcValue);
  212. return pRet;
  213. }
  214. //***************************************************************************
  215. //
  216. // BOOL CIndexCache::SetAt
  217. //
  218. // DESCRIPTION:
  219. //
  220. // Sets a cache entry.
  221. //
  222. // PARAMETERS:
  223. //
  224. // pwcAdd string to store
  225. // iIndex cache index to use
  226. //
  227. // RETURN VALUE:
  228. //
  229. //
  230. //***************************************************************************
  231. BOOL CIndexCache::SetAt(
  232. IN WCHAR * pwcAdd,
  233. IN int iIndex)
  234. {
  235. WCHAR * pValue = new WCHAR[wcslen(pwcAdd)+1];
  236. if(pValue == NULL)
  237. return FALSE;
  238. wcscpy(pValue, pwcAdd);
  239. // Note that if created, the CCacheEntry object owns the string and
  240. // will take care of freeing it
  241. CCacheEntry * pNew = new CCacheEntry(pValue);
  242. if(pNew == NULL)
  243. {
  244. delete pValue;
  245. return FALSE;
  246. }
  247. if(iIndex < m_Array.Size())
  248. {
  249. m_Array.SetAt(iIndex, pNew);
  250. return TRUE;
  251. }
  252. if(CFlexArray::no_error == m_Array.InsertAt(iIndex, pNew))
  253. return TRUE;
  254. {
  255. delete pNew;
  256. return FALSE;
  257. }
  258. }