2 * PhysicsFS File abstraction - ruby interface
4 * Author:: Ed Sinjiashvili (slimb@vlinkmail.com)
11 #include "rb_physfs.h"
12 #include "rb_physfs_file.h"
13 #include "physfsrwops.h"
15 VALUE classPhysfsFile;
18 * construct new PhysicsFS::File object
20 VALUE physfs_file_new (PHYSFS_File *file)
25 return Data_Wrap_Struct (classPhysfsFile, 0, 0, file);
29 * PhysicsFS::File#close
31 * Close the file. It's illegal to use the object after its closure.
33 VALUE physfs_file_close (VALUE self)
37 Data_Get_Struct (self, PHYSFS_File, file);
42 result = PHYSFS_close (file);
51 * PhysicsFS::File#read obj_size, num_objects
53 * Read *objCount* objects which are *objSize* each.
54 * return String instance containing raw data or nil if failure.
55 * #length of string will reflect real number of objects read.
57 VALUE physfs_file_read (VALUE self, VALUE objSize, VALUE objCount)
64 Data_Get_Struct (self, PHYSFS_File, file);
66 return Qnil; //wasted file - no read possible
68 buffer = malloc (FIX2UINT(objSize) * FIX2UINT(objCount));
72 objRead = PHYSFS_read (file, buffer, FIX2UINT(objSize), FIX2UINT(objCount));
79 result = rb_str_new (buffer, objRead * FIX2UINT(objSize));
85 * PhysicsFS::File#write buffer, obj_size, num_objects
87 * return nil on failure or number of objects written.
89 VALUE physfs_file_write (VALUE self, VALUE buf, VALUE objSize, VALUE objCount)
94 Data_Get_Struct (self, PHYSFS_File, file);
98 result = PHYSFS_write (file, STR2CSTR(buf),
99 FIX2UINT(objSize), FIX2UINT(objCount));
103 return INT2FIX(result);
107 * PhysicsFS::File#eof?
109 VALUE physfs_file_eof (VALUE self)
114 Data_Get_Struct (self, PHYSFS_File, file);
118 result = PHYSFS_eof (file);
127 * PhysicsFS::File#tell
129 * tells current position in file
131 VALUE physfs_file_tell (VALUE self)
136 Data_Get_Struct (self, PHYSFS_File, file);
140 result = PHYSFS_tell (file);
145 return INT2FIX(result);
149 * PhysicsFS::File#seek pos
151 * seek to pos in file
153 VALUE physfs_file_seek (VALUE self, VALUE pos)
158 Data_Get_Struct (self, PHYSFS_File, file);
162 result = PHYSFS_seek (file, FIX2LONG(pos));
171 * PhysicsFS::File#length
173 VALUE physfs_file_length (VALUE self)
178 Data_Get_Struct (self, PHYSFS_File, file);
182 result = PHYSFS_fileLength (file);
187 return INT2FIX(result);
191 * PhysicsFS::File#to_rwops
193 * File object is converted to RWops object.
194 * File object becomes unusable after that - every operation
195 * should be done through new-born RWops object.
197 VALUE physfs_file_to_rwops (VALUE self)
202 Data_Get_Struct (self, PHYSFS_File, file);
206 rwops = PHYSFSRWOPS_makeRWops (file);
210 DATA_PTR(self) = 0; // oh, gosh, we've sacrificed ourselves!
211 return sdl_rwops_new (rwops);
214 void init_physfs_file (void)
216 classPhysfsFile = rb_define_class_under (modulePhysfs, "File", rb_cObject);
218 rb_define_method (classPhysfsFile, "close", physfs_file_close, 0);
219 rb_define_method (classPhysfsFile, "eof?", physfs_file_eof, 0);
220 rb_define_method (classPhysfsFile, "tell", physfs_file_tell, 0);
221 rb_define_method (classPhysfsFile, "seek", physfs_file_seek, 1);
222 rb_define_method (classPhysfsFile, "length", physfs_file_length, 0);
223 rb_define_method (classPhysfsFile, "read", physfs_file_read, 2);
224 rb_define_method (classPhysfsFile, "write", physfs_file_write, 3);
225 rb_define_method (classPhysfsFile, "to_rwops", physfs_file_to_rwops, 0);