home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / win32 / include / cximage / xmemfile.h < prev   
C/C++ Source or Header  |  2004-02-29  |  2KB  |  70 lines

  1. #if !defined(__xmemfile_h)
  2. #define __xmemfile_h
  3.  
  4. #include "xfile.h"
  5.  
  6. //////////////////////////////////////////////////////////
  7. class DLL_EXP CxMemFile : public CxFile
  8.     {
  9. public:
  10.     CxMemFile(BYTE* pBuffer = NULL, DWORD size = 0)
  11.     {
  12.         m_pBuffer = pBuffer;
  13.         m_Position = 0;
  14.         m_Size = m_Edge = size;
  15.         m_bFreeOnClose = (bool)(pBuffer==0);
  16.     }
  17. //////////////////////////////////////////////////////////
  18.     ~CxMemFile()
  19.     {
  20.         Close();
  21.     }
  22. //////////////////////////////////////////////////////////
  23.     virtual bool Close()
  24.     {
  25.         if ( (m_pBuffer) && (m_bFreeOnClose) ){
  26.             free(m_pBuffer);
  27.             m_pBuffer = NULL;
  28.             m_Size = 0;
  29.         }
  30.         return true;
  31.     }
  32. //////////////////////////////////////////////////////////
  33.     bool Open()
  34.     {
  35.         if (m_pBuffer) return false;    // Can't re-open without closing first
  36.  
  37.         m_Position = m_Size = m_Edge = 0;
  38.         m_pBuffer=(BYTE*)malloc(0);
  39.         m_bFreeOnClose = true;
  40.  
  41.         return (m_pBuffer!=0);
  42.     }
  43. //////////////////////////////////////////////////////////
  44. BYTE* GetBuffer(bool bDetachBuffer = true) { m_bFreeOnClose = !bDetachBuffer; return m_pBuffer;}
  45. //////////////////////////////////////////////////////////
  46.     virtual size_t    Read(void *buffer, size_t size, size_t count);
  47.     virtual size_t    Write(const void *buffer, size_t size, size_t count);
  48.     virtual bool    Seek(long offset, int origin);
  49.     virtual long    Tell();
  50.     virtual long    Size();
  51.     virtual bool    Flush();
  52.     virtual bool    Eof();
  53.     virtual long    Error();
  54.     virtual bool    PutC(unsigned char c);
  55.     virtual long    GetC();
  56.  
  57. protected:
  58.     void    Alloc(DWORD nBytes);
  59.     void    Free();
  60.  
  61. protected:
  62.     BYTE*    m_pBuffer;
  63.     DWORD    m_Size;
  64.     bool    m_bFreeOnClose;
  65.     long    m_Position;    //current position
  66.     long    m_Edge;        //buffer size
  67.     };
  68.  
  69. #endif
  70.