rb_physfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * PhysicsFS - ruby interface
  3. *
  4. * Author:: Ed Sinjiashvili (slimb@vlinkmail.com)
  5. * License:: LGPL
  6. */
  7. #include "physfs.h"
  8. #include "ruby.h"
  9. #include "rb_physfs.h"
  10. #include "rb_physfs_file.h"
  11. VALUE modulePhysfs;
  12. /*
  13. * PhysicsFS::init str
  14. *
  15. * initialize PhysicsFS
  16. */
  17. VALUE physfs_init (VALUE self, VALUE str)
  18. {
  19. int result = PHYSFS_init (STR2CSTR(str));
  20. if (result)
  21. return Qtrue;
  22. return Qfalse;
  23. }
  24. /*
  25. * PhysicsFS::deinit
  26. */
  27. VALUE physfs_deinit (VALUE self)
  28. {
  29. if (PHYSFS_deinit ())
  30. return Qtrue;
  31. return Qfalse;
  32. }
  33. /*
  34. * PhysicsFS::version
  35. *
  36. * return PhysicsFS::Version object
  37. */
  38. VALUE physfs_version (VALUE self)
  39. {
  40. char evalStr[200];
  41. PHYSFS_Version ver;
  42. PHYSFS_getLinkedVersion (&ver);
  43. sprintf (evalStr, "PhysicsFS::Version.new %d, %d, %d",
  44. ver.major, ver.minor, ver.patch);
  45. return rb_eval_string (evalStr);
  46. }
  47. /*
  48. * PhysicsFS::supported_archives
  49. *
  50. * return Array of PhysicsFS::ArchiveInfo objects
  51. */
  52. VALUE physfs_supported_archives (VALUE self)
  53. {
  54. const PHYSFS_ArchiveInfo **info = PHYSFS_supportedArchiveTypes();
  55. VALUE klass = rb_const_get (modulePhysfs, rb_intern ("ArchiveInfo"));
  56. VALUE ary = rb_ary_new ();
  57. VALUE params[4];
  58. while ( *info != 0 )
  59. {
  60. params[0] = rb_str_new2 ((*info)->extension);
  61. params[1] = rb_str_new2 ((*info)->description);
  62. params[2] = rb_str_new2 ((*info)->author);
  63. params[3] = rb_str_new2 ((*info)->url);
  64. rb_ary_push (ary, rb_class_new_instance (4, params, klass));
  65. info++;
  66. }
  67. return ary;
  68. }
  69. /*
  70. * PhysicsFS::last_error
  71. *
  72. * return string representation of last PhysicsFS error
  73. */
  74. VALUE physfs_last_error (VALUE self)
  75. {
  76. const char *last_error = PHYSFS_getLastError ();
  77. if (last_error == 0)
  78. last_error = "";
  79. return rb_str_new2 (last_error);
  80. }
  81. /*
  82. * PhysicsFS::dir_separator
  83. *
  84. * return platform directory separator
  85. */
  86. VALUE physfs_dir_separator (VALUE self)
  87. {
  88. return rb_str_new2 (PHYSFS_getDirSeparator ());
  89. }
  90. /*
  91. * PhysicsFS::permit_symlinks boolValue
  92. *
  93. * turn symlinks support on/off
  94. */
  95. VALUE physfs_permit_symlinks (VALUE self, VALUE allow)
  96. {
  97. int p = 1;
  98. if (allow == Qfalse || allow == Qnil)
  99. p = 0;
  100. PHYSFS_permitSymbolicLinks (p);
  101. return Qtrue;
  102. }
  103. /*
  104. * PhysicsFS::cdrom_dirs
  105. *
  106. * return Array of strings containing available CDs
  107. */
  108. VALUE physfs_cdrom_dirs (VALUE self)
  109. {
  110. char **cds = PHYSFS_getCdRomDirs();
  111. char **i;
  112. VALUE ary = rb_ary_new ();
  113. for (i = cds; *i != 0; i++)
  114. rb_ary_push (ary, rb_str_new2 (*i));
  115. PHYSFS_freeList (cds);
  116. return ary;
  117. }
  118. /*
  119. * PhysicsFS::base_dir
  120. *
  121. * return base directory
  122. */
  123. VALUE physfs_base_dir (VALUE self)
  124. {
  125. const char *base_dir = PHYSFS_getBaseDir ();
  126. if (base_dir == 0)
  127. base_dir = "";
  128. return rb_str_new2 (base_dir);
  129. }
  130. /*
  131. * PhysicsFS::user_dir
  132. *
  133. * return user directory
  134. */
  135. VALUE physfs_user_dir (VALUE self)
  136. {
  137. const char *user_dir = PHYSFS_getBaseDir ();
  138. if (user_dir == 0)
  139. user_dir = "";
  140. return rb_str_new2 (user_dir);
  141. }
  142. /*
  143. * PhysicsFS::write_dir
  144. *
  145. * return write directory
  146. */
  147. VALUE physfs_write_dir (VALUE self)
  148. {
  149. const char *write_dir = PHYSFS_getWriteDir ();
  150. if (write_dir == 0)
  151. return Qnil;
  152. return rb_str_new2 (write_dir);
  153. }
  154. /*
  155. * PhysicsFS::write_dir= str
  156. *
  157. * set write directory to *str*
  158. */
  159. VALUE physfs_set_write_dir (VALUE self, VALUE str)
  160. {
  161. int result = PHYSFS_setWriteDir (STR2CSTR(str));
  162. if (result)
  163. return Qtrue;
  164. return Qfalse;
  165. }
  166. /*
  167. * PhysicsFS::add_to_search_path str, append
  168. *
  169. * if append > 0 - append str to search path, otherwise prepend it
  170. */
  171. VALUE physfs_add_search_path (VALUE self, VALUE str, VALUE append)
  172. {
  173. int result = PHYSFS_addToSearchPath (STR2CSTR(str), FIX2INT(append));
  174. if (result)
  175. return Qtrue;
  176. return Qfalse;
  177. }
  178. /*
  179. * PhysicsFS::remove_from_search_path str
  180. *
  181. * removes str from search path
  182. */
  183. VALUE physfs_remove_search_path (VALUE self, VALUE str)
  184. {
  185. int result = PHYSFS_removeFromSearchPath (STR2CSTR(str));
  186. if (result)
  187. return Qtrue;
  188. return Qfalse;
  189. }
  190. /*
  191. * PhysicsFS::search_path
  192. *
  193. * return current search_path - as array of strings
  194. */
  195. VALUE physfs_search_path (VALUE self)
  196. {
  197. char **path = PHYSFS_getSearchPath ();
  198. char **i;
  199. VALUE ary = rb_ary_new ();
  200. for (i = path ; *i != 0; i++)
  201. rb_ary_push (ary, rb_str_new2 (*i));
  202. PHYSFS_freeList (path);
  203. return ary;
  204. }
  205. //
  206. VALUE physfs_setSaneConfig(VALUE self, VALUE org, VALUE app, VALUE ext,
  207. VALUE includeCdroms, VALUE archivesFirst)
  208. {
  209. int res = PHYSFS_setSaneConfig (STR2CSTR(org), STR2CSTR(app), STR2CSTR(ext),
  210. RTEST(includeCdroms), RTEST(archivesFirst));
  211. if (res)
  212. return Qtrue;
  213. return Qfalse;
  214. }
  215. /*
  216. * PhysicsFS::mkdir newdir
  217. *
  218. * create new directory
  219. */
  220. VALUE physfs_mkdir (VALUE self, VALUE newdir)
  221. {
  222. int result = PHYSFS_mkdir (STR2CSTR(newdir));
  223. if (result)
  224. return Qtrue;
  225. return Qfalse;
  226. }
  227. /*
  228. * PhysicsFS::delete name
  229. *
  230. * delete file with name
  231. */
  232. VALUE physfs_delete (VALUE self, VALUE name)
  233. {
  234. int result = PHYSFS_delete (STR2CSTR(name));
  235. if (result)
  236. return Qtrue;
  237. return Qfalse;
  238. }
  239. /*
  240. * PhysicsFS::real_dir name
  241. *
  242. * return real directory (in search path) of a name
  243. */
  244. VALUE physfs_real_dir (VALUE self, VALUE name)
  245. {
  246. const char *path = PHYSFS_getRealDir (STR2CSTR(name));
  247. if (path == 0)
  248. return Qnil;
  249. return rb_str_new2 (path);
  250. }
  251. /*
  252. * PhysicsFS::enumerate dir
  253. *
  254. * list a dir from a search path
  255. */
  256. VALUE physfs_enumerate (VALUE self, VALUE dir)
  257. {
  258. char **files = PHYSFS_enumerateFiles (STR2CSTR(dir));
  259. char **i;
  260. VALUE ary = rb_ary_new ();
  261. for (i = files; *i != 0; i++)
  262. rb_ary_push (ary, rb_str_new2 (*i));
  263. PHYSFS_freeList (files);
  264. return ary;
  265. }
  266. /*
  267. * PhysicsFS::exists? name
  268. *
  269. * does a file with name exist?
  270. */
  271. VALUE physfs_exists (VALUE self, VALUE name)
  272. {
  273. int result = PHYSFS_exists (STR2CSTR(name));
  274. if (result)
  275. return Qtrue;
  276. return Qfalse;
  277. }
  278. /*
  279. * PhysicsFS::is_directory? name
  280. *
  281. * return true if name is directory
  282. */
  283. VALUE physfs_is_directory (VALUE self, VALUE name)
  284. {
  285. int result = PHYSFS_isDirectory (STR2CSTR(name));
  286. if (result)
  287. return Qtrue;
  288. return Qfalse;
  289. }
  290. /*
  291. * PhysicsFS::is_symlink? name
  292. *
  293. * return true if name is symlink
  294. */
  295. VALUE physfs_is_symlink (VALUE self, VALUE name)
  296. {
  297. int result = PHYSFS_isSymbolicLink (STR2CSTR(name));
  298. if (result)
  299. return Qtrue;
  300. return Qfalse;
  301. }
  302. /*
  303. * PhysicsFS::last_mod_time name
  304. *
  305. * return last modification time of a file
  306. */
  307. VALUE physfs_last_mod_time (VALUE self, VALUE name)
  308. {
  309. int result = PHYSFS_getLastModTime (STR2CSTR(name));
  310. return INT2FIX(result);
  311. }
  312. /*
  313. * PhysicsFS::open_read name
  314. *
  315. * return +PhysicsFS::File+ ready for reading
  316. */
  317. VALUE physfs_open_read (VALUE self, VALUE name)
  318. {
  319. PHYSFS_file *file = PHYSFS_openRead (STR2CSTR(name));
  320. return physfs_file_new (file);
  321. }
  322. /*
  323. * PhysicsFS::open_write name
  324. *
  325. * return PhysicsFS::File ready for writing
  326. */
  327. VALUE physfs_open_write (VALUE self, VALUE name)
  328. {
  329. PHYSFS_file *file = PHYSFS_openWrite (STR2CSTR(name));
  330. return physfs_file_new (file);
  331. }
  332. /*
  333. * PhysicsFS::open_append name
  334. *
  335. * return PhysicsFS::File ready for appending
  336. */
  337. VALUE physfs_open_append (VALUE self, VALUE name)
  338. {
  339. PHYSFS_file *file = PHYSFS_openAppend (STR2CSTR(name));
  340. return physfs_file_new (file);
  341. }
  342. void Init_physfs_so (void)
  343. {
  344. modulePhysfs = rb_define_module ("PhysicsFS");
  345. rb_define_singleton_method (modulePhysfs, "init_internal", physfs_init, 1);
  346. rb_define_singleton_method (modulePhysfs, "deinit", physfs_deinit, 0);
  347. rb_define_singleton_method (modulePhysfs, "version", physfs_version, 0);
  348. rb_define_singleton_method (modulePhysfs, "supported_archives",
  349. physfs_supported_archives, 0);
  350. rb_define_singleton_method (modulePhysfs, "last_error",
  351. physfs_last_error, 0);
  352. rb_define_singleton_method (modulePhysfs, "dir_separator",
  353. physfs_dir_separator, 0);
  354. rb_define_singleton_method (modulePhysfs, "permit_symlinks",
  355. physfs_permit_symlinks, 1);
  356. rb_define_singleton_method (modulePhysfs, "cdrom_dirs",
  357. physfs_cdrom_dirs, 0);
  358. rb_define_singleton_method (modulePhysfs, "base_dir", physfs_base_dir, 0);
  359. rb_define_singleton_method (modulePhysfs, "user_dir", physfs_user_dir, 0);
  360. rb_define_singleton_method (modulePhysfs, "write_dir", physfs_write_dir, 0);
  361. rb_define_singleton_method (modulePhysfs, "write_dir=",
  362. physfs_set_write_dir, 1);
  363. rb_define_singleton_method (modulePhysfs, "add_to_search_path",
  364. physfs_add_search_path, 2);
  365. rb_define_singleton_method (modulePhysfs, "remove_from_search_path",
  366. physfs_remove_search_path, 1);
  367. rb_define_singleton_method (modulePhysfs, "search_path",
  368. physfs_search_path, 0);
  369. rb_define_singleton_method (modulePhysfs, "set_sane_config",
  370. physfs_setSaneConfig, 5);
  371. rb_define_singleton_method (modulePhysfs, "mkdir", physfs_mkdir, 1);
  372. rb_define_singleton_method (modulePhysfs, "delete", physfs_delete, 1);
  373. rb_define_singleton_method (modulePhysfs, "real_dir",
  374. physfs_real_dir, 1);
  375. rb_define_singleton_method (modulePhysfs, "enumerate", physfs_enumerate, 1);
  376. rb_define_singleton_method (modulePhysfs, "exists?", physfs_exists, 1);
  377. rb_define_singleton_method (modulePhysfs, "is_directory?",
  378. physfs_is_directory, 1);
  379. rb_define_singleton_method (modulePhysfs, "is_symlink?",
  380. physfs_is_symlink, 1);
  381. rb_define_singleton_method (modulePhysfs, "last_mod_time",
  382. physfs_last_mod_time, 1);
  383. rb_define_singleton_method (modulePhysfs, "open_read",
  384. physfs_open_read, 1);
  385. rb_define_singleton_method (modulePhysfs, "open_write",
  386. physfs_open_write, 1);
  387. rb_define_singleton_method (modulePhysfs, "open_append",
  388. physfs_open_append, 1);
  389. init_physfs_file ();
  390. init_sdl_rwops ();
  391. }
  392. /*
  393. // Local Variables:
  394. // mode: C
  395. // c-indentation-style: "stroustrup"
  396. // indent-tabs-mode: nil
  397. // End:
  398. */