2 * $Id: ext.c 30 2005-04-12 18:38:31Z tilman $
4 * Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #define CHECK_KEY(key) \
32 if (rb_funcall (key, rb_intern ("include?"), \
33 1, INT2FIX (0)) == Qtrue) \
34 rb_raise (rb_eArgError, "key must not contain binary zeroes");
36 #define CHECK_CLOSED(ef) \
38 rb_raise (rb_eIOError, "closed stream");
40 static VALUE c_close (VALUE self);
43 c_free (Eet_File **ef)
60 return Data_Make_Struct (klass, Eet_File *, NULL, c_free, ef);
65 * Eet::File.open(file [, mode] ) -> ef or nil
66 * Eet::File.open(file [, mode] ) { |ef| block } -> nil
68 * If a block isn't specified, Eet::File.open is a synonym for
70 * If a block is given, it will be invoked with the
71 * Eet::File object as a parameter, and the file will be
72 * automatically closed when the block terminates. The call always
73 * returns +nil+ in this case.
76 c_open (int argc, VALUE *argv, VALUE klass)
78 VALUE obj = rb_class_new_instance (argc, argv, klass);
80 if (rb_block_given_p ())
81 return rb_ensure (rb_yield, obj, c_close, obj);
88 * Eet::File.new(file [, mode] ) -> ef or nil
90 * Creates an Eet::File object for _file_.
92 * _file_ is opened with the specified mode (defaulting to "r").
93 * Possible values for _mode_ are "r" for read-only access,
94 * "w" for write-only access and "r+" for read/write access.
97 c_init (int argc, VALUE *argv, VALUE self)
99 VALUE file = Qnil, mode = Qnil;
100 Eet_File **ef = NULL;
101 Eet_File_Mode m = EET_FILE_MODE_READ;
102 const char *tmp, *cfile;
104 Data_Get_Struct (self, Eet_File *, ef);
106 rb_scan_args (argc, argv, "11", &file, &mode);
108 cfile = StringValuePtr (file);
113 tmp = StringValuePtr (mode);
114 if (!strcmp (tmp, "r+"))
115 m = EET_FILE_MODE_READ_WRITE;
116 else if (!strcmp (tmp, "w"))
117 m = EET_FILE_MODE_WRITE;
118 else if (strcmp (tmp, "r"))
119 rb_raise (rb_eArgError, "illegal access mode %s", tmp);
124 *ef = eet_open (cfile, m);
127 case EET_FILE_MODE_READ_WRITE:
128 case EET_FILE_MODE_WRITE:
129 tmp = "Permission denied - %s";
132 tmp = "File not found - %s";
136 rb_raise (rb_eRuntimeError, tmp, cfile);
146 * Closes _ef_ and flushes any pending writes.
147 * _ef_ is unavailable for any further data operations;
148 * an +IOError+ is raised if such an attempt is made.
150 * Eet::File objects are automatically closed when they
151 * are claimed by the garbage collector.
156 Eet_File **ef = NULL;
158 Data_Get_Struct (self, Eet_File *, ef);
171 * ef.list([glob]) -> array
173 * Returns an Array of entries in _ef_ that match the shell glob
174 * _glob_ (defaulting to "*").
177 c_list (int argc, VALUE *argv, VALUE self)
179 VALUE glob = Qnil, ret;
180 Eet_File **ef = NULL;
181 char **entries, *tmp = "*";
184 Data_Get_Struct (self, Eet_File *, ef);
187 switch (eet_mode_get (*ef)) {
188 case EET_FILE_MODE_READ:
189 case EET_FILE_MODE_READ_WRITE:
192 rb_raise (rb_eIOError, "cannot list entries");
195 rb_scan_args (argc, argv, "01", &glob);
198 tmp = StringValuePtr (glob);
202 entries = eet_list (*ef, tmp, &count);
204 for (i = 0; i < count; i++)
205 rb_ary_push (ret, rb_str_new2 (entries[i]));
214 * ef.delete(key) -> ef
216 * Deletes the entry from _ef_ that is stored as _key_.
217 * If the entry cannot be deleted, an +IOError+ is raised,
218 * otherwise _ef_ is returned.
221 c_delete (VALUE self, VALUE key)
223 Eet_File **ef = NULL;
226 Data_Get_Struct (self, Eet_File *, ef);
231 tmp = StringValuePtr (key);
233 if (!eet_delete (*ef, tmp))
234 rb_raise (rb_eIOError, "cannot delete entry - %s", tmp);
241 * ef.read(key) -> string
243 * Reads an entry from _ef_ that is stored as _key_.
244 * If the data cannot be read, an +IOError+ is raised,
245 * otherwise a String is returned that contains the data.
248 c_read (VALUE self, VALUE key)
251 Eet_File **ef = NULL;
255 Data_Get_Struct (self, Eet_File *, ef);
260 data = eet_read (*ef, StringValuePtr (key), &size);
262 rb_raise (rb_eIOError, "cannot read entry - %s", key);
264 ret = rb_str_new (data, size);
273 * ef.write(key, data [, compress] ) -> integer
275 * Stores _data_ in _ef_ as _key_.
276 * If _compress_ is true (which is the default), the data will be
278 * If the data cannot be written, an +IOError+ is raised,
279 * otherwise a the number of bytes written is returned.
282 c_write (int argc, VALUE *argv, VALUE self)
284 VALUE key = Qnil, buf = Qnil, comp = Qnil;
285 Eet_File **ef = NULL;
288 Data_Get_Struct (self, Eet_File *, ef);
291 rb_scan_args (argc, argv, "21", &key, &buf, &comp);
299 n = eet_write (*ef, StringValuePtr (key),
300 StringValuePtr (buf), RSTRING (buf)->len,
303 rb_raise (rb_eIOError, "couldn't write to file");
310 * ef.read_image(key) -> array
312 * Reads an image entry from _ef_ that is stored as _key_.
313 * If the data cannot be read, an +IOError+ is raised,
314 * otherwise an Array is returned that contains the image data,
315 * the image width, the image height, a boolean that indicates
316 * whether the image has an alpha channel or not and a hash
317 * that contains the compression options that were used to store
318 * the image (see Eet::File#write_image).
321 c_read_image (VALUE self, VALUE key)
324 Eet_File **ef = NULL;
326 int w = 0, h = 0, has_alpha = 0, level = 0, quality = 0, lossy = 0;
328 Data_Get_Struct (self, Eet_File *, ef);
333 data = eet_data_image_read (*ef, StringValuePtr (key), &w, &h,
334 &has_alpha, &level, &quality,
337 rb_raise (rb_eIOError, "cannot read entry - %s", key);
339 comp = rb_hash_new ();
340 rb_hash_aset (comp, ID2SYM (rb_intern ("lossy")), INT2FIX (lossy));
341 rb_hash_aset (comp, ID2SYM (rb_intern ("level")), INT2FIX (level));
342 rb_hash_aset (comp, ID2SYM (rb_intern ("quality")), INT2FIX (quality));
344 ret = rb_ary_new3 (5, rb_str_new (data, w * h * 4),
345 INT2FIX (w), INT2FIX (h),
346 has_alpha ? Qtrue : Qfalse, comp);
354 * ef.write_image(key, image_data, w, h [, alpha] [, comp] ) -> integer
356 * Stores _image_data_ with width _w_ and height _h_ in _ef_ as _key_.
357 * Pass true for _alpha_ if the image contains an alpha channel.
358 * You can control how the image is compressed by passing _comp_, which
359 * is a hash whose :lossy entry is true if the image should be
360 * compressed lossily. If :lossy is true, the :quality entry
361 * (0-100, default 100) sets the compression quality.
362 * If :lossy is false, the :level entry (0-9, default 9) sets the
363 * compression level. If _comp_ isn't passed, then the
364 * image is stored losslessly.
365 * If the data cannot be written, an +IOError+ is raised,
366 * otherwise a the number of bytes written is returned.
369 c_write_image (int argc, VALUE *argv, VALUE self)
371 VALUE key = Qnil, buf = Qnil, w = Qnil, h = Qnil, has_alpha = Qnil;
372 VALUE comp = Qnil, tmp;
373 Eet_File **ef = NULL;
374 int n, lossy = 0, level = 9, quality = 100;
376 Data_Get_Struct (self, Eet_File *, ef);
379 rb_scan_args (argc, argv, "42", &key, &buf, &w, &h, &has_alpha,
382 if (NIL_P (has_alpha))
387 Check_Type (w, T_FIXNUM);
388 Check_Type (h, T_FIXNUM);
391 Check_Type (comp, T_HASH);
393 tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("lossy")));
395 lossy = FIX2INT (tmp);
397 tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("level")));
399 level = FIX2INT (tmp);
401 tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("quality")));
403 quality = FIX2INT (tmp);
406 if (!RSTRING (buf)->len)
409 n = eet_data_image_write (*ef, StringValuePtr (key),
410 StringValuePtr (buf),
411 FIX2INT (w), FIX2INT (h),
413 level, quality, lossy);
415 rb_raise (rb_eIOError, "couldn't write to file");
425 m = rb_define_module ("Eet");
427 c = rb_define_class_under (m, "File", rb_cObject);
428 rb_define_alloc_func (c, c_alloc);
429 rb_define_singleton_method (c, "open", c_open, -1);
430 rb_define_method (c, "initialize", c_init, -1);
431 rb_define_method (c, "close", c_close, 0);
432 rb_define_method (c, "list", c_list, -1);
433 rb_define_method (c, "delete", c_delete, 1);
434 rb_define_method (c, "read", c_read, 1);
435 rb_define_method (c, "write", c_write, -1);
436 rb_define_method (c, "read_image", c_read_image, 1);
437 rb_define_method (c, "write_image", c_write_image, -1);