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.

272 lines
4.0 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. CQueue.cpp
  5. Abstract:
  6. The implementation of the C++
  7. Queue class.
  8. Notes:
  9. Unicode only.
  10. History:
  11. 10/10/2000 a-fwills Created
  12. --*/
  13. #include "CQueue.h"
  14. /*++
  15. Routine Description:
  16. CQNode c'tor
  17. Arguments:
  18. lpwszString(in): Null-delimited string assigned to CQNode on
  19. creation, which takes place when CQueue Enqueues a string.
  20. Return Value:
  21. --*/
  22. CQNode::CQNode(
  23. LPWSTR lpwszString
  24. )
  25. {
  26. int nLen = wcslen(lpwszString);
  27. LPWSTR lpwAlloc;
  28. // Will be null if HeapAlloc fails. This will result in
  29. // FALSE return value when trying to Enqueue strings in CQueue.
  30. m_lpwszString = 0;
  31. m_pNext = 0;
  32. if(!(lpwAlloc = (LPWSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
  33. sizeof(WCHAR)*(nLen+1))))
  34. return;
  35. m_lpwszString = lpwAlloc;
  36. wcscpy(m_lpwszString, lpwszString);
  37. }
  38. /*++
  39. Routine Description:
  40. CQNode d'tor
  41. Arguments:
  42. None.
  43. Return Value:
  44. None.
  45. --*/
  46. CQNode::~CQNode(
  47. )
  48. {
  49. if(m_lpwszString)
  50. HeapFree(GetProcessHeap(), 0, m_lpwszString);
  51. }
  52. /*++
  53. Routine Description:
  54. CQNode copy c'tor
  55. Arguments:
  56. lpwString(out): Null delimited buffer to contain string
  57. enqueued contained by CQNode.
  58. nMaxLen(in): Maximum length available in buffer lpwString
  59. for copy of CQNode string. String returned will be
  60. null-delimited.
  61. Return Value:
  62. TRUE: CQNode successfully allocated storage for a string
  63. on creation (is non-null), and was able to return a
  64. string of zero lenght to nMaxLen length.
  65. FALSE: CQNode does not contain a valid storage space for
  66. a string to return.
  67. --*/
  68. BOOL CQNode::Copy(
  69. LPWSTR lpwString,
  70. int nMaxLen
  71. )
  72. {
  73. if(!m_lpwszString)
  74. return FALSE;
  75. wcsncpy(lpwString, m_lpwszString, nMaxLen);
  76. *(lpwString+nMaxLen) = 0;
  77. return TRUE;
  78. }
  79. /*++
  80. Routine Description:
  81. CQueue c'tor
  82. Arguments:
  83. None.
  84. Return Value:
  85. --*/
  86. CQueue::CQueue(
  87. )
  88. {
  89. m_cSize = 0;
  90. m_pHead = m_pTail = 0;
  91. }
  92. /*++
  93. Routine Description:
  94. CQeue d'tor
  95. Arguments:
  96. None.
  97. Return Value:
  98. --*/
  99. CQueue::~CQueue(
  100. )
  101. {
  102. CQNode *pQNode;
  103. while(m_pHead) {
  104. pQNode = m_pHead;
  105. m_pHead = m_pHead->m_pNext;
  106. delete pQNode;
  107. }
  108. }
  109. /*++
  110. Routine Description:
  111. Removes a string from the queue.
  112. Arguments:
  113. lpwString(out): buffer to contain null-delimited string
  114. being dequeued.
  115. nMaxLen(in): maximum length available for string in buffer.
  116. Return Value:
  117. TRUE: Queue is not empty and string was successfully
  118. returned in buffer lpwString.
  119. FALSE: Queue was empty. No string was returned in buffer.
  120. --*/
  121. BOOL CQueue::Dequeue(
  122. LPWSTR lpwString,
  123. int nMaxLen,
  124. BOOL fPersist
  125. )
  126. {
  127. CQNode *pQNode;
  128. if (fPersist) {
  129. pQNode = m_pHead;
  130. pQNode->Copy(lpwString, nMaxLen);
  131. } else {
  132. if(!m_pHead)
  133. return FALSE;
  134. pQNode = m_pHead;
  135. m_pHead = m_pHead->m_pNext;
  136. if(!m_pHead)
  137. m_pTail = 0;
  138. pQNode->Copy(lpwString, nMaxLen);
  139. delete pQNode;
  140. m_cSize--;
  141. }
  142. return TRUE;
  143. }
  144. /*++
  145. Routine Description:
  146. Adds a string to the queue.
  147. Arguments:
  148. lpwszString(in): null-delimited string to enqueue.
  149. Return Value:
  150. --*/
  151. BOOL CQueue::Enqueue(
  152. LPWSTR lpwszString
  153. )
  154. {
  155. CQNode *pNode = new CQNode(lpwszString), *pTail;
  156. pTail = pNode;
  157. if(m_pTail)
  158. m_pTail->m_pNext = pTail;
  159. m_pTail = pTail;
  160. if(!m_pHead)
  161. m_pHead = m_pTail;
  162. m_cSize++;
  163. return true;
  164. }
  165. /*++
  166. Routine Description:
  167. Retrives the length of the queue.
  168. Arguments:
  169. None.
  170. Return Value:
  171. The length of the current string at the head of the queue.
  172. Will return -1 if no strings are enqueued.
  173. --*/
  174. int CQueue::GetLength(
  175. )
  176. {
  177. if(!m_pHead)
  178. return -1;
  179. return m_pHead->GetLength();
  180. }