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_data_stream.h
30 #include "vogl_core.h"
34 enum data_stream_attribs
36 cDataStreamReadable = 1,
37 cDataStreamWritable = 2,
38 cDataStreamSeekable = 4
41 const int64_t DATA_STREAM_SIZE_UNKNOWN = cINT64_MAX;
42 const int64_t DATA_STREAM_SIZE_INFINITE = cUINT64_MAX;
46 data_stream(const data_stream &);
47 data_stream &operator=(const data_stream &);
51 data_stream(const char *pName, uint attribs);
53 virtual ~data_stream()
57 virtual data_stream *get_parent()
70 typedef uint16 attribs_t;
71 inline attribs_t get_attribs() const
76 inline bool is_opened() const
81 inline bool is_readable() const
83 return utils::is_bit_set(m_attribs, cDataStreamReadable);
85 inline bool is_writable() const
87 return utils::is_bit_set(m_attribs, cDataStreamWritable);
89 inline bool is_seekable() const
91 return utils::is_bit_set(m_attribs, cDataStreamSeekable);
94 // true if stream has latched an error
95 inline bool get_error() const
100 inline const dynamic_string &get_name() const
104 inline void set_name(const char *pName)
109 virtual uint read(void *pBuf, uint len) = 0;
110 uint64_t read64(void *pBuf, uint64_t len);
112 virtual uint64_t skip(uint64_t len);
114 virtual uint write(const void *pBuf, uint len) = 0;
115 virtual bool flush() = 0;
117 virtual bool is_size_known() const
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;
126 virtual uint64_t get_ofs() const = 0;
127 virtual bool seek(int64_t ofs, bool relative) = 0;
129 virtual const void *get_ptr() const
134 inline int read_byte()
137 if (read(&c, 1) != 1)
141 inline bool write_byte(uint8 c)
143 return write(&c, 1) == 1;
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);
153 return write(&bom, sizeof(bom)) == sizeof(bom);
156 bool read_array(vector<uint8> &buf);
157 bool write_array(const vector<uint8> &buf);
159 void set_user_data(void *p)
163 void *get_user_data() const
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);
172 dynamic_string m_name;
180 inline void set_error()
184 inline void clear_error()
189 inline void post_seek()