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.

166 lines
3.3 KiB

  1. // CSPCache.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <windows.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include "cspcache.h"
  8. #define CacheAlloc(cBytes) (HeapAlloc(GetProcessHeap(), 0, cBytes))
  9. #define CacheFree(pv) (HeapFree(GetProcessHeap(), 0, pv))
  10. int main(int argc, char* argv[])
  11. {
  12. LPSTR pszKey1 = "Key1";
  13. LPSTR pszKey2 = "Key12";
  14. LPSTR pszKey3 = "Key123";
  15. LPSTR pszKey4 = "Key1234";
  16. CACHE_HANDLE hCache = NULL;
  17. LPSTR pszData = NULL;
  18. if (CacheAddItem(&hCache, pszKey1, (PVOID) pszKey1, (PVOID *) &pszData))
  19. exit(1);
  20. if (CacheAddItem(&hCache, pszKey2, (PVOID) pszKey2, (PVOID *) &pszData))
  21. exit(1);
  22. if (CacheAddItem(&hCache, pszKey3, (PVOID) pszKey3, (PVOID *) &pszData))
  23. exit(1);
  24. if (CacheAddItem(&hCache, pszKey4, (PVOID) pszKey4, (PVOID *) &pszData))
  25. exit(1);
  26. pszData = (LPSTR) CacheGetItem(&hCache, "KeyNothing");
  27. pszData = (LPSTR) CacheGetItem(&hCache, pszKey3);
  28. pszData = NULL;
  29. if (CacheAddItem(&hCache, pszKey3, pszKey4, (PVOID *) &pszData))
  30. exit(1);
  31. pszData = (LPSTR) CacheGetItem(&hCache, pszKey3);
  32. pszData = (LPSTR) CacheDeleteItem(&hCache, pszKey2);
  33. CacheDeleteCache(hCache);
  34. return 0;
  35. }
  36. PVOID CacheGetItem(
  37. IN CACHE_HANDLE *phCache,
  38. IN LPSTR pszKey)
  39. {
  40. PCACHE_ITEM pCacheItem = NULL, pPrevItem = NULL;
  41. if (NULL == *phCache)
  42. return NULL;
  43. pCacheItem = *phCache;
  44. pPrevItem = *phCache;
  45. while (NULL != pCacheItem && 0 != strcmp(pszKey, pCacheItem->rgszKey))
  46. {
  47. pPrevItem = pCacheItem;
  48. pCacheItem = pCacheItem->pNext;
  49. }
  50. if (NULL == pCacheItem)
  51. return NULL; // Item not found
  52. // Move item to the front of the cache list
  53. // if it isn't already.
  54. if (pCacheItem != *phCache)
  55. {
  56. pPrevItem->pNext = pCacheItem->pNext;
  57. pCacheItem->pNext = *phCache;
  58. *phCache = pCacheItem;
  59. }
  60. return pCacheItem->pvData;
  61. }
  62. void CacheDeleteCache(
  63. IN OUT CACHE_HANDLE hCache)
  64. {
  65. PCACHE_ITEM p1 = hCache;
  66. PCACHE_ITEM p2 = NULL;
  67. while (NULL != p1)
  68. {
  69. p2 = p1->pNext;
  70. CacheFree(p1);
  71. p1 = p2;
  72. }
  73. }
  74. DWORD CacheAddItem(
  75. IN OUT CACHE_HANDLE *phCache,
  76. IN LPSTR pszKey,
  77. IN PVOID pvData,
  78. OUT PVOID *ppvOldData)
  79. {
  80. PCACHE_ITEM pCacheItem = *phCache;
  81. PCACHE_ITEM pPrevItem = NULL;
  82. *ppvOldData = NULL;
  83. while (NULL != pCacheItem)
  84. {
  85. // Does item already exist?
  86. if (0 == strcmp(pszKey, pCacheItem->rgszKey))
  87. {
  88. *ppvOldData = pCacheItem->pvData;
  89. break;
  90. }
  91. else
  92. {
  93. pPrevItem = pCacheItem;
  94. pCacheItem = pCacheItem->pNext;
  95. }
  96. }
  97. if (NULL == pCacheItem)
  98. {
  99. // Add new items to end of cache list
  100. if (NULL == (pCacheItem = (PCACHE_ITEM) CacheAlloc(sizeof(CACHE_ITEM))))
  101. return ERROR_NOT_ENOUGH_MEMORY;
  102. memset(pCacheItem, 0, sizeof(CACHE_ITEM));
  103. if (NULL == *phCache)
  104. *phCache = pCacheItem;
  105. else
  106. pPrevItem->pNext = pCacheItem;
  107. }
  108. strncpy(pCacheItem->rgszKey, pszKey, MAX_KEY_SIZE);
  109. pCacheItem->pvData = pvData;
  110. return 0;
  111. }
  112. PVOID CacheDeleteItem(
  113. IN CACHE_HANDLE *phCache,
  114. LPSTR pszKey)
  115. {
  116. PCACHE_ITEM pCacheItem = *phCache, pPrevItem = *phCache;
  117. PVOID pvData = NULL;
  118. while (NULL != pCacheItem && 0 != strcmp(pszKey, pCacheItem->rgszKey))
  119. {
  120. pPrevItem = pCacheItem;
  121. pCacheItem = pCacheItem->pNext;
  122. }
  123. if (NULL == pCacheItem)
  124. return NULL; // Specified key not found
  125. pPrevItem->pNext = pCacheItem->pNext;
  126. pvData = pCacheItem->pvData;
  127. CacheFree(pCacheItem);
  128. return pvData;
  129. }