comparison src/console/Data_Reader.h @ 316:fb513e10174e trunk

[svn] - merge libconsole-blargg into mainline libconsole: + obsoletes plugins-ugly:sapplug
author nenolod
date Thu, 30 Nov 2006 19:54:33 -0800
parents
children 986f098da058
comparison
equal deleted inserted replaced
315:2294f3a6f136 316:fb513e10174e
1 // Data reader interface for uniform access
2
3 // File_Extractor 0.4.0
4 #ifndef DATA_READER_H
5 #define DATA_READER_H
6
7 #undef DATA_READER_H
8 // allow blargg_config.h to #include Data_Reader.h
9 #include "blargg_config.h"
10 #ifndef DATA_READER_H
11 #define DATA_READER_H
12
13 // Supports reading and finding out how many bytes are remaining
14 class Data_Reader {
15 public:
16 Data_Reader() { }
17 virtual ~Data_Reader() { }
18
19 static const char eof_error []; // returned by read() when request goes beyond end
20
21 typedef const char* error_t; // NULL if successful
22
23 // Read at most count bytes and return number actually read, or <= 0 if error
24 virtual long read_avail( void*, long n ) = 0;
25
26 // Read exactly count bytes and return error if they couldn't be read
27 virtual error_t read( void*, long count );
28
29 // Number of bytes remaining until end of file
30 virtual long remain() const = 0;
31
32 // Read and discard count bytes
33 virtual error_t skip( long count );
34
35 private:
36 // noncopyable
37 Data_Reader( const Data_Reader& );
38 Data_Reader& operator = ( const Data_Reader& );
39 };
40
41 // Supports seeking in addition to Data_Reader operations
42 class File_Reader : public Data_Reader {
43 public:
44 // Size of file
45 virtual long size() const = 0;
46
47 // Current position in file
48 virtual long tell() const = 0;
49
50 // Go to new position
51 virtual error_t seek( long ) = 0;
52
53 long remain() const;
54 error_t skip( long n );
55 };
56
57 // Disk file reader
58 class Std_File_Reader : public File_Reader {
59 public:
60 error_t open( const char* path );
61 void close();
62
63 public:
64 Std_File_Reader();
65 ~Std_File_Reader();
66 long size() const;
67 error_t read( void*, long );
68 long read_avail( void*, long );
69 long tell() const;
70 error_t seek( long );
71 private:
72 void* file_;
73 };
74
75 // Treats range of memory as a file
76 class Mem_File_Reader : public File_Reader {
77 public:
78 Mem_File_Reader( const void*, long size );
79
80 public:
81 long size() const;
82 long read_avail( void*, long );
83 long tell() const;
84 error_t seek( long );
85 private:
86 const char* const begin;
87 const long size_;
88 long pos;
89 };
90
91 // Makes it look like there are only count bytes remaining
92 class Subset_Reader : public Data_Reader {
93 public:
94 Subset_Reader( Data_Reader*, long count );
95
96 public:
97 long remain() const;
98 long read_avail( void*, long );
99 private:
100 Data_Reader* in;
101 long remain_;
102 };
103
104 // Joins already-read header and remaining data into original file (to avoid seeking)
105 class Remaining_Reader : public Data_Reader {
106 public:
107 Remaining_Reader( void const* header, long size, Data_Reader* );
108
109 public:
110 long remain() const;
111 long read_avail( void*, long );
112 error_t read( void*, long );
113 private:
114 char const* header;
115 char const* header_end;
116 Data_Reader* in;
117 long read_first( void* out, long count );
118 };
119
120 // Invokes callback function to read data. Size of data must be specified in advance.
121 class Callback_Reader : public Data_Reader {
122 public:
123 typedef error_t (*callback_t)( void* data, void* out, long count );
124 Callback_Reader( callback_t, long size, void* data = 0 );
125 public:
126 long read_avail( void*, long );
127 error_t read( void*, long );
128 long remain() const;
129 private:
130 callback_t const callback;
131 void* const data;
132 long remain_;
133 };
134
135 #ifdef HAVE_ZLIB_H
136 // Gzip compressed file reader
137 class Gzip_File_Reader : public File_Reader {
138 public:
139 error_t open( const char* path );
140 void close();
141
142 public:
143 Gzip_File_Reader();
144 ~Gzip_File_Reader();
145 long size() const;
146 long read_avail( void*, long );
147 long tell() const;
148 error_t seek( long );
149 private:
150 void* file_;
151 long size_;
152 };
153 #endif
154
155 #endif
156 #endif