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_dynamic_stream.h
30 #include "vogl_core.h"
31 #include "vogl_data_stream.h"
35 class dynamic_stream : public data_stream
38 dynamic_stream(uint initial_size, const char *pName = "dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable)
39 : data_stream(pName, attribs),
42 open(initial_size, pName, attribs);
45 dynamic_stream(const void *pBuf, uint size, const char *pName = "dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable)
46 : data_stream(pName, attribs),
49 open(pBuf, size, pName, attribs);
59 virtual ~dynamic_stream()
63 bool open(uint initial_size = 0, const char *pName = "dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable)
69 m_buf.resize(initial_size);
71 m_name.set(pName ? pName : "dynamic_stream");
72 m_attribs = static_cast<attribs_t>(attribs);
76 bool reopen(const char *pName, uint attribs)
80 return open(0, pName, attribs);
83 m_name.set(pName ? pName : "dynamic_stream");
84 m_attribs = static_cast<attribs_t>(attribs);
88 bool open(const void *pBuf, uint size, const char *pName = "dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable)
97 memcpy(&m_buf[0], pBuf, size);
100 m_name.set(pName ? pName : "dynamic_stream");
101 m_attribs = static_cast<attribs_t>(attribs);
134 const vogl::vector<uint8> &get_buf() const
138 vogl::vector<uint8> &get_buf()
143 void reserve(uint size)
151 virtual const void *get_ptr() const
153 return m_buf.is_empty() ? NULL : &m_buf[0];
156 virtual uint read(void *pBuf, uint len)
158 VOGL_ASSERT(len <= 0x7FFFFFFF);
160 if ((!m_opened) || (!is_readable()) || (!len))
163 VOGL_ASSERT(m_ofs <= m_buf.size());
165 uint bytes_left = m_buf.size() - m_ofs;
167 len = math::minimum<uint>(len, bytes_left);
170 memcpy(pBuf, &m_buf[m_ofs], len);
177 virtual uint write(const void *pBuf, uint len)
179 VOGL_ASSERT(len <= 0x7FFFFFFF);
181 if ((!m_opened) || (!is_writable()) || (!len))
184 VOGL_ASSERT(m_ofs <= m_buf.size());
186 uint new_ofs = m_ofs + len;
187 if (new_ofs > m_buf.size())
188 m_buf.resize(new_ofs);
190 memcpy(&m_buf[m_ofs], pBuf, len);
204 virtual uint64_t get_size() const
212 virtual uint64_t get_remaining() const
217 VOGL_ASSERT(m_ofs <= m_buf.size());
219 return m_buf.size() - m_ofs;
222 virtual uint64_t get_ofs() const
230 virtual bool seek(int64_t ofs, bool relative)
232 if ((!m_opened) || (!is_seekable()))
235 int64_t new_ofs = relative ? (m_ofs + ofs) : ofs;
239 else if (new_ofs > m_buf.size())
242 m_ofs = static_cast<uint>(new_ofs);
250 vogl::vector<uint8> m_buf;