]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_data_stream.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_data_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_data_stream.h
28 #pragma once
29
30 #include "vogl_core.h"
31
32 namespace vogl
33 {
34     enum data_stream_attribs
35     {
36         cDataStreamReadable = 1,
37         cDataStreamWritable = 2,
38         cDataStreamSeekable = 4
39     };
40
41     const int64_t DATA_STREAM_SIZE_UNKNOWN = cINT64_MAX;
42     const int64_t DATA_STREAM_SIZE_INFINITE = cUINT64_MAX;
43
44     class data_stream
45     {
46         data_stream(const data_stream &);
47         data_stream &operator=(const data_stream &);
48
49     public:
50         data_stream();
51         data_stream(const char *pName, uint attribs);
52
53         virtual ~data_stream()
54         {
55         }
56
57         virtual data_stream *get_parent()
58         {
59             return NULL;
60         }
61
62         virtual bool close()
63         {
64             m_opened = false;
65             m_error = false;
66             m_got_cr = false;
67             return true;
68         }
69
70         typedef uint16 attribs_t;
71         inline attribs_t get_attribs() const
72         {
73             return m_attribs;
74         }
75
76         inline bool is_opened() const
77         {
78             return m_opened;
79         }
80
81         inline bool is_readable() const
82         {
83             return utils::is_bit_set(m_attribs, cDataStreamReadable);
84         }
85         inline bool is_writable() const
86         {
87             return utils::is_bit_set(m_attribs, cDataStreamWritable);
88         }
89         inline bool is_seekable() const
90         {
91             return utils::is_bit_set(m_attribs, cDataStreamSeekable);
92         }
93
94         // true if stream has latched an error
95         inline bool get_error() const
96         {
97             return m_error;
98         }
99
100         inline const dynamic_string &get_name() const
101         {
102             return m_name;
103         }
104         inline void set_name(const char *pName)
105         {
106             m_name.set(pName);
107         }
108
109         virtual uint read(void *pBuf, uint len) = 0;
110         uint64_t read64(void *pBuf, uint64_t len);
111
112         virtual uint64_t skip(uint64_t len);
113
114         virtual uint write(const void *pBuf, uint len) = 0;
115         virtual bool flush() = 0;
116
117         virtual bool is_size_known() const
118         {
119             return true;
120         }
121
122         // Returns DATA_STREAM_SIZE_UNKNOWN if size hasn't been determined yet, or DATA_STREAM_SIZE_INFINITE for infinite streams.
123         virtual uint64_t get_size() const = 0;
124         virtual uint64_t get_remaining() const = 0;
125
126         virtual uint64_t get_ofs() const = 0;
127         virtual bool seek(int64_t ofs, bool relative) = 0;
128
129         virtual const void *get_ptr() const
130         {
131             return NULL;
132         }
133
134         inline int read_byte()
135         {
136             uint8 c;
137             if (read(&c, 1) != 1)
138                 return -1;
139             return c;
140         }
141         inline bool write_byte(uint8 c)
142         {
143             return write(&c, 1) == 1;
144         }
145
146         bool read_line(dynamic_string &str);
147         bool printf(const char *p, ...) VOGL_ATTRIBUTE_PRINTF(2, 3);
148         bool puts(const char *p);
149         bool puts(const dynamic_string &str);
150         bool write_bom()
151         {
152             uint16 bom = 0xFEFF;
153             return write(&bom, sizeof(bom)) == sizeof(bom);
154         }
155
156         bool read_array(vector<uint8> &buf);
157         bool write_array(const vector<uint8> &buf);
158
159         void set_user_data(void *p)
160         {
161             m_pUser_data = p;
162         }
163         void *get_user_data() const
164         {
165             return m_pUser_data;
166         }
167
168         // Writes the entire contents of a binary file to the stream, starting at the current file position.
169         bool write_file_data(const char *pFilename);
170
171     protected:
172         dynamic_string m_name;
173         void *m_pUser_data;
174
175         attribs_t m_attribs;
176         bool m_opened : 1;
177         bool m_error : 1;
178         bool m_got_cr : 1;
179
180         inline void set_error()
181         {
182             m_error = true;
183         }
184         inline void clear_error()
185         {
186             m_error = false;
187         }
188
189         inline void post_seek()
190         {
191             m_got_cr = false;
192         }
193     };
194
195 } // namespace vogl