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.

214 lines
4.4 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Module : Common
  4. // Description : Generic Parser and CommandLine Definitions
  5. // Dependency on MFC for CString
  6. //
  7. // File : genparse.h
  8. // Author : kulor
  9. // Date : 05/08/2000
  10. //
  11. // History :
  12. //
  13. ///////////////////////////////////////////////////////////////////////////
  14. #pragma once
  15. ///////////////////////////////////////////////////////////////////////////
  16. #ifndef INOUT
  17. #define INOUT
  18. #endif
  19. #ifndef IN
  20. #define IN
  21. #endif
  22. #ifndef OUT
  23. #define OUT
  24. #endif
  25. #define SPACE TEXT (' ')
  26. #define EQUALSTO TEXT ('=')
  27. ///////////////////////////////////////////////////////////////////////////
  28. inline bool SZIsValid
  29. (
  30. IN LPCTSTR psz
  31. )
  32. {
  33. return ( psz != NULL ) && ( *psz != NULL );
  34. }
  35. ///////////////////////////////////////////////////////////////////////////
  36. inline void SkipSpaces
  37. (
  38. INOUT LPCTSTR pszText
  39. )
  40. {
  41. while (pszText && *pszText == SPACE)
  42. pszText++;
  43. }
  44. ///////////////////////////////////////////////////////////////////////////
  45. inline long Cstrncpy
  46. (
  47. INOUT CString &sDest,
  48. IN LPCTSTR pszSrc,
  49. IN LONG cText
  50. )
  51. {
  52. long nCopied = cText;
  53. while (cText-- > 0) {
  54. sDest += *pszSrc++;
  55. }
  56. sDest += TCHAR( NULL );
  57. return (nCopied);
  58. }
  59. ///////////////////////////////////////////////////////////////////////////
  60. class CGenParser {
  61. public:
  62. CGenParser ()
  63. {
  64. m_pszDelims = TEXT ("-/");
  65. Initialize ( NULL, NULL );
  66. }
  67. CGenParser ( LPCTSTR pszText , LPCTSTR pszDelims = NULL )
  68. {
  69. Initialize (pszText, pszDelims );
  70. }
  71. virtual ~CGenParser ()
  72. {
  73. }
  74. void SetDelims ( LPCTSTR pszDelims )
  75. {
  76. if ( pszDelims )
  77. m_pszDelims = pszDelims;
  78. }
  79. virtual void Initialize ( LPCTSTR pszText , LPCTSTR pszDelims = NULL )
  80. {
  81. m_pszText = pszText;
  82. SetDelims (pszDelims);
  83. }
  84. bool IsOneof ( TCHAR ch, LPCTSTR pszText , LONG cText )
  85. {
  86. for ( int i=0 ; i<cText ; i++ ) {
  87. if ( pszText[i] == ch )
  88. return true;
  89. }
  90. return false;
  91. }
  92. virtual bool IsDelimiter ( TCHAR ch )
  93. {
  94. for ( int i=0 ; m_pszDelims[i] ; i++ ) {
  95. if ( ch == m_pszDelims[i] )
  96. return true;
  97. }
  98. return false;
  99. }
  100. virtual LPCTSTR GetNext ()
  101. {
  102. while ( SZIsValid (m_pszText) ) {
  103. if ( IsDelimiter (*m_pszText) ) {
  104. LPCTSTR pszBuf = m_pszText;
  105. m_pszText++;
  106. return ( pszBuf );
  107. }
  108. m_pszText++;
  109. }
  110. return ( NULL );
  111. }
  112. protected:
  113. LPCTSTR m_pszText;
  114. LPCTSTR m_pszDelims;
  115. };
  116. ///////////////////////////////////////////////////////////////////////////
  117. class CGenCommandLine : public CGenParser {
  118. typedef CGenParser BaseClass;
  119. public:
  120. CGenCommandLine ( void ) : BaseClass ()
  121. {}
  122. CGenCommandLine ( LPCTSTR pszCmdLine ) : BaseClass ( pszCmdLine )
  123. {}
  124. virtual ~CGenCommandLine ( void )
  125. {}
  126. virtual LPCTSTR GetNext ()
  127. {
  128. m_sToken.Empty ();
  129. m_sValue.Empty ();
  130. LPCTSTR pszRetVal = NULL;
  131. // get the next argv
  132. if ( (pszRetVal = BaseClass::GetNext ()) != NULL ) {
  133. LPCTSTR pszBuf = pszRetVal;
  134. LONG cText = 0;
  135. // extract the LHS
  136. while ( SZIsValid( pszBuf+cText ) && pszBuf[cText] != SPACE && pszBuf[cText] != EQUALSTO )
  137. cText++;
  138. // ignore delimiter
  139. Cstrncpy ( m_sToken, pszBuf+1, cText-1 );
  140. pszBuf += cText;
  141. // move to first char after LHS
  142. cText = 0;
  143. pszBuf++;
  144. // eat white space
  145. SkipSpaces(pszBuf);
  146. if ( IsDelimiter ( *pszBuf ) == false ) {
  147. // extract the RHS
  148. while ( SZIsValid( pszBuf+cText ) && pszBuf[cText] != SPACE )
  149. cText++;
  150. Cstrncpy ( m_sValue, pszBuf, cText );
  151. }
  152. // make the data pointer advance
  153. BaseClass::m_pszText = pszBuf + cText;
  154. }
  155. return pszRetVal;
  156. }
  157. LPCTSTR GetToken () { return m_sToken; }
  158. LPCTSTR GetValue () { return m_sValue; }
  159. private:
  160. CString m_sToken;
  161. CString m_sValue;
  162. };
  163. ///////////////////////////////////////////////////////////////////////////