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