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.

224 lines
6.8 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1994 - 1995.
  5. //
  6. // File: cmdparse.cxx
  7. //
  8. // Contents: Member functions implementation of CCmdLineParserA class
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 11-08-94 SriniG Created
  15. // 11-22-96 Ericne Removed CString dependancy,
  16. // made template, improved efficiency
  17. //
  18. //----------------------------------------------------------------------------
  19. #include "cmdparse.hxx"
  20. // This is a type-safe way of dealing with template character constants
  21. const char CCmdLineParserTemplate<char>::m_slash = '/';
  22. const char CCmdLineParserTemplate<char>::m_hyphen = '-';
  23. const WCHAR CCmdLineParserTemplate<WCHAR>::m_slash = L'/';
  24. const WCHAR CCmdLineParserTemplate<WCHAR>::m_hyphen = L'-';
  25. //
  26. // Helper functions
  27. //
  28. inline int StringCompare( const char * szFirst, const char * szSecond )
  29. {
  30. return strcmp( szFirst, szSecond );
  31. }
  32. inline int StringCompare( const wchar_t * szFirst, const wchar_t * szSecond )
  33. {
  34. return wcscmp( szFirst, szSecond );
  35. }
  36. //+---------------------------------------------------------------------------
  37. //
  38. // Function: CCmdLineParserTemplate
  39. //
  40. // Synopsis: Constructor
  41. //
  42. // Arguments: [argc] -- The "main" parameter
  43. // [argv] -- The "main" parameter
  44. //
  45. // Returns:
  46. //
  47. // History: 11-08-94 SriniG Created
  48. // 11-22-96 Ericne Removed CString dependancy,
  49. // made template, improved efficiency
  50. //
  51. // Notes:
  52. //
  53. //----------------------------------------------------------------------------
  54. template<class T>
  55. CCmdLineParserTemplate<T>::CCmdLineParserTemplate( int & argc, T **& argv )
  56. : m_argc( argc ),
  57. m_argv( argv )
  58. {
  59. int iLoop = 0;
  60. // argc and argv are modifiable. Change all switches that start with
  61. // m_hyphen to m_slash.
  62. for( iLoop = 1; iLoop < m_argc; iLoop++ )
  63. {
  64. if( m_hyphen == *argv[ iLoop ] )
  65. *argv[ iLoop ] = m_slash;
  66. }
  67. }
  68. template<class T>
  69. CCmdLineParserTemplate<T>::~CCmdLineParserTemplate()
  70. {
  71. }
  72. //+---------------------------------------------------------------------------
  73. //
  74. // Function: IsFlagExist
  75. //
  76. // Synopsis: Checks for the existence of a flag in the cmd line
  77. //
  78. // Arguments: [szFlag] -- flag string, don't include m_slash or m_hyphen
  79. //
  80. // Returns: True if the flag exists, False if not
  81. //
  82. // History: 11-08-94 SriniG Created
  83. // 11-22-96 Ericne Removed CString dependancy,
  84. // made template, improved efficiency
  85. //
  86. // Notes:
  87. //
  88. //----------------------------------------------------------------------------
  89. template<class T>
  90. BOOL CCmdLineParserTemplate<T>::IsFlagExist( const T * szFlag )
  91. {
  92. for( int iLoop = 1; iLoop < m_argc; iLoop++ )
  93. {
  94. if( ( m_slash == *m_argv[ iLoop ] || m_hyphen == *m_argv[ iLoop ] ) &&
  95. ( 0 == StringCompare( m_argv[ iLoop ] + 1, szFlag ) ) )
  96. {
  97. // Keep track of which params have been referenced
  98. *m_argv[ iLoop ] = m_hyphen;
  99. return( TRUE );
  100. }
  101. }
  102. return( FALSE );
  103. }
  104. //+---------------------------------------------------------------------------
  105. //
  106. // Function: EnumerateFlag
  107. //
  108. // Synopsis: Enumerates the parameters associated with a given flag
  109. //
  110. // Arguments: [szFlag] -- The flag
  111. // [pParams] -- pointer to the array of parameter strings
  112. // [cCount] -- Number of parameters associated with the flag.
  113. //
  114. // Returns: True if flag exists, False if the flag doesn't
  115. //
  116. // History: 11-08-94 SriniG Created
  117. // 11-22-96 Ericne Removed CString dependancy,
  118. // made template, improved efficiency
  119. //
  120. // Notes:
  121. //
  122. //----------------------------------------------------------------------------
  123. template<class T>
  124. BOOL CCmdLineParserTemplate<T>::EnumerateFlag( const T * szFlag,
  125. T **& pParams,
  126. int & nCount )
  127. {
  128. // Initialize the reference parameters
  129. pParams = NULL;
  130. nCount = 0;
  131. // Make sure we have a valid string
  132. if( NULL == szFlag )
  133. return( FALSE );
  134. // Make pParams to point to the first parameter. cCount should give
  135. // the number of parameters that follow flag
  136. for( int iLoop = 1; iLoop < m_argc; iLoop++ )
  137. {
  138. if( ( m_slash == *m_argv[ iLoop ] || m_hyphen == *m_argv[ iLoop ] ) &&
  139. ( 0 == StringCompare( m_argv[ iLoop ] + 1, szFlag ) ) )
  140. {
  141. // Keep track of which params have been referenced
  142. *m_argv[ iLoop ] = m_hyphen;
  143. // Count the number of parameters following the flag
  144. for( int jLoop = iLoop + 1; jLoop < m_argc; jLoop++ )
  145. {
  146. // BUGBUG can't have a negative number as a parameter.
  147. if( m_slash == *m_argv[ jLoop ] ||
  148. m_hyphen == *m_argv[ jLoop ] )
  149. break;
  150. else
  151. ++nCount;
  152. }
  153. // If we found parameters, set pParams to point to the first
  154. if( nCount > 0 )
  155. {
  156. pParams = m_argv + iLoop + 1;
  157. }
  158. // No need to look further
  159. return( TRUE );
  160. }
  161. }
  162. return( FALSE );
  163. }
  164. //+---------------------------------------------------------------------------
  165. //
  166. // Member: ::GetNextFlag
  167. //
  168. // Synopsis: Can get the flags in order. Can be used after attempting
  169. // to retrieve all accepted parameters -- any parameters left over
  170. // are not recognized
  171. //
  172. // Arguments: [szFlag] -- reference to a character pointer
  173. //
  174. // Returns: FALSE if there are no more flags to return
  175. //
  176. // History: 11-22-96 Ericne Created
  177. //
  178. // Notes:
  179. //
  180. //----------------------------------------------------------------------------
  181. template<class T>
  182. BOOL CCmdLineParserTemplate<T>::GetNextFlag( T *& szFlag )
  183. {
  184. // Initialize the reference parameter
  185. szFlag = NULL;
  186. // Find the first unreferenced parameter and return a pointer to it
  187. for( int iLoop = 1; iLoop < m_argc; iLoop++ )
  188. {
  189. if( m_slash == *m_argv[ iLoop ] )
  190. {
  191. *m_argv[ iLoop ] = m_hyphen;
  192. szFlag = m_argv[ iLoop ] + 1;
  193. return( TRUE );
  194. }
  195. }
  196. return( FALSE );
  197. } //::GetNextFlag