annotate src/console/Data_Reader.cxx @ 1770:af8c3e5a9f61

warning cleanups
author William Pitcock <nenolod@atheme.org>
date Mon, 24 Sep 2007 11:17:39 -0500
parents 986f098da058
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
1 // File_Extractor 0.4.0. http://www.slack.net/~ant/
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
2
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
3 #include "Data_Reader.h"
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
4
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
5 #include "blargg_endian.h"
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
6 #include <assert.h>
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
7 #include <string.h>
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
8 #include <stdio.h>
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
9
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
10 /* Copyright (C) 2005-2006 Shay Green. This module is free software; you
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
11 can redistribute it and/or modify it under the terms of the GNU Lesser
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
12 General Public License as published by the Free Software Foundation; either
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
13 version 2.1 of the License, or (at your option) any later version. This
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
14 module is distributed in the hope that it will be useful, but WITHOUT ANY
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
17 details. You should have received a copy of the GNU Lesser General Public
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
18 License along with this module; if not, write to the Free Software Foundation,
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
20
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
21 #include "blargg_source.h"
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
22
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
23 const char Data_Reader::eof_error [] = "Unexpected end of file";
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
24
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
25 blargg_err_t Data_Reader::read( void* p, long s )
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
26 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
27 long result = read_avail( p, s );
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
28 if ( result != s )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
29 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
30 if ( result >= 0 && result < s )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
31 return eof_error;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
32
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
33 return "Read error";
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
34 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
35
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
36 return 0;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
37 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
38
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
39 blargg_err_t Data_Reader::skip( long count )
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
40 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
41 char buf [512];
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
42 while ( count )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
43 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
44 long n = sizeof buf;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
45 if ( n > count )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
46 n = count;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
47 count -= n;
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
48 RETURN_ERR( read( buf, n ) );
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
49 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
50 return 0;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
51 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
52
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
53 long File_Reader::remain() const { return size() - tell(); }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
54
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
55 blargg_err_t File_Reader::skip( long n )
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
56 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
57 assert( n >= 0 );
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
58 if ( !n )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
59 return 0;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
60 return seek( tell() + n );
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
61 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
62
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
63 // Subset_Reader
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
64
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
65 Subset_Reader::Subset_Reader( Data_Reader* dr, long size )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
66 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
67 in = dr;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
68 remain_ = dr->remain();
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
69 if ( remain_ > size )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
70 remain_ = size;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
71 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
72
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
73 long Subset_Reader::remain() const { return remain_; }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
74
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
75 long Subset_Reader::read_avail( void* p, long s )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
76 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
77 if ( s > remain_ )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
78 s = remain_;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
79 remain_ -= s;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
80 return in->read_avail( p, s );
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
81 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
82
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
83 // Remaining_Reader
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
84
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
85 Remaining_Reader::Remaining_Reader( void const* h, long size, Data_Reader* r )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
86 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
87 header = (char const*) h;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
88 header_end = header + size;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
89 in = r;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
90 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
91
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
92 long Remaining_Reader::remain() const { return header_end - header + in->remain(); }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
93
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
94 long Remaining_Reader::read_first( void* out, long count )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
95 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
96 long first = header_end - header;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
97 if ( first )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
98 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
99 if ( first > count )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
100 first = count;
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
101 void const* old = header;
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
102 header += first;
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
103 memcpy( out, old, first );
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
104 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
105 return first;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
106 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
107
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
108 long Remaining_Reader::read_avail( void* out, long count )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
109 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
110 long first = read_first( out, count );
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
111 long second = count - first;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
112 if ( second )
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
113 {
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
114 second = in->read_avail( (char*) out + first, second );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
115 if ( second <= 0 )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
116 return second;
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
117 }
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
118 return first + second;
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
119 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
120
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
121 blargg_err_t Remaining_Reader::read( void* out, long count )
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
122 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
123 long first = read_first( out, count );
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
124 long second = count - first;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
125 if ( !second )
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
126 return 0;
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
127 return in->read( (char*) out + first, second );
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
128 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
129
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
130 // Mem_File_Reader
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
131
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
132 Mem_File_Reader::Mem_File_Reader( const void* p, long s ) :
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
133 begin( (const char*) p ),
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
134 size_( s )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
135 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
136 pos = 0;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
137 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
138
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
139 long Mem_File_Reader::size() const { return size_; }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
140
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
141 long Mem_File_Reader::read_avail( void* p, long s )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
142 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
143 long r = remain();
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
144 if ( s > r )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
145 s = r;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
146 memcpy( p, begin + pos, s );
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
147 pos += s;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
148 return s;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
149 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
150
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
151 long Mem_File_Reader::tell() const { return pos; }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
152
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
153 blargg_err_t Mem_File_Reader::seek( long n )
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
154 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
155 if ( n > size_ )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
156 return eof_error;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
157 pos = n;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
158 return 0;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
159 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
160
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
161 // Callback_Reader
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
162
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
163 Callback_Reader::Callback_Reader( callback_t c, long size, void* d ) :
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
164 callback( c ),
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
165 data( d )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
166 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
167 remain_ = size;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
168 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
169
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
170 long Callback_Reader::remain() const { return remain_; }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
171
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
172 long Callback_Reader::read_avail( void* out, long count )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
173 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
174 if ( count > remain_ )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
175 count = remain_;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
176 if ( Callback_Reader::read( out, count ) )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
177 count = -1;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
178 return count;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
179 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
180
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
181 blargg_err_t Callback_Reader::read( void* out, long count )
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
182 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
183 if ( count > remain_ )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
184 return eof_error;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
185 return callback( data, out, count );
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
186 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
187
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
188 // Std_File_Reader
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
189
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
190 Std_File_Reader::Std_File_Reader() : file_( 0 ) { }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
191
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
192 Std_File_Reader::~Std_File_Reader() { close(); }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
193
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
194 blargg_err_t Std_File_Reader::open( const char* path )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
195 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
196 file_ = fopen( path, "rb" );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
197 if ( !file_ )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
198 return "Couldn't open file";
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
199 return 0;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
200 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
201
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
202 long Std_File_Reader::size() const
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
203 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
204 long pos = tell();
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
205 fseek( (FILE*) file_, 0, SEEK_END );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
206 long result = tell();
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
207 fseek( (FILE*) file_, pos, SEEK_SET );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
208 return result;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
209 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
210
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
211 long Std_File_Reader::read_avail( void* p, long s )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
212 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
213 return fread( p, 1, s, (FILE*) file_ );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
214 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
215
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
216 blargg_err_t Std_File_Reader::read( void* p, long s )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
217 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
218 if ( s == (long) fread( p, 1, s, (FILE*) file_ ) )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
219 return 0;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
220 if ( feof( (FILE*) file_ ) )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
221 return eof_error;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
222 return "Couldn't read from file";
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
223 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
224
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
225 long Std_File_Reader::tell() const { return ftell( (FILE*) file_ ); }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
226
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
227 blargg_err_t Std_File_Reader::seek( long n )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
228 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
229 if ( !fseek( (FILE*) file_, n, SEEK_SET ) )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
230 return 0;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
231 if ( n > size() )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
232 return eof_error;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
233 return "Error seeking in file";
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
234 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
235
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
236 void Std_File_Reader::close()
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
237 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
238 if ( file_ )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
239 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
240 fclose( (FILE*) file_ );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
241 file_ = 0;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
242 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
243 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
244
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
245 // Gzip_File_Reader
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
246
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
247 #ifdef HAVE_ZLIB_H
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
248
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
249 #include "zlib.h"
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
250
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
251 static const char* get_gzip_eof( const char* path, long* eof )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
252 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
253 FILE* file = fopen( path, "rb" );
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
254 if ( !file )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
255 return "Couldn't open file";
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
256
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
257 unsigned char buf [4];
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
258 if ( fread( buf, 2, 1, file ) > 0 && buf [0] == 0x1F && buf [1] == 0x8B )
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
259 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
260 fseek( file, -4, SEEK_END );
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
261 fread( buf, 4, 1, file );
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
262 *eof = get_le32( buf );
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
263 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
264 else
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
265 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
266 fseek( file, 0, SEEK_END );
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
267 *eof = ftell( file );
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
268 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
269 const char* err = (ferror( file ) || feof( file )) ? "Couldn't get file size" : 0;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
270 fclose( file );
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
271 return err;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
272 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
273
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
274 Gzip_File_Reader::Gzip_File_Reader() : file_( 0 ) { }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
275
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
276 Gzip_File_Reader::~Gzip_File_Reader() { close(); }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
277
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
278 blargg_err_t Gzip_File_Reader::open( const char* path )
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
279 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
280 close();
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
281
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
282 RETURN_ERR( get_gzip_eof( path, &size_ ) );
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
283
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
284 file_ = gzopen( path, "rb" );
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
285 if ( !file_ )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
286 return "Couldn't open file";
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
287
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
288 return 0;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
289 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
290
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
291 long Gzip_File_Reader::size() const { return size_; }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
292
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
293 long Gzip_File_Reader::read_avail( void* p, long s ) { return gzread( file_, p, s ); }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
294
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
295 long Gzip_File_Reader::tell() const { return gztell( file_ ); }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
296
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents: 316
diff changeset
297 blargg_err_t Gzip_File_Reader::seek( long n )
316
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
298 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
299 if ( gzseek( file_, n, SEEK_SET ) >= 0 )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
300 return 0;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
301 if ( n > size_ )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
302 return eof_error;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
303 return "Error seeking in file";
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
304 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
305
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
306 void Gzip_File_Reader::close()
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
307 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
308 if ( file_ )
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
309 {
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
310 gzclose( file_ );
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
311 file_ = 0;
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
312 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
313 }
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
314
fb513e10174e [svn] - merge libconsole-blargg into mainline libconsole:
nenolod
parents:
diff changeset
315 #endif