1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 // File: vogl_buffer_stream.h
30 #include "vogl_core.h"
31 #include "vogl_data_stream.h"
35 class buffer_stream : public data_stream
46 buffer_stream(void *p, size_t size)
55 buffer_stream(const void *p, size_t size)
64 virtual ~buffer_stream()
68 bool open(const void *p, size_t size)
78 m_pBuf = (uint8 *)(p);
81 m_attribs = cDataStreamSeekable | cDataStreamReadable;
85 bool open(void *p, size_t size)
95 m_pBuf = static_cast<uint8 *>(p);
98 m_attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable;
116 const void *get_buf() const
125 virtual const void *get_ptr() const
130 virtual uint read(void *pBuf, uint len)
132 VOGL_ASSERT(len <= 0x7FFFFFFF);
134 if ((!m_opened) || (!is_readable()) || (!len))
137 VOGL_ASSERT(m_ofs <= m_size);
139 uint64_t bytes_left = m_size - m_ofs;
141 len = static_cast<uint>(math::minimum<uint64_t>(len, bytes_left));
144 memcpy(pBuf, &m_pBuf[m_ofs], len);
151 virtual uint write(const void *pBuf, uint len)
153 VOGL_ASSERT(len <= 0x7FFFFFFF);
155 if ((!m_opened) || (!is_writable()) || (!len))
158 VOGL_ASSERT(m_ofs <= m_size);
160 uint64_t bytes_left = m_size - m_ofs;
162 len = static_cast<uint>(math::minimum<uint64_t>(len, bytes_left));
165 memcpy(&m_pBuf[m_ofs], pBuf, len);
180 virtual uint64_t get_size() const
188 virtual uint64_t get_remaining() const
193 VOGL_ASSERT(m_ofs <= m_size);
195 return m_size - m_ofs;
198 virtual uint64_t get_ofs() const
206 virtual bool seek(int64_t ofs, bool relative)
208 if ((!m_opened) || (!is_seekable()))
211 int64_t new_ofs = relative ? (m_ofs + ofs) : ofs;
215 else if (new_ofs > static_cast<int64_t>(m_size))
218 m_ofs = static_cast<size_t>(new_ofs);