Mercurial > audlegacy
comparison Plugins/Input/console/abstract_file.cpp @ 493:c04dff121e1d trunk
[svn] hostile merge, phase 2: reimport based on new plugin code
author | nenolod |
---|---|
date | Tue, 24 Jan 2006 20:19:01 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
492:ccb68bad47b2 | 493:c04dff121e1d |
---|---|
1 | |
2 #include "abstract_file.h" | |
3 | |
4 #include <assert.h> | |
5 #include <string.h> | |
6 #include <stddef.h> | |
7 #include <stdlib.h> | |
8 | |
9 /* Copyright (C) 2005 Shay Green. Permission is hereby granted, free of | |
10 charge, to any person obtaining a copy of this software module and associated | |
11 documentation files (the "Software"), to deal in the Software without | |
12 restriction, including without limitation the rights to use, copy, modify, | |
13 merge, publish, distribute, sublicense, and/or sell copies of the Software, and | |
14 to permit persons to whom the Software is furnished to do so, subject to the | |
15 following conditions: The above copyright notice and this permission notice | |
16 shall be included in all copies or substantial portions of the Software. THE | |
17 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
18 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
19 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
20 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
21 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | |
23 | |
24 // to do: remove? | |
25 #ifndef RAISE_ERROR | |
26 #define RAISE_ERROR( str ) return str | |
27 #endif | |
28 | |
29 typedef Data_Reader::error_t error_t; | |
30 | |
31 error_t Data_Writer::write( const void*, long ) { return NULL; } | |
32 | |
33 void Data_Writer::satisfy_lame_linker_() { } | |
34 | |
35 error_t Data_Reader::read( void* p, long s ) | |
36 { | |
37 long result = read_avail( p, s ); | |
38 if ( result != s ) | |
39 { | |
40 if ( result >= 0 && result < s ) | |
41 RAISE_ERROR( "Unexpected end-of-file" ); | |
42 | |
43 RAISE_ERROR( "Read error" ); | |
44 } | |
45 | |
46 return NULL; | |
47 } | |
48 | |
49 error_t Data_Reader::skip( long count ) | |
50 { | |
51 char buf [512]; | |
52 while ( count ) | |
53 { | |
54 int n = sizeof buf; | |
55 if ( n > count ) | |
56 n = count; | |
57 count -= n; | |
58 RAISE_ERROR( read( buf, n ) ); | |
59 } | |
60 return NULL; | |
61 } | |
62 | |
63 long File_Reader::remain() const | |
64 { | |
65 return size() - tell(); | |
66 } | |
67 | |
68 error_t File_Reader::skip( long n ) | |
69 { | |
70 assert( n >= 0 ); | |
71 if ( n ) | |
72 RAISE_ERROR( seek( tell() + n ) ); | |
73 | |
74 return NULL; | |
75 } | |
76 | |
77 | |
78 // Subset_Reader | |
79 | |
80 Subset_Reader::Subset_Reader( Data_Reader* in_, long size ) : | |
81 in( in_ ), | |
82 remain_( in_->remain() ) | |
83 { | |
84 if ( remain_ > size ) | |
85 remain_ = size; | |
86 } | |
87 | |
88 long Subset_Reader::remain() const { | |
89 return remain_; | |
90 } | |
91 | |
92 long Subset_Reader::read_avail( void* p, long s ) | |
93 { | |
94 if ( s > remain_ ) | |
95 s = remain_; | |
96 remain_ -= s; | |
97 return in->read_avail( p, s ); | |
98 } | |
99 | |
100 // Mem_File_Reader | |
101 | |
102 Mem_File_Reader::Mem_File_Reader( const void* p, long s ) : | |
103 begin( (const char*) p ), | |
104 pos( 0 ), | |
105 size_( s ) | |
106 { | |
107 } | |
108 | |
109 long Mem_File_Reader::size() const { | |
110 return size_; | |
111 } | |
112 | |
113 long Mem_File_Reader::read_avail( void* p, long s ) | |
114 { | |
115 long r = remain(); | |
116 if ( s > r ) | |
117 s = r; | |
118 memcpy( p, begin + pos, s ); | |
119 pos += s; | |
120 return s; | |
121 } | |
122 | |
123 long Mem_File_Reader::tell() const { | |
124 return pos; | |
125 } | |
126 | |
127 error_t Mem_File_Reader::seek( long n ) | |
128 { | |
129 if ( n > size_ ) | |
130 RAISE_ERROR( "Tried to go past end of file" ); | |
131 pos = n; | |
132 return NULL; | |
133 } | |
134 | |
135 // Std_File_Reader | |
136 | |
137 Std_File_Reader::Std_File_Reader() : file_( NULL ) { | |
138 } | |
139 | |
140 Std_File_Reader::~Std_File_Reader() { | |
141 close(); | |
142 } | |
143 | |
144 error_t Std_File_Reader::open( const char* path ) | |
145 { | |
146 file_ = fopen( path, "rb" ); | |
147 if ( !file_ ) | |
148 RAISE_ERROR( "Couldn't open file" ); | |
149 return NULL; | |
150 } | |
151 | |
152 long Std_File_Reader::size() const | |
153 { | |
154 long pos = tell(); | |
155 fseek( file_, 0, SEEK_END ); | |
156 long result = tell(); | |
157 fseek( file_, pos, SEEK_SET ); | |
158 return result; | |
159 } | |
160 | |
161 long Std_File_Reader::read_avail( void* p, long s ) { | |
162 return (long) fread( p, 1, s, file_ ); | |
163 } | |
164 | |
165 long Std_File_Reader::tell() const { | |
166 return ftell( file_ ); | |
167 } | |
168 | |
169 error_t Std_File_Reader::seek( long n ) | |
170 { | |
171 if ( fseek( file_, n, SEEK_SET ) != 0 ) | |
172 RAISE_ERROR( "Error seeking in file" ); | |
173 return NULL; | |
174 } | |
175 | |
176 void Std_File_Reader::close() | |
177 { | |
178 if ( file_ ) { | |
179 fclose( file_ ); | |
180 file_ = NULL; | |
181 } | |
182 } | |
183 | |
184 // Std_File_Writer | |
185 | |
186 Std_File_Writer::Std_File_Writer() : file_( NULL ) { | |
187 } | |
188 | |
189 Std_File_Writer::~Std_File_Writer() { | |
190 close(); | |
191 } | |
192 | |
193 error_t Std_File_Writer::open( const char* path ) | |
194 { | |
195 file_ = fopen( path, "wb" ); | |
196 if ( !file_ ) | |
197 RAISE_ERROR( "Couldn't open file for writing" ); | |
198 | |
199 // to do: increase file buffer size | |
200 //setvbuf( file_, NULL, _IOFBF, 32 * 1024L ); | |
201 | |
202 return NULL; | |
203 } | |
204 | |
205 error_t Std_File_Writer::write( const void* p, long s ) | |
206 { | |
207 long result = (long) fwrite( p, 1, s, file_ ); | |
208 if ( result != s ) | |
209 RAISE_ERROR( "Couldn't write to file" ); | |
210 return NULL; | |
211 } | |
212 | |
213 void Std_File_Writer::close() | |
214 { | |
215 if ( file_ ) { | |
216 fclose( file_ ); | |
217 file_ = NULL; | |
218 } | |
219 } | |
220 | |
221 // Mem_Writer | |
222 | |
223 Mem_Writer::Mem_Writer( void* p, long s, int b ) | |
224 { | |
225 data_ = (char*) p; | |
226 size_ = 0; | |
227 allocated = s; | |
228 mode = b ? ignore_excess : fixed; | |
229 } | |
230 | |
231 Mem_Writer::Mem_Writer() | |
232 { | |
233 data_ = NULL; | |
234 size_ = 0; | |
235 allocated = 0; | |
236 mode = expanding; | |
237 } | |
238 | |
239 Mem_Writer::~Mem_Writer() | |
240 { | |
241 if ( mode == expanding ) | |
242 free( data_ ); | |
243 } | |
244 | |
245 error_t Mem_Writer::write( const void* p, long s ) | |
246 { | |
247 long remain = allocated - size_; | |
248 if ( s > remain ) | |
249 { | |
250 if ( mode == fixed ) | |
251 RAISE_ERROR( "Tried to write more data than expected" ); | |
252 | |
253 if ( mode == ignore_excess ) | |
254 { | |
255 s = remain; | |
256 } | |
257 else // expanding | |
258 { | |
259 long new_allocated = size_ + s; | |
260 new_allocated += (new_allocated >> 1) + 2048; | |
261 void* p = realloc( data_, new_allocated ); | |
262 if ( !p ) | |
263 RAISE_ERROR( "Out of memory" ); | |
264 data_ = (char*) p; | |
265 allocated = new_allocated; | |
266 } | |
267 } | |
268 | |
269 assert( size_ + s <= allocated ); | |
270 memcpy( data_ + size_, p, s ); | |
271 size_ += s; | |
272 | |
273 return NULL; | |
274 } | |
275 | |
276 // Null_Writer | |
277 | |
278 error_t Null_Writer::write( const void*, long ) | |
279 { | |
280 return NULL; | |
281 } | |
282 |