ANCHOR.CXX
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:6k
源码类别:

Windows编程

开发平台:

Visual C++

  1. //+---------------------------------------------------------------------------
  2. //
  3. //  Copyright (C) 1992 - 1997 Microsoft Corporation.
  4. //
  5. //  File:       anchor.cxx
  6. //
  7. //  Contents:   Parsing algorithm for anchor tag in Html
  8. //
  9. //  Classes:    CAnchorTag
  10. //
  11. //----------------------------------------------------------------------------
  12. #include <pch.cxx>
  13. #pragma hdrstop
  14. #include <anchor.hxx>
  15. #include <htmlguid.hxx>
  16. //+-------------------------------------------------------------------------
  17. //
  18. //  Method:     CAnchorTag::CAnchorTag
  19. //
  20. //  Synopsis:   Constructor
  21. //
  22. //  Arguments:  [htmlIFilter]    -- Html IFilter
  23. //              [serialStream]   -- Input stream
  24. //
  25. //--------------------------------------------------------------------------
  26. CAnchorTag::CAnchorTag( CHtmlIFilter& htmlIFilter,
  27.                         CSerialStream& serialStream )
  28.     : CHtmlElement(htmlIFilter, serialStream),
  29.       _eState(StartAnchor),
  30.       _pwcHrefBuf(0),
  31.       _uLenHrefBuf(0),
  32.       _cHrefChars(0),
  33.       _cHrefCharsFiltered(0)
  34. {
  35. }
  36. //+-------------------------------------------------------------------------
  37. //
  38. //  Method:     CAnchorTag::~CAnchorTag
  39. //
  40. //  Synopsis:   Destructor
  41. //
  42. //  Arguments:  [htmlIFilter]    -- Html IFilter
  43. //              [serialStream]   -- Input stream
  44. //
  45. //--------------------------------------------------------------------------
  46. CAnchorTag::~CAnchorTag()
  47. {
  48.     delete[] _pwcHrefBuf;
  49. }
  50. //+-------------------------------------------------------------------------
  51. //
  52. //  Method:     CAnchorTag::GetChunk
  53. //
  54. //  Synopsis:   Gets the next chunk and returns chunk information in pStat
  55. //
  56. //  Arguments:  [pStat] -- chunk information returned here
  57. //
  58. //--------------------------------------------------------------------------
  59. SCODE CAnchorTag::GetChunk( STAT_CHUNK * pStat )
  60. {
  61.     //
  62.     // Toggle state
  63.     //
  64.     if ( _eState == StartAnchor )
  65.         _eState = EndAnchor;
  66.     else
  67.         _eState = StartAnchor;
  68.     SCODE sc = SwitchToNextHtmlElement( pStat );
  69.     return sc;
  70. }
  71. //+-------------------------------------------------------------------------
  72. //
  73. //  Method:     CAnchorTag::GetText
  74. //
  75. //  Synopsis:   Retrieves text from current chunk
  76. //
  77. //  Arguments:  [pcwcOutput] -- count of UniCode characters in buffer
  78. //              [awcBuffer]  -- buffer for text
  79. //
  80. //--------------------------------------------------------------------------
  81. SCODE CAnchorTag::GetText( ULONG *pcwcOutput, WCHAR *awcBuffer )
  82. {
  83.     if ( _eState == StartAnchor )
  84.     {
  85.         *pcwcOutput = 0;
  86.         return FILTER_E_NO_MORE_TEXT;
  87.     }
  88.     ULONG cCharsRemaining = _cHrefChars - _cHrefCharsFiltered;
  89.     if ( cCharsRemaining == 0 )
  90.     {
  91.         *pcwcOutput = 0;
  92.         return FILTER_E_NO_MORE_TEXT;
  93.     }
  94.     if ( *pcwcOutput < cCharsRemaining )
  95.     {
  96.         RtlCopyMemory( awcBuffer,
  97.                        _pwcHrefBuf + _cHrefCharsFiltered,
  98.                        *pcwcOutput * sizeof(WCHAR) );
  99.         _cHrefCharsFiltered += *pcwcOutput;
  100.         return S_OK;
  101.     }
  102.     else
  103.     {
  104.         RtlCopyMemory( awcBuffer,
  105.                        _pwcHrefBuf + _cHrefCharsFiltered,
  106.                        cCharsRemaining * sizeof(WCHAR) );
  107.         _cHrefCharsFiltered += cCharsRemaining;
  108.         *pcwcOutput = cCharsRemaining;
  109.         return FILTER_S_LAST_TEXT;
  110.     }
  111. }
  112. //+-------------------------------------------------------------------------
  113. //
  114. //  Method:     CAnchorTag::InitStatChunk
  115. //
  116. //  Synopsis:   Initializes the STAT_CHUNK
  117. //
  118. //  Arguments:  [pStat] -- STAT_CHUNK to initialize
  119. //
  120. //--------------------------------------------------------------------------
  121. void CAnchorTag::InitStatChunk( STAT_CHUNK *pStat )
  122. {
  123.     pStat->idChunk = _htmlIFilter.GetNextChunkId();
  124.     pStat->flags = CHUNK_TEXT;
  125.     pStat->locale = _htmlIFilter.GetLocale();
  126.     pStat->attribute.guidPropSet = CLSID_HtmlInformation;
  127.     pStat->attribute.psProperty.ulKind = PRSPEC_PROPID;
  128.     pStat->attribute.psProperty.propid = PID_HREF;
  129.     pStat->breakType = CHUNK_EOS;
  130.     if ( _eState == StartAnchor)
  131.     {
  132.         //
  133.         // Start tag, hence read href field into local buffer
  134.         //
  135.         _scanner.ReadTagIntoBuffer();
  136.         WCHAR *pwcHrefBuf;
  137.         unsigned cHrefChars;
  138.         _scanner.ScanTagBuffer( L"href="", pwcHrefBuf, cHrefChars );
  139.         //
  140.         // Need to grow internal buffer ?
  141.         //
  142.         if ( cHrefChars > _uLenHrefBuf )
  143.         {
  144.             delete[] _pwcHrefBuf;
  145.             _pwcHrefBuf = 0;
  146.             _uLenHrefBuf = 0;
  147.             _pwcHrefBuf = newk(mtNewX, NULL) WCHAR[cHrefChars];
  148.             _uLenHrefBuf = cHrefChars;
  149.         }
  150.         RtlCopyMemory( _pwcHrefBuf, pwcHrefBuf, cHrefChars*sizeof(WCHAR) );
  151.         _cHrefChars = cHrefChars;
  152.         _cHrefCharsFiltered = 0;
  153.         pStat->idChunkSource = 0;
  154.         pStat->cwcStartSource = 0;
  155.         pStat->cwcLenSource = 0;
  156.     }
  157.     else
  158.     {
  159.         Win4Assert( _eState == EndAnchor );
  160.         _scanner.EatTag();
  161.         //
  162.         // Set up the filter region to be the one between the start and end
  163.         // anchor tags, i.e. the region belonging to the current Html element,
  164.         // because we haven't changed state yet.
  165.         //
  166.         _htmlIFilter.GetCurHtmlElement()->InitFilterRegion( pStat->idChunkSource,
  167.                                                             pStat->cwcStartSource,
  168.                                                             pStat->cwcLenSource );
  169.     }
  170. }