]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_buffer_stream.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_buffer_stream.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_buffer_stream.h
28 #pragma once
29
30 #include "vogl_core.h"
31 #include "vogl_data_stream.h"
32
33 namespace vogl
34 {
35     class buffer_stream : public data_stream
36     {
37     public:
38         buffer_stream()
39             : data_stream(),
40               m_pBuf(NULL),
41               m_size(0),
42               m_ofs(0)
43         {
44         }
45
46         buffer_stream(void *p, size_t size)
47             : data_stream(),
48               m_pBuf(NULL),
49               m_size(0),
50               m_ofs(0)
51         {
52             open(p, size);
53         }
54
55         buffer_stream(const void *p, size_t size)
56             : data_stream(),
57               m_pBuf(NULL),
58               m_size(0),
59               m_ofs(0)
60         {
61             open(p, size);
62         }
63
64         virtual ~buffer_stream()
65         {
66         }
67
68         bool open(const void *p, size_t size)
69         {
70             VOGL_ASSERT(p);
71
72             close();
73
74             if ((!p) || (!size))
75                 return false;
76
77             m_opened = true;
78             m_pBuf = (uint8 *)(p);
79             m_size = size;
80             m_ofs = 0;
81             m_attribs = cDataStreamSeekable | cDataStreamReadable;
82             return true;
83         }
84
85         bool open(void *p, size_t size)
86         {
87             VOGL_ASSERT(p);
88
89             close();
90
91             if ((!p) || (!size))
92                 return false;
93
94             m_opened = true;
95             m_pBuf = static_cast<uint8 *>(p);
96             m_size = size;
97             m_ofs = 0;
98             m_attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable;
99             return true;
100         }
101
102         virtual bool close()
103         {
104             if (m_opened)
105             {
106                 m_opened = false;
107                 m_pBuf = NULL;
108                 m_size = 0;
109                 m_ofs = 0;
110                 return true;
111             }
112
113             return false;
114         }
115
116         const void *get_buf() const
117         {
118             return m_pBuf;
119         }
120         void *get_buf()
121         {
122             return m_pBuf;
123         }
124
125         virtual const void *get_ptr() const
126         {
127             return m_pBuf;
128         }
129
130         virtual uint read(void *pBuf, uint len)
131         {
132             VOGL_ASSERT(len <= 0x7FFFFFFF);
133
134             if ((!m_opened) || (!is_readable()) || (!len))
135                 return 0;
136
137             VOGL_ASSERT(m_ofs <= m_size);
138
139             uint64_t bytes_left = m_size - m_ofs;
140
141             len = static_cast<uint>(math::minimum<uint64_t>(len, bytes_left));
142
143             if (len)
144                 memcpy(pBuf, &m_pBuf[m_ofs], len);
145
146             m_ofs += len;
147
148             return len;
149         }
150
151         virtual uint write(const void *pBuf, uint len)
152         {
153             VOGL_ASSERT(len <= 0x7FFFFFFF);
154
155             if ((!m_opened) || (!is_writable()) || (!len))
156                 return 0;
157
158             VOGL_ASSERT(m_ofs <= m_size);
159
160             uint64_t bytes_left = m_size - m_ofs;
161
162             len = static_cast<uint>(math::minimum<uint64_t>(len, bytes_left));
163
164             if (len)
165                 memcpy(&m_pBuf[m_ofs], pBuf, len);
166
167             m_ofs += len;
168
169             return len;
170         }
171
172         virtual bool flush()
173         {
174             if (!m_opened)
175                 return false;
176
177             return true;
178         }
179
180         virtual uint64_t get_size() const
181         {
182             if (!m_opened)
183                 return 0;
184
185             return m_size;
186         }
187
188         virtual uint64_t get_remaining() const
189         {
190             if (!m_opened)
191                 return 0;
192
193             VOGL_ASSERT(m_ofs <= m_size);
194
195             return m_size - m_ofs;
196         }
197
198         virtual uint64_t get_ofs() const
199         {
200             if (!m_opened)
201                 return 0;
202
203             return m_ofs;
204         }
205
206         virtual bool seek(int64_t ofs, bool relative)
207         {
208             if ((!m_opened) || (!is_seekable()))
209                 return false;
210
211             int64_t new_ofs = relative ? (m_ofs + ofs) : ofs;
212
213             if (new_ofs < 0)
214                 return false;
215             else if (new_ofs > static_cast<int64_t>(m_size))
216                 return false;
217
218             m_ofs = static_cast<size_t>(new_ofs);
219
220             post_seek();
221
222             return true;
223         }
224
225     private:
226         uint8 *m_pBuf;
227         size_t m_size;
228         size_t m_ofs;
229     };
230
231 } // namespace vogl