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.

276 lines
7.6 KiB

  1. //+------------------------------------------------------------------
  2. //
  3. // File: cintcmd.cxx
  4. //
  5. // Contents: implementation for CIntCmdlineObj
  6. //
  7. // Synoposis: Encapsulates a command line switch which takes an
  8. // integer value, eg: /maxusers:10
  9. //
  10. // Classes: CIntCmdlineObj
  11. //
  12. // Functions:
  13. //
  14. // History: 12/27/91 Lizch Created
  15. // 04/17/92 Lizch Converted to NLS_STR
  16. // 07/29/92 davey Added nlsType and nlsLineArgType
  17. // 09/09/92 Lizch Changed SUCCESS to NO_ERROR
  18. // 09/18/92 Lizch Precompile headers
  19. // 11/14/92 DwightKr Updates for new version of NLS_STR
  20. // 10/14/93 DeanE Converted to WCHAR
  21. //
  22. //-------------------------------------------------------------------
  23. #include <comtpch.hxx>
  24. #pragma hdrstop
  25. #include <cmdlinew.hxx> // public cmdlinew stuff
  26. #include "_clw.hxx" // private cmdlinew stuff
  27. #include <ctype.h> // is functions
  28. INT StringToInt (LPCNSTR nszInt, INT *pInt);
  29. LPCNSTR nszCmdlineInt = _TEXTN("Takes an integer ");
  30. LPCNSTR nszLineArgInt = _TEXTN("<int> ");
  31. //+------------------------------------------------------------------
  32. //
  33. // Member: CIntCmdlineObj destructor
  34. //
  35. // Synoposis: Frees any memory associated with the object
  36. //
  37. // History: Added to allow casting of pValue 05/12/92 Lizch
  38. //
  39. //-------------------------------------------------------------------
  40. CIntCmdlineObj::~CIntCmdlineObj()
  41. {
  42. delete (INT *)_pValue;
  43. _pValue = NULL;
  44. }
  45. //+------------------------------------------------------------------
  46. //
  47. // Member: CIntCmdlineObj::SetValue, public
  48. //
  49. // Synposis: Stores the integer value specified after the switch
  50. // string, eg. 10 for /maxusers:10
  51. //
  52. // Effects: This implementation for the virtual method SetValue
  53. // converts the characters following the switch to an integer.
  54. // It allocates memory for the integer. If there is no
  55. // equator character, or if there is no character following
  56. // the equator character, _pValue remains NULL.
  57. //
  58. // Arguments: [nszArg] - the string following the switch on the
  59. // command line. Includes the equator (eg.
  60. // ':' or '=' ), if any.
  61. //
  62. // Requires:
  63. //
  64. // Returns: CMDLINE_NO_ERROR, CMDLINE_ERROR_OUT_OF_MEMORY,
  65. // CMDLINE_ERROR_TOO_BIG, CMDLINE_ERROR_INVALID_VALUE
  66. //
  67. // History: Created 12/27/91 Lizch
  68. // Converted to NLS_STR 04/17/92 Lizch
  69. //
  70. //-------------------------------------------------------------------
  71. INT CIntCmdlineObj::SetValue(LPCNSTR nszArg)
  72. {
  73. INT nRet;
  74. // delete any existing _pValue
  75. delete (INT *)_pValue;
  76. _pValue = NULL;
  77. _pValue = new INT;
  78. if (_pValue == NULL)
  79. {
  80. nRet = CMDLINE_ERROR_OUT_OF_MEMORY;
  81. }
  82. else
  83. {
  84. // I'm using this rather than c runtime atoi so that I
  85. // can detect error conditions like overflow and non-digits.
  86. //
  87. nRet = StringToInt(nszArg, (INT *)_pValue);
  88. if (nRet != CMDLINE_NO_ERROR)
  89. {
  90. delete (INT *)_pValue;
  91. _pValue = NULL;
  92. }
  93. }
  94. return(nRet);
  95. }
  96. //+------------------------------------------------------------------
  97. //
  98. // Member: CIntCmdlineObj::GetValue, public
  99. //
  100. // Synopsis: Returns a pointer to the switch value
  101. //
  102. // Arguments: none
  103. //
  104. // Returns: a integer pointer to the switch value, not including
  105. // the equater character
  106. //
  107. // History: Created 05/27/92 Lizch
  108. //
  109. //-------------------------------------------------------------------
  110. const int * CIntCmdlineObj::GetValue()
  111. {
  112. return((int *)_pValue);
  113. }
  114. //+------------------------------------------------------------------
  115. //
  116. // Member: CIntCmdlineObj::DisplayValue, public
  117. //
  118. // Synoposis: Prints the stored command line value according to
  119. // current display method. This will generally be to stdout.
  120. //
  121. // History: Created 12/27/91 Lizch
  122. // Converted to NLS_STR 04/17/92 Lizch
  123. //
  124. //-------------------------------------------------------------------
  125. void CIntCmdlineObj::DisplayValue()
  126. {
  127. if (_pValue != NULL)
  128. {
  129. _sNprintf(_nszErrorBuf,
  130. _TEXTN("Command line switch %s has value %d\n"),
  131. _pnszSwitch,
  132. *(int *)_pValue);
  133. (*_pfnDisplay)(_nszErrorBuf);
  134. }
  135. else
  136. {
  137. DisplayNoValue();
  138. }
  139. }
  140. //+------------------------------------------------------------------
  141. //
  142. // Function: StringToInt
  143. //
  144. // Synoposis: Converts ascii string to integer, checking for overflow,
  145. // and illegal characters. Only +, - and digits are accepted
  146. //
  147. // Arguments: [nszInt] - ascii string to convert
  148. // [pInt] - pointer to converted integer
  149. //
  150. // Returns: CMDLINE_NO_ERROR, CMDLINE_ERROR_INVALID_VALUE,
  151. // CMDLINE_ERROR_TOO_BIG
  152. //
  153. // History: Created 12/17/91 Lizch
  154. //
  155. // Notes: I'm using this rather than c runtime atoi so that I
  156. // can detect error conditions like overflow and non-digits.
  157. //
  158. //-------------------------------------------------------------------
  159. INT StringToInt(LPCNSTR nszInt, INT *pInt)
  160. {
  161. short sNegator = 1;
  162. INT iResult = 0;
  163. INT iRC = CMDLINE_NO_ERROR;
  164. // Skip any leading spaces - these can occur if the command line
  165. // switch incorporates spaces, eg "/a: 123"
  166. while (_isnspace(*nszInt))
  167. {
  168. nszInt++;
  169. }
  170. switch (*nszInt)
  171. {
  172. case L'-':
  173. sNegator = -1;
  174. nszInt++;
  175. break;
  176. case L'+':
  177. sNegator = 1;
  178. nszInt++;
  179. break;
  180. default:
  181. break;
  182. }
  183. for (;*nszInt != nchClNull; nszInt++)
  184. {
  185. if (!_isndigit(*nszInt))
  186. {
  187. iResult = 0;
  188. iRC = CMDLINE_ERROR_INVALID_VALUE;
  189. break;
  190. }
  191. iResult = (iResult * 10) + (*nszInt - '0');
  192. // Since iResult doesn't get it's sign added until later,
  193. // I can test for overflow by checking if it goes negative
  194. if (iResult < 0)
  195. {
  196. iResult = 0;
  197. iRC = CMDLINE_ERROR_TOO_BIG;
  198. break;
  199. }
  200. }
  201. *pInt = ((int)(iResult * sNegator));
  202. return(iRC);
  203. }
  204. //+-------------------------------------------------------------------
  205. //
  206. // Method: CIntCmdlineObj::QueryCmdlineType, protected, const
  207. //
  208. // Synoposis: returns a character pointer to the cmdline type.
  209. //
  210. // Arguments: None.
  211. //
  212. // Returns: const NCHAR pointer to string.
  213. //
  214. // History: 28-Jul-92 davey Created.
  215. //
  216. //--------------------------------------------------------------------
  217. LPCNSTR CIntCmdlineObj::QueryCmdlineType() const
  218. {
  219. return(nszCmdlineInt);
  220. }
  221. //+-------------------------------------------------------------------
  222. //
  223. // Method: CIntCmdlineObj::QueryLineArgType, protected, const
  224. //
  225. // Synoposis: returns a character pointer to the line arg type.
  226. //
  227. // Arguments: None.
  228. //
  229. // Returns: const NCHAR pointer to string.
  230. //
  231. // History: 28-Jul-92 davey Created.
  232. //
  233. //--------------------------------------------------------------------
  234. LPCNSTR CIntCmdlineObj::QueryLineArgType() const
  235. {
  236. // if user has not defined one then give default one
  237. if (_pnszLineArgType == NULL)
  238. {
  239. return(nszLineArgInt);
  240. }
  241. else
  242. {
  243. return(_pnszLineArgType);
  244. }
  245. }