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.

292 lines
7.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1992, Microsoft Corporation.
  4. //
  5. // File: STREAMS.HXX
  6. //
  7. // Contents:
  8. //
  9. // Classes: CStream, CStreamA, CStreamW, CStreamASCIIStr, CStreamUnicodeStr
  10. //
  11. // History: 29-Jul-92 MikeHew Created
  12. // 23-Sep-92 AmyA Added CStreamUnicodeStr
  13. // Rewrote CStreamASCIIStr
  14. // 02-Nov-92 AmyA Added CSubStream
  15. // 20-Nov-92 AmyA Rewrote all streams for input/
  16. // output buffering
  17. //
  18. // Notes:
  19. //
  20. //----------------------------------------------------------------------------
  21. #pragma once
  22. #ifdef DISPLAY_INCLUDES
  23. #pragma message( "#include <" __FILE__ ">..." )
  24. #endif
  25. #include <sys\types.h> // for BOOL typedef
  26. #include <memory.h> // for memcpy
  27. //+---------------------------------------------------------------------------
  28. //
  29. // Class: CStream
  30. //
  31. // Purpose: General stream interface
  32. //
  33. // History: 29-Jul-92 MikeHew Created
  34. // 18-Nov-92 AmyA Removed GetChar(), now in CStreamA
  35. // and CStreamW
  36. //
  37. // Notes:
  38. //
  39. //----------------------------------------------------------------------------
  40. class CStream
  41. {
  42. public:
  43. CStream () : _eof(FALSE) {}
  44. virtual ~CStream() {};
  45. enum SEEK
  46. {
  47. SET = 0, // Seek from beginning of stream
  48. CUR = 1, // Seek from current position
  49. END = 2 // Seek from end
  50. };
  51. //
  52. // Status functions
  53. //
  54. virtual BOOL Ok() = 0;
  55. inline BOOL Eof() { return _eof; }
  56. //
  57. // Input from stream functions
  58. //
  59. virtual unsigned APINOT Read( void *dest, unsigned size ) = 0;
  60. //
  61. // Output to stream functions
  62. //
  63. virtual unsigned APINOT Write( const void *source, unsigned size ) = 0;
  64. //
  65. // Misc
  66. //
  67. virtual int APINOT Seek( LONG offset, SEEK origin ) = 0;
  68. protected:
  69. BOOL _eof;
  70. };
  71. #if !defined( EOF )
  72. #define EOF (-1)
  73. #endif // EOF
  74. //+---------------------------------------------------------------------------
  75. //
  76. // Class: CStreamA
  77. //
  78. // Purpose: General stream interface for ASCII streams
  79. //
  80. // History: 18-Nov-92 AmyA Created
  81. //
  82. //----------------------------------------------------------------------------
  83. class CStreamA: public CStream
  84. {
  85. friend class CSubStreamA;
  86. public:
  87. CStreamA () : _pBuf ( 0 ), _pCur ( 0 ), _pEnd ( 0 ) {}
  88. virtual ~CStreamA() {};
  89. //
  90. // Status functions
  91. //
  92. virtual BOOL Ok() = 0;
  93. //
  94. // Input from stream functions
  95. //
  96. inline int GetChar ();
  97. virtual unsigned APINOT Read( void *dest, unsigned size ) = 0;
  98. //
  99. // Output to stream functions
  100. //
  101. virtual unsigned APINOT Write( const void *source, unsigned size ) = 0;
  102. //
  103. // Misc
  104. //
  105. virtual int APINOT Seek( LONG offset, CStream::SEEK origin ) = 0;
  106. protected:
  107. virtual BOOL FillBuf() = 0;
  108. EXPORTDEF int APINOT GetBuf();
  109. BYTE* _pBuf;
  110. BYTE* _pCur;
  111. BYTE* _pEnd;
  112. };
  113. inline int CStreamA::GetChar()
  114. {
  115. if ( _pCur != _pEnd )
  116. {
  117. return *_pCur++;
  118. }
  119. return GetBuf();
  120. }
  121. //+---------------------------------------------------------------------------
  122. //
  123. // Class: CStreamW
  124. //
  125. // Purpose: General stream interface for UniCode streams
  126. //
  127. // History: 18-Nov-92 AmyA Created
  128. //
  129. //----------------------------------------------------------------------------
  130. class CStreamW: public CStream
  131. {
  132. public:
  133. CStreamW () : _pBuf ( 0 ), _pCur ( 0 ), _pEnd ( 0 ) {}
  134. virtual ~CStreamW() {};
  135. //
  136. // Status functions
  137. //
  138. virtual BOOL Ok() = 0;
  139. //
  140. // Input from stream functions
  141. //
  142. inline int GetChar ();
  143. virtual unsigned APINOT Read( void *dest, unsigned size ) = 0;
  144. //
  145. // Output to stream functions
  146. //
  147. virtual unsigned APINOT Write( const void *source, unsigned size ) = 0;
  148. //
  149. // Misc
  150. //
  151. virtual int APINOT Seek( LONG offset, CStream::SEEK origin ) = 0;
  152. protected:
  153. virtual BOOL FillBuf() = 0;
  154. EXPORTDEF int APINOT GetBuf();
  155. WCHAR* _pBuf;
  156. WCHAR* _pCur;
  157. WCHAR* _pEnd;
  158. };
  159. inline int CStreamW::GetChar()
  160. {
  161. if ( _pCur != _pEnd )
  162. {
  163. return *_pCur++;
  164. }
  165. return GetBuf();
  166. }
  167. //+---------------------------------------------------------------------------
  168. //
  169. // Class: CStreamASCIIStr
  170. //
  171. // Purpose: Used for accessing an ASCII string as a stream
  172. //
  173. // Arguments: [pBuf] - the ASCII string
  174. // [cb] - the length of the string
  175. //
  176. // History: 04-Aug-92 MikeHew Modified for new streams
  177. // 22-Sep-92 AmyA Rewrote for non-NULL term. strings
  178. //
  179. // Notes: Leftover from old stream classes
  180. //
  181. //----------------------------------------------------------------------------
  182. class CStreamASCIIStr: public CStreamA
  183. {
  184. public:
  185. inline CStreamASCIIStr ( const char *pBuf, unsigned cb );
  186. inline ~CStreamASCIIStr () {}
  187. BOOL Ok() { return _pBuf != 0; }
  188. EXPORTDEF unsigned APINOT Read( void *dest, unsigned size );
  189. unsigned APINOT Write( const void *source, unsigned size ) { return 0; }
  190. int APINOT Seek( LONG, CStream::SEEK ) { return FALSE; }
  191. private:
  192. inline BOOL FillBuf() { _eof = TRUE; return FALSE; }
  193. };
  194. inline CStreamASCIIStr::CStreamASCIIStr ( const char *pBuf, unsigned cb )
  195. {
  196. // _pBuf is cast to non-const because although we are not writing to it
  197. // it is defined in the base class and other classes do write to it.
  198. _pBuf = (BYTE*)pBuf;
  199. _pCur = _pBuf;
  200. _pEnd = _pBuf + cb;
  201. }
  202. //+---------------------------------------------------------------------------
  203. //
  204. // Class: CStreamUnicodeStr
  205. //
  206. // Purpose: Used for accessing a Unicode string as a stream
  207. //
  208. // Arguments: [pBuf] - the Unicode string
  209. // [cwc] - the length of the string
  210. //
  211. // History: 23-Sep-92 AmyA Created.
  212. //
  213. //----------------------------------------------------------------------------
  214. class CStreamUnicodeStr: public CStreamW
  215. {
  216. public:
  217. inline CStreamUnicodeStr ( const WCHAR *pBuf, unsigned cwc );
  218. inline ~CStreamUnicodeStr () {}
  219. BOOL Ok() { return _pBuf != 0; }
  220. inline unsigned APINOT Read( void *dest, unsigned size ) { return 0; }
  221. unsigned APINOT Write( const void *source, unsigned size ) { return 0; }
  222. int APINOT Seek( LONG, CStream::SEEK ) { return FALSE; }
  223. private:
  224. inline BOOL FillBuf() { _eof = TRUE; return FALSE; }
  225. };
  226. inline CStreamUnicodeStr::CStreamUnicodeStr ( const WCHAR *pBuf, unsigned cwc )
  227. {
  228. // _pBuf is cast to non-const because although we are not writing to it
  229. // it is defined in the base class and other classes do write to it.
  230. _pBuf = (WCHAR*)pBuf;
  231. _pCur = _pBuf;
  232. _pEnd = _pBuf + cwc;
  233. }