Merge pull request #25959 from neo1973/TagLib_deprecation_warnings
[xbmc.git] / lib / libUPnP / Neptune / Source / Core / NptDataBuffer.cpp
blobc0b1c80a3d218e16f5375be5e032712b805f42e6
1 /*****************************************************************
3 | Neptune - Data Buffer
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
6 | All rights reserved.
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 ****************************************************************/
32 /*----------------------------------------------------------------------
33 | includes
34 +---------------------------------------------------------------------*/
35 #include "NptDataBuffer.h"
36 #include "NptUtils.h"
37 #include "NptResults.h"
39 /*----------------------------------------------------------------------
40 | NPT_DataBuffer::NPT_DataBuffer
41 +---------------------------------------------------------------------*/
42 NPT_DataBuffer::NPT_DataBuffer() :
43 m_BufferIsLocal(true),
44 m_Buffer(NULL),
45 m_BufferSize(0),
46 m_DataSize(0)
50 /*----------------------------------------------------------------------
51 | NPT_DataBuffer::NPT_DataBuffer
52 +---------------------------------------------------------------------*/
53 NPT_DataBuffer::NPT_DataBuffer(NPT_Size bufferSize) :
54 m_BufferIsLocal(true),
55 m_Buffer(bufferSize?new NPT_Byte[bufferSize]:NULL),
56 m_BufferSize(bufferSize),
57 m_DataSize(0)
61 /*----------------------------------------------------------------------
62 | NPT_DataBuffer::NPT_DataBuffer
63 +---------------------------------------------------------------------*/
64 NPT_DataBuffer::NPT_DataBuffer(const void* data, NPT_Size data_size, bool copy) :
65 m_BufferIsLocal(copy),
66 m_Buffer(copy?(data_size?new NPT_Byte[data_size]:NULL):reinterpret_cast<NPT_Byte*>(const_cast<void*>(data))),
67 m_BufferSize(data_size),
68 m_DataSize(data_size)
70 if (copy && data_size) NPT_CopyMemory(m_Buffer, data, data_size);
73 /*----------------------------------------------------------------------
74 | NPT_DataBuffer::NPT_DataBuffer
75 +---------------------------------------------------------------------*/
76 NPT_DataBuffer::NPT_DataBuffer(const NPT_DataBuffer& other) :
77 m_BufferIsLocal(true),
78 m_Buffer(NULL),
79 m_BufferSize(other.m_DataSize),
80 m_DataSize(other.m_DataSize)
82 if (m_BufferSize) {
83 m_Buffer = new NPT_Byte[m_BufferSize];
84 NPT_CopyMemory(m_Buffer, other.m_Buffer, m_BufferSize);
88 /*----------------------------------------------------------------------
89 | NPT_DataBuffer::~NPT_DataBuffer
90 +---------------------------------------------------------------------*/
91 NPT_DataBuffer::~NPT_DataBuffer()
93 Clear();
96 /*----------------------------------------------------------------------
97 | NPT_DataBuffer::Clear
98 +---------------------------------------------------------------------*/
99 NPT_Result
100 NPT_DataBuffer::Clear()
102 if (m_BufferIsLocal) {
103 delete[] m_Buffer;
105 m_Buffer = NULL;
106 m_DataSize = 0;
107 m_BufferSize = 0;
109 return NPT_SUCCESS;
112 /*----------------------------------------------------------------------
113 | NPT_DataBuffer::operator=
114 +---------------------------------------------------------------------*/
115 NPT_DataBuffer&
116 NPT_DataBuffer::operator=(const NPT_DataBuffer& copy)
118 // do nothing if we're assigning to ourselves
119 if (this != &copy) {
120 Clear();
122 m_BufferIsLocal = true;
123 m_BufferSize = copy.m_BufferSize;
124 m_DataSize = copy.m_DataSize;
126 if (m_BufferSize) {
127 m_Buffer = new NPT_Byte[m_BufferSize];
128 NPT_CopyMemory(m_Buffer, copy.m_Buffer, m_BufferSize);
131 return *this;
134 /*----------------------------------------------------------------------
135 | NPT_DataBuffer::operator==
136 +---------------------------------------------------------------------*/
137 bool
138 NPT_DataBuffer::operator==(const NPT_DataBuffer& other) const
140 // check that the sizes match
141 if (m_DataSize != other.m_DataSize) return false;
143 return NPT_MemoryEqual(m_Buffer,
144 other.m_Buffer,
145 m_DataSize);
148 /*----------------------------------------------------------------------
149 | NPT_DataBuffer::SetBuffer
150 +---------------------------------------------------------------------*/
151 NPT_Result
152 NPT_DataBuffer::SetBuffer(NPT_Byte* buffer, NPT_Size buffer_size)
154 Clear();
156 // we're now using an external buffer
157 m_BufferIsLocal = false;
158 m_Buffer = buffer;
159 m_BufferSize = buffer_size;
160 m_DataSize = 0;
162 return NPT_SUCCESS;
165 /*----------------------------------------------------------------------
166 | NPT_DataBuffer::SetBufferSize
167 +---------------------------------------------------------------------*/
168 NPT_Result
169 NPT_DataBuffer::SetBufferSize(NPT_Size buffer_size)
171 if (m_BufferIsLocal) {
172 return ReallocateBuffer(buffer_size);
173 } else {
174 return NPT_ERROR_NOT_SUPPORTED; // you cannot change the
175 // buffer management mode
179 /*----------------------------------------------------------------------
180 | NPT_DataBuffer::Reserve
181 +---------------------------------------------------------------------*/
182 NPT_Result
183 NPT_DataBuffer::Reserve(NPT_Size size)
185 if (size <= m_BufferSize) return NPT_SUCCESS;
187 // try doubling the buffer to accomodate for the new size
188 NPT_Size new_size = m_BufferSize*2;
189 if (new_size < size) new_size = size;
190 return SetBufferSize(new_size);
193 /*----------------------------------------------------------------------
194 | NPT_DataBuffer::SetDataSize
195 +---------------------------------------------------------------------*/
196 NPT_Result
197 NPT_DataBuffer::SetDataSize(NPT_Size size)
199 if (size > m_BufferSize) {
200 // the buffer is too small, we need to reallocate it
201 if (m_BufferIsLocal) {
202 NPT_CHECK(ReallocateBuffer(size));
203 } else {
204 // we cannot reallocate an external buffer
205 return NPT_ERROR_NOT_SUPPORTED;
208 m_DataSize = size;
209 return NPT_SUCCESS;
212 /*----------------------------------------------------------------------
213 | NPT_DataBuffer::SetData
214 +---------------------------------------------------------------------*/
215 NPT_Result
216 NPT_DataBuffer::SetData(const NPT_Byte* data, NPT_Size size)
218 if (size > m_BufferSize) {
219 if (m_BufferIsLocal) {
220 NPT_CHECK(ReallocateBuffer(size));
221 } else {
222 return NPT_ERROR_INVALID_STATE;
225 if (data) NPT_CopyMemory(m_Buffer, data, size);
226 m_DataSize = size;
228 return NPT_SUCCESS;
231 /*----------------------------------------------------------------------
232 | NPT_DataBuffer::ReallocateBuffer
233 +---------------------------------------------------------------------*/
234 NPT_Result
235 NPT_DataBuffer::ReallocateBuffer(NPT_Size size)
237 // check that the existing data fits
238 if (m_DataSize > size) return NPT_ERROR_INVALID_PARAMETERS;
240 // allocate a new buffer only if size is not zero
241 if (!size) return NPT_ERROR_INVALID_PARAMETERS;
243 NPT_Byte* newBuffer = new NPT_Byte[size];
245 // copy the contents of the previous buffer, if any
246 if (m_Buffer && m_DataSize) {
247 NPT_CopyMemory(newBuffer, m_Buffer, m_DataSize);
250 // destroy the previous buffer
251 delete[] m_Buffer;
253 // use the new buffer
254 m_Buffer = newBuffer;
255 m_BufferSize = size;
257 return NPT_SUCCESS;