/
rb_physfs_file.c
226 lines (182 loc) · 4.66 KB
/
rb_physfs_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* PhysicsFS File abstraction - ruby interface
*
* Author:: Ed Sinjiashvili (slimb@vlinkmail.com)
* License:: LGPL
*/
#include "physfs.h"
#include "ruby.h"
#include "rb_physfs.h"
#include "rb_physfs_file.h"
#include "physfsrwops.h"
VALUE classPhysfsFile;
/*
* construct new PhysicsFS::File object
*/
VALUE physfs_file_new (PHYSFS_file *file)
{
if (file == 0)
return Qnil;
return Data_Wrap_Struct (classPhysfsFile, 0, 0, file);
}
/*
* PhysicsFS::File#close
*
* Close the file. It's illegal to use the object after its closure.
*/
VALUE physfs_file_close (VALUE self)
{
int result;
PHYSFS_file *file;
Data_Get_Struct (self, PHYSFS_file, file);
if (file == 0)
return Qfalse;
result = PHYSFS_close (file);
DATA_PTR(self) = 0;
if (result)
return Qtrue;
return Qfalse;
}
/*
* PhysicsFS::File#read obj_size, num_objects
*
* Read *objCount* objects which are *objSize* each.
* return String instance containing raw data or nil if failure.
* #length of string will reflect real number of objects read.
*/
VALUE physfs_file_read (VALUE self, VALUE objSize, VALUE objCount)
{
int objRead;
void *buffer;
VALUE result;
PHYSFS_file *file;
Data_Get_Struct (self, PHYSFS_file, file);
if (file == 0)
return Qnil; //wasted file - no read possible
buffer = malloc (FIX2UINT(objSize) * FIX2UINT(objCount));
if (buffer == 0)
return Qnil;
objRead = PHYSFS_read (file, buffer, FIX2UINT(objSize), FIX2UINT(objCount));
if (objRead == -1)
{
free (buffer);
return Qnil;
}
result = rb_str_new (buffer, objRead * FIX2UINT(objSize));
free (buffer);
return result;
}
/*
* PhysicsFS::File#write buffer, obj_size, num_objects
*
* return nil on failure or number of objects written.
*/
VALUE physfs_file_write (VALUE self, VALUE buf, VALUE objSize, VALUE objCount)
{
int result;
PHYSFS_file *file;
Data_Get_Struct (self, PHYSFS_file, file);
if (file == 0)
return Qnil;
result = PHYSFS_write (file, STR2CSTR(buf),
FIX2UINT(objSize), FIX2UINT(objCount));
if (result == -1)
return Qnil;
return INT2FIX(result);
}
/*
* PhysicsFS::File#eof?
*/
VALUE physfs_file_eof (VALUE self)
{
int result;
PHYSFS_file *file;
Data_Get_Struct (self, PHYSFS_file, file);
if (file == 0)
return Qnil;
result = PHYSFS_eof (file);
if (result)
return Qtrue;
return Qfalse;
}
/*
* PhysicsFS::File#tell
*
* tells current position in file
*/
VALUE physfs_file_tell (VALUE self)
{
int result;
PHYSFS_file *file;
Data_Get_Struct (self, PHYSFS_file, file);
if (file == 0)
return Qnil;
result = PHYSFS_tell (file);
if (result == -1)
return Qnil;
return INT2FIX(result);
}
/*
* PhysicsFS::File#seek pos
*
* seek to pos in file
*/
VALUE physfs_file_seek (VALUE self, VALUE pos)
{
int result;
PHYSFS_file *file;
Data_Get_Struct (self, PHYSFS_file, file);
if (file == 0)
return Qnil;
result = PHYSFS_seek (file, FIX2LONG(pos));
if (result)
return Qtrue;
return Qfalse;
}
/*
* PhysicsFS::File#length
*/
VALUE physfs_file_length (VALUE self)
{
int result;
PHYSFS_file *file;
Data_Get_Struct (self, PHYSFS_file, file);
if (file == 0)
return Qnil;
result = PHYSFS_fileLength (file);
if (result == -1)
return Qnil;
return INT2FIX(result);
}
/*
* PhysicsFS::File#to_rwops
*
* File object is converted to RWops object.
* File object becomes unusable after that - every operation
* should be done through new-born RWops object.
*/
VALUE physfs_file_to_rwops (VALUE self)
{
PHYSFS_file *file;
SDL_RWops *rwops;
Data_Get_Struct (self, PHYSFS_file, file);
if (file == 0)
return Qnil;
rwops = PHYSFSRWOPS_makeRWops (file);
if (rwops == 0)
return Qnil;
DATA_PTR(self) = 0; // oh, gosh, we've sacrificed ourselves!
return sdl_rwops_new (rwops);
}
void init_physfs_file (void)
{
classPhysfsFile = rb_define_class_under (modulePhysfs, "File", rb_cObject);
rb_define_method (classPhysfsFile, "close", physfs_file_close, 0);
rb_define_method (classPhysfsFile, "eof?", physfs_file_eof, 0);
rb_define_method (classPhysfsFile, "tell", physfs_file_tell, 0);
rb_define_method (classPhysfsFile, "seek", physfs_file_seek, 1);
rb_define_method (classPhysfsFile, "length", physfs_file_length, 0);
rb_define_method (classPhysfsFile, "read", physfs_file_read, 2);
rb_define_method (classPhysfsFile, "write", physfs_file_write, 3);
rb_define_method (classPhysfsFile, "to_rwops", physfs_file_to_rwops, 0);
}