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.

232 lines
5.3 KiB

  1. #include <windows.h>
  2. #include "list.h"
  3. //----------------------------------------------------------------------------
  4. //
  5. // Description:
  6. // this implements a simple C++ list class routine that will let us build
  7. // up a list of an arbitrary long number of items. An item can be any
  8. // simple type or structure.
  9. //
  10. //----------------------------------------------------------------------------
  11. CList::CList()
  12. {
  13. m_pListHead=m_pListCurr=m_pListTail=NULL;
  14. }
  15. CList::~CList()
  16. {
  17. RemoveAll();
  18. }
  19. //----------------------------------------------------------------------------
  20. //
  21. // Description:
  22. // Add an item to the list. The length of the item is assumed correct
  23. // for the passed in item. Items are added to the end of the list.
  24. //
  25. // Arguments:
  26. // pData - pointer to the data to add
  27. // nBytes - the number of bytes of pData
  28. //
  29. // Returns: TRUE if the data is succesfully added, otherwise FALSE.
  30. // a-anilk; Just donot absolutely Duplicate Entries
  31. //
  32. //----------------------------------------------------------------------------
  33. BOOL
  34. CList::Add(PVOID pData, UINT nBytes)
  35. {
  36. PLIST tmp;
  37. if ((nBytes == 0) || (pData == NULL))
  38. return FALSE;
  39. tmp=new LIST;
  40. if (NULL == tmp)
  41. return FALSE;
  42. tmp->pData=new BYTE[nBytes];
  43. if (NULL == tmp->pData)
  44. {
  45. delete tmp;
  46. return FALSE;
  47. }
  48. // Donot add duplicate entries that come one after another...
  49. if ( m_pListHead != NULL )
  50. {
  51. if (! memcmp(m_pListHead->pData, pData, nBytes ) )
  52. {
  53. delete [] tmp->pData; // raid 113787
  54. delete tmp;
  55. return FALSE;
  56. }
  57. }
  58. CopyMemory(tmp->pData,pData,nBytes);
  59. tmp->nBytes=nBytes;
  60. tmp->next=NULL;
  61. tmp->prev=m_pListTail;
  62. if (IsEmpty())
  63. {
  64. m_pListHead=tmp;
  65. }
  66. else
  67. {
  68. if (m_pListTail != NULL)
  69. m_pListTail->next=tmp;
  70. }
  71. m_pListTail=tmp;
  72. return TRUE;
  73. }
  74. //----------------------------------------------------------------------------
  75. //
  76. // Description:
  77. // Remove all the items from the list.
  78. //
  79. //----------------------------------------------------------------------------
  80. void
  81. CList::RemoveAll()
  82. {
  83. while(!IsEmpty())
  84. RemoveHead(NULL);
  85. }
  86. //----------------------------------------------------------------------------
  87. //
  88. // Description:
  89. // Remove just the item at the head of the list. If the passed in buffer
  90. // is not NULL, it will overwrite the buffer with the contents of the data.
  91. // This code assumes the passed in pData buffer is large enough for the
  92. // stored data item. If the passed in pData is NULL, then the head item
  93. // is simply discarded.
  94. //
  95. // Arguments:
  96. // pData - a buffer to overwrite with the head item. Can be NULL.
  97. //
  98. //----------------------------------------------------------------------------
  99. void
  100. CList::RemoveHead(PVOID pData)
  101. {
  102. PLIST tmp;
  103. if (!IsEmpty())
  104. {
  105. // make sure m_pListCurr is always NULL or someplace valid
  106. if (m_pListCurr == m_pListHead)
  107. m_pListCurr=m_pListHead->next;
  108. tmp=m_pListHead;
  109. m_pListHead=m_pListHead->next;
  110. if (tmp->pData != NULL)
  111. {
  112. if (pData != NULL)
  113. CopyMemory(pData,tmp->pData,tmp->nBytes);
  114. delete[] (tmp->pData);
  115. }
  116. delete tmp;
  117. if (!IsEmpty())
  118. m_pListHead->prev=NULL;
  119. else
  120. m_pListTail=NULL;
  121. }
  122. }
  123. //----------------------------------------------------------------------------
  124. //
  125. // Description:
  126. // RemoveHead(NULL, NULL) <=> RemoveHead(NULL)
  127. //
  128. // RemoveHead(pData,NULL) <=> RemoveHead(pData)
  129. //
  130. // RemoveHead(NULL,&nBytes) - sets nBytes to the size of the data in the
  131. // head of the list, nothing is removed
  132. //
  133. // RemoveHead(pData,&nBytes)- copies the data in the head of the list into
  134. // pData up to the min of the size of data in the
  135. // head of the list or nBytes. The head of the
  136. // list is removed.
  137. //
  138. //----------------------------------------------------------------------------
  139. void
  140. CList::RemoveHead(PVOID pData, PUINT pnBytes)
  141. {
  142. PLIST tmp;
  143. UINT nBytes;
  144. if (pnBytes == NULL)
  145. {
  146. RemoveHead(pData);
  147. return;
  148. }
  149. if (pData == NULL)
  150. {
  151. // they just want the size, so return it.
  152. if (IsEmpty())
  153. *pnBytes=0;
  154. else
  155. *pnBytes=m_pListHead->nBytes;
  156. return;
  157. }
  158. if (IsEmpty())
  159. {
  160. *pnBytes=0;
  161. return;
  162. }
  163. // make sure m_pListCurr is always NULL or someplace valid
  164. if (m_pListCurr == m_pListHead)
  165. m_pListCurr=m_pListHead->next;
  166. tmp=m_pListHead;
  167. m_pListHead=m_pListHead->next;
  168. //
  169. // only copy the min size of the two
  170. //
  171. nBytes=min((*pnBytes),tmp->nBytes);
  172. if (tmp->pData != NULL)
  173. {
  174. CopyMemory(pData,tmp->pData,nBytes);
  175. *pnBytes=nBytes;
  176. delete[] (tmp->pData);
  177. }
  178. else
  179. *pnBytes=0;
  180. delete tmp;
  181. if (!IsEmpty())
  182. m_pListHead->prev=NULL;
  183. else
  184. m_pListTail=NULL;
  185. }