Updated Hungarian translation by GonoszTopi
[amule.git] / src / MemFile.cpp
blob49e01be98ff83ee2ead9f5dc31a84c1582477566
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.
20 //
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 lenght 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 m_buffer = (byte*)realloc(m_buffer, newsize);
119 m_BufferSize = newsize;
121 MULE_VALIDATE_STATE(m_buffer, wxT("CMemFile: Failed to (re)allocate buffer"));
125 sint64 CMemFile::doRead(void* buffer, size_t count) const
127 MULE_VALIDATE_PARAMS(buffer, wxT("CMemFile: Attempting to read to invalid buffer"));
129 // Handle reads past EOF
130 if (m_position > m_fileSize) {
131 return 0;
132 } else if (m_position + count > m_fileSize) {
133 count = m_fileSize - m_position;
136 if (count) {
137 memcpy(buffer, m_buffer + m_position, count);
138 m_position += count;
141 return count;
145 sint64 CMemFile::doWrite(const void* buffer, size_t count)
147 MULE_VALIDATE_PARAMS(buffer, wxT("CMemFile: Attempting to write to invalid buffer"));
148 MULE_VALIDATE_STATE(!m_readonly, wxT("CMemFile: Attempted to write to a read-only buffer."));
150 // Needs more space?
151 if (m_position + count > m_BufferSize) {
152 enlargeBuffer(m_position + count);
155 MULE_VALIDATE_STATE(m_position + count <= m_BufferSize, wxT("CMemFile: Buffer not resized to needed size."));
157 memcpy(m_buffer + m_position, buffer, count);
158 m_position += count;
160 if (m_position > m_fileSize) {
161 m_fileSize = m_position;
164 return count;
168 sint64 CMemFile::doSeek(sint64 offset) const
170 MULE_VALIDATE_PARAMS(offset >= 0, wxT("CMemFile: Invalid seek, position, must be positive."));
172 return m_position = offset;
176 void CMemFile::ResetData()
178 wxCHECK_RET(!m_readonly, wxT("Trying to reset read-only buffer"));
180 memset(m_buffer, 0, m_BufferSize);
181 m_fileSize = 0;
182 m_position = 0;
185 // File_checked_for_headers