~ubuntu-branches/ubuntu/saucy/gnash/saucy-proposed

« back to all changes in this revision

Viewing changes to libbase/tu_file.h

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack
  • Date: 2008-10-13 14:29:49 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20081013142949-f6qdvnu4mn05ltdc
Tags: 0.8.4~~bzr9980-0ubuntu1
* new upstream release 0.8.4 (LP: #240325)
* ship new lib usr/lib/gnash/libmozsdk.so.* in mozilla-plugin-gnash
  - update debian/mozilla-plugin-gnash.install
* ship new lib usr/lib/gnash/libgnashnet.so.* in gnash-common
  - update debian/gnash-common.install
* add basic debian/build_head script to build latest CVS head packages.
  - add debian/build_head
* new sound architecture requires build depend on libsdl1.2-dev
  - update debian/control
* head build script now has been completely migrated to bzr (upstream +
  ubuntu)
  - update debian/build_head
* disable kde gui until klash/qt4 has been fixed; keep kde packages as empty
  packages for now.
  - update debian/rules
  - debian/klash.install
  - debian/klash.links
  - debian/klash.manpages
  - debian/konqueror-plugin-gnash.install
* drop libkonq5-dev build dependency accordingly
  - update debian/control
* don't install headers manually anymore. gnash doesnt provide a -dev
  package after all
  - update debian/rules
* update libs installed in gnash-common; libgnashserver-*.so is not available
  anymore (removed); in turn we add the new libgnashcore-*.so
  - update debian/gnash-common.install
* use -Os for optimization and properly pass CXXFLAGS=$(CFLAGS) to configure
  - update debian/rules
* touch firefox .autoreg in postinst of mozilla plugin
  - update debian/mozilla-plugin-gnash.postinst
* link gnash in ubufox plugins directory for the plugin alternative switcher
  - add debian/mozilla-plugin-gnash.links
* suggest ubufox accordingly
  - update debian/control
* add new required build-depends on libgif-dev
  - update debian/control
* add Xb-Npp-Description and Xb-Npp-File as new plugin database meta data
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
#ifndef TU_FILE_H
10
10
#define TU_FILE_H
11
11
 
12
 
 
13
 
#include "tu_config.h" // needed ?
 
12
#include "dsodefs.h" // DSOEXPORT
14
13
#include "utility.h"
 
14
#include "IOChannel.h" // for inheritance
15
15
 
16
16
#include <cstdio>
17
17
 
18
 
class membuf;
19
 
struct SDL_RWops;
20
 
 
21
 
enum
22
 
{
23
 
    TU_FILE_NO_ERROR = 0,
24
 
    TU_FILE_OPEN_ERROR,
25
 
    TU_FILE_READ_ERROR,
26
 
    TU_FILE_WRITE_ERROR,
27
 
    TU_FILE_SEEK_ERROR,
28
 
    TU_FILE_CLOSE_ERROR
29
 
};
30
 
 
 
18
namespace gnash {
31
19
 
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
35
23
// libraries.
36
 
class DSOEXPORT tu_file
 
24
class DSOEXPORT tu_file : public gnash::IOChannel
37
25
{
38
26
public:
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);
48
 
    
49
 
    // The generic constructor; supply functions for the implementation.
50
 
    tu_file(
51
 
        void * appdata,
52
 
        read_func rf,
53
 
        write_func wf,
54
 
        seek_func sf,
55
 
        seek_to_end_func ef,
56
 
        tell_func tf,
57
 
        get_eof_func gef,
58
 
        get_err_func ger,
59
 
        get_stream_size_func gss,
60
 
        close_func cf=NULL);
61
 
    
 
27
 
62
28
    // Make a file from an ordinary FILE*.
63
29
    tu_file(FILE* fp, bool autoclose);
64
30
    
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);
68
 
    
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);
72
 
    
73
 
    // Make a memory-buffer file for read/write.
74
 
    enum memory_buffer_enum { memory_buffer };
75
 
    tu_file(memory_buffer_enum m);
76
 
    
77
 
    // A read-only memory-buffer with predefined data.
78
 
    tu_file(memory_buffer_enum m, int size, void* data);
79
 
    
80
31
    ~tu_file();
81
32
    
82
 
    /// Copy remaining contents of *in into *this.
83
 
    //
84
 
    /// TODO: define what happens when the stream
85
 
    ///       is in error condition, see get_error().
86
 
    ///
87
 
    void copy_from(tu_file* in);
88
 
 
89
 
    /// Copy remaining contents of *this into *out.
90
 
    //
91
 
    /// TODO: define what happens when the stream
92
 
    ///       is in error condition, see get_error().
93
 
    ///
94
 
    void copy_to(membuf* out);
95
 
    
96
 
    /// Copy a fixed number of bytes from *in to *this.
97
 
    //
98
 
    /// Returns number of bytes copied.
99
 
    ///
100
 
    /// TODO: define what happens when either one of the streams
101
 
    ///       is in error condition, see get_error().
102
 
    ///
103
 
    int copy_bytes(tu_file* in, int bytes);
104
 
    
105
 
 
106
33
    /// \brief Read a 32-bit word from a little-endian stream.
107
34
    /// returning it as a native-endian word.
108
35
    //
109
36
    /// TODO: define what happens when the stream
110
37
    ///       is in error condition, see get_error().
111
38
    ///
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;
118
 
        return(result);
 
39
    boost::uint32_t read_le32() 
 
40
    {
 
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;
 
46
            return(result);
119
47
    }
120
48
        
121
49
        /// \brief Read a 64-bit word from a little-ending stream,
124
52
        /// TODO: define what happens when the stream is in
125
53
        ///       error condition, see get_error().
126
54
        /// TODO: define a platform-neutral type for 64 bits.
127
 
        long double read_le_double64() {
 
55
        long double read_le_double64()
 
56
        {
128
57
                return static_cast<long double> (
129
58
                        static_cast<boost::int64_t> (read_le32()) |
130
59
                        static_cast<boost::int64_t> (read_le32()) << 32
136
65
    /// TODO: define what happens when the stream
137
66
    ///       is in error condition, see get_error().
138
67
    ///
139
 
    boost::uint16_t     read_le16() {
140
 
        boost::uint16_t result = (boost::uint16_t)read8();
141
 
        result |= (boost::uint16_t)read8() << 8;
142
 
        return(result);
 
68
    boost::uint16_t read_le16()
 
69
    {
 
70
            boost::uint16_t result = static_cast<boost::uint16_t>(read8());
 
71
            result |= static_cast<boost::uint16_t>(read8()) << 8;
 
72
            return(result);
143
73
    }
144
74
 
145
75
    /// \brief Write a 32-bit word to a little-endian stream.
147
77
    /// TODO: define what happens when the stream
148
78
    ///       is in error condition, see get_error().
149
79
    ///
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)
 
81
    {
 
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));
155
86
    }
156
87
 
157
88
    /// \brief Write a 16-bit word to a little-endian stream.
159
90
    /// TODO: define what happens when the stream
160
91
    ///       is in error condition, see get_error().
161
92
    ///
162
 
    void        write_le16(boost::uint16_t u) {
163
 
        write8((boost::uint8_t)u);
164
 
        write8((boost::uint8_t)(u>>8));
 
93
    void write_le16(boost::uint16_t u)
 
94
    {
 
95
        write8(static_cast<boost::int8_t>(u));
 
96
        write8(static_cast<boost::int8_t>(u>>8));
165
97
    }
166
98
    
167
99
    /// \brief Read a single byte from the stream
169
101
    /// TODO: define what happens when the stream
170
102
    ///       is in error condition, see get_error().
171
103
    ///
172
 
    boost::uint8_t      read_byte() { return read8(); }
 
104
    boost::uint8_t read_byte() { return read8(); }
173
105
 
174
106
    /// \brief write a single byte to the stream
175
107
    //
176
108
    /// TODO: define what happens when the stream
177
109
    ///       is in error condition, see get_error().
178
110
    ///
179
 
    void        write_byte(boost::uint8_t u) { write8(u); }
 
111
    void write_byte(boost::uint8_t u) { write8(u); }
180
112
    
181
113
    /// \brief Read the given number of bytes from the stream
182
114
    //
183
115
    /// TODO: define what happens when the stream
184
116
    ///       is in error condition, see get_error().
185
117
    ///
186
 
    int         read_bytes(void* dst, int num) { return m_read(dst, num, m_data); }
 
118
    int read(void* dst, int num);
187
119
 
188
120
    /// \brief Write the given number of bytes to the stream
189
121
    //
190
122
    /// TODO: define what happens when the stream
191
123
    ///       is in error condition, see get_error().
192
124
    ///
193
 
    int         write_bytes(const void* src, int num) { return m_write(src, num, m_data); }
194
 
    
195
 
    /// \brief Write a 0-terminated string to a stream.
196
 
    //
197
 
    /// TODO: define what happens when the stream
198
 
    ///       is in error condition, see get_error().
199
 
    ///
200
 
    void write_string(const char* src);
201
 
    
202
 
    /// \brief
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.
205
 
    //
206
 
    /// Stops at the first \0 character if it comes before max_length.
207
 
    ///
208
 
    /// Guarantees termination of the string.
209
 
    ///
210
 
    /// @return ??
211
 
    ///
212
 
    /// TODO: define what to return when the stream
213
 
    ///       is in error condition, see get_error().
214
 
    ///
215
 
    int read_string(char* dst, int max_length);
216
 
    
217
 
    /// \brief Write a 32-bit float to a stream.
218
 
    //
219
 
    /// TODO: define what to return when the stream
220
 
    ///       is in error condition, see get_error().
221
 
    void        write_float32(float value);
222
 
 
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);
227
126
 
228
127
    /// \brief Return current stream position
229
128
    //
230
129
    /// TODO: define what to return when the stream
231
130
    ///       is in error condition, see get_error().
232
131
    ///
233
 
    int get_position() const { return m_tell(m_data); }
 
132
    int tell() const;
234
133
 
235
134
    /// \brief Seek to the specified position
236
135
    //
240
139
    ///
241
140
    /// @return 0 on success, or TU_FILE_SEEK_ERROR on failure.
242
141
    ///
243
 
    int set_position(int p) { return m_seek(p, m_data); }
 
142
    int seek(int p);
244
143
 
245
144
    /// \brief Seek to the end of the stream
246
145
    //
247
146
    /// TODO: define what happens when an error occurs
248
147
    ///
249
 
    void        go_to_end() { m_seek_to_end(m_data); }
 
148
    void go_to_end();
250
149
 
251
150
    /// \brief Return true if the end of the stream has been reached.
252
151
    //
253
152
    /// TODO: define what to return when in error condition
254
153
    /// see get_error().
255
154
    ///
256
 
    bool        get_eof() { return m_get_eof(m_data); }
 
155
    bool eof() const;
257
156
    
258
157
    /// \brief Return non-zero if the stream is in an error state
259
158
    //
263
162
    /// There are some rough meaning for possible returned values
264
163
    /// but I don't think they make much sense currently.
265
164
    ///
266
 
    int get_error() { return m_get_err(m_data); }
 
165
    int get_error() const;
267
166
    
268
167
 
269
168
    /// \brief Get the size of the stream
270
 
    int get_size() { return m_get_stream_size(m_data); }
271
 
 
272
 
    // \brief printf-style convenience function.
273
 
    int printf(const char* fmt, ...);
274
 
    
275
 
    // \brief UNSAFE back door, for testing only.
276
 
    void*       get_app_data_DEBUG() { return m_data; }
277
 
    
 
169
    int size() const;
278
170
    
279
171
private:
280
 
    boost::uint64_t     read64() {
281
 
        boost::uint64_t u;
282
 
        m_read(&u, 8, m_data);
283
 
        return u;
284
 
    }
285
 
    boost::uint32_t     read32() {
286
 
        boost::uint32_t u;
287
 
        m_read(&u, 4, m_data);
288
 
        return u;
289
 
    }
290
 
    boost::uint16_t     read16() {
291
 
        boost::uint16_t u;
292
 
        m_read(&u, 2, m_data);
293
 
        return u;
294
 
    }
295
 
    boost::uint8_t      read8() {
296
 
        boost::uint8_t u;
297
 
        m_read(&u, 1, m_data);
298
 
        return u;
299
 
    }
300
 
    
301
 
    void        write64(boost::uint64_t u) {
302
 
        m_write(&u, 8, m_data);
303
 
    }
304
 
    void        write32(boost::uint32_t u) {
305
 
        m_write(&u, 4, m_data);
306
 
    }
307
 
    void        write16(boost::uint16_t u) {
308
 
        m_write(&u, 2, m_data);
309
 
    }
310
 
    void        write8(boost::uint8_t u) {
311
 
        m_write(&u, 1, m_data);
312
 
    }
313
 
    
314
 
    void        close();
315
 
    
316
 
    void *              m_data;
317
 
    read_func           m_read;
318
 
    write_func          m_write;
319
 
    seek_func           m_seek;
320
 
    seek_to_end_func    m_seek_to_end;
321
 
    tell_func           m_tell;
322
 
    get_eof_func        m_get_eof;
323
 
    get_err_func        m_get_err;
324
 
    get_stream_size_func        m_get_stream_size;
325
 
    close_func          m_close;
 
172
 
 
173
    boost::uint64_t     read64()
 
174
    {
 
175
        boost::uint64_t u;
 
176
        read(&u, 8);
 
177
        return u;
 
178
    }
 
179
 
 
180
    boost::uint32_t     read32()
 
181
    {
 
182
        boost::uint32_t u;
 
183
        read(&u, 4);
 
184
        return u;
 
185
    }
 
186
 
 
187
    boost::uint16_t read16()
 
188
    {
 
189
        boost::uint16_t u;
 
190
        read(&u, 2);
 
191
        return u;
 
192
    }
 
193
    
 
194
    boost::uint8_t      read8()
 
195
    {
 
196
        boost::uint8_t u;
 
197
        read(&u, 1);
 
198
        return u;
 
199
    }
 
200
    
 
201
    void write64(boost::uint64_t u)
 
202
    {
 
203
        write(&u, 8);
 
204
    }
 
205
 
 
206
    void write32(boost::uint32_t u)
 
207
    {
 
208
        write(&u, 4);
 
209
    }
 
210
 
 
211
    void write16(boost::uint16_t u)
 
212
    {
 
213
        write(&u, 2);
 
214
    }
 
215
 
 
216
    void write8(boost::uint8_t u)
 
217
    {
 
218
        write(&u, 1);
 
219
    }
 
220
    
 
221
    void close();
 
222
    
 
223
    void *      m_data;
 
224
 
 
225
    bool _autoclose;
 
226
 
326
227
};
327
228
 
328
229
 
329
 
//
330
 
// Some inline stuff.
331
 
//
332
 
 
333
 
 
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)
338
 
{
339
 
    union alias {
340
 
        float   f;
341
 
        boost::uint32_t i;
342
 
    } u;
343
 
    compiler_assert(sizeof(alias) == sizeof(boost::uint32_t));
344
 
    
345
 
    u.f = value;
346
 
    write_le32(u.i);
347
 
}
348
 
 
349
 
 
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.
355
 
{
356
 
    union {
357
 
        float   f;
358
 
        boost::uint32_t i;
359
 
    } u;
360
 
    compiler_assert(sizeof(u) == sizeof(u.i));
361
 
    
362
 
    u.i = read_le32();
363
 
    return u.f;
364
 
}
365
 
 
 
230
} // namespace gnash
366
231
#endif // TU_FILE_H
367
232
 
368
233