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.

425 lines
13 KiB

  1. //+------------------------------------------------------------------
  2. //
  3. // File: cstrlist.cxx
  4. //
  5. // Contents: implementation for CStrListCmdlineObj
  6. //
  7. // Synoposis: Encapsulates a command line switch that takes a list
  8. // of strings.
  9. //
  10. // Classes: CStrListCmdlineObj
  11. //
  12. // Functions:
  13. //
  14. // History: 12/27/91 Lizch Created
  15. // 04/17/92 Lizch Converted to NLS_STR
  16. // 05/11/92 Lizch Incorporated review feedback
  17. // 07/07/92 Lizch Added GetValue method
  18. // 07/29/92 davey Added nlsType and nlsLineArgType
  19. // 09/09/92 Lizch Changed SUCCESS to NO_ERROR
  20. // 09/18/92 Lizch Precompile headers
  21. // 10/14/93 DeanE Converted to WCHAR
  22. // 12/23/93 XimingZ Converted to CWStrList
  23. //
  24. //-------------------------------------------------------------------
  25. #include <comtpch.hxx>
  26. #pragma hdrstop
  27. #include <cmdlinew.hxx> // public cmdlinew stuff
  28. #include "_clw.hxx" // private cmdlinew stuff
  29. #include <ctype.h> // is functions
  30. LPCNSTR nszDefaultDelims = _TEXTN(",; ");
  31. LPCNSTR nszCmdlineType = _TEXTN("Takes a list of strings ");
  32. LPCNSTR nszLineArgType = _TEXTN("<string_list>");
  33. //+------------------------------------------------------------------
  34. //
  35. // Function: CStrListCmdlineObj Constructor (1 of 2)
  36. //
  37. // Member: CStrListCmdlineObj
  38. //
  39. // Synoposis: Initialises switch string, usage values and whether the
  40. // switch is mandatory. This constructor gives no default value
  41. // for this switch. Also initialises the Delimiters
  42. //
  43. // Effects:
  44. //
  45. // Arguments: [nszSwitch] - the expected switch string
  46. // [nszUsage] - the usage statement to display
  47. // [fMustHave] - whether the switch is mandatory or not.
  48. // if it is, an error will be generated if
  49. // the switch is not specified on the
  50. // command line. Defaults to FALSE.
  51. // [nszLineArg] - line argument string
  52. //
  53. // Returns: none, but sets _iLastError
  54. //
  55. // History: 04/17/92 Lizch Created
  56. // 08/03/92 Davey Added nlsLineArg
  57. //
  58. //-------------------------------------------------------------------
  59. CStrListCmdlineObj::CStrListCmdlineObj(
  60. LPCNSTR nszSwitch,
  61. LPCNSTR nszUsage,
  62. BOOL fMustHave,
  63. LPCNSTR nszLineArg) :
  64. CBaseCmdlineObj(nszSwitch, nszUsage, fMustHave, nszLineArg),
  65. _pNStrList(NULL)
  66. {
  67. SetDelims(nszDefaultDelims);
  68. }
  69. //+------------------------------------------------------------------
  70. //
  71. // Function: CStrListCmdlineObj Constructor (2 of 2)
  72. //
  73. // Member: CStrListCmdlineObj
  74. //
  75. // Synoposis: Initialises switch string, usage strings and default value.
  76. // Also initialises the Delimiters
  77. // This constructor is only used if a switch is optional.
  78. //
  79. // Effects: Sets fMandatory to FALSE (ie. switch is optional)
  80. //
  81. // Arguments: [nszSwitch] - the expected switch string
  82. // [nszUsage] - the usage statement to display
  83. // [nszDefault] - the value to be used if no switch is
  84. // specified on the command line.
  85. // [nszLineArg] - line argument string
  86. //
  87. // Returns: none, but sets _iLastError
  88. //
  89. // History: 04/17/92 Lizch Created.
  90. // 08/03/92 Davey Added nlsLineArg
  91. //
  92. //-------------------------------------------------------------------
  93. CStrListCmdlineObj::CStrListCmdlineObj(
  94. LPCNSTR nszSwitch,
  95. LPCNSTR nszUsage,
  96. LPCNSTR nszDefault,
  97. LPCNSTR nszLineArg) :
  98. CBaseCmdlineObj(nszSwitch, nszUsage, nszDefault, nszLineArg),
  99. _pNStrList(NULL)
  100. {
  101. SetDelims(nszDefaultDelims);
  102. }
  103. //+------------------------------------------------------------------
  104. //
  105. // Member: CStrListCmdlineObj::SetDelims, public
  106. //
  107. // Synoposis: Sets Delimiters for items within the list.
  108. //
  109. // Arguments: [nszDelims] - the list of possible separators
  110. //
  111. // Returns: ERROR_NOT_ENOUGH_MEMORY or NO_ERROR
  112. //
  113. // History: Created 12/27/91 Lizch
  114. // Converted to NLS_STR 04/17/92 Lizch
  115. //
  116. //-------------------------------------------------------------------
  117. INT CStrListCmdlineObj::SetDelims(LPCNSTR nszDelims)
  118. {
  119. INT nRet = CMDLINE_NO_ERROR;
  120. _pnszListDelims = new NCHAR[_ncslen(nszDelims)+1];
  121. if (NULL == _pnszListDelims)
  122. {
  123. nRet = CMDLINE_ERROR_OUT_OF_MEMORY;
  124. }
  125. else
  126. {
  127. _ncscpy(_pnszListDelims, nszDelims);
  128. }
  129. return(nRet);
  130. }
  131. //+------------------------------------------------------------------
  132. //
  133. // Member: CStrListCmdlineObj::~CStrListCmdlineObj
  134. //
  135. // Synoposis: Frees the string list associated with the object
  136. //
  137. // History: Created 12/27/91 Lizch
  138. // Converted to NLS_STR 04/17/92 Lizch
  139. // Converted to WSTRLIST 12/23/93 XimingZ
  140. //
  141. //-------------------------------------------------------------------
  142. CStrListCmdlineObj::~CStrListCmdlineObj()
  143. {
  144. delete (NCHAR *)_pValue;
  145. _pValue = NULL;
  146. delete _pNStrList;
  147. delete _pnszListDelims;
  148. }
  149. //+------------------------------------------------------------------
  150. //
  151. // Member: CStrListCmdlineObj::SetValue, public
  152. //
  153. // Synoposis: Uses the CnStrList class to store the list of strings
  154. // following a command line switch, eg. "M1" "M2" "M3" from
  155. // "/computers:M1,M2,M3"
  156. //
  157. // Arguments: [nszArg] - the string following the switch on the command
  158. // line. Excludes the equator (eg. ':' or '=' ).
  159. //
  160. // Returns: CMDLINE_NO_ERROR or CMDLINE_ERROR_OUT_OF_MEMORY
  161. //
  162. // History: Created 12/27/91 Lizch
  163. // Converted to NLS_STR 04/17/92 Lizch
  164. // Converted to CnStrList 12/23/93 XimingZ
  165. //
  166. //-------------------------------------------------------------------
  167. INT CStrListCmdlineObj::SetValue(LPCNSTR nszArg)
  168. {
  169. INT nRet = CMDLINE_NO_ERROR;
  170. // delete any existing pValue and pWStrList.
  171. if (_pValue != NULL)
  172. {
  173. delete (NCHAR *)_pValue;
  174. _pValue = NULL;
  175. }
  176. if (_pNStrList != NULL)
  177. {
  178. delete _pNStrList;
  179. _pNStrList = NULL;
  180. }
  181. if (nszArg == NULL)
  182. {
  183. _pValue = NULL;
  184. }
  185. else
  186. {
  187. _pValue = new NCHAR[_ncslen(nszArg)+1];
  188. if (_pValue == NULL)
  189. {
  190. nRet = CMDLINE_ERROR_OUT_OF_MEMORY;
  191. }
  192. else
  193. {
  194. _ncscpy((NCHAR *)_pValue, nszArg);
  195. }
  196. _pNStrList = new CnStrList(nszArg, _pnszListDelims);
  197. if (_pNStrList == NULL ||
  198. _pNStrList->QueryError() != NSTRLIST_NO_ERROR)
  199. {
  200. nRet = CMDLINE_ERROR_OUT_OF_MEMORY;
  201. }
  202. }
  203. return(nRet);
  204. }
  205. //+------------------------------------------------------------------
  206. //
  207. // Member: CStrListCmdlineObj::ResetValue, public
  208. //
  209. // Synopsis: Releases all the internal elements of the object.
  210. //
  211. //
  212. //
  213. // Arguments: [nszArg] - the string following the switch on the command
  214. // line. Excludes the equator (eg. ':' or '=' ).
  215. //
  216. // Returns: CMDLINE_NO_ERROR or CMDLINE_ERROR_OUT_OF_MEMORY
  217. //
  218. // History: Created 06/13/97 MariusB
  219. //
  220. //-------------------------------------------------------------------
  221. void CStrListCmdlineObj::ResetValue()
  222. {
  223. // delete any existing pValue and pWStrList.
  224. if (_pValue != NULL)
  225. {
  226. delete (NCHAR *)_pValue;
  227. _pValue = NULL;
  228. }
  229. if (_pNStrList != NULL)
  230. {
  231. delete _pNStrList;
  232. _pNStrList = NULL;
  233. }
  234. }
  235. //+------------------------------------------------------------------
  236. //
  237. // Member: CStrListCmdlineObj::GetValue, public
  238. //
  239. // Synposis: Returns pointer to NCHAR string that holds the next value.
  240. // If no next value exists, NULL is returned. The returned
  241. // string will stay alive only within the lifetime of the
  242. // current CStrListCmdlineObj object and before any call to
  243. // SetValue.
  244. //
  245. // Arguments: void
  246. //
  247. // Returns: LPCNSTR
  248. //
  249. // History: Created 7/07/92 Lizch
  250. // Changed to CnStrList 12/23/93 XimingZ
  251. //
  252. //-------------------------------------------------------------------
  253. LPCNSTR CStrListCmdlineObj::GetValue()
  254. {
  255. if (_pNStrList == NULL)
  256. {
  257. return NULL;
  258. }
  259. return _pNStrList->Next();
  260. }
  261. //+------------------------------------------------------------------
  262. //
  263. // Member: CStrListCmdlineObj::Reset, public
  264. //
  265. // Synposis: Reset string iterator.
  266. //
  267. // Arguments: void
  268. //
  269. // Returns: none
  270. //
  271. // History: Created 12/23/93 XimingZ
  272. //
  273. //-------------------------------------------------------------------
  274. VOID CStrListCmdlineObj::Reset()
  275. {
  276. if (_pNStrList != NULL)
  277. {
  278. _pNStrList->Reset();
  279. }
  280. }
  281. //+------------------------------------------------------------------
  282. //
  283. // Member: CStrListCmdlineObj::DisplayValue, public
  284. //
  285. // Synoposis: Prints the stored command line value to stdout.
  286. //
  287. // History: Created 12/27/91 Lizch
  288. // Converted to NLS_STR 04/17/92 Lizch
  289. // Changed to CnStrList 12/23/93 XimingZ
  290. //
  291. //-------------------------------------------------------------------
  292. void CStrListCmdlineObj::DisplayValue()
  293. {
  294. if (_pValue != NULL)
  295. {
  296. LPCNSTR pnszItem;
  297. CnStrList StringList((NCHAR *)_pValue, _pnszListDelims);
  298. _sNprintf (_nszErrorBuf,
  299. _TEXTN("Switch %ls specified the following strings\n"),
  300. _pnszSwitch);
  301. (_pfnDisplay)(_nszErrorBuf);
  302. while ((pnszItem = StringList.Next()) != NULL)
  303. {
  304. _sNprintf(_nszErrorBuf, _TEXTN("\t%ls\n"), pnszItem);
  305. (*_pfnDisplay)(_nszErrorBuf);
  306. }
  307. }
  308. else
  309. {
  310. DisplayNoValue();
  311. }
  312. }
  313. //+-------------------------------------------------------------------
  314. //
  315. // Method: CStrListCmdlineObj::QueryCmdlineType, protected, const
  316. //
  317. // Synoposis: returns a character pointer to the cmdline type.
  318. //
  319. // Arguments: None.
  320. //
  321. // Returns: const NCHAR pointer to string.
  322. //
  323. // History: 28-Jul-92 davey Created.
  324. //
  325. //--------------------------------------------------------------------
  326. const NCHAR *CStrListCmdlineObj::QueryCmdlineType() const
  327. {
  328. return(nszCmdlineType);
  329. }
  330. //+-------------------------------------------------------------------
  331. //
  332. // Method: CStrListCmdlineObj::QueryLineArgType, protected, const
  333. //
  334. // Synoposis: returns a character pointer to the line arg type.
  335. //
  336. // Arguments: None.
  337. //
  338. // Returns: const NLS_STR reference to string.
  339. //
  340. // History: 28-Jul-92 davey Created.
  341. //
  342. //--------------------------------------------------------------------
  343. const NCHAR *CStrListCmdlineObj::QueryLineArgType() const
  344. {
  345. // if user has not defined one then give default one
  346. if (_pnszLineArgType == NULL)
  347. {
  348. return(nszLineArgType);
  349. }
  350. else
  351. {
  352. return(_pnszLineArgType);
  353. }
  354. }
  355. //+------------------------------------------------------------------
  356. //
  357. // Member: CStrListCmdlineObj::DisplaySpecialUsage, protected
  358. //
  359. // Synoposis: Prints the switch usage statement to stdout
  360. //
  361. // Arguments: [usDisplayWidth] - total possible width available
  362. // to display
  363. // [usIndent] - amount to indent.
  364. // [pusWidth] - space to print on current line
  365. //
  366. // Returns: error code from QueryError,
  367. // error code from DisplayStringByWords
  368. //
  369. // History: 12/27/91 Lizch Created
  370. // 04/17/92 Lizch Converted to NLS_STR
  371. // 07/29/92 Davey Modified to work with new usage display
  372. //
  373. //-------------------------------------------------------------------
  374. INT CStrListCmdlineObj::DisplaySpecialUsage(
  375. USHORT usDisplayWidth,
  376. USHORT usIndent,
  377. USHORT *pusWidth)
  378. {
  379. NCHAR nszBuf[150];
  380. _sNprintf(nszBuf,
  381. _TEXTN(" The strings in the list are separated by one of the")
  382. _TEXTN(" following character(s): \"%ls\"."),
  383. _pnszListDelims);
  384. return(DisplayStringByWords(
  385. nszBuf,
  386. usDisplayWidth,
  387. usIndent,
  388. pusWidth));
  389. }