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.

168 lines
6.2 KiB

  1. /////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // fusion\xmlparser\BufferedStream.hxx
  4. //
  5. /////////////////////////////////////////////////////////////////////////////////
  6. #ifndef _FUSION_XMLPARSER__BUFFEREDSTREAM_H_INCLUDE_
  7. #define _FUSION_XMLPARSER__BUFFEREDSTREAM_H_INCLUDE_
  8. #pragma once
  9. #include "encodingstream.hxx"
  10. // Returned from nextChar when a new buffer is read. This gives the
  11. // caller some idea of download progress without having to count
  12. // characters. Just call nextChar again to continue on as normal.
  13. #define E_DATA_AVAILABLE 0xC00CE600L
  14. #define E_DATA_REALLOCATE 0xC00CE601L
  15. //------------------------------------------------------------------------
  16. // This class adds buffering and auto-growing semantics to an IStream
  17. // so that a variable length chunk of an IStream can be collected
  18. // in memory for processing using Mark() and getToken() methods.
  19. // It also supports collapsing of newlines into 0x20 if you use
  20. // nextChar2 instead of nextChar.
  21. // It also guarentees a line buffer so that a pointer to the
  22. // beginning of the line can be returned in error conditions.
  23. // (for the degenerate case where there are no new lines, it
  24. // returns the last 100 characters).
  25. //
  26. // Alternatively, buffers can be appended instead of
  27. // using an IStream. In this case the BufferedStream returns
  28. // E_PENDING until the last buffer is appended. Use AppendData instead
  29. // of Load(IStream.
  30. class XMLStream;
  31. class BufferedStream
  32. {
  33. public:
  34. BufferedStream(XMLStream *pXMLStream);
  35. ~BufferedStream();
  36. // Method 1: pass in an IStream. The IStream must return unicode
  37. // characters.
  38. HRESULT Load(
  39. /* [unique][in] */ EncodingStream *pStm);
  40. // Method 2: append raw buffers, set lastBuffer to TRUE you are ready to
  41. // return E_ENDOFINPUT. Length is number of chars in buffer. To do unicode
  42. // you must provide a byte order mark (0xFFFE or OxFEFF depending
  43. // on whether it is bigendian or little endian).
  44. HRESULT AppendData(const BYTE* buffer, ULONG length, BOOL lastBuffer);
  45. HRESULT Reset();
  46. // Get next char from buffer , if EOF, set fEOF to be true
  47. HRESULT nextChar(
  48. /* [out] */ WCHAR* ch,
  49. /* [out] */ bool* fEOF);
  50. // Marks the last character read as the start of a buffer
  51. // that grows until Mark is called again. You can mark backwards
  52. // from last character read anywhere up to last marked position
  53. // by passing a non-zero delta. For example, to mark the
  54. // position at the 3rd last character read, call Mark(3);
  55. // xiaoyu : _lCurrent always points to the char to read next
  56. inline void Mark(long back = 0)
  57. {
  58. _lMark = (_lCurrent > back) ? (_lCurrent - back - 1) : 0;
  59. if (_lLinepos != _lCurrent)
  60. {
  61. // only move the marked line position forward, if we haven't
  62. // marked the actual new line characters. This ensures we
  63. // return useful information from getLineBuf.
  64. _lMarkedline = _lLine;
  65. _lMarkedlinepos = _lLinepos;
  66. }
  67. }
  68. // Returns a pointer to a contiguous block of text accumulated
  69. // from the last time Mark() was called up to but not including
  70. // the last character read. (This allows a parser to have a
  71. // lookahead character that is not included in the token).
  72. HRESULT getToken(const WCHAR**p, long* len);
  73. HRESULT switchEncoding(const WCHAR * charset, ULONG len);
  74. // Returns Marked position.
  75. long getLine();
  76. long getLinePos();
  77. WCHAR* getLineBuf(ULONG* len, ULONG* startpos);
  78. long getInputPos(); // absolute position.
  79. long getTokenLength() // convenience function.
  80. {
  81. return (_lCurrent - 1 - _lMark);
  82. }
  83. inline bool isWhiteSpace(WCHAR ch) // no matter what value of "ch"
  84. {
  85. UNUSED(ch);
  86. return (_lLastWhiteSpace == _lCurrent);
  87. }
  88. inline void setWhiteSpace(bool flag = true)
  89. {
  90. _lLastWhiteSpace = flag ? _lCurrent : _lCurrent-1;
  91. }
  92. void init();
  93. // Lock/UnLock is another level on top of Mark/Reset that
  94. // works as follows. If you Lock(), then the buffer keeps everything
  95. // until you UnLock at which time it resets the "Marked" position to
  96. // the Locked() position. This is so that you can scan through
  97. // a series of tokens, but then return all of them in one chunk.
  98. void Lock();
  99. void UnLock();
  100. // Freezing the buffer makes the buffer always grow WITHOUT shifting
  101. // data around in the buffer. This makes it valid to hold on to pointers
  102. // in the buffer until the buffer is unfrozen.
  103. HRESULT Freeze();
  104. HRESULT UnFreeze();
  105. // NTRAID#NTBUG9 - 571792 - jonwis - 2002/04/25 - Dead code removal
  106. #ifdef FUSION_USE_OLD_XML_PARSER_SOURCE
  107. WCHAR* getEncoding();
  108. #endif
  109. // Special XML optimization.
  110. HRESULT scanPCData(
  111. /* [out] */ WCHAR* ch,
  112. /* [out] */ bool* fWhitespace);
  113. private:
  114. WCHAR nextChar();
  115. HRESULT fillBuffer();
  116. HRESULT prepareForInput();
  117. HRESULT doSwitchEncoding();
  118. long getNewStart();
  119. REncodingStream _pStmInput; // input stream
  120. WCHAR* _pchBuffer; // buffer containing chars from input stream.
  121. long _lCurrent; // current read position in buffer
  122. long _lCurrent2; // used when collapsing white space.
  123. long _lSize; // total size of buffer.
  124. long _lMark; // start of current token.
  125. long _lUsed; // amount of buffer currently used.
  126. WCHAR _chLast; // last character returned.
  127. long _lLine; // current line number
  128. long _lLinepos; // position of start of last line.
  129. long _lMarkedline; // current line number of marked position.
  130. long _lMarkedlinepos;
  131. long _lStartAt; // The number of unicode characters before the current buffer
  132. bool _fEof;
  133. bool _fNotified;
  134. bool _fFrozen;
  135. long _lLockCount;
  136. long _lLockedPos;
  137. long _lLockedLine;
  138. long _lLockedLinePos;
  139. long _lLastWhiteSpace;
  140. long _lMidPoint;
  141. Encoding* _pPendingEncoding;
  142. XMLStream *_pXMLStream; // regular pointer pointing back to the XMLStream object
  143. };
  144. #endif // _BUFFEREDSTREAM_HXX