13
#include "tu_config.h" // needed ?
12
#include "dsodefs.h" // DSOEXPORT
14
13
#include "utility.h"
14
#include "IOChannel.h" // for inheritance
32
20
// a file abstraction that can be customized with callbacks.
33
21
// Designed to be easy to hook up to FILE*, SDL_RWops*, or
34
22
// whatever stream type(s) you might use in your game or
36
class DSOEXPORT tu_file
24
class DSOEXPORT tu_file : public gnash::IOChannel
39
typedef int (* read_func)(void* dst, int bytes, void* appdata);
40
typedef int (* write_func)(const void* src, int bytes, void* appdata);
41
typedef int (* seek_func)(int pos, void* appdata);
42
typedef int (* seek_to_end_func)(void* appdata);
43
typedef int (* tell_func)(void* appdata);
44
typedef bool (* get_eof_func)(void* appdata);
45
typedef int (* get_err_func)(void* appdata);
46
typedef long (* get_stream_size_func)(void* appdata);
47
typedef int (* close_func)(void* appdata);
49
// The generic constructor; supply functions for the implementation.
59
get_stream_size_func gss,
62
28
// Make a file from an ordinary FILE*.
63
29
tu_file(FILE* fp, bool autoclose);
65
// Optional: if you're using SDL, this is a constructor to create
66
// a tu_file from an SDL_RWops* stream.
67
tu_file(SDL_RWops* sdl_stream, bool autoclose);
69
// Open a file using ordinary fopen(). Automatically closes the
70
// file when we are destroyed.
71
tu_file(const char* name, const char* mode);
73
// Make a memory-buffer file for read/write.
74
enum memory_buffer_enum { memory_buffer };
75
tu_file(memory_buffer_enum m);
77
// A read-only memory-buffer with predefined data.
78
tu_file(memory_buffer_enum m, int size, void* data);
82
/// Copy remaining contents of *in into *this.
84
/// TODO: define what happens when the stream
85
/// is in error condition, see get_error().
87
void copy_from(tu_file* in);
89
/// Copy remaining contents of *this into *out.
91
/// TODO: define what happens when the stream
92
/// is in error condition, see get_error().
94
void copy_to(membuf* out);
96
/// Copy a fixed number of bytes from *in to *this.
98
/// Returns number of bytes copied.
100
/// TODO: define what happens when either one of the streams
101
/// is in error condition, see get_error().
103
int copy_bytes(tu_file* in, int bytes);
106
33
/// \brief Read a 32-bit word from a little-endian stream.
107
34
/// returning it as a native-endian word.
109
36
/// TODO: define what happens when the stream
110
37
/// is in error condition, see get_error().
112
boost::uint32_t read_le32() {
113
// read8() is boost::uint8_t, so no masks with 0xff are required.
114
boost::uint32_t result = (boost::uint32_t)read8();
115
result |= (boost::uint32_t)read8() << 8;
116
result |= (boost::uint32_t)read8() << 16;
117
result |= (boost::uint32_t)read8() << 24;
39
boost::uint32_t read_le32()
41
// read8() is boost::uint8_t, so no masks with 0xff are required.
42
boost::uint32_t result = static_cast<boost::uint32_t>(read8());
43
result |= static_cast<boost::uint32_t>(read8()) << 8;
44
result |= static_cast<boost::uint32_t>(read8()) << 16;
45
result |= static_cast<boost::uint32_t>(read8()) << 24;
121
49
/// \brief Read a 64-bit word from a little-ending stream,
147
77
/// TODO: define what happens when the stream
148
78
/// is in error condition, see get_error().
150
void write_le32(boost::uint32_t u) {
151
write8((boost::uint8_t)u);
152
write8((boost::uint8_t)(u>>8));
153
write8((boost::uint8_t)(u>>16));
154
write8((boost::uint8_t)(u>>24));
80
void write_le32(boost::uint32_t u)
82
write8(static_cast<boost::int8_t>(u));
83
write8(static_cast<boost::int8_t>(u>>8));
84
write8(static_cast<boost::int8_t>(u>>16));
85
write8(static_cast<boost::int8_t>(u>>24));
157
88
/// \brief Write a 16-bit word to a little-endian stream.
169
101
/// TODO: define what happens when the stream
170
102
/// is in error condition, see get_error().
172
boost::uint8_t read_byte() { return read8(); }
104
boost::uint8_t read_byte() { return read8(); }
174
106
/// \brief write a single byte to the stream
176
108
/// TODO: define what happens when the stream
177
109
/// is in error condition, see get_error().
179
void write_byte(boost::uint8_t u) { write8(u); }
111
void write_byte(boost::uint8_t u) { write8(u); }
181
113
/// \brief Read the given number of bytes from the stream
183
115
/// TODO: define what happens when the stream
184
116
/// is in error condition, see get_error().
186
int read_bytes(void* dst, int num) { return m_read(dst, num, m_data); }
118
int read(void* dst, int num);
188
120
/// \brief Write the given number of bytes to the stream
190
122
/// TODO: define what happens when the stream
191
123
/// is in error condition, see get_error().
193
int write_bytes(const void* src, int num) { return m_write(src, num, m_data); }
195
/// \brief Write a 0-terminated string to a stream.
197
/// TODO: define what happens when the stream
198
/// is in error condition, see get_error().
200
void write_string(const char* src);
203
/// Read up to max_length characters, returns the number of characters
204
/// read, or -1 if the string length is longer than max_length.
206
/// Stops at the first \0 character if it comes before max_length.
208
/// Guarantees termination of the string.
212
/// TODO: define what to return when the stream
213
/// is in error condition, see get_error().
215
int read_string(char* dst, int max_length);
217
/// \brief Write a 32-bit float to a stream.
219
/// TODO: define what to return when the stream
220
/// is in error condition, see get_error().
221
void write_float32(float value);
223
/// \brief Read a 32-bit float from a stream.
224
/// TODO: define what to return when the stream
225
/// is in error condition, see get_error().
226
float read_float32();
125
int write(const void* src, int num);
228
127
/// \brief Return current stream position
230
129
/// TODO: define what to return when the stream
231
130
/// is in error condition, see get_error().
233
int get_position() const { return m_tell(m_data); }
235
134
/// \brief Seek to the specified position
263
162
/// There are some rough meaning for possible returned values
264
163
/// but I don't think they make much sense currently.
266
int get_error() { return m_get_err(m_data); }
165
int get_error() const;
269
168
/// \brief Get the size of the stream
270
int get_size() { return m_get_stream_size(m_data); }
272
// \brief printf-style convenience function.
273
int printf(const char* fmt, ...);
275
// \brief UNSAFE back door, for testing only.
276
void* get_app_data_DEBUG() { return m_data; }
280
boost::uint64_t read64() {
282
m_read(&u, 8, m_data);
285
boost::uint32_t read32() {
287
m_read(&u, 4, m_data);
290
boost::uint16_t read16() {
292
m_read(&u, 2, m_data);
295
boost::uint8_t read8() {
297
m_read(&u, 1, m_data);
301
void write64(boost::uint64_t u) {
302
m_write(&u, 8, m_data);
304
void write32(boost::uint32_t u) {
305
m_write(&u, 4, m_data);
307
void write16(boost::uint16_t u) {
308
m_write(&u, 2, m_data);
310
void write8(boost::uint8_t u) {
311
m_write(&u, 1, m_data);
320
seek_to_end_func m_seek_to_end;
322
get_eof_func m_get_eof;
323
get_err_func m_get_err;
324
get_stream_size_func m_get_stream_size;
173
boost::uint64_t read64()
180
boost::uint32_t read32()
187
boost::uint16_t read16()
194
boost::uint8_t read8()
201
void write64(boost::uint64_t u)
206
void write32(boost::uint32_t u)
211
void write16(boost::uint16_t u)
216
void write8(boost::uint8_t u)
330
// Some inline stuff.
334
/// \brief Write a 32-bit float to a stream in little-endian order.
335
/// @@ This currently relies on host FP format being the same as the Flash one
336
/// (presumably IEEE 754).
337
inline void tu_file::write_float32(float value)
343
compiler_assert(sizeof(alias) == sizeof(boost::uint32_t));
350
/// \brief Read a 32-bit float from a little-endian stream.
351
/// @@ This currently relies on host FP format being the same as the Flash one
352
/// (presumably IEEE 754).
353
inline float tu_file::read_float32()
354
// Read a 32-bit little-endian float from this file.
360
compiler_assert(sizeof(u) == sizeof(u.i));
366
231
#endif // TU_FILE_H