2 * $Id: ext.c 13 2005-03-27 18:35:51Z 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) \
30 Check_Type ((key), T_STRING); \
31 if (rb_funcall ((key), rb_intern ("include?"), \
32 1, INT2FIX (0)) == Qtrue) \
33 rb_raise (rb_eArgError, "key must not contain binary zeroes");
35 static VALUE c_close (VALUE self);
38 c_free (Eet_File **ef)
55 return Data_Make_Struct (klass, Eet_File *, NULL, c_free, ef);
60 * Eet::File.open(file [, mode] ) -> ef or nil
61 * Eet::File.open(file [, mode] ) { |ef| block } -> nil
63 * If a block isn't specified, Eet::File.open is a synonym for
65 * If a block is given, it will be invoked with the
66 * Eet::File object as a parameter, and the file will be
67 * automatically closed when the block terminates. The call always
68 * returns +nil+ in this case.
71 c_open (int argc, VALUE *argv, VALUE klass)
73 VALUE obj = rb_class_new_instance (argc, argv, klass);
75 if (rb_block_given_p ())
76 return rb_ensure (rb_yield, obj, c_close, obj);
83 * Eet::File.new(file [, mode] ) -> ef or nil
85 * Creates an Eet::File object for _file_.
87 * _file_ is opened with the specified mode (defaulting to "r").
88 * Possible values for _mode_ are "r" for read-only access,
89 * "w" for write-only access and "r+" for read/write access.
92 c_init (int argc, VALUE *argv, VALUE self)
94 VALUE file = Qnil, mode = Qnil;
96 Eet_File_Mode m = EET_FILE_MODE_READ;
99 Data_Get_Struct (self, Eet_File *, ef);
101 rb_scan_args (argc, argv, "11", &file, &mode);
103 Check_Type (file, T_STRING);
106 Check_Type (mode, T_STRING);
108 tmp = StringValuePtr (mode);
109 if (!strcmp (tmp, "r+"))
110 m = EET_FILE_MODE_READ_WRITE;
111 else if (!strcmp (tmp, "w"))
112 m = EET_FILE_MODE_WRITE;
113 else if (strcmp (tmp, "r"))
114 rb_raise (rb_eArgError, "illegal access mode %s", tmp);
119 *ef = eet_open (StringValuePtr (file), m);
122 case EET_FILE_MODE_READ_WRITE:
123 case EET_FILE_MODE_WRITE:
124 tmp = "Permission denied - %s";
127 tmp = "File not found - %s";
131 rb_raise (rb_eRuntimeError, tmp, file);
141 * Closes _ef_ and flushes any pending writes.
142 * _ef_ is unavailable for any further data operations;
143 * an +IOError+ is raised if such an attempt is made.
145 * Eet::File objects are automatically closed when they
146 * are claimed by the garbage collector.
151 Eet_File **ef = NULL;
153 Data_Get_Struct (self, Eet_File *, ef);
156 rb_raise (rb_eIOError, "closed stream");
169 * ef.list([glob]) -> array
171 * Returns an Array of entries in _ef_ that match the shell glob
172 * _glob_ (defaulting to "*").
175 c_list (int argc, VALUE *argv, VALUE self)
177 VALUE glob = Qnil, ret;
178 Eet_File **ef = NULL;
179 char **entries, *tmp = "*";
182 Data_Get_Struct (self, Eet_File *, ef);
185 rb_raise (rb_eIOError, "closed stream");
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 Check_Type (glob, T_STRING);
199 tmp = StringValuePtr (glob);
204 entries = eet_list (*ef, tmp, &count);
206 for (i = 0; i < count; i++)
207 rb_ary_push (ret, rb_str_new2 (entries[i]));
216 * ef.delete(key) -> ef
218 * Deletes the entry from _ef_ that is stored as _key_.
219 * If the entry cannot be deleted, an +IOError+ is raised,
220 * otherwise _ef_ is returned.
223 c_delete (VALUE self, VALUE key)
225 Eet_File **ef = NULL;
228 Data_Get_Struct (self, Eet_File *, ef);
231 rb_raise (rb_eIOError, "closed stream");
235 tmp = StringValuePtr (key);
237 if (!eet_delete (*ef, tmp))
238 rb_raise (rb_eIOError, "cannot delete entry - %s", tmp);
245 * ef.read(key) -> string
247 * Reads an entry from _ef_ that is stored as _key_.
248 * If the data cannot be read, an +IOError+ is raised,
249 * otherwise a String is returned that contains the data.
252 c_read (VALUE self, VALUE key)
255 Eet_File **ef = NULL;
259 Data_Get_Struct (self, Eet_File *, ef);
262 rb_raise (rb_eIOError, "closed stream");
266 data = eet_read (*ef, StringValuePtr (key), &size);
268 rb_raise (rb_eIOError, "cannot read entry - %s", key);
270 ret = rb_str_new (data, size);
279 * ef.write(key, data [, compress] ) -> integer
281 * Stores _data_ in _ef_ as _key_.
282 * If _compress_ is true (which is the default), the data will be
284 * If the data cannot be written, an +IOError+ is raised,
285 * otherwise a the number of bytes written is returned.
288 c_write (int argc, VALUE *argv, VALUE self)
290 VALUE key = Qnil, buf = Qnil, comp = Qnil;
291 Eet_File **ef = NULL;
294 Data_Get_Struct (self, Eet_File *, ef);
297 rb_raise (rb_eIOError, "closed stream");
299 rb_scan_args (argc, argv, "21", &key, &buf, &comp);
305 Check_Type (buf, T_STRING);
307 n = eet_write (*ef, StringValuePtr (key),
308 StringValuePtr (buf), RSTRING (buf)->len,
311 rb_raise (rb_eIOError, "couldn't write to file");
318 * ef.read_image(key) -> array
320 * Reads an image entry from _ef_ that is stored as _key_.
321 * If the data cannot be read, an +IOError+ is raised,
322 * otherwise an Array is returned that contains the image data,
323 * the image width, the image height, a boolean that indicates
324 * whether the image has an alpha channel or not and a hash
325 * that contains the compression options that were used to store
326 * the image (see Eet::File#write_image).
329 c_read_image (VALUE self, VALUE key)
332 Eet_File **ef = NULL;
334 int w = 0, h = 0, has_alpha = 0, level = 0, quality = 0, lossy = 0;
336 Data_Get_Struct (self, Eet_File *, ef);
339 rb_raise (rb_eIOError, "closed stream");
343 data = eet_data_image_read (*ef, StringValuePtr (key), &w, &h,
344 &has_alpha, &level, &quality,
347 rb_raise (rb_eIOError, "cannot read entry - %s", key);
349 comp = rb_hash_new ();
350 rb_hash_aset (comp, ID2SYM (rb_intern ("lossy")), INT2FIX (lossy));
351 rb_hash_aset (comp, ID2SYM (rb_intern ("level")), INT2FIX (level));
352 rb_hash_aset (comp, ID2SYM (rb_intern ("quality")), INT2FIX (quality));
354 ret = rb_ary_new3 (5, rb_str_new (data, w * h * 4),
355 INT2FIX (w), INT2FIX (h),
356 has_alpha ? Qtrue : Qfalse, comp);
364 * ef.write_image(key, image_data, w, h [, alpha] [, comp] ) -> integer
366 * Stores _image_data_ with width _w_ and height _h_ in _ef_ as _key_.
367 * Pass true for _alpha_ if the image contains an alpha channel.
368 * You can control how the image is compressed by passing _comp_, which
369 * is a hash whose :lossy entry is true if the image should be
370 * compressed lossily. If :lossy is true, the :quality entry
371 * (0-100, default 100) sets the compression quality.
372 * If :lossy is false, the :level entry (0-9, default 9) sets the
373 * compression level. If _comp_ isn't passed, then the
374 * image is stored losslessly.
375 * If the data cannot be written, an +IOError+ is raised,
376 * otherwise a the number of bytes written is returned.
379 c_write_image (int argc, VALUE *argv, VALUE self)
381 VALUE key = Qnil, buf = Qnil, w = Qnil, h = Qnil, has_alpha = Qnil;
382 VALUE comp = Qnil, tmp;
383 Eet_File **ef = NULL;
384 int n, lossy = 0, level = 9, quality = 100;
386 Data_Get_Struct (self, Eet_File *, ef);
389 rb_raise (rb_eIOError, "closed stream");
391 rb_scan_args (argc, argv, "42", &key, &buf, &w, &h, &has_alpha,
394 if (NIL_P (has_alpha))
398 Check_Type (buf, T_STRING);
399 Check_Type (w, T_FIXNUM);
400 Check_Type (h, T_FIXNUM);
403 Check_Type (comp, T_HASH);
405 tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("lossy")));
407 lossy = FIX2INT (tmp);
409 tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("level")));
411 level = FIX2INT (tmp);
413 tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("quality")));
415 quality = FIX2INT (tmp);
418 if (!RSTRING (buf)->len)
421 n = eet_data_image_write (*ef, StringValuePtr (key),
422 StringValuePtr (buf),
423 FIX2INT (w), FIX2INT (h),
425 level, quality, lossy);
427 rb_raise (rb_eIOError, "couldn't write to file");
437 m = rb_define_module ("Eet");
439 c = rb_define_class_under (m, "File", rb_cObject);
440 rb_define_alloc_func (c, c_alloc);
441 rb_define_singleton_method (c, "open", c_open, -1);
442 rb_define_method (c, "initialize", c_init, -1);
443 rb_define_method (c, "close", c_close, 0);
444 rb_define_method (c, "list", c_list, -1);
445 rb_define_method (c, "delete", c_delete, 1);
446 rb_define_method (c, "read", c_read, 1);
447 rb_define_method (c, "write", c_write, -1);
448 rb_define_method (c, "read_image", c_read_image, 1);
449 rb_define_method (c, "write_image", c_write_image, -1);