Leaked source code of windows server 2003
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.

303 lines
6.6 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. DWORD dwLen = lstrlen(pAdd)+1;
  165. TCHAR * pValue = new TCHAR[dwLen];
  166. if(pValue == NULL)
  167. return FALSE;
  168. StringCchCopyW(pValue, dwLen, pAdd);
  169. // Note that if created, the CCacheEntry object owns the string and
  170. // will take care of freeing it
  171. CCacheEntry * pNew = new CCacheEntry(pValue, iIndex);
  172. if(pNew == NULL)
  173. {
  174. delete pValue;
  175. return FALSE;
  176. }
  177. int iRet = m_Array.Add(pNew);
  178. if(iRet == CFlexArray::no_error)
  179. return TRUE;
  180. {
  181. delete pNew;
  182. return FALSE;
  183. }
  184. }
  185. //***************************************************************************
  186. //
  187. // WCHAR * CIndexCache::GetWString
  188. //
  189. // DESCRIPTION:
  190. //
  191. // Gets a string from the cache.
  192. //
  193. // PARAMETERS:
  194. //
  195. // iIndex cache index
  196. //
  197. // RETURN VALUE:
  198. //
  199. // pointer to string, does not need to be freed. NULL if the index
  200. // is invalid.
  201. //***************************************************************************
  202. WCHAR * CIndexCache::GetWString(
  203. IN int iIndex)
  204. {
  205. DWORD dwLen;
  206. if(iIndex >= m_Array.Size())
  207. return NULL;
  208. CCacheEntry * pEntry = (CCacheEntry *)m_Array.GetAt(iIndex);
  209. if(pEntry == NULL)
  210. return NULL;
  211. dwLen = wcslen(pEntry->m_pwcValue)+1;
  212. WCHAR * pRet = new WCHAR[dwLen];
  213. if(pRet)
  214. StringCchCopyW(pRet, dwLen, pEntry->m_pwcValue);
  215. return pRet;
  216. }
  217. //***************************************************************************
  218. //
  219. // BOOL CIndexCache::SetAt
  220. //
  221. // DESCRIPTION:
  222. //
  223. // Sets a cache entry.
  224. //
  225. // PARAMETERS:
  226. //
  227. // pwcAdd string to store
  228. // iIndex cache index to use
  229. //
  230. // RETURN VALUE:
  231. //
  232. //
  233. //***************************************************************************
  234. BOOL CIndexCache::SetAt(
  235. IN WCHAR * pwcAdd,
  236. IN int iIndex)
  237. {
  238. DWORD dwLen = wcslen(pwcAdd)+1;
  239. WCHAR * pValue = new WCHAR[dwLen];
  240. if(pValue == NULL)
  241. return FALSE;
  242. StringCchCopyW(pValue, dwLen, pwcAdd);
  243. // Note that if created, the CCacheEntry object owns the string and
  244. // will take care of freeing it
  245. CCacheEntry * pNew = new CCacheEntry(pValue);
  246. if(pNew == NULL)
  247. {
  248. delete pValue;
  249. return FALSE;
  250. }
  251. if(iIndex < m_Array.Size())
  252. {
  253. m_Array.SetAt(iIndex, pNew);
  254. return TRUE;
  255. }
  256. if(CFlexArray::no_error == m_Array.InsertAt(iIndex, pNew))
  257. return TRUE;
  258. {
  259. delete pNew;
  260. return FALSE;
  261. }
  262. }