2 * $Id: ext.c 28 2005-04-11 20:58:23Z 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 static VALUE c_close (VALUE self);
39 c_free (Eet_File **ef)
56 return Data_Make_Struct (klass, Eet_File *, NULL, c_free, ef);
61 * Eet::File.open(file [, mode] ) -> ef or nil
62 * Eet::File.open(file [, mode] ) { |ef| block } -> nil
64 * If a block isn't specified, Eet::File.open is a synonym for
66 * If a block is given, it will be invoked with the
67 * Eet::File object as a parameter, and the file will be
68 * automatically closed when the block terminates. The call always
69 * returns +nil+ in this case.
72 c_open (int argc, VALUE *argv, VALUE klass)
74 VALUE obj = rb_class_new_instance (argc, argv, klass);
76 if (rb_block_given_p ())
77 return rb_ensure (rb_yield, obj, c_close, obj);
84 * Eet::File.new(file [, mode] ) -> ef or nil
86 * Creates an Eet::File object for _file_.
88 * _file_ is opened with the specified mode (defaulting to "r").
89 * Possible values for _mode_ are "r" for read-only access,
90 * "w" for write-only access and "r+" for read/write access.
93 c_init (int argc, VALUE *argv, VALUE self)
95 VALUE file = Qnil, mode = Qnil;
97 Eet_File_Mode m = EET_FILE_MODE_READ;
100 Data_Get_Struct (self, Eet_File *, ef);
102 rb_scan_args (argc, argv, "11", &file, &mode);
109 tmp = StringValuePtr (mode);
110 if (!strcmp (tmp, "r+"))
111 m = EET_FILE_MODE_READ_WRITE;
112 else if (!strcmp (tmp, "w"))
113 m = EET_FILE_MODE_WRITE;
114 else if (strcmp (tmp, "r"))
115 rb_raise (rb_eArgError, "illegal access mode %s", tmp);
120 *ef = eet_open (StringValuePtr (file), m);
123 case EET_FILE_MODE_READ_WRITE:
124 case EET_FILE_MODE_WRITE:
125 tmp = "Permission denied - %s";
128 tmp = "File not found - %s";
132 rb_raise (rb_eRuntimeError, tmp, file);
142 * Closes _ef_ and flushes any pending writes.
143 * _ef_ is unavailable for any further data operations;
144 * an +IOError+ is raised if such an attempt is made.
146 * Eet::File objects are automatically closed when they
147 * are claimed by the garbage collector.
152 Eet_File **ef = NULL;
154 Data_Get_Struct (self, Eet_File *, ef);
157 rb_raise (rb_eIOError, "closed stream");
170 * ef.list([glob]) -> array
172 * Returns an Array of entries in _ef_ that match the shell glob
173 * _glob_ (defaulting to "*").
176 c_list (int argc, VALUE *argv, VALUE self)
178 VALUE glob = Qnil, ret;
179 Eet_File **ef = NULL;
180 char **entries, *tmp = "*";
183 Data_Get_Struct (self, Eet_File *, ef);
186 rb_raise (rb_eIOError, "closed stream");
188 switch (eet_mode_get (*ef)) {
189 case EET_FILE_MODE_READ:
190 case EET_FILE_MODE_READ_WRITE:
193 rb_raise (rb_eIOError, "cannot list entries");
196 rb_scan_args (argc, argv, "01", &glob);
199 tmp = StringValuePtr (glob);
203 entries = eet_list (*ef, tmp, &count);
205 for (i = 0; i < count; i++)
206 rb_ary_push (ret, rb_str_new2 (entries[i]));
215 * ef.delete(key) -> ef
217 * Deletes the entry from _ef_ that is stored as _key_.
218 * If the entry cannot be deleted, an +IOError+ is raised,
219 * otherwise _ef_ is returned.
222 c_delete (VALUE self, VALUE key)
224 Eet_File **ef = NULL;
227 Data_Get_Struct (self, Eet_File *, ef);
230 rb_raise (rb_eIOError, "closed stream");
234 tmp = StringValuePtr (key);
236 if (!eet_delete (*ef, tmp))
237 rb_raise (rb_eIOError, "cannot delete entry - %s", tmp);
244 * ef.read(key) -> string
246 * Reads an entry from _ef_ that is stored as _key_.
247 * If the data cannot be read, an +IOError+ is raised,
248 * otherwise a String is returned that contains the data.
251 c_read (VALUE self, VALUE key)
254 Eet_File **ef = NULL;
258 Data_Get_Struct (self, Eet_File *, ef);
261 rb_raise (rb_eIOError, "closed stream");
265 data = eet_read (*ef, StringValuePtr (key), &size);
267 rb_raise (rb_eIOError, "cannot read entry - %s", key);
269 ret = rb_str_new (data, size);
278 * ef.write(key, data [, compress] ) -> integer
280 * Stores _data_ in _ef_ as _key_.
281 * If _compress_ is true (which is the default), the data will be
283 * If the data cannot be written, an +IOError+ is raised,
284 * otherwise a the number of bytes written is returned.
287 c_write (int argc, VALUE *argv, VALUE self)
289 VALUE key = Qnil, buf = Qnil, comp = Qnil;
290 Eet_File **ef = NULL;
293 Data_Get_Struct (self, Eet_File *, ef);
296 rb_raise (rb_eIOError, "closed stream");
298 rb_scan_args (argc, argv, "21", &key, &buf, &comp);
306 n = eet_write (*ef, StringValuePtr (key),
307 StringValuePtr (buf), RSTRING (buf)->len,
310 rb_raise (rb_eIOError, "couldn't write to file");
317 * ef.read_image(key) -> array
319 * Reads an image entry from _ef_ that is stored as _key_.
320 * If the data cannot be read, an +IOError+ is raised,
321 * otherwise an Array is returned that contains the image data,
322 * the image width, the image height, a boolean that indicates
323 * whether the image has an alpha channel or not and a hash
324 * that contains the compression options that were used to store
325 * the image (see Eet::File#write_image).
328 c_read_image (VALUE self, VALUE key)
331 Eet_File **ef = NULL;
333 int w = 0, h = 0, has_alpha = 0, level = 0, quality = 0, lossy = 0;
335 Data_Get_Struct (self, Eet_File *, ef);
338 rb_raise (rb_eIOError, "closed stream");
342 data = eet_data_image_read (*ef, StringValuePtr (key), &w, &h,
343 &has_alpha, &level, &quality,
346 rb_raise (rb_eIOError, "cannot read entry - %s", key);
348 comp = rb_hash_new ();
349 rb_hash_aset (comp, ID2SYM (rb_intern ("lossy")), INT2FIX (lossy));
350 rb_hash_aset (comp, ID2SYM (rb_intern ("level")), INT2FIX (level));
351 rb_hash_aset (comp, ID2SYM (rb_intern ("quality")), INT2FIX (quality));
353 ret = rb_ary_new3 (5, rb_str_new (data, w * h * 4),
354 INT2FIX (w), INT2FIX (h),
355 has_alpha ? Qtrue : Qfalse, comp);
363 * ef.write_image(key, image_data, w, h [, alpha] [, comp] ) -> integer
365 * Stores _image_data_ with width _w_ and height _h_ in _ef_ as _key_.
366 * Pass true for _alpha_ if the image contains an alpha channel.
367 * You can control how the image is compressed by passing _comp_, which
368 * is a hash whose :lossy entry is true if the image should be
369 * compressed lossily. If :lossy is true, the :quality entry
370 * (0-100, default 100) sets the compression quality.
371 * If :lossy is false, the :level entry (0-9, default 9) sets the
372 * compression level. If _comp_ isn't passed, then the
373 * image is stored losslessly.
374 * If the data cannot be written, an +IOError+ is raised,
375 * otherwise a the number of bytes written is returned.
378 c_write_image (int argc, VALUE *argv, VALUE self)
380 VALUE key = Qnil, buf = Qnil, w = Qnil, h = Qnil, has_alpha = Qnil;
381 VALUE comp = Qnil, tmp;
382 Eet_File **ef = NULL;
383 int n, lossy = 0, level = 9, quality = 100;
385 Data_Get_Struct (self, Eet_File *, ef);
388 rb_raise (rb_eIOError, "closed stream");
390 rb_scan_args (argc, argv, "42", &key, &buf, &w, &h, &has_alpha,
393 if (NIL_P (has_alpha))
398 Check_Type (w, T_FIXNUM);
399 Check_Type (h, T_FIXNUM);
402 Check_Type (comp, T_HASH);
404 tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("lossy")));
406 lossy = FIX2INT (tmp);
408 tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("level")));
410 level = FIX2INT (tmp);
412 tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("quality")));
414 quality = FIX2INT (tmp);
417 if (!RSTRING (buf)->len)
420 n = eet_data_image_write (*ef, StringValuePtr (key),
421 StringValuePtr (buf),
422 FIX2INT (w), FIX2INT (h),
424 level, quality, lossy);
426 rb_raise (rb_eIOError, "couldn't write to file");
436 m = rb_define_module ("Eet");
438 c = rb_define_class_under (m, "File", rb_cObject);
439 rb_define_alloc_func (c, c_alloc);
440 rb_define_singleton_method (c, "open", c_open, -1);
441 rb_define_method (c, "initialize", c_init, -1);
442 rb_define_method (c, "close", c_close, 0);
443 rb_define_method (c, "list", c_list, -1);
444 rb_define_method (c, "delete", c_delete, 1);
445 rb_define_method (c, "read", c_read, 1);
446 rb_define_method (c, "write", c_write, -1);
447 rb_define_method (c, "read_image", c_read_image, 1);
448 rb_define_method (c, "write_image", c_write_image, -1);