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.

350 lines
8.7 KiB

  1. /*****************************************************************************
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1987-1999 **/
  4. /*****************************************************************************/
  5. /*****************************************************************************
  6. File : listhndl.cxx
  7. Title : general purpose list handler
  8. :
  9. Description : this file handles the general purpose list routines
  10. History :
  11. 16-Oct-1990 VibhasC Created
  12. 11-Dec-1990 DonnaLi Fixed include dependencies
  13. *****************************************************************************/
  14. #pragma warning ( disable : 4514 )
  15. /****************************************************************************
  16. *** local defines
  17. ***************************************************************************/
  18. #define IS_AGGREGATE_TYPE(NodeType) ( (NodeType == NODE_ARRAY) || \
  19. (NodeType == NODE_STRUCT) )
  20. #define ADJUST_OFFSET(Offset, M, AlignFactor) \
  21. Offset += (M = Offset % AlignFactor) ? (AlignFactor-M) : 0
  22. /****************************************************************************
  23. *** include files
  24. ***************************************************************************/
  25. #include "nulldefs.h"
  26. extern "C" {
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <malloc.h>
  31. typedef char far * FARPOINTER;
  32. }
  33. #include "gramutil.hxx"
  34. /****************************************************************************
  35. *** external procedures
  36. ***************************************************************************/
  37. int AttrComp( void *, void *);
  38. /****************************************************************************
  39. *** external data
  40. ***************************************************************************/
  41. /****************************************************************************
  42. *** local data
  43. ***************************************************************************/
  44. /*****************************************************************************
  45. * general purpose list (iterator) control functions
  46. *****************************************************************************/
  47. /****************************************************************************
  48. _gplist:
  49. the private memory allocator
  50. ****************************************************************************/
  51. // initialize the memory allocator for _gplist
  52. FreeListMgr
  53. _gplist::MyFreeList( sizeof (_gplist ) );
  54. gplistmgr::~gplistmgr( void )
  55. {
  56. while(pFirst)
  57. {
  58. pCurrent = pFirst->pNext;
  59. delete pFirst;
  60. pFirst = pCurrent;
  61. }
  62. }
  63. STATUS_T
  64. gplistmgr::Discard()
  65. {
  66. while(pFirst)
  67. {
  68. pCurrent = pFirst->pNext;
  69. delete pFirst;
  70. pFirst = pCurrent;
  71. }
  72. pTail = pFirst;
  73. return STATUS_OK;
  74. }
  75. STATUS_T
  76. gplistmgr::Insert(
  77. void * pNewElement )
  78. {
  79. struct _gplist *pNew = new struct _gplist( pNewElement );
  80. if(pNew != (struct _gplist *)NULL)
  81. {
  82. if(pTail != (struct _gplist *)NULL)
  83. {
  84. pTail->pNext = pNew;
  85. }
  86. pTail = pNew;
  87. if(pFirst == NULL) pFirst = pNew;
  88. if(pCurrent == NULL) pCurrent = pNew;
  89. return STATUS_OK;
  90. }
  91. return OUT_OF_MEMORY;
  92. }
  93. STATUS_T
  94. gplistmgr::InsertHead(
  95. void * pNewElement )
  96. {
  97. struct _gplist *pNew = new struct _gplist( pNewElement );
  98. if(pNew != (struct _gplist *)NULL)
  99. {
  100. pNew->pNext = pFirst;
  101. pFirst = pNew;
  102. pCurrent = pNew;
  103. if(pTail == NULL) pTail = pNew;
  104. return STATUS_OK;
  105. }
  106. return OUT_OF_MEMORY;
  107. }
  108. STATUS_T
  109. gplistmgr::RemoveHead( void )
  110. {
  111. struct _gplist *pDel;
  112. pDel = pFirst;
  113. pFirst = pFirst->pNext;
  114. if ( pCurrent == pDel )
  115. {
  116. pCurrent = pFirst;
  117. }
  118. if ( pFirst == NULL )
  119. {
  120. pTail = NULL;
  121. }
  122. delete pDel;
  123. return STATUS_OK;
  124. }
  125. STATUS_T
  126. gplistmgr::GetCurrent(
  127. void **ppReturn )
  128. {
  129. if( pCurrent != (struct _gplist *)NULL )
  130. {
  131. (*ppReturn) = pCurrent->pElement;
  132. return STATUS_OK;
  133. }
  134. return I_ERR_NO_PEER;
  135. }
  136. short
  137. gplistmgr::GetCount()
  138. {
  139. short cnt = 0;
  140. struct _gplist * pCur = pFirst;
  141. while( pCur )
  142. {
  143. cnt++;
  144. pCur = pCur->pNext;
  145. };
  146. return cnt;
  147. }
  148. STATUS_T
  149. gplistmgr::MergeToTail(
  150. gplistmgr *pSrcList,
  151. BOOL bDeleteList )
  152. {
  153. if(pSrcList)
  154. {
  155. if (pSrcList->pFirst)
  156. {
  157. if ( pTail ) // glue to tail of current list
  158. {
  159. pTail->pNext = pSrcList->pFirst;
  160. pTail = pSrcList->pTail;
  161. }
  162. else // add to empty list
  163. {
  164. pFirst = pSrcList->pFirst;
  165. pTail = pSrcList->pTail;
  166. }
  167. }
  168. // Clear pointer in previous list since
  169. // we are assuming ownership of the nodes.
  170. pSrcList->pFirst = NULL;
  171. pSrcList->pTail = NULL;
  172. // delete the source list.
  173. if ( bDeleteList )
  174. delete pSrcList;
  175. }
  176. return STATUS_OK;
  177. }
  178. STATUS_T
  179. gplistmgr::Clone(
  180. gplistmgr *pOldList )
  181. {
  182. if(pOldList)
  183. {
  184. struct _gplist * pCur = pOldList->pFirst;
  185. struct _gplist * pNew;
  186. while ( pCur )
  187. {
  188. pNew = new struct _gplist( pCur->pElement );
  189. if ( pTail )
  190. {
  191. pTail->pNext = pNew;
  192. pTail = pNew;
  193. }
  194. else // first element
  195. {
  196. pFirst =
  197. pCurrent =
  198. pTail = pNew;
  199. }
  200. pCur = pCur->pNext;
  201. }
  202. }
  203. return STATUS_OK;
  204. }
  205. /**************************************************************************
  206. * public functions for type_node_list
  207. **************************************************************************/
  208. type_node_list::type_node_list( void )
  209. {
  210. }
  211. type_node_list::type_node_list(
  212. node_skl * p)
  213. {
  214. SetPeer( p );
  215. }
  216. STATUS_T
  217. type_node_list::SetPeer(
  218. class node_skl *pNode )
  219. {
  220. return Insert( (void *)pNode );
  221. }
  222. STATUS_T
  223. type_node_list::GetPeer(
  224. class node_skl **pNode )
  225. {
  226. return GetNext ( (void **)pNode );
  227. }
  228. STATUS_T
  229. type_node_list::GetFirst(
  230. class node_skl **pNode )
  231. {
  232. STATUS_T Status;
  233. if( (Status = Init()) == STATUS_OK)
  234. {
  235. Status = GetNext( (void**)pNode );
  236. }
  237. return Status;
  238. }
  239. short
  240. IndexedList::Insert(
  241. void *pData
  242. )
  243. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  244. Routine Description:
  245. Insert a pointer into a list and return a unique index representing that
  246. pointer. Duplicate pointers return the same index.
  247. ReturnValue:
  248. The index of the pointer in the list.
  249. ----------------------------------------------------------------------------*/
  250. {
  251. _gplist *pCurrent = pFirst;
  252. short i = 0;
  253. while ( NULL != pCurrent )
  254. {
  255. if ( pCurrent->pElement == pData )
  256. return i;
  257. ++i;
  258. pCurrent = pCurrent->pNext;
  259. }
  260. gplistmgr::Insert( pData );
  261. return i;
  262. }
  263. short
  264. IndexedStringList::Insert(
  265. char * pString
  266. )
  267. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  268. Routine Description:
  269. Insert a strint into the list such that the string is associated with a
  270. unqiue index. If the name already exists don't insert it again and
  271. return the index of the previous entry.
  272. ReturnValue:
  273. The index of the string in the list.
  274. ----------------------------------------------------------------------------*/
  275. {
  276. _gplist *pCurrent = pFirst;
  277. short i = 0;
  278. while ( NULL != pCurrent )
  279. {
  280. if ( 0 == strcmp( (char *) pCurrent->pElement, pString ) )
  281. return i;
  282. ++i;
  283. pCurrent = pCurrent->pNext;
  284. }
  285. gplistmgr::Insert( pString );
  286. return i;
  287. }