1 /* 2 * The Progressive Graphics File; http://www.libpgf.org 3 * 4 * $Date: 2007-06-11 10:56:17 +0200 (Mo, 11 Jun 2007) $ 5 * $Revision: 299 $ 6 * 7 * This file Copyright (C) 2006 xeraina GmbH, Switzerland 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE 11 * as published by the Free Software Foundation; either version 2.1 12 * of the License, or (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 22 */ 23 24 ////////////////////////////////////////////////////////////////////// 25 /// @file PGFstream.h 26 /// @brief PGF stream class 27 /// @author C. Stamm 28 29 #ifndef PGF_STREAM_H 30 #define PGF_STREAM_H 31 32 #include "PGFtypes.h" 33 #include <new> 34 35 ///////////////////////////////////////////////////////////////////// 36 /// Abstract stream base class. 37 /// @author C. Stamm 38 /// @brief Abstract stream base class 39 class CPGFStream { 40 public: 41 ////////////////////////////////////////////////////////////////////// 42 /// Standard constructor. CPGFStream()43 CPGFStream() {} 44 45 ////////////////////////////////////////////////////////////////////// 46 /// Standard destructor. ~CPGFStream()47 virtual ~CPGFStream() {} 48 49 ////////////////////////////////////////////////////////////////////// 50 /// Write some bytes out of a buffer into this stream. 51 /// @param count A pointer to a value containing the number of bytes should be written. After this call it contains the number of written bytes. 52 /// @param buffer A memory buffer 53 virtual void Write(int *count, void *buffer)=0; 54 55 ////////////////////////////////////////////////////////////////////// 56 /// Read some bytes from this stream and stores them into a buffer. 57 /// @param count A pointer to a value containing the number of bytes should be read. After this call it contains the number of read bytes. 58 /// @param buffer A memory buffer 59 virtual void Read(int *count, void *buffer)=0; 60 61 ////////////////////////////////////////////////////////////////////// 62 /// Set stream position either absolute or relative. 63 /// @param posMode A position mode (FSFromStart, FSFromCurrent, FSFromEnd) 64 /// @param posOff A new stream position (absolute positioning) or a position offset (relative positioning) 65 virtual void SetPos(short posMode, INT64 posOff)=0; 66 67 ////////////////////////////////////////////////////////////////////// 68 /// Get current stream position. 69 /// @return Current stream position 70 virtual UINT64 GetPos() const=0; 71 72 ////////////////////////////////////////////////////////////////////// 73 /// Check stream validity. 74 /// @return True if stream and current position is valid 75 virtual bool IsValid() const=0; 76 }; 77 78 ///////////////////////////////////////////////////////////////////// 79 /// A PGF stream subclass for external storage files. 80 /// @author C. Stamm 81 /// @brief File stream class 82 class CPGFFileStream : public CPGFStream { 83 protected: 84 HANDLE m_hFile; ///< file handle 85 86 public: CPGFFileStream()87 CPGFFileStream() : m_hFile(0) {} 88 /// Constructor 89 /// @param hFile File handle CPGFFileStream(HANDLE hFile)90 CPGFFileStream(HANDLE hFile) : m_hFile(hFile) {} 91 /// @return File handle GetHandle()92 HANDLE GetHandle() { return m_hFile; } 93 ~CPGFFileStream()94 virtual ~CPGFFileStream() { m_hFile = 0; } 95 virtual void Write(int *count, void *buffer); // throws IOException 96 virtual void Read(int *count, void *buffer); // throws IOException 97 virtual void SetPos(short posMode, INT64 posOff); // throws IOException 98 virtual UINT64 GetPos() const; // throws IOException IsValid()99 virtual bool IsValid() const { return m_hFile != 0; } 100 }; 101 102 ///////////////////////////////////////////////////////////////////// 103 /// A PGF stream subclass for internal memory. 104 /// @author C. Stamm 105 /// @brief Memory stream class 106 class CPGFMemoryStream : public CPGFStream { 107 protected: 108 UINT8 *m_buffer, *m_pos;///< buffer start address and current buffer address 109 UINT8 *m_eos; ///< end of stream (first address beyond written area) 110 size_t m_size; ///< buffer size 111 bool m_allocated; ///< indicates a new allocated buffer 112 113 public: 114 /// Constructor 115 /// @param size Size of new allocated memory buffer 116 CPGFMemoryStream(size_t size); 117 118 /// Constructor. Use already allocated memory of given size 119 /// @param pBuffer Memory location 120 /// @param size Memory size 121 CPGFMemoryStream(UINT8 *pBuffer, size_t size); 122 123 /// Use already allocated memory of given size 124 /// @param pBuffer Memory location 125 /// @param size Memory size 126 void Reinitialize(UINT8 *pBuffer, size_t size); 127 ~CPGFMemoryStream()128 virtual ~CPGFMemoryStream() { 129 m_pos = 0; 130 if (m_allocated) { 131 // the memory buffer has been allocated inside of CPMFmemoryStream constructor 132 delete[] m_buffer; m_buffer = 0; 133 } 134 } 135 136 virtual void Write(int *count, void *buffer); // throws IOException 137 virtual void Read(int *count, void *buffer); 138 virtual void SetPos(short posMode, INT64 posOff); // throws IOException GetPos()139 virtual UINT64 GetPos() const { ASSERT(IsValid()); return m_pos - m_buffer; } IsValid()140 virtual bool IsValid() const { return m_buffer != 0; } 141 142 /// @return Memory size GetSize()143 size_t GetSize() const { return m_size; } 144 /// @return Memory buffer GetBuffer()145 const UINT8* GetBuffer() const { return m_buffer; } 146 /// @return Memory buffer GetBuffer()147 UINT8* GetBuffer() { return m_buffer; } 148 /// @return relative position of end of stream (= stream length) GetEOS()149 UINT64 GetEOS() const { ASSERT(IsValid()); return m_eos - m_buffer; } 150 /// @param length Stream length (= relative position of end of stream) SetEOS(UINT64 length)151 void SetEOS(UINT64 length) { ASSERT(IsValid()); m_eos = m_buffer + length; } 152 }; 153 154 ///////////////////////////////////////////////////////////////////// 155 /// A PGF stream subclass for internal memory files. Usable only with MFC. 156 /// @author C. Stamm 157 /// @brief Cached memory file stream class 158 #ifdef _MFC_VER 159 class CPGFMemFileStream : public CPGFStream { 160 protected: 161 CMemFile *m_memFile; ///< MFC memory file 162 public: CPGFMemFileStream(CMemFile * memFile)163 CPGFMemFileStream(CMemFile *memFile) : m_memFile(memFile) {} IsValid()164 virtual bool IsValid() const { return m_memFile != nullptr; } ~CPGFMemFileStream()165 virtual ~CPGFMemFileStream() {} 166 virtual void Write(int *count, void *buffer); // throws IOException 167 virtual void Read(int *count, void *buffer); // throws IOException 168 virtual void SetPos(short posMode, INT64 posOff); // throws IOException 169 virtual UINT64 GetPos() const; // throws IOException 170 }; 171 #endif 172 173 ///////////////////////////////////////////////////////////////////// 174 /// A PGF stream subclass for IStream. Usable only with COM. 175 /// @author C. Stamm 176 /// @brief COM IStream class 177 #if defined(WIN32) || defined(WINCE) 178 class CPGFIStream : public CPGFStream { 179 protected: 180 IStream *m_stream; ///< COM+ IStream 181 public: CPGFIStream(IStream * stream)182 CPGFIStream(IStream *stream) : m_stream(stream) { m_stream->AddRef(); } IsValid()183 virtual bool IsValid() const { return m_stream != 0; } ~CPGFIStream()184 virtual ~CPGFIStream() { m_stream->Release(); } 185 virtual void Write(int *count, void *buffer); // throws IOException 186 virtual void Read(int *count, void *buffer); // throws IOException 187 virtual void SetPos(short posMode, INT64 posOff); // throws IOException 188 virtual UINT64 GetPos() const; // throws IOException GetIStream()189 IStream* GetIStream() const { return m_stream; } 190 }; 191 #endif 192 193 #endif // PGF_STREAM_H 194