1 #include "xmemfile.h"
\r
3 //////////////////////////////////////////////////////////
\r
4 CxMemFile::CxMemFile(BYTE* pBuffer, DWORD size)
\r
8 m_Size = m_Edge = size;
\r
9 m_bFreeOnClose = (bool)(pBuffer==0);
\r
11 //////////////////////////////////////////////////////////
\r
12 CxMemFile::~CxMemFile()
\r
16 //////////////////////////////////////////////////////////
\r
17 bool CxMemFile::Close()
\r
19 if ( (m_pBuffer) && (m_bFreeOnClose) ){
\r
26 //////////////////////////////////////////////////////////
\r
27 bool CxMemFile::Open()
\r
29 if (m_pBuffer) return false; // Can't re-open without closing first
\r
31 m_Position = m_Size = m_Edge = 0;
\r
32 m_pBuffer=(BYTE*)malloc(1);
\r
33 m_bFreeOnClose = true;
\r
35 return (m_pBuffer!=0);
\r
37 //////////////////////////////////////////////////////////
\r
38 BYTE* CxMemFile::GetBuffer(bool bDetachBuffer)
\r
40 //can only detach, avoid inadvertantly attaching to
\r
41 // memory that may not be ours [Jason De Arte]
\r
43 m_bFreeOnClose = false;
\r
46 //////////////////////////////////////////////////////////
\r
47 size_t CxMemFile::Read(void *buffer, size_t size, size_t count)
\r
49 if (buffer==NULL) return 0;
\r
51 if (m_pBuffer==NULL) return 0;
\r
52 if (m_Position >= (long)m_Size) return 0;
\r
54 long nCount = (long)(count*size);
\r
55 if (nCount == 0) return 0;
\r
58 if (m_Position + nCount > (long)m_Size)
\r
59 nRead = (m_Size - m_Position);
\r
63 memcpy(buffer, m_pBuffer + m_Position, nRead);
\r
64 m_Position += nRead;
\r
66 return (size_t)(nRead/size);
\r
68 //////////////////////////////////////////////////////////
\r
69 size_t CxMemFile::Write(const void *buffer, size_t size, size_t count)
\r
71 if (m_pBuffer==NULL) return 0;
\r
72 if (buffer==NULL) return 0;
\r
74 long nCount = (long)(count*size);
\r
75 if (nCount == 0) return 0;
\r
77 if (m_Position + nCount > m_Edge){
\r
78 if (!Alloc(m_Position + nCount)){
\r
83 memcpy(m_pBuffer + m_Position, buffer, nCount);
\r
85 m_Position += nCount;
\r
87 if (m_Position > (long)m_Size) m_Size = m_Position;
\r
91 //////////////////////////////////////////////////////////
\r
92 bool CxMemFile::Seek(long offset, int origin)
\r
94 if (m_pBuffer==NULL) return false;
\r
95 long lNewPos = m_Position;
\r
97 if (origin == SEEK_SET) lNewPos = offset;
\r
98 else if (origin == SEEK_CUR) lNewPos += offset;
\r
99 else if (origin == SEEK_END) lNewPos = m_Size + offset;
\r
102 if (lNewPos < 0) lNewPos = 0;
\r
104 m_Position = lNewPos;
\r
107 //////////////////////////////////////////////////////////
\r
108 long CxMemFile::Tell()
\r
110 if (m_pBuffer==NULL) return -1;
\r
113 //////////////////////////////////////////////////////////
\r
114 long CxMemFile::Size()
\r
116 if (m_pBuffer==NULL) return -1;
\r
119 //////////////////////////////////////////////////////////
\r
120 bool CxMemFile::Flush()
\r
122 if (m_pBuffer==NULL) return false;
\r
125 //////////////////////////////////////////////////////////
\r
126 bool CxMemFile::Eof()
\r
128 if (m_pBuffer==NULL) return true;
\r
129 return (m_Position >= (long)m_Size);
\r
131 //////////////////////////////////////////////////////////
\r
132 long CxMemFile::Error()
\r
134 if (m_pBuffer==NULL) return -1;
\r
135 return (m_Position > (long)m_Size);
\r
137 //////////////////////////////////////////////////////////
\r
138 bool CxMemFile::PutC(unsigned char c)
\r
140 if (m_pBuffer==NULL) return false;
\r
142 if (m_Position >= m_Edge){
\r
143 if (!Alloc(m_Position + 1)){
\r
148 m_pBuffer[m_Position++] = c;
\r
150 if (m_Position > (long)m_Size) m_Size = m_Position;
\r
154 //////////////////////////////////////////////////////////
\r
155 long CxMemFile::GetC()
\r
157 if (Eof()) return EOF;
\r
158 return *(BYTE*)((BYTE*)m_pBuffer + m_Position++);
\r
160 //////////////////////////////////////////////////////////
\r
161 char * CxMemFile::GetS(char *string, int n)
\r
167 if (c == EOF) return 0;
\r
168 string[i++] = (char)c;
\r
169 if (c == '\n') break;
\r
174 //////////////////////////////////////////////////////////
\r
175 long CxMemFile::Scanf(const char *format, void* output)
\r
179 //////////////////////////////////////////////////////////
\r
180 bool CxMemFile::Alloc(DWORD dwNewLen)
\r
182 if (dwNewLen > (DWORD)m_Edge)
\r
184 // find new buffer size
\r
185 DWORD dwNewBufferSize = (DWORD)(((dwNewLen>>16)+1)<<16);
\r
187 // allocate new buffer
\r
188 if (m_pBuffer == NULL) m_pBuffer = (BYTE*)malloc(dwNewBufferSize);
\r
189 else m_pBuffer = (BYTE*)realloc(m_pBuffer, dwNewBufferSize);
\r
190 // I own this buffer now (caller knows nothing about it)
\r
191 m_bFreeOnClose = true;
\r
193 m_Edge = dwNewBufferSize;
\r
195 return (m_pBuffer!=0);
\r
197 //////////////////////////////////////////////////////////
\r
198 void CxMemFile::Free()
\r
202 //////////////////////////////////////////////////////////
\r