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.

242 lines
5.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2001
  5. //
  6. // File: gen.cxx
  7. //
  8. // Contents: Generic filter code
  9. //
  10. // History: 1-May-2001 kumarp created
  11. //
  12. //--------------------------------------------------------------------------
  13. #include <pch.cxx>
  14. #pragma hdrstop
  15. #include "gen.hxx"
  16. #include "genifilt.hxx"
  17. #include "genflt.hxx"
  18. #include "rxutil.h"
  19. GenScanner::GenScanner ()
  20. : _pStream(0),
  21. _cLines( 0 )
  22. {
  23. }
  24. void GenScanner::Init ( CFilterTextStream * pStream )
  25. {
  26. _pStream = pStream;
  27. _cLines = 0;
  28. }
  29. int GenScanner::GetLine( PWSTR pszBuf, UINT BufSize, FILTERREGION* pRegion )
  30. {
  31. int c, i=0;
  32. _pStream->GetRegion ( *pRegion, -1, 0 );
  33. while (((c = _pStream->GetChar()) != -1) &&
  34. ( c != L'\n' ) &&
  35. ( i < (int) BufSize ))
  36. {
  37. pszBuf[i++] = (WCHAR) c;
  38. }
  39. pszBuf[i] = 0;
  40. if ( c == -1 )
  41. {
  42. return 0;
  43. }
  44. else if ( ( c == L'\n' ) || ( i > 0 ) )
  45. {
  46. _cLines++;
  47. }
  48. return 1;
  49. }
  50. GenParser::GenParser ()
  51. : _iVal(0)
  52. {
  53. _strName[0] = L'\0';
  54. _attribute.ulKind = PRSPEC_LPWSTR;
  55. _attribute.lpwstr = PROP_FUNC;
  56. _psVal[Function].ulKind = PRSPEC_LPWSTR;
  57. _psVal[Function].lpwstr = PROP_FUNC;
  58. _psVal[Lines].ulKind = PRSPEC_LPWSTR;
  59. _psVal[Lines].lpwstr = PROP_LINES;
  60. _aVal[Function] = 0;
  61. _aVal[Lines] = 0;
  62. }
  63. GenParser::~GenParser()
  64. {
  65. delete _aVal[Function];
  66. delete _aVal[Lines];
  67. }
  68. void GenParser::Init ( CFilterTextStream * pStream )
  69. {
  70. DWORD dwError = NO_ERROR;
  71. _scanner.Init(pStream);
  72. dwError = RxInit();
  73. if ( dwError != NO_ERROR )
  74. {
  75. DbgPrint( "RxInit failed: %x\n", dwError );
  76. THROW( CException( dwError ) );
  77. }
  78. }
  79. void ConvertSpecialCharsToUnderscore( IN PWSTR pszName )
  80. {
  81. WCHAR ch;
  82. static PCWSTR c_szSpecialChars = L"-+*/:,<>|";
  83. while ( ch = *pszName )
  84. {
  85. if ( wcschr( c_szSpecialChars, ch ) )
  86. {
  87. *pszName = L'_';
  88. }
  89. pszName++;
  90. }
  91. }
  92. BOOL GenParser::Parse()
  93. {
  94. _cwcCopiedName = 0;
  95. WCHAR idbuf[MAX_LINE_SIZE];
  96. UINT MatchStart, MatchLength;
  97. #if DBG
  98. // DbgPrint("GenParser::Parse\n");
  99. #endif
  100. while ( _scanner.GetLine( _buf, MAX_LINE_SIZE, &_regionName ) > 0 )
  101. {
  102. if ( ParseLine( _buf, &MatchStart, &MatchLength ))
  103. {
  104. ASSERT( MatchLength < MAXIDENTIFIER );
  105. wcsncpy ( _strName, _buf + MatchStart, MatchLength );
  106. _strName[MatchLength] = 0;
  107. _regionName.cwcStart += MatchStart + 1;
  108. _regionName.cwcExtent = MatchLength;
  109. ConvertSpecialCharsToUnderscore( _strName );
  110. #if DBG
  111. DbgPrint("GenParser::Parse: tag: %ws @ %d, [%d,%d,%d]\n", _strName, _scanner.Lines(), _regionName.idChunk, _regionName.cwcStart, _regionName.cwcExtent );
  112. #endif
  113. DefineTag();
  114. return TRUE;
  115. }
  116. }
  117. if ( _aVal[Lines] == 0 )
  118. {
  119. _aVal[Lines] = new CStorageVariant;
  120. if ( 0 == _aVal[Lines] )
  121. THROW( CException( E_OUTOFMEMORY ) );
  122. }
  123. _aVal[Lines]->SetUI4( _scanner.Lines() );
  124. return FALSE; // we only end up here on EOF
  125. }
  126. void GenParser::DefineTag()
  127. {
  128. _tokenType = ttFunction;
  129. _attribute.lpwstr = PROP_FUNC;
  130. if ( _aVal[Function] == 0 )
  131. {
  132. _aVal[Function] = new CStorageVariant;
  133. if ( 0 == _aVal[Function] )
  134. THROW( CException( E_OUTOFMEMORY ) );
  135. }
  136. _aVal[Function]->SetLPWSTR( _strName, _aVal[Function]->Count() );
  137. #if DBG
  138. // DbgPrint("DefineTag: %ws @ %d\n", _strName, _scanner.Lines());
  139. #endif
  140. }
  141. void GenParser::GetRegion ( FILTERREGION& region )
  142. {
  143. switch (_tokenType)
  144. {
  145. case ttFunction:
  146. region = _regionName;
  147. break;
  148. }
  149. }
  150. BOOL GenParser::GetTokens ( ULONG * pcwcBuffer, WCHAR * awcBuffer )
  151. {
  152. ULONG cwc = *pcwcBuffer;
  153. *pcwcBuffer = 0;
  154. if (_strName[0] == L'\0')
  155. {
  156. awcBuffer[*pcwcBuffer] = L'\0';
  157. return TRUE;
  158. }
  159. cwc -= *pcwcBuffer;
  160. WCHAR * awc = awcBuffer + *pcwcBuffer;
  161. WCHAR * strName = _strName + _cwcCopiedName;
  162. ULONG cwcName = wcslen( strName );
  163. if ( cwcName > cwc )
  164. {
  165. wcsncpy( awc, strName, cwc );
  166. _cwcCopiedName += cwc;
  167. return FALSE;
  168. }
  169. wcscpy( awc, strName );
  170. *pcwcBuffer += cwcName;
  171. _cwcCopiedName += cwcName;
  172. return TRUE;
  173. }
  174. BOOL GenParser::GetValueAttribute( PROPSPEC & ps )
  175. {
  176. for ( ; _iVal <= Lines && 0 == _aVal[_iVal]; _iVal++ )
  177. continue;
  178. if ( _iVal > Lines )
  179. return FALSE;
  180. else
  181. {
  182. ps = _psVal[_iVal];
  183. return TRUE;
  184. }
  185. }
  186. PROPVARIANT * GenParser::GetValue()
  187. {
  188. if ( _iVal > Lines )
  189. return 0;
  190. CStorageVariant * pTemp = _aVal[_iVal];
  191. _aVal[_iVal] = 0;
  192. _iVal++;
  193. return (PROPVARIANT *)(void *)pTemp;
  194. }