Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/quicknes/fex/Data_Reader.h
2 views
1
// Lightweight interface for reading data from byte stream
2
3
// File_Extractor 1.0.0
4
#ifndef DATA_READER_H
5
#define DATA_READER_H
6
7
#include "blargg_common.h"
8
9
/* Some functions accept a long instead of int for convenience where caller has
10
a long due to some other interface, and would otherwise have to get a warning,
11
or cast it (and verify that it wasn't outside the range of an int).
12
13
To really support huge (>2GB) files, long isn't a solution, since there's no
14
guarantee it's more than 32 bits. We'd need to use long long (if available), or
15
something compiler-specific, and change all places file sizes or offsets are
16
used. */
17
18
// Supports reading and finding out how many bytes are remaining
19
class Data_Reader {
20
public:
21
22
// Reads min(*n,remain()) bytes and sets *n to this number, thus trying to read more
23
// tham remain() bytes doesn't result in error, just *n being set to remain().
24
blargg_err_t read_avail( void* p, int* n );
25
blargg_err_t read_avail( void* p, long* n );
26
27
// Reads exactly n bytes, or returns error if they couldn't ALL be read.
28
// Reading past end of file results in blargg_err_file_eof.
29
blargg_err_t read( void* p, int n );
30
31
// Number of bytes remaining until end of file
32
BOOST::uint64_t remain() const { return remain_; }
33
34
// Reads and discards n bytes. Skipping past end of file results in blargg_err_file_eof.
35
blargg_err_t skip( int n );
36
37
virtual ~Data_Reader() { }
38
39
private:
40
// noncopyable
41
Data_Reader( const Data_Reader& );
42
Data_Reader& operator = ( const Data_Reader& );
43
44
// Derived interface
45
protected:
46
Data_Reader() : remain_( 0 ) { }
47
48
// Sets remain
49
void set_remain( BOOST::uint64_t n ) { assert( n >= 0 ); remain_ = n; }
50
51
// Do same as read(). Guaranteed that 0 < n <= remain(). Value of remain() is updated
52
// AFTER this call succeeds, not before. set_remain() should NOT be called from this.
53
virtual blargg_err_t read_v( void*, int n ) BLARGG_PURE( { (void)n; return blargg_ok; } )
54
55
// Do same as skip(). Guaranteed that 0 < n <= remain(). Default just reads data
56
// and discards it. Value of remain() is updated AFTER this call succeeds, not
57
// before. set_remain() should NOT be called from this.
58
virtual blargg_err_t skip_v( int n );
59
60
// Implementation
61
public:
62
BLARGG_DISABLE_NOTHROW
63
64
private:
65
BOOST::uint64_t remain_;
66
};
67
68
69
// Supports seeking in addition to Data_Reader operations
70
class File_Reader : public Data_Reader {
71
public:
72
73
// Size of file
74
BOOST::uint64_t size() const { return size_; }
75
76
// Current position in file
77
BOOST::uint64_t tell() const { return size_ - remain(); }
78
79
// Goes to new position
80
blargg_err_t seek( BOOST::uint64_t );
81
82
// Derived interface
83
protected:
84
// Sets size and resets position
85
void set_size( BOOST::uint64_t n ) { size_ = n; Data_Reader::set_remain( n ); }
86
void set_size( int n ) { set_size( STATIC_CAST(BOOST::uint64_t, n) ); }
87
void set_size( long n ) { set_size( STATIC_CAST(BOOST::uint64_t, n) ); }
88
89
// Sets reported position
90
void set_tell( BOOST::uint64_t i ) { assert( 0 <= i && i <= size_ ); Data_Reader::set_remain( size_ - i ); }
91
92
// Do same as seek(). Guaranteed that 0 <= n <= size(). Value of tell() is updated
93
// AFTER this call succeeds, not before. set_* functions should NOT be called from this.
94
virtual blargg_err_t seek_v( BOOST::uint64_t n ) BLARGG_PURE( { (void)n; return blargg_ok; } )
95
96
// Implementation
97
protected:
98
File_Reader() : size_( 0 ) { }
99
100
virtual blargg_err_t skip_v( BOOST::uint64_t );
101
102
private:
103
BOOST::uint64_t size_;
104
105
void set_remain(); // avoid accidental use of set_remain
106
};
107
108
109
// Reads from file on disk
110
class Std_File_Reader : public File_Reader {
111
public:
112
113
// Opens file
114
blargg_err_t open( const char path [] );
115
116
// Closes file if one was open
117
void close();
118
119
// Switches to unbuffered mode. Useful if buffering is already being
120
// done at a higher level.
121
void make_unbuffered();
122
123
// Implementation
124
public:
125
Std_File_Reader();
126
virtual ~Std_File_Reader();
127
128
protected:
129
virtual blargg_err_t read_v( void*, int );
130
virtual blargg_err_t seek_v( BOOST::uint64_t );
131
132
private:
133
void* file_;
134
};
135
136
137
// Treats range of memory as a file
138
class Mem_File_Reader : public File_Reader {
139
public:
140
141
Mem_File_Reader( const void* begin, long size );
142
143
// Implementation
144
protected:
145
virtual blargg_err_t read_v( void*, int );
146
virtual blargg_err_t seek_v( int );
147
148
private:
149
const char* const begin;
150
};
151
152
153
// Allows only count bytes to be read from reader passed
154
class Subset_Reader : public Data_Reader {
155
public:
156
157
Subset_Reader( Data_Reader*, BOOST::uint64_t count );
158
159
// Implementation
160
protected:
161
virtual blargg_err_t read_v( void*, int );
162
163
private:
164
Data_Reader* const in;
165
};
166
167
168
// Joins already-read header and remaining data into original file.
169
// Meant for cases where you've already read header and don't want
170
// to seek and re-read data (for efficiency).
171
class Remaining_Reader : public Data_Reader {
172
public:
173
174
Remaining_Reader( void const* header, int header_size, Data_Reader* );
175
176
// Implementation
177
protected:
178
virtual blargg_err_t read_v( void*, int );
179
180
private:
181
Data_Reader* const in;
182
void const* header;
183
int header_remain;
184
};
185
186
187
// Invokes callback function to read data
188
extern "C" { // necessary to be usable from C
189
typedef const char* (*callback_reader_func_t)(
190
void* user_data, // Same value passed to constructor
191
void* out, // Buffer to place data into
192
int count // Number of bytes to read
193
);
194
}
195
class Callback_Reader : public Data_Reader {
196
public:
197
typedef callback_reader_func_t callback_t;
198
Callback_Reader( callback_t, BOOST::uint64_t size, void* user_data );
199
200
// Implementation
201
protected:
202
virtual blargg_err_t read_v( void*, int );
203
204
private:
205
callback_t const callback;
206
void* const user_data;
207
};
208
209
210
// Invokes callback function to read data
211
extern "C" { // necessary to be usable from C
212
typedef const char* (*callback_file_reader_func_t)(
213
void* user_data, // Same value passed to constructor
214
void* out, // Buffer to place data into
215
int count, // Number of bytes to read
216
BOOST::uint64_t pos // Position in file to read from
217
);
218
}
219
class Callback_File_Reader : public File_Reader {
220
public:
221
typedef callback_file_reader_func_t callback_t;
222
Callback_File_Reader( callback_t, BOOST::uint64_t size, void* user_data );
223
224
// Implementation
225
protected:
226
virtual blargg_err_t read_v( void*, int );
227
virtual blargg_err_t seek_v( int );
228
229
private:
230
callback_t const callback;
231
void* const user_data;
232
};
233
234
235
#ifdef HAVE_ZLIB_H
236
237
// Reads file compressed with gzip (or uncompressed)
238
class Gzip_File_Reader : public File_Reader {
239
public:
240
241
// Opens possibly gzipped file
242
blargg_err_t open( const char path [] );
243
244
// Closes file if one was open
245
void close();
246
247
// Implementation
248
public:
249
Gzip_File_Reader();
250
~Gzip_File_Reader();
251
252
protected:
253
virtual blargg_err_t read_v( void*, int );
254
virtual blargg_err_t seek_v( int );
255
256
private:
257
// void* so "zlib.h" doesn't have to be included here
258
void* file_;
259
};
260
#endif
261
262
char* blargg_to_utf8( const blargg_wchar_t* );
263
blargg_wchar_t* blargg_to_wide( const char* );
264
265
#endif
266
267