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.

171 lines
4.3 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1991 - 1996.
  5. //
  6. // File: Smatch.hxx
  7. //
  8. // Classes: CScopeMatch
  9. //
  10. // History: 07 Feb 96 KyleP Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #pragma once
  14. //+---------------------------------------------------------------------------
  15. //
  16. // Class: CScopeMatch
  17. //
  18. // Purpose: A small helper class to test if a given path matches a
  19. // scope or not.
  20. //
  21. // History: 1-22-96 srikants Created
  22. //
  23. // Notes: This is useful for determining if a given path is in
  24. // a scope or not.
  25. //
  26. //----------------------------------------------------------------------------
  27. class CScopeMatch
  28. {
  29. public:
  30. CScopeMatch( ) :
  31. _pwcsRoot(0), _len(0)
  32. {
  33. }
  34. CScopeMatch( WCHAR const * pwcsRoot, ULONG len ) :
  35. _pwcsRoot(0), _len(0)
  36. {
  37. Init( pwcsRoot, len );
  38. }
  39. void Init( WCHAR const * pwcsRoot, ULONG len )
  40. {
  41. Win4Assert( 0 == _len || 0 != _pwcsRoot );
  42. _pwcsRoot = pwcsRoot;
  43. _len = len;
  44. if ( _len > 0 && L'\\' == _pwcsRoot[_len-1] )
  45. {
  46. if( _len > 1 )
  47. _len--;
  48. else
  49. {
  50. _pwcsRoot = 0;
  51. _len = 0;
  52. }
  53. }
  54. }
  55. inline BOOL IsInScope( WCHAR const * pwcsPath, ULONG len ) const;
  56. inline BOOL IsPrefix( WCHAR const * pwcsPath, ULONG len ) const;
  57. inline BOOL IsNullScope() const { return 0 == _pwcsRoot; }
  58. private:
  59. WCHAR const * _pwcsRoot;
  60. ULONG _len;
  61. };
  62. //+---------------------------------------------------------------------------
  63. //
  64. // Member: CScopeMatch::IsInScope
  65. //
  66. // Synopsis: Tests if a given path is in the scope of this object.
  67. // I.e., is the path in the scope a prefix of the input
  68. // path.
  69. //
  70. // Arguments: [wcsPath] - Path to be tested. Must NOT be NULL.
  71. // [len] - Length of this path.
  72. //
  73. // Returns: TRUE if the given path is in the scope. FALSE o/w
  74. //
  75. // History: 1-22-96 srikants Created
  76. //
  77. //----------------------------------------------------------------------------
  78. inline BOOL CScopeMatch::IsInScope( WCHAR const * wcsPath, ULONG len ) const
  79. {
  80. //
  81. // It is possible to have a NULL string ("") with length 0.
  82. //
  83. Win4Assert( 0 == len || 0 != wcsPath );
  84. // if the scope root is NULL, match with everything
  85. if ( 0 == _pwcsRoot || 0 == _len )
  86. return TRUE;
  87. //
  88. // Test directory must be at least as long as scope
  89. //
  90. if ( _len > len )
  91. return( FALSE );
  92. else if ( _len < len )
  93. {
  94. //
  95. // Make sure path has a path separator at the same place the
  96. // scope directory name ends.
  97. //
  98. if ( wcsPath[_len] != L'\\' )
  99. return( FALSE );
  100. }
  101. return RtlEqualMemory( wcsPath, _pwcsRoot, _len * sizeof(WCHAR) );
  102. }
  103. //+---------------------------------------------------------------------------
  104. //
  105. // Member: CScopeMatch::IsPrefix
  106. //
  107. // Synopsis: Tests if a given path is a prefix of the scope.
  108. //
  109. // Arguments: [wcsPath] - Path to be tested. May be NULL. May
  110. // optionally have a \ at the end.
  111. // [len] - Length of this path.
  112. //
  113. // Returns: TRUE if the given path prefixes the scope. FALSE o/w
  114. //
  115. // History: 1-22-96 srikants Created
  116. //
  117. //----------------------------------------------------------------------------
  118. inline BOOL CScopeMatch::IsPrefix( WCHAR const * wcsPath, ULONG len ) const
  119. {
  120. // remove optional trailing '\' on input path
  121. if ( len > 0 && wcsPath[len-1] == L'\\')
  122. len --;
  123. // Null paths prefix everything.
  124. if ( 0 == len || 0 == wcsPath )
  125. return TRUE;
  126. //
  127. // Test directory name must be up to as long as the scope
  128. //
  129. if ( len > _len )
  130. return FALSE;
  131. else if ( len < _len )
  132. {
  133. //
  134. // Make sure the scope name has a path separator at the same place the
  135. // input directory name ends
  136. //
  137. if ( _pwcsRoot[ len ] != L'\\' )
  138. return FALSE;
  139. }
  140. return RtlEqualMemory( wcsPath, _pwcsRoot, len * sizeof(WCHAR) );
  141. }