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.

132 lines
2.8 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright(c) Microsoft Corp., 1998 **
  4. //*********************************************************************
  5. #include "pre.h"
  6. CStorage::CStorage(void)
  7. {
  8. // initialize all items
  9. for (int i=0; i<MAX_STORGE_ITEM; i++)
  10. {
  11. m_pItem[i] = NULL;
  12. }
  13. }
  14. CStorage::~CStorage(void)
  15. {
  16. // Clean up
  17. for (int i=0; i<MAX_STORGE_ITEM; i++)
  18. {
  19. if (m_pItem[i])
  20. {
  21. if (m_pItem[i]->pData)
  22. {
  23. delete [] m_pItem[i]->pData;
  24. }
  25. delete m_pItem[i];
  26. }
  27. }
  28. }
  29. // Associate the data with a key and puts it in storage
  30. BOOL CStorage::Set(
  31. STORAGEKEY key,
  32. void far * pData,
  33. DWORD dwSize
  34. )
  35. {
  36. // checks for existence of previous item
  37. if (m_pItem[key])
  38. {
  39. // Checks if a new allocation is necessary
  40. if (m_pItem[key]->dwSize < dwSize )
  41. {
  42. // Too small, new reallocation
  43. if (m_pItem[key]->pData)
  44. {
  45. delete [] m_pItem[key]->pData;
  46. m_pItem[key]->pData = (void*) new CHAR[dwSize];
  47. }
  48. }
  49. }
  50. else
  51. {
  52. // Allocate a new item
  53. m_pItem[key] = new ITEM;
  54. if (m_pItem[key])
  55. {
  56. m_pItem[key]->pData = (void*) new CHAR[dwSize];
  57. }
  58. else
  59. {
  60. return FALSE;
  61. }
  62. }
  63. if (m_pItem[key]->pData)
  64. {
  65. memcpy( m_pItem[key]->pData, pData, dwSize );
  66. m_pItem[key]->dwSize = dwSize;
  67. return TRUE;
  68. }
  69. return FALSE;
  70. }
  71. // Get the data with the specified key
  72. void* CStorage::Get(STORAGEKEY key)
  73. {
  74. if (key < MAX_STORGE_ITEM)
  75. {
  76. if (m_pItem[key])
  77. {
  78. return m_pItem[key]->pData;
  79. }
  80. }
  81. return NULL;
  82. }
  83. // Compare the data with the specified key with the data
  84. // pointed by pData with size dwSize
  85. BOOL CStorage::Compare
  86. (
  87. STORAGEKEY key,
  88. void far * pData,
  89. DWORD dwSize
  90. )
  91. {
  92. // Make sure key is within our range
  93. if (key < MAX_STORGE_ITEM)
  94. {
  95. // make sure item is non-null
  96. if (m_pItem[key])
  97. {
  98. // make sure item has data
  99. if (m_pItem[key]->pData && pData)
  100. {
  101. if (m_pItem[key]->dwSize == dwSize)
  102. {
  103. if (memcmp(m_pItem[key]->pData,
  104. pData,
  105. dwSize) == 0)
  106. {
  107. return TRUE;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. return FALSE;
  114. }