Updated changelog
[amule.git] / src / MemFile.cpp
blob4632bf41469ed59fe0b23ba1cfa3b962b95a1304
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "MemFile.h" // Interface declarations
29 CMemFile::CMemFile(unsigned int growthRate)
31 m_buffer = NULL;
32 m_BufferSize = 0;
33 m_fileSize = 0;
34 m_growthRate = growthRate;
35 m_position = 0;
36 m_delete = true;
37 m_readonly = false;
41 CMemFile::CMemFile(byte* buffer, size_t bufferSize)
43 MULE_VALIDATE_PARAMS(buffer, wxT("CMemFile: Attempted to attach invalid buffer."));
45 m_buffer = buffer;
46 m_BufferSize = bufferSize;
47 m_fileSize = bufferSize;
48 m_growthRate = 0;
49 m_position = 0;
50 m_delete = false;
51 m_readonly = false;
54 CMemFile::CMemFile(const byte* buffer, size_t bufferSize)
56 MULE_VALIDATE_PARAMS(buffer, wxT("CMemFile: Attempted to attach invalid buffer."));
58 m_buffer = const_cast<byte*>(buffer);
59 m_BufferSize = bufferSize;
60 m_fileSize = bufferSize;
61 m_growthRate = 0;
62 m_position = 0;
63 m_delete = false;
64 m_readonly = true;
67 CMemFile::~CMemFile()
69 if (m_delete) {
70 free(m_buffer);
75 uint64 CMemFile::GetPosition() const
77 return m_position;
81 void CMemFile::SetLength(size_t newLen)
83 MULE_VALIDATE_STATE(!m_readonly, wxT("CMemFile: Attempted to change length on a read-only buffer."));
85 if (newLen > m_BufferSize) {
86 enlargeBuffer(newLen);
89 if (newLen < m_position) {
90 m_position = newLen;
93 m_fileSize = newLen;
97 uint64 CMemFile::GetLength() const
99 return m_fileSize;
103 void CMemFile::enlargeBuffer(size_t size)
105 MULE_VALIDATE_PARAMS(size >= m_BufferSize, wxT("CMemFile: Attempted to shrink buffer."));
106 MULE_VALIDATE_STATE(m_delete, wxT("CMemFile: Attempted to grow an attached buffer."));
107 MULE_VALIDATE_STATE(!m_readonly, wxT("CMemFile: Attempted to grow a read-only buffer."));
109 size_t newsize = m_BufferSize;
111 if (m_growthRate) {
112 newsize = ((size + m_growthRate - 1) / m_growthRate) * m_growthRate;
113 } else {
114 // No growth-rate specified. Change to exactly the size specified.
115 newsize = size;
118 byte *tmp = (byte*)realloc(m_buffer, newsize);
119 if (tmp) {
120 m_buffer = tmp;
121 m_BufferSize = newsize;
124 MULE_VALIDATE_STATE(tmp, wxT("CMemFile: Failed to (re)allocate buffer"));
128 sint64 CMemFile::doRead(void* buffer, size_t count) const
130 MULE_VALIDATE_PARAMS(buffer, wxT("CMemFile: Attempting to read to invalid buffer"));
132 // Handle reads past EOF
133 if (m_position > m_fileSize) {
134 return 0;
135 } else if (m_position + count > m_fileSize) {
136 count = m_fileSize - m_position;
139 if (count) {
140 memcpy(buffer, m_buffer + m_position, count);
141 m_position += count;
144 return count;
148 sint64 CMemFile::doWrite(const void* buffer, size_t count)
150 MULE_VALIDATE_PARAMS(buffer, wxT("CMemFile: Attempting to write to invalid buffer"));
151 MULE_VALIDATE_STATE(!m_readonly, wxT("CMemFile: Attempted to write to a read-only buffer."));
153 // Needs more space?
154 if (m_position + count > m_BufferSize) {
155 enlargeBuffer(m_position + count);
158 MULE_VALIDATE_STATE(m_position + count <= m_BufferSize, wxT("CMemFile: Buffer not resized to needed size."));
160 memcpy(m_buffer + m_position, buffer, count);
161 m_position += count;
163 if (m_position > m_fileSize) {
164 m_fileSize = m_position;
167 return count;
171 sint64 CMemFile::doSeek(sint64 offset) const
173 MULE_VALIDATE_PARAMS(offset >= 0, wxT("CMemFile: Invalid seek, position, must be positive."));
175 return m_position = offset;
179 void CMemFile::ResetData()
181 wxCHECK_RET(!m_readonly, wxT("Trying to reset read-only buffer"));
183 memset(m_buffer, 0, m_BufferSize);
184 m_fileSize = 0;
185 m_position = 0;
188 // File_checked_for_headers