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.

204 lines
5.6 KiB

  1. /////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // fusion\xmlparser\EncodingStream.hxx
  4. //
  5. /////////////////////////////////////////////////////////////////////////////////
  6. #ifndef _FUSION_XMLPARSER__ENCODINGSTREAM_H_INCLUDE_
  7. #define _FUSION_XMLPARSER__ENCODINGSTREAM_H_INCLUDE_
  8. #pragma once
  9. #include "codepage.h"
  10. #include "charencoder.hxx"
  11. #include "core.hxx" //UNUSED() is used
  12. #include <ole2.h>
  13. #include <xmlparser.h>
  14. #include <objbase.h>
  15. typedef _reference<IStream> RStream;
  16. #include <crtdbg.h>
  17. #define Assert(x) _ASSERT(x)
  18. class EncodingStream : public _unknown<IStream, &IID_IStream>
  19. {
  20. protected:
  21. EncodingStream(IStream * stream);
  22. ~EncodingStream();
  23. public:
  24. // create an EncodingStream for input
  25. static IStream * newEncodingStream(IStream * stream);
  26. #ifdef FUSION_USE_OLD_XML_PARSER_SOURCE
  27. // create an EncodingStream for OUTPUT
  28. static IStream * EncodingStream::newEncodingStream(IStream * stream, Encoding * encoding);
  29. #endif
  30. HRESULT STDMETHODCALLTYPE Read(void * pv, ULONG cb, ULONG * pcbRead);
  31. #ifdef FUSION_USE_OLD_XML_PARSER_SOURCE
  32. HRESULT STDMETHODCALLTYPE Write(void const* pv, ULONG cb, ULONG * pcbWritten);
  33. #else
  34. HRESULT STDMETHODCALLTYPE Write(void const* pv, ULONG cb, ULONG * pcbWritten)
  35. {
  36. UNUSED(pv);
  37. UNUSED(cb);
  38. UNUSED(pcbWritten);
  39. return E_NOTIMPL;
  40. }
  41. #endif
  42. virtual HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER * plibNewPosition)
  43. {
  44. UNUSED(dlibMove);
  45. UNUSED(dwOrigin);
  46. UNUSED(plibNewPosition);
  47. return E_NOTIMPL;
  48. }
  49. virtual HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER libNewSize)
  50. {
  51. UNUSED(libNewSize);
  52. return E_NOTIMPL;
  53. }
  54. virtual HRESULT STDMETHODCALLTYPE CopyTo(IStream * pstm, ULARGE_INTEGER cb, ULARGE_INTEGER * pcbRead, ULARGE_INTEGER * pcbWritten)
  55. {
  56. UNUSED(pstm);
  57. UNUSED(cb);
  58. UNUSED(pcbRead);
  59. UNUSED(pcbWritten);
  60. return E_NOTIMPL;
  61. }
  62. virtual HRESULT STDMETHODCALLTYPE Commit(DWORD grfCommitFlags)
  63. {
  64. return stream->Commit(grfCommitFlags);
  65. }
  66. virtual HRESULT STDMETHODCALLTYPE Revert(void)
  67. {
  68. return E_NOTIMPL;
  69. }
  70. virtual HRESULT STDMETHODCALLTYPE LockRegion( ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
  71. {
  72. UNUSED(libOffset);
  73. UNUSED(cb);
  74. UNUSED(dwLockType);
  75. return E_NOTIMPL;
  76. }
  77. virtual HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
  78. {
  79. UNUSED(libOffset);
  80. UNUSED(cb);
  81. UNUSED(dwLockType);
  82. return E_NOTIMPL;
  83. }
  84. virtual HRESULT STDMETHODCALLTYPE Stat(STATSTG * pstatstg, DWORD grfStatFlag)
  85. {
  86. UNUSED(pstatstg);
  87. UNUSED(grfStatFlag);
  88. return E_NOTIMPL;
  89. }
  90. virtual HRESULT STDMETHODCALLTYPE Clone(IStream ** ppstm)
  91. {
  92. UNUSED(ppstm);
  93. return E_NOTIMPL;
  94. }
  95. ///////////////////////////////////////////////////////////
  96. // public methods
  97. //
  98. /**
  99. * Defines the character encoding of the input stream.
  100. * The new character encoding must agree with the encoding determined by the constructer.
  101. * setEncoding is used to clarify between encodings that are not fully determinable
  102. * through the first four bytes in a stream and not to change the encoding.
  103. * This method must be called within BUFFERSIZE reads() after construction.
  104. */
  105. HRESULT switchEncodingAt(Encoding * newEncoding, int newPosition);
  106. #ifdef FUSION_USE_OLD_XML_PARSER_SOURCE
  107. /**
  108. * Gets encoding
  109. */
  110. Encoding * getEncoding();
  111. #endif
  112. // For Read EncodingStreams, this method can be used to push raw data
  113. // which is an alternate approach to providing another IStream.
  114. HRESULT AppendData(const BYTE* buffer, ULONG length, BOOL lastBuffer);
  115. HRESULT BufferData();
  116. void setReadStream(bool flag) { _fReadStream = flag; }
  117. #ifdef FUSION_USE_OLD_XML_PARSER_SOURCE
  118. void SetMimeType(const WCHAR * pwszMimeType, int length);
  119. void SetCharset(const WCHAR * pwszCharset, int length);
  120. #endif
  121. private:
  122. /**
  123. * Buffer Size
  124. */
  125. static const int BUFFERSIZE;
  126. HRESULT autoDetect();
  127. HRESULT prepareForInput(ULONG minlen);
  128. /**
  129. * Character encoding variables : xiaoyu: only encoding is used for reading, other three used for writeXML
  130. */
  131. CODEPAGE codepage; // code page number
  132. Encoding * encoding; // encoding
  133. //bool _fTextXML; // MIME type, true: "text/xml", false: "application/xml"
  134. //bool _fSetCharset; // Whether the charset has been set from outside. e.g, when mime type text/xml or application/xml
  135. // has charset parameter
  136. /**
  137. * Multibyte buffer
  138. */
  139. BYTE *buf; // storage for multibyte bytes
  140. ULONG bufsize;
  141. UINT bnext; // point to next available byte in the rawbuffer
  142. ULONG btotal; // total number of bytes in the rawbuffer
  143. int startAt; // where the buffer starts at in the input stream
  144. /**
  145. * Function pointer to convert from multibyte to unicode
  146. */
  147. WideCharFromMultiByteFunc * pfnWideCharFromMultiByte;
  148. #ifdef FUSION_USE_OLD_XML_PARSER_SOURCE
  149. /**
  150. * Function pointer to convert from unicode to multibytes
  151. */
  152. WideCharToMultiByteFunc * pfnWideCharToMultiByte;
  153. #endif
  154. UINT maxCharSize; // maximum number of bytes of a wide char
  155. // xiaoyu : used for writeXML,
  156. RStream stream;
  157. bool isInput;
  158. bool lastBuffer;
  159. bool _fEOF;
  160. bool _fUTF8BOM;
  161. bool _fReadStream; // lets Read() method call Read() on wrapped stream object.
  162. DWORD _dwMode; // MLANG context.
  163. };
  164. typedef _reference<EncodingStream> REncodingStream;
  165. #endif _FUSION_XMLPARSER__ENCODINGSTREAM_H_INCLUDE_