2 * PhysicsFS - ruby interface
4 * Author:: Ed Sinjiashvili (slimb@vlinkmail.com)
11 #include "rb_physfs.h"
12 #include "rb_physfs_file.h"
19 * initialize PhysicsFS
21 VALUE physfs_init (VALUE self, VALUE str)
23 int result = PHYSFS_init (STR2CSTR(str));
34 VALUE physfs_deinit (VALUE self)
45 * return PhysicsFS::Version object
47 VALUE physfs_version (VALUE self)
52 PHYSFS_getLinkedVersion (&ver);
54 sprintf (evalStr, "PhysicsFS::Version.new %d, %d, %d",
55 ver.major, ver.minor, ver.patch);
56 return rb_eval_string (evalStr);
60 * PhysicsFS::supported_archives
62 * return Array of PhysicsFS::ArchiveInfo objects
64 VALUE physfs_supported_archives (VALUE self)
66 const PHYSFS_ArchiveInfo **info = PHYSFS_supportedArchiveTypes();
67 VALUE klass = rb_const_get (modulePhysfs, rb_intern ("ArchiveInfo"));
68 VALUE ary = rb_ary_new ();
73 params[0] = rb_str_new2 ((*info)->extension);
74 params[1] = rb_str_new2 ((*info)->description);
75 params[2] = rb_str_new2 ((*info)->author);
76 params[3] = rb_str_new2 ((*info)->url);
78 rb_ary_push (ary, rb_class_new_instance (4, params, klass));
86 * PhysicsFS::last_error
88 * return string representation of last PhysicsFS error
90 VALUE physfs_last_error (VALUE self)
92 const char *last_error = PHYSFS_getLastError ();
97 return rb_str_new2 (last_error);
101 * PhysicsFS::dir_separator
103 * return platform directory separator
105 VALUE physfs_dir_separator (VALUE self)
107 return rb_str_new2 (PHYSFS_getDirSeparator ());
111 * PhysicsFS::permit_symlinks boolValue
113 * turn symlinks support on/off
115 VALUE physfs_permit_symlinks (VALUE self, VALUE allow)
119 if (allow == Qfalse || allow == Qnil)
122 PHYSFS_permitSymbolicLinks (p);
127 * PhysicsFS::cdrom_dirs
129 * return Array of strings containing available CDs
131 VALUE physfs_cdrom_dirs (VALUE self)
133 char **cds = PHYSFS_getCdRomDirs();
135 VALUE ary = rb_ary_new ();
137 for (i = cds; *i != 0; i++)
138 rb_ary_push (ary, rb_str_new2 (*i));
140 PHYSFS_freeList (cds);
145 * PhysicsFS::base_dir
147 * return base directory
149 VALUE physfs_base_dir (VALUE self)
151 const char *base_dir = PHYSFS_getBaseDir ();
155 return rb_str_new2 (base_dir);
159 * PhysicsFS::user_dir
161 * return user directory
163 VALUE physfs_user_dir (VALUE self)
165 const char *user_dir = PHYSFS_getBaseDir ();
169 return rb_str_new2 (user_dir);
173 * PhysicsFS::write_dir
175 * return write directory
177 VALUE physfs_write_dir (VALUE self)
179 const char *write_dir = PHYSFS_getWriteDir ();
183 return rb_str_new2 (write_dir);
187 * PhysicsFS::write_dir= str
189 * set write directory to *str*
191 VALUE physfs_set_write_dir (VALUE self, VALUE str)
193 int result = PHYSFS_setWriteDir (STR2CSTR(str));
201 * PhysicsFS::add_to_search_path str, append
203 * if append > 0 - append str to search path, otherwise prepend it
205 VALUE physfs_add_search_path (VALUE self, VALUE str, VALUE append)
207 int result = PHYSFS_addToSearchPath (STR2CSTR(str), FIX2INT(append));
214 * PhysicsFS::remove_from_search_path str
216 * removes str from search path
218 VALUE physfs_remove_search_path (VALUE self, VALUE str)
220 int result = PHYSFS_removeFromSearchPath (STR2CSTR(str));
227 * PhysicsFS::search_path
229 * return current search_path - as array of strings
231 VALUE physfs_search_path (VALUE self)
233 char **path = PHYSFS_getSearchPath ();
235 VALUE ary = rb_ary_new ();
237 for (i = path ; *i != 0; i++)
238 rb_ary_push (ary, rb_str_new2 (*i));
240 PHYSFS_freeList (path);
245 VALUE physfs_setSaneConfig(VALUE self, VALUE org, VALUE app, VALUE ext,
246 VALUE includeCdroms, VALUE archivesFirst)
248 int res = PHYSFS_setSaneConfig (STR2CSTR(org), STR2CSTR(app), STR2CSTR(ext),
249 RTEST(includeCdroms), RTEST(archivesFirst));
257 * PhysicsFS::mkdir newdir
259 * create new directory
261 VALUE physfs_mkdir (VALUE self, VALUE newdir)
263 int result = PHYSFS_mkdir (STR2CSTR(newdir));
270 * PhysicsFS::delete name
272 * delete file with name
274 VALUE physfs_delete (VALUE self, VALUE name)
276 int result = PHYSFS_delete (STR2CSTR(name));
283 * PhysicsFS::real_dir name
285 * return real directory (in search path) of a name
287 VALUE physfs_real_dir (VALUE self, VALUE name)
289 const char *path = PHYSFS_getRealDir (STR2CSTR(name));
293 return rb_str_new2 (path);
297 * PhysicsFS::enumerate dir
299 * list a dir from a search path
301 VALUE physfs_enumerate (VALUE self, VALUE dir)
303 char **files = PHYSFS_enumerateFiles (STR2CSTR(dir));
305 VALUE ary = rb_ary_new ();
307 for (i = files; *i != 0; i++)
308 rb_ary_push (ary, rb_str_new2 (*i));
310 PHYSFS_freeList (files);
315 * PhysicsFS::exists? name
317 * does a file with name exist?
319 VALUE physfs_exists (VALUE self, VALUE name)
321 int result = PHYSFS_exists (STR2CSTR(name));
328 * PhysicsFS::is_directory? name
330 * return true if name is directory
332 VALUE physfs_is_directory (VALUE self, VALUE name)
334 int result = PHYSFS_isDirectory (STR2CSTR(name));
341 * PhysicsFS::is_symlink? name
343 * return true if name is symlink
345 VALUE physfs_is_symlink (VALUE self, VALUE name)
347 int result = PHYSFS_isSymbolicLink (STR2CSTR(name));
354 * PhysicsFS::last_mod_time name
356 * return last modification time of a file
358 VALUE physfs_last_mod_time (VALUE self, VALUE name)
360 int result = PHYSFS_getLastModTime (STR2CSTR(name));
362 return INT2FIX(result);
366 * PhysicsFS::open_read name
368 * return +PhysicsFS::File+ ready for reading
370 VALUE physfs_open_read (VALUE self, VALUE name)
372 PHYSFS_File *file = PHYSFS_openRead (STR2CSTR(name));
373 return physfs_file_new (file);
377 * PhysicsFS::open_write name
379 * return PhysicsFS::File ready for writing
381 VALUE physfs_open_write (VALUE self, VALUE name)
383 PHYSFS_File *file = PHYSFS_openWrite (STR2CSTR(name));
384 return physfs_file_new (file);
388 * PhysicsFS::open_append name
390 * return PhysicsFS::File ready for appending
392 VALUE physfs_open_append (VALUE self, VALUE name)
394 PHYSFS_File *file = PHYSFS_openAppend (STR2CSTR(name));
395 return physfs_file_new (file);
398 void Init_physfs_so (void)
400 modulePhysfs = rb_define_module ("PhysicsFS");
402 rb_define_singleton_method (modulePhysfs, "init_internal", physfs_init, 1);
403 rb_define_singleton_method (modulePhysfs, "deinit", physfs_deinit, 0);
404 rb_define_singleton_method (modulePhysfs, "version", physfs_version, 0);
405 rb_define_singleton_method (modulePhysfs, "supported_archives",
406 physfs_supported_archives, 0);
407 rb_define_singleton_method (modulePhysfs, "last_error",
408 physfs_last_error, 0);
409 rb_define_singleton_method (modulePhysfs, "dir_separator",
410 physfs_dir_separator, 0);
411 rb_define_singleton_method (modulePhysfs, "permit_symlinks",
412 physfs_permit_symlinks, 1);
413 rb_define_singleton_method (modulePhysfs, "cdrom_dirs",
414 physfs_cdrom_dirs, 0);
415 rb_define_singleton_method (modulePhysfs, "base_dir", physfs_base_dir, 0);
416 rb_define_singleton_method (modulePhysfs, "user_dir", physfs_user_dir, 0);
418 rb_define_singleton_method (modulePhysfs, "write_dir", physfs_write_dir, 0);
419 rb_define_singleton_method (modulePhysfs, "write_dir=",
420 physfs_set_write_dir, 1);
422 rb_define_singleton_method (modulePhysfs, "add_to_search_path",
423 physfs_add_search_path, 2);
424 rb_define_singleton_method (modulePhysfs, "remove_from_search_path",
425 physfs_remove_search_path, 1);
426 rb_define_singleton_method (modulePhysfs, "search_path",
427 physfs_search_path, 0);
429 rb_define_singleton_method (modulePhysfs, "set_sane_config",
430 physfs_setSaneConfig, 5);
432 rb_define_singleton_method (modulePhysfs, "mkdir", physfs_mkdir, 1);
433 rb_define_singleton_method (modulePhysfs, "delete", physfs_delete, 1);
434 rb_define_singleton_method (modulePhysfs, "real_dir",
436 rb_define_singleton_method (modulePhysfs, "enumerate", physfs_enumerate, 1);
437 rb_define_singleton_method (modulePhysfs, "exists?", physfs_exists, 1);
438 rb_define_singleton_method (modulePhysfs, "is_directory?",
439 physfs_is_directory, 1);
440 rb_define_singleton_method (modulePhysfs, "is_symlink?",
441 physfs_is_symlink, 1);
442 rb_define_singleton_method (modulePhysfs, "last_mod_time",
443 physfs_last_mod_time, 1);
445 rb_define_singleton_method (modulePhysfs, "open_read",
446 physfs_open_read, 1);
447 rb_define_singleton_method (modulePhysfs, "open_write",
448 physfs_open_write, 1);
449 rb_define_singleton_method (modulePhysfs, "open_append",
450 physfs_open_append, 1);
459 // c-indentation-style: "stroustrup"
460 // indent-tabs-mode: nil