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.

399 lines
7.9 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name :
  4. itemlist.cpp
  5. Abstract:
  6. Class to parse different parameters coming in from the inf
  7. Author:
  8. Christopher Achille (cachille)
  9. Project:
  10. Internet Services Setup
  11. Revision History:
  12. June 2001: Created
  13. --*/
  14. #include "stdafx.h"
  15. // Constructor for CItemList
  16. //
  17. // Initialize everything to NULL's and 0's
  18. CItemList::CItemList()
  19. : m_dwItemsinList(0),
  20. m_pItems(NULL)
  21. {
  22. }
  23. // Destructor for CItemList
  24. //
  25. //
  26. CItemList::~CItemList()
  27. {
  28. if ( m_pItems )
  29. {
  30. delete m_pItems;
  31. m_pItems = NULL;
  32. }
  33. }
  34. // function: FindNextItem
  35. //
  36. // Search through the string and find the begining of the
  37. // next item
  38. //
  39. // Parameters:
  40. // szLine - The string to be parsed
  41. // cTermChar - The termination character to use
  42. //
  43. // Return
  44. // NULL - No next termination character could be found
  45. // pointer - The string where the next splitting char is.
  46. LPTSTR
  47. CItemList::FindNextItem(LPTSTR szLine, TCHAR cTermChar)
  48. {
  49. LPTSTR szTermination = _tcschr(szLine, cTermChar);
  50. LPTSTR szOpenParen = _tcschr(szLine, '(' );
  51. LPTSTR szCloseParen = szOpenParen ? _tcschr(szOpenParen, ')' ) : NULL;
  52. if ( (szOpenParen == NULL) ||
  53. (szCloseParen == NULL) ||
  54. (szTermination < szOpenParen)
  55. )
  56. {
  57. return szTermination;
  58. }
  59. // If there is a (xxx), then lets find the termination char after that
  60. szTermination = _tcschr(szCloseParen, cTermChar);
  61. return szTermination;
  62. }
  63. // function: LoadSubList
  64. //
  65. // Load a sublist of items
  66. // A sublist, is a list inside of parenthesis
  67. //
  68. // Parameters
  69. // szList - The list of items (ie. "(test|foo|bar)"
  70. //
  71. // Return
  72. // TRUE - Loaded successfully
  73. // FALSE - Failed to load
  74. BOOL
  75. CItemList::LoadSubList(LPTSTR szList)
  76. {
  77. LPTSTR szOpenParen = _tcschr(szList, '(' );
  78. LPTSTR szCloseParen = szOpenParen ? _tcschr(szOpenParen, ')' ) : NULL;
  79. if (szOpenParen && szCloseParen)
  80. {
  81. BOOL bRet;
  82. *szCloseParen = '\0';
  83. bRet = LoadList(szList + 1);
  84. *szCloseParen = ')';
  85. return bRet;
  86. }
  87. return LoadList(szList);
  88. }
  89. // function: LoadList
  90. //
  91. // Load a list of items into our array
  92. //
  93. // Parameters:
  94. // szList - A string containind a comma seperated list of items
  95. //
  96. // Return:
  97. // FALSE - We could not load the list (either memory problems, or it was
  98. // incorrectly formatted)
  99. // TRUE - We loaded the list
  100. //
  101. BOOL
  102. CItemList::LoadList(LPTSTR szList)
  103. {
  104. DWORD dwNumItems = 0;
  105. DWORD dwCurrentItem;
  106. DWORD dwListLen;
  107. LPTSTR szListCurrent;
  108. if (szList == NULL)
  109. {
  110. // No pointer was passed in
  111. return FALSE;
  112. }
  113. // Find the number of items in list
  114. szListCurrent = szList;
  115. if (*szListCurrent)
  116. {
  117. while (szListCurrent)
  118. {
  119. // Increment the Items
  120. dwNumItems++;
  121. szListCurrent = FindNextItem(szListCurrent, ITEMLIST_TERMINATIONCHARACTER);
  122. if (szListCurrent)
  123. {
  124. szListCurrent++;
  125. }
  126. }
  127. }
  128. dwListLen = (_tcslen(szList) + 1) * sizeof(TCHAR);
  129. if ( !m_Buff.Resize( dwListLen ) )
  130. {
  131. // Could not allocate memory
  132. return FALSE;
  133. }
  134. if ( dwNumItems )
  135. {
  136. if ( m_pItems )
  137. {
  138. delete m_pItems;
  139. }
  140. m_pItems = new ( LPTSTR[dwNumItems] );
  141. if ( !m_pItems )
  142. {
  143. // Could not allocate memory
  144. return FALSE;
  145. }
  146. }
  147. // Copy the List into our own memory
  148. memcpy(m_Buff.QueryPtr(), szList, dwListLen);
  149. m_dwItemsinList = dwNumItems;
  150. // Terminate each item in list, and set pointer accordingly
  151. szListCurrent = (LPTSTR) m_Buff.QueryPtr();
  152. dwCurrentItem = 0;
  153. while ( (szListCurrent) &&
  154. (dwCurrentItem < m_dwItemsinList )
  155. )
  156. {
  157. // Set pointer for each item
  158. m_pItems[dwCurrentItem++] = szListCurrent;
  159. szListCurrent = FindNextItem(szListCurrent, ITEMLIST_TERMINATIONCHARACTER);
  160. if (szListCurrent)
  161. {
  162. *szListCurrent = '\0';
  163. szListCurrent++;
  164. }
  165. }
  166. return TRUE;
  167. }
  168. // function: GetItem
  169. //
  170. // Get an item in the list, according to its index
  171. //
  172. // Parameters
  173. // dwIndex - Index of the Item (0 Based)
  174. //
  175. // Return:
  176. // A Pointer to the begining of that string
  177. LPTSTR
  178. CItemList::GetItem(DWORD dwIndex)
  179. {
  180. if ( dwIndex >= m_dwItemsinList )
  181. {
  182. return NULL;
  183. }
  184. return m_pItems[dwIndex];
  185. }
  186. // function: GetNumberOfItems
  187. //
  188. // return the number of items in the list
  189. //
  190. DWORD
  191. CItemList::GetNumberOfItems()
  192. {
  193. return m_dwItemsinList;
  194. }
  195. // function: FindItem
  196. //
  197. // Find an Item in the list
  198. //
  199. // Parameters:
  200. // szSearchString - The string that we want to find
  201. //
  202. // Return
  203. // TRUE - It was found
  204. // FALSE - It was not found
  205. BOOL
  206. CItemList::FindItem(LPTSTR szSearchString, BOOL bCaseSensitive )
  207. {
  208. DWORD dwCurrentItem;
  209. for ( dwCurrentItem = 0; dwCurrentItem < m_dwItemsinList; dwCurrentItem++ )
  210. {
  211. if ( bCaseSensitive )
  212. {
  213. // Case Sensitive Compare
  214. if ( _tcscmp( m_pItems[dwCurrentItem], szSearchString ) == 0)
  215. {
  216. // Found item
  217. return TRUE;
  218. }
  219. }
  220. else
  221. {
  222. // Case Insensitive Compare
  223. if ( _tcsicmp( m_pItems[dwCurrentItem], szSearchString ) == 0)
  224. {
  225. // Found item
  226. return TRUE;
  227. }
  228. }
  229. }
  230. return FALSE;
  231. }
  232. // function: IsNumber
  233. //
  234. // Determines if the parameter that we are looking at is a number
  235. //
  236. // Parameter
  237. // dwIndex - The index of the parameter to look at
  238. //
  239. // Return
  240. // TRUE - It is a number
  241. // FALSE - It is not a number
  242. BOOL
  243. CItemList::IsNumber(DWORD dwIndex)
  244. {
  245. LPTSTR szNumber = GetItem(dwIndex);
  246. BOOL bHex = FALSE;
  247. if (!szNumber)
  248. {
  249. return FALSE;
  250. }
  251. szNumber = SkipWhiteSpaces(szNumber);
  252. // Skip "0x" if it exists, if not we will assume base 10 number
  253. if ( _tcsncmp( szNumber, _T("0x"), 2 ) == 0)
  254. {
  255. szNumber += 2;
  256. bHex = TRUE;
  257. }
  258. while ( ( *szNumber != '\0' ) &&
  259. ( ( ( *szNumber >= '0' ) && ( *szNumber <= '9' ) ) ||
  260. ( ( *szNumber >= 'a' ) && ( *szNumber <= 'f' ) && ( bHex ) ) ||
  261. ( ( *szNumber >= 'A' ) && ( *szNumber <= 'F' ) && ( bHex ) )
  262. )
  263. )
  264. {
  265. szNumber ++;
  266. }
  267. szNumber = SkipWhiteSpaces(szNumber);
  268. return ( *szNumber == '\0' );
  269. }
  270. // function: GetNumber
  271. //
  272. // Get the value of this param as a number
  273. //
  274. // Parameters:
  275. // dwIndex - Index of item to find
  276. DWORD
  277. CItemList::GetNumber(DWORD dwIndex)
  278. {
  279. LPTSTR szNumber = GetItem(dwIndex);
  280. BOOL bHex = FALSE;
  281. DWORD dwVal = 0;
  282. if ( !szNumber ||
  283. !IsNumber(dwIndex) )
  284. {
  285. return 0;
  286. }
  287. szNumber = SkipWhiteSpaces(szNumber);
  288. // Skip "0x" if it exists, if not we will assume base 10 number
  289. if ( _tcsncmp( szNumber, _T("0x"), 2 ) == 0)
  290. {
  291. szNumber += 2;
  292. bHex = TRUE;
  293. }
  294. while ( ( ( *szNumber >= '0' ) && ( *szNumber <= '9' ) ) ||
  295. ( ( *szNumber >= 'a' ) && ( *szNumber <= 'f' ) ) ||
  296. ( ( *szNumber >= 'A' ) && ( *szNumber <= 'F' ) )
  297. )
  298. {
  299. dwVal = dwVal * (bHex ? 16 : 10);
  300. if ( ( *szNumber >= '0' ) && ( *szNumber <= '9' ) )
  301. {
  302. dwVal = dwVal + *szNumber - '0';
  303. }
  304. else
  305. if ( ( *szNumber >= 'a' ) && ( *szNumber <= 'f' ) )
  306. {
  307. dwVal = dwVal + 10 + *szNumber - 'a';
  308. }
  309. else
  310. if ( ( *szNumber >= 'A' ) && ( *szNumber <= 'F' ) )
  311. {
  312. dwVal = dwVal + 10 + *szNumber - 'A';
  313. }
  314. szNumber++;
  315. }
  316. return dwVal;
  317. }
  318. // function: SkipWhiteSpaces
  319. //
  320. // Skips white spaces
  321. //
  322. // Parameter
  323. // szLine - The line to start skipping st
  324. //
  325. // Return:
  326. // pointer to first non white character
  327. //
  328. LPTSTR
  329. CItemList::SkipWhiteSpaces(LPTSTR szLine)
  330. {
  331. while ( ( *szLine == ' ' ) ||
  332. ( *szLine == '\t'))
  333. {
  334. szLine++;
  335. }
  336. return szLine;
  337. }