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.

300 lines
8.6 KiB

  1. //+------------------------------------------------------------------
  2. //
  3. // File: cboolcmd.cxx
  4. //
  5. // Contents: implementation for CBoolCmdlineObj
  6. //
  7. // Synoposis: Encapsulates a command line switch which takes a
  8. // Bool value. If the command line switch is present, the
  9. // value is set to TRUE, otherwise it is set to FALSE
  10. //
  11. // Classes: CBoolCmdlineObj
  12. //
  13. // Functions:
  14. // AsciiToBool private, local
  15. // CBoolCmdlineObj::~CBoolCmdlineObj public
  16. // CBoolCmdlineObj::DisplayValue public
  17. // CBoolCmdlineObj::GetValue public
  18. // CBoolCmdlineObj::SetValue public
  19. //
  20. // History: 06/15/92 DeanE Created
  21. // 07/29/92 davey Added nlsType and nlsLineArgType
  22. // 09/09/92 Lizch Changed SUCCESS to NO_ERROR
  23. // 09/18/92 Lizch Precompile headers
  24. // 11/14/92 DwightKr Updates for new version of NLS_STR
  25. // 10/14/93 DeanE Converted to WCHAR
  26. //
  27. //-------------------------------------------------------------------
  28. #include <comtpch.hxx>
  29. #pragma hdrstop
  30. #include <cmdlinew.hxx> // public cmdlinew stuff
  31. #include "_clw.hxx" // private cmdlinew stuff
  32. #include <ctype.h> // is functions
  33. INT StringToBool(LPCNSTR nszBool, BOOL *pBool);
  34. LPCNSTR nszCmdlineBool = _TEXTN("Flag ");
  35. LPCNSTR nszLineArgBool = _TEXTN("[TRUE|FALSE] ");
  36. NCHAR nchBoolEquater = _TEXTN(':');
  37. // BUGBUG - base Equater needs to be a string, then the equator for
  38. // a bool is L'<base equator><space>';
  39. //
  40. //+------------------------------------------------------------------
  41. //
  42. // Function: CBoolCmdlineObj Constructor
  43. //
  44. // Member: CBoolCmdlineObj
  45. //
  46. // Synoposis: Initialises switch string, usage strings and default value.
  47. //
  48. // Effects: Sets fSecondArg to FALSE, so that knows there is
  49. // no argument following switch.
  50. //
  51. // Arguments: [nszSwitch] - the expected switch string
  52. // [nszUsage] - the usage statement to display
  53. // [nszDefault] - the value to be used if no switch is
  54. // specified on the command line. Defaults
  55. // to FALSE if not specified.
  56. //
  57. // Returns: none, but sets _iLastError
  58. //
  59. // History: Created 12/27/91 Lizch
  60. // Converted to NLS_STR 04/17/92 Lizch
  61. //
  62. //-------------------------------------------------------------------
  63. CBoolCmdlineObj::CBoolCmdlineObj(
  64. LPCNSTR nszSwitch,
  65. LPCNSTR nszUsage,
  66. LPCNSTR nszDefault) :
  67. CBaseCmdlineObj(nszSwitch, nszUsage, nszDefault)
  68. {
  69. _fSecondArg = TRUE;
  70. SetEquater(nchBoolEquater);
  71. }
  72. //+------------------------------------------------------------------
  73. //
  74. // Member: CBoolCmdlineObj::SetValue, public
  75. //
  76. // Synposis: Sets _pValue to point to the proper value of the switch
  77. //
  78. // Arguments: [nszString] - the string following the switch on the
  79. // command line. Includes the equator (eg. ':'
  80. // or '=' ), if any. It is ignored for this
  81. // command line type.
  82. //
  83. // Returns: CMDLINE_NO_ERROR, CMDLINE_ERROR_OUT_OF_MEMORY
  84. //
  85. // History: Created 6/15/92 DeanE Stolen from Security implementation
  86. //
  87. //-------------------------------------------------------------------
  88. INT CBoolCmdlineObj::SetValue(LPCNSTR nszString)
  89. {
  90. INT nRet = CMDLINE_NO_ERROR;
  91. // Delete old value if it exists
  92. delete (BOOL *)_pValue;
  93. _pValue = NULL;
  94. // Allocate space for a new value and initialize it
  95. _pValue = new BOOL;
  96. if (_pValue == NULL)
  97. {
  98. nRet = CMDLINE_ERROR_OUT_OF_MEMORY;
  99. }
  100. else
  101. {
  102. nRet = StringToBool(nszString, (BOOL *)_pValue);
  103. if (nRet != CMDLINE_NO_ERROR)
  104. {
  105. delete (BOOL *)_pValue;
  106. _pValue = NULL;
  107. }
  108. }
  109. return(nRet);
  110. }
  111. //+------------------------------------------------------------------
  112. //
  113. // Member: CBoolCmdlineObj::GetValue, public
  114. //
  115. // Synposis: Returns pointer to BOOL that has the value.
  116. //
  117. // Arguments: void
  118. //
  119. // Returns: BOOL *
  120. //
  121. // History: Created 6/15/92 DeanE Stolen from Security implementation
  122. //
  123. //-------------------------------------------------------------------
  124. const BOOL *CBoolCmdlineObj::GetValue()
  125. {
  126. return((BOOL *)_pValue);
  127. }
  128. //+------------------------------------------------------------------
  129. //
  130. // Member: CBoolCmdlineObj::DisplayValue, public
  131. //
  132. // Synoposis: prints the stored command line value according to
  133. // current display method. This will generally be to stdout.
  134. //
  135. // History: 6/15/92 DeanE Stolen from Security implementation
  136. // 7/31/92 davey Modified to match other cmdline objs.
  137. //
  138. //-------------------------------------------------------------------
  139. void CBoolCmdlineObj::DisplayValue()
  140. {
  141. if (_pValue != NULL)
  142. {
  143. if (*(BOOL *)_pValue == TRUE)
  144. {
  145. _sNprintf(_nszErrorBuf,
  146. _TEXTN("Command line switch %s is Set\n"),
  147. _pnszSwitch);
  148. }
  149. else
  150. {
  151. _sNprintf(_nszErrorBuf,
  152. _TEXTN("Command line switch %s is Not Set\n"),
  153. _pnszSwitch);
  154. }
  155. }
  156. else
  157. {
  158. _sNprintf(_nszErrorBuf,
  159. _TEXTN("Command line switch %s has no value\n"),
  160. _pnszSwitch);
  161. }
  162. (*_pfnDisplay)(_nszErrorBuf);
  163. }
  164. //+------------------------------------------------------------------
  165. //
  166. // Member: CBoolCmdlineObj::~CBoolCmdlineObj, public
  167. //
  168. // Synoposis: Cleans up CBoolCmdlineObj objects.
  169. //
  170. // History: Created 6/15/92 DeanE Stolen from Security implementation
  171. //
  172. //-------------------------------------------------------------------
  173. CBoolCmdlineObj::~CBoolCmdlineObj()
  174. {
  175. delete (BOOL *)_pValue;
  176. _pValue = (BOOL *)NULL;
  177. }
  178. //+------------------------------------------------------------------
  179. //
  180. // Function: StringToBool
  181. //
  182. // Synoposis: Converts ascii string to the equivalent Boolean value,
  183. // TRUE for "true", FALSE for "false", or TRUE for a null
  184. // string or string starting with a space.
  185. //
  186. // Arguments: [nszBool] - string to convert
  187. // [pBool] - pointer to converted boolean
  188. //
  189. // Returns: CMDLINE_NO_ERROR, CMDLINE_ERROR_INVALID_VALUE
  190. //
  191. // History: Created 12/17/91 Lizch
  192. // Implemented as AsciiToBool 6/15/92 DeanE
  193. // Renamed StringToBool 10/13/93 DeanE
  194. //
  195. //-------------------------------------------------------------------
  196. INT StringToBool(LPCNSTR nszBool, BOOL *pBool)
  197. {
  198. INT iRC = CMDLINE_NO_ERROR;
  199. // If the string given to us is a null string or begins with a space,
  200. // the function was called from the parser and the command line arg
  201. // was given (with no value). We want the value to be TRUE, however.
  202. //
  203. if ((nchClNull == *nszBool) || (nchClSpace == *nszBool))
  204. {
  205. *pBool = TRUE;
  206. }
  207. else
  208. // Otherwise we set the boolean to a value explicitly based on
  209. // the string passed, either 'true' or 'false', case insensitive
  210. //
  211. if (0 == _ncsicmp(nszBool, nszBoolTrue) ||
  212. 0 == _ncsicmp(nszBool, nszBoolOne))
  213. {
  214. *pBool = TRUE;
  215. }
  216. else
  217. if (0 == _ncsicmp(nszBool, nszBoolFalse) ||
  218. 0 == _ncsicmp(nszBool, nszBoolZero))
  219. {
  220. *pBool = FALSE;
  221. }
  222. else
  223. {
  224. iRC = CMDLINE_ERROR_INVALID_VALUE;
  225. }
  226. return(iRC);
  227. }
  228. //+-------------------------------------------------------------------
  229. //
  230. // Method: CBoolCmdlineObj::QueryCmdlineType, protected, const
  231. //
  232. // Synoposis: returns a character pointer to the cmdline type.
  233. //
  234. // Arguments: None.
  235. //
  236. // Returns: const NCHAR pointer to string.
  237. //
  238. // History: 28-Jul-92 davey Created.
  239. //
  240. //--------------------------------------------------------------------
  241. LPCNSTR CBoolCmdlineObj::QueryCmdlineType() const
  242. {
  243. return(nszCmdlineBool);
  244. }
  245. //+-------------------------------------------------------------------
  246. //
  247. // Method: CBoolCmdlineObj::QueryLineArgType, protected, const
  248. //
  249. // Synoposis: returns a character pointer to the line arg type.
  250. //
  251. // Arguments: None.
  252. //
  253. // Returns: const NCHAR pointer to string.
  254. //
  255. // History: 28-Jul-92 davey Created.
  256. //
  257. //--------------------------------------------------------------------
  258. LPCNSTR CBoolCmdlineObj::QueryLineArgType() const
  259. {
  260. LPCNSTR nszRet;
  261. // if user has not defined one then give default one
  262. if (NULL == _pnszLineArgType)
  263. {
  264. nszRet = nszLineArgBool;
  265. }
  266. else
  267. {
  268. nszRet = _pnszLineArgType;
  269. }
  270. return(nszRet);
  271. }