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