File indexing completed on 2025-01-05 03:56:38

0001 /*
0002  * The Progressive Graphics File; http://www.libpgf.org
0003  *
0004  * $Date: 2007-06-11 10:56:17 +0200 (Mo, 11 Jun 2007) $
0005  * $Revision: 299 $
0006  *
0007  * This file Copyright (C) 2006 xeraina GmbH, Switzerland
0008  *
0009  * This program is free software; you can redistribute it and/or
0010  * modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
0011  * as published by the Free Software Foundation; either version 2.1
0012  * of the License, or (at your option) any later version.
0013  *
0014  * This program is distributed in the hope that it will be useful,
0015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  * GNU General Public License for more details.
0018  *
0019  * You should have received a copy of the GNU General Public License
0020  * along with this program; if not, write to the Free Software
0021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
0022  */
0023 
0024 //////////////////////////////////////////////////////////////////////
0025 /// @file PGFstream.h
0026 /// @brief PGF stream class
0027 /// @author C. Stamm
0028 
0029 #ifndef PGF_STREAM_H
0030 #define PGF_STREAM_H
0031 
0032 #include "PGFtypes.h"
0033 #include <new>
0034 
0035 #include "digikam_export.h"
0036 
0037 /////////////////////////////////////////////////////////////////////
0038 /// Abstract stream base class.
0039 /// @author C. Stamm
0040 /// @brief Abstract stream base class
0041 class DIGIKAM_EXPORT CPGFStream {
0042 public:
0043     //////////////////////////////////////////////////////////////////////
0044     /// Standard constructor.
0045     CPGFStream() {}
0046 
0047     //////////////////////////////////////////////////////////////////////
0048     /// Standard destructor.
0049     virtual ~CPGFStream() {}
0050 
0051     //////////////////////////////////////////////////////////////////////
0052     /// Write some bytes out of a buffer into this stream.
0053     /// @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.
0054     /// @param buffer A memory buffer
0055     virtual void Write(int *count, void *buffer)=0;
0056 
0057     //////////////////////////////////////////////////////////////////////
0058     /// Read some bytes from this stream and stores them into a buffer.
0059     /// @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.
0060     /// @param buffer A memory buffer
0061     virtual void Read(int *count, void *buffer)=0;
0062 
0063     //////////////////////////////////////////////////////////////////////
0064     /// Set stream position either absolute or relative.
0065     /// @param posMode A position mode (FSFromStart, FSFromCurrent, FSFromEnd)
0066     /// @param posOff A new stream position (absolute positioning) or a position offset (relative positioning)
0067     virtual void SetPos(short posMode, INT64 posOff)=0;
0068 
0069     //////////////////////////////////////////////////////////////////////
0070     /// Get current stream position.
0071     /// @return Current stream position
0072     virtual UINT64 GetPos() const=0;
0073 
0074     //////////////////////////////////////////////////////////////////////
0075     /// Check stream validity.
0076     /// @return True if stream and current position is valid
0077     virtual bool IsValid() const=0;
0078 };
0079 
0080 /////////////////////////////////////////////////////////////////////
0081 /// A PGF stream subclass for external storage files.
0082 /// @author C. Stamm
0083 /// @brief File stream class
0084 class DIGIKAM_EXPORT CPGFFileStream : public CPGFStream {
0085 protected:
0086     HANDLE m_hFile; ///< file handle
0087 
0088 public:
0089     CPGFFileStream() : m_hFile(0) {}
0090     /// Constructor
0091     /// @param hFile File handle
0092     CPGFFileStream(HANDLE hFile) : m_hFile(hFile) {}
0093     /// @return File handle
0094     HANDLE GetHandle() { return m_hFile; }
0095 
0096     virtual ~CPGFFileStream() { m_hFile = 0; }
0097     virtual void Write(int *count, void *buffer); // throws IOException
0098     virtual void Read(int *count, void *buffer); // throws IOException
0099     virtual void SetPos(short posMode, INT64 posOff); // throws IOException
0100     virtual UINT64 GetPos() const; // throws IOException
0101     virtual bool   IsValid() const  { return m_hFile != 0; }
0102 };
0103 
0104 /////////////////////////////////////////////////////////////////////
0105 /// A PGF stream subclass for internal memory.
0106 /// @author C. Stamm
0107 /// @brief Memory stream class
0108 class DIGIKAM_EXPORT CPGFMemoryStream : public CPGFStream {
0109 protected:
0110     UINT8 *m_buffer, *m_pos;///< buffer start address and current buffer address
0111     UINT8 *m_eos;           ///< end of stream (first address beyond written area)
0112     size_t m_size;          ///< buffer size
0113     bool   m_allocated;     ///< indicates a new allocated buffer
0114 
0115 public:
0116     /// Constructor
0117     /// @param size Size of new allocated memory buffer
0118     CPGFMemoryStream(size_t size);
0119 
0120     /// Constructor. Use already allocated memory of given size
0121     /// @param pBuffer Memory location
0122     /// @param size Memory size
0123     CPGFMemoryStream(UINT8 *pBuffer, size_t size);
0124 
0125     /// Use already allocated memory of given size
0126     /// @param pBuffer Memory location
0127     /// @param size Memory size
0128     void Reinitialize(UINT8 *pBuffer, size_t size);
0129 
0130     virtual ~CPGFMemoryStream() {
0131         m_pos = 0;
0132         if (m_allocated) {
0133             // the memory buffer has been allocated inside of CPMFmemoryStream constructor
0134             delete[] m_buffer; m_buffer = 0;
0135         }
0136     }
0137 
0138     virtual void Write(int *count, void *buffer); // throws IOException
0139     virtual void Read(int *count, void *buffer);
0140     virtual void SetPos(short posMode, INT64 posOff); // throws IOException
0141     virtual UINT64 GetPos() const { ASSERT(IsValid()); return m_pos - m_buffer; }
0142     virtual bool   IsValid() const  { return m_buffer != 0; }
0143 
0144     /// @return Memory size
0145     size_t GetSize() const          { return m_size; }
0146     /// @return Memory buffer
0147     const UINT8* GetBuffer() const  { return m_buffer; }
0148     /// @return Memory buffer
0149     UINT8* GetBuffer()              { return m_buffer; }
0150     /// @return relative position of end of stream (= stream length)
0151     UINT64 GetEOS() const           { ASSERT(IsValid()); return m_eos - m_buffer; }
0152     /// @param length Stream length (= relative position of end of stream)
0153     void SetEOS(UINT64 length)      { ASSERT(IsValid()); m_eos = m_buffer + length; }
0154 };
0155 
0156 /////////////////////////////////////////////////////////////////////
0157 /// A PGF stream subclass for internal memory files. Usable only with MFC.
0158 /// @author C. Stamm
0159 /// @brief Cached memory file stream class
0160 #ifdef _MFC_VER
0161 class DIGIKAM_EXPORT CPGFMemFileStream : public CPGFStream {
0162 protected:
0163     CMemFile *m_memFile;    ///< MFC memory file
0164 public:
0165     CPGFMemFileStream(CMemFile *memFile) : m_memFile(memFile) {}
0166     virtual bool    IsValid() const { return m_memFile != nullptr; }
0167     virtual ~CPGFMemFileStream() {}
0168     virtual void Write(int *count, void *buffer); // throws IOException
0169     virtual void Read(int *count, void *buffer); // throws IOException
0170     virtual void SetPos(short posMode, INT64 posOff); // throws IOException
0171     virtual UINT64 GetPos() const; // throws IOException
0172 };
0173 #endif
0174 
0175 /////////////////////////////////////////////////////////////////////
0176 /// A PGF stream subclass for IStream. Usable only with COM.
0177 /// @author C. Stamm
0178 /// @brief COM IStream class
0179 #if defined(WIN32) || defined(WINCE)
0180 class DIGIKAM_EXPORT CPGFIStream : public CPGFStream {
0181 protected:
0182     IStream *m_stream;  ///< COM+ IStream
0183 public:
0184     CPGFIStream(IStream *stream) : m_stream(stream) { m_stream->AddRef(); }
0185     virtual bool IsValid() const    { return m_stream != 0; }
0186     virtual ~CPGFIStream() { m_stream->Release(); }
0187     virtual void Write(int *count, void *buffer); // throws IOException
0188     virtual void Read(int *count, void *buffer); // throws IOException
0189     virtual void SetPos(short posMode, INT64 posOff); // throws IOException
0190     virtual UINT64 GetPos() const; // throws IOException
0191     IStream* GetIStream() const { return m_stream; }
0192 };
0193 #endif
0194 
0195 #endif // PGF_STREAM_H