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.

227 lines
5.5 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation 1992 - 1993.
  6. //
  7. // File: nstrlist.cxx
  8. //
  9. // Contents: Implementation of class CnStrList
  10. //
  11. // Functions: CnStrList::CnStrList
  12. // CnStrList::~CnStrList
  13. // CnStrList::QueryError
  14. // CnStrList::Next
  15. // CnStrList::Reset
  16. // CnStrList::Append
  17. //
  18. // History: XimingZ 23-Dec-1993 Created
  19. //
  20. //------------------------------------------------------------------------
  21. #include <comtpch.hxx>
  22. #pragma hdrstop
  23. #include <cmdlinew.hxx> // public cmdlinew stuff
  24. #include "_clw.hxx" // private cmdlinew stuff
  25. #include <ctype.h> // is functions
  26. #include <wstrlist.hxx>
  27. //+-----------------------------------------------------------------------
  28. //
  29. // Function: CnStrList::CnStrList
  30. //
  31. // Synopsis: Constructor, which creates a string list.
  32. //
  33. // Arguments: [pnszItems] -- Supplied string consisting of zero or more
  34. // item strings separated by delimiters. Two
  35. // consecutive delimiters mean an item of
  36. // an empty string in between.
  37. // [pnszDelims] -- Supplied set of delimiter characters.
  38. //
  39. // Returns: Nothing
  40. //
  41. // History: XimingZ 23-Dec-1993 Created
  42. //
  43. //------------------------------------------------------------------------
  44. CnStrList::CnStrList(LPCNSTR pnszItems, LPCNSTR pnszDelims) :
  45. _head(NULL), _tail(NULL), _next(NULL), _iLastError(NSTRLIST_NO_ERROR)
  46. {
  47. LPCNSTR pnszNewItem;
  48. PNSTR pnszLocalItems;
  49. PNSTR pnszHead;
  50. BOOL fDone;
  51. SetError(NSTRLIST_NO_ERROR);
  52. if (pnszItems == NULL)
  53. {
  54. // No items.
  55. return;
  56. }
  57. // Make a local copy of items.
  58. pnszLocalItems = new NCHAR[_ncslen(pnszItems) + 1];
  59. if (pnszLocalItems == NULL)
  60. {
  61. SetError(NSTRLIST_ERROR_OUT_OF_MEMORY);
  62. return;
  63. }
  64. _ncscpy(pnszLocalItems, pnszItems);
  65. pnszHead = pnszLocalItems;
  66. fDone = FALSE;
  67. while (fDone == FALSE)
  68. {
  69. pnszNewItem = (LPCNSTR)pnszLocalItems; // Beginning of a new item.
  70. // Search for next delimiter or end of given string.
  71. while (*pnszLocalItems != _TEXTN('\0') &&
  72. _ncschr(pnszDelims, *pnszLocalItems) == NULL)
  73. {
  74. pnszLocalItems++;
  75. }
  76. if (*pnszLocalItems == _TEXTN('\0'))
  77. {
  78. // End of string.
  79. fDone = TRUE;
  80. }
  81. else
  82. {
  83. // Replace end of item with L'\0' for Append.
  84. *pnszLocalItems = _TEXTN('\0');
  85. }
  86. // Append the item to the list.
  87. if (Append(pnszNewItem) == FALSE)
  88. {
  89. SetError(NSTRLIST_ERROR_OUT_OF_MEMORY);
  90. fDone = TRUE;
  91. }
  92. pnszLocalItems++;
  93. }
  94. delete pnszHead;
  95. }
  96. //+-----------------------------------------------------------------------
  97. //
  98. // Function: CnStrList::~CnStrList
  99. //
  100. // Synopsis: Destructor.
  101. //
  102. // Arguments: None.
  103. //
  104. // Returns: Nothing
  105. //
  106. // History: XimingZ 23-Dec-1993 Created
  107. //
  108. //------------------------------------------------------------------------
  109. CnStrList::~CnStrList()
  110. {
  111. NSTRLIST *pNext;
  112. while (_head != NULL)
  113. {
  114. pNext = _head->pNext;
  115. delete [] _head->pnszStr;
  116. delete [] _head;
  117. _head = pNext;
  118. }
  119. }
  120. //+-----------------------------------------------------------------------
  121. //
  122. // Function: CnStrList::Next
  123. //
  124. // Synopsis: Get next item.
  125. //
  126. // Arguments: None.
  127. //
  128. // Returns: Next item if there is one, or else NULL.
  129. //
  130. // History: XimingZ 23-Dec-1993 Created
  131. //
  132. //------------------------------------------------------------------------
  133. LPCNSTR CnStrList::Next()
  134. {
  135. LPCNSTR pnsz;
  136. if (_next == NULL)
  137. {
  138. return NULL;
  139. }
  140. pnsz = _next->pnszStr;
  141. _next = _next->pNext;
  142. return pnsz;
  143. }
  144. //+-----------------------------------------------------------------------
  145. //
  146. // Function: CnStrList::Reset
  147. //
  148. // Synopsis: Reset the iterator.
  149. //
  150. // Arguments: None
  151. //
  152. // Returns: None
  153. //
  154. // History: XimingZ 23-Dec-1993 Created
  155. //
  156. //------------------------------------------------------------------------
  157. VOID CnStrList::Reset()
  158. {
  159. _next = _head;
  160. }
  161. //+-----------------------------------------------------------------------
  162. //
  163. // Function: CnStrList::Append
  164. //
  165. // Synopsis: Append a string to the list.
  166. //
  167. // Arguments: [pnszItem] -- Supplied string.
  168. //
  169. // Returns: TRUE if the function succeeds or else (out of memory) FALSE.
  170. //
  171. // History: XimingZ 23-Dec-1993 Created
  172. //
  173. //------------------------------------------------------------------------
  174. BOOL CnStrList::Append(LPCNSTR pnszItem)
  175. {
  176. // Construct a new node.
  177. NSTRLIST *pNode = new NSTRLIST[1];
  178. if (pNode == NULL)
  179. {
  180. return FALSE;
  181. }
  182. pNode->pnszStr = new NCHAR [_ncslen(pnszItem) + 1];
  183. if (pNode->pnszStr == NULL)
  184. {
  185. delete [] pNode;
  186. return FALSE;
  187. }
  188. _ncscpy(pNode->pnszStr, pnszItem);
  189. pNode->pNext = NULL;
  190. // Add it to the list.
  191. if (_head == NULL)
  192. {
  193. _next = _head = pNode;
  194. }
  195. else
  196. {
  197. _tail->pNext = pNode;
  198. }
  199. _tail = pNode;
  200. return TRUE;
  201. }