6574906a65f3d9021329d6301a5f1e35cb24ac6e
[supertux.git] / src / unison / physfs-1.1.1 / extras / physfs_rb / physfs / rb_sdl_rwops.c
1 /*
2  * SDL_RWops - ruby interface
3  *
4  * Author::     Ed Sinjiashvili (slimb@vlinkmail.com)
5  * License::    LGPL
6  */
7
8 #include "SDL_rwops.h"
9 #include "ruby.h"
10
11 #include "rb_physfs.h"
12 #include "rb_sdl_rwops.h"
13
14 VALUE classRWops;
15
16 /*
17  * RWops constructor
18  */
19 VALUE sdl_rwops_new (SDL_RWops *ops)
20 {
21     VALUE result; 
22
23     if (ops == 0)
24         return Qnil;
25
26     result = Data_Wrap_Struct (classRWops, 0, SDL_FreeRW, ops);
27     return result;
28 }
29
30 /*
31  * PhysicsFS::RWops::from_file name, mode
32  *
33  * create RWops object from file
34  */
35 VALUE sdl_rwops_from_file (VALUE self, VALUE name, VALUE mode)
36 {
37     SDL_RWops *ops = SDL_RWFromFile(STR2CSTR(name), STR2CSTR(mode));
38     return sdl_rwops_new (ops);
39 }
40
41 /*
42  * PhysicsFS::RWops::from_memory string
43  *
44  * create RWops object from memory
45  */
46 VALUE sdl_rwops_from_mem (VALUE self, VALUE str)
47 {
48     int   len      = RSTRING(str)->len;
49     void *mem      = STR2CSTR(str);
50     SDL_RWops *ops = SDL_RWFromMem(mem, len);
51
52     return sdl_rwops_new (ops);
53 }
54
55 /*
56  * PhysicsFS::RWops#seek offset, whence
57  *
58  * position RWops object 
59  */
60 VALUE sdl_rwops_seek (VALUE self, VALUE offset, VALUE whence)
61 {
62     int result;
63     SDL_RWops *ops;
64     
65     Data_Get_Struct (self, SDL_RWops, ops);
66     if (ops == 0)
67         return Qnil;
68
69     result = SDL_RWseek(ops, FIX2INT(offset), FIX2INT(whence));
70     return INT2FIX(result);
71 }
72
73 /*
74  * PhysicsFS::RWops#close
75  *
76  * close RWops. No use of the object is possible after that.
77  */
78 VALUE sdl_rwops_close (VALUE self)
79 {
80     int result;
81     SDL_RWops *ops;
82     
83     Data_Get_Struct (self, SDL_RWops, ops);
84     if (ops == 0)
85         return Qnil;
86     
87     result = SDL_RWclose (ops);
88     DATA_PTR(self) = 0;
89
90     return INT2FIX(result);
91 }
92
93 /*
94  * PhysicsFS::RWops#read
95  *
96  * read from RWops object objCount objSize'd entities.
97  * return string containing raw data or nil
98  */
99 VALUE sdl_rwops_read (VALUE self, VALUE objSize, VALUE objCount)
100 {
101     int objRead;
102     void *buffer;
103     VALUE result;
104     SDL_RWops *ops;
105
106     Data_Get_Struct (self, SDL_RWops, ops);
107     if (ops == 0)
108         return Qnil;
109
110     buffer = malloc (FIX2UINT(objSize) * FIX2UINT(objCount));
111     if (buffer == 0)
112         return Qnil;
113
114     objRead = SDL_RWread (ops, buffer, FIX2UINT(objSize), FIX2UINT(objCount));
115     if (objRead == -1)
116     {
117         free (buffer);
118         return Qnil;
119     }
120
121     result = rb_str_new (buffer, objRead * FIX2UINT(objSize));
122     free (buffer);
123     return result;
124 }
125
126 /*
127  * PhysicsFS::RWops#write buffer, size, n
128  *
129  * write raw string containing n objects size length each.
130  * return number of objects written or nil
131  */
132 VALUE sdl_rwops_write (VALUE self, VALUE buffer, VALUE size, VALUE n)
133 {
134     int result;
135     SDL_RWops *ops;
136
137     Data_Get_Struct (self, SDL_RWops, ops);
138     if (ops == 0)
139         return Qnil;
140
141     result = SDL_RWwrite (ops, STR2CSTR(buffer), FIX2INT(size), FIX2INT(n));
142         
143     if (result == -1)
144         return Qnil;
145
146     return INT2FIX(result);
147 }
148
149 void init_sdl_rwops (void)
150 {
151     classRWops = rb_define_class_under (modulePhysfs, "RWops", rb_cObject);
152     
153     rb_define_method (classRWops, "seek",  sdl_rwops_seek,  2);
154     rb_define_method (classRWops, "read",  sdl_rwops_read,  2);
155     rb_define_method (classRWops, "write", sdl_rwops_write, 3);
156     rb_define_method (classRWops, "close", sdl_rwops_close, 0);
157     
158     rb_define_singleton_method (classRWops, "from_file", 
159                                 sdl_rwops_from_file, 2);
160     rb_define_singleton_method (classRWops, "from_memory", 
161                                 sdl_rwops_from_mem, 1);
162 }