2 * $Id: ext.c 49 2005-05-30 19:52:36Z 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.
30 #define CHECK_KEY(key) \
31 if (rb_funcall (key, id_include, 1, INT2FIX (0)) == Qtrue) \
32 rb_raise (rb_eArgError, "key must not contain binary zeroes");
34 #define CHECK_CLOSED(ef) \
36 rb_raise (rb_eIOError, "closed stream");
38 #ifdef WORDS_BIGENDIAN
40 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
41 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
42 #else /* !WORDS_BIGENDIAN */
43 # define BSWAP32(x) (x)
44 #endif /* WORDS_BIGENDIAN */
46 static VALUE c_close (VALUE self);
48 static VALUE cStream, cChunk,
49 eEetError, eNameError, ePropError;
50 static ID id_include, id_serialize, id_push, id_empty,
51 id_to_eet_chunks, id_to_eet_name, id_to_eet_properties,
55 c_free (Eet_File **ef)
72 return Data_Make_Struct (klass, Eet_File *, NULL, c_free, ef);
77 * Eet::File.open(file [, mode] ) -> ef or nil
78 * Eet::File.open(file [, mode] ) { |ef| block } -> nil
80 * If a block isn't specified, Eet::File.open is a synonym for
82 * If a block is given, it will be invoked with the
83 * Eet::File object as a parameter, and the file will be
84 * automatically closed when the block terminates. The call always
85 * returns +nil+ in this case.
88 c_open (int argc, VALUE *argv, VALUE klass)
90 VALUE obj = rb_class_new_instance (argc, argv, klass);
92 if (rb_block_given_p ())
93 return rb_ensure (rb_yield, obj, c_close, obj);
100 * Eet::File.new(file [, mode] ) -> ef or nil
102 * Creates an Eet::File object for _file_.
104 * _file_ is opened with the specified mode (defaulting to "r").
105 * Possible values for _mode_ are "r" for read-only access,
106 * "w" for write-only access and "r+" for read/write access.
109 c_init (int argc, VALUE *argv, VALUE self)
111 VALUE file = Qnil, mode = Qnil;
112 Eet_File **ef = NULL;
113 Eet_File_Mode m = EET_FILE_MODE_READ;
114 const char *tmp, *cfile;
116 Data_Get_Struct (self, Eet_File *, ef);
118 rb_scan_args (argc, argv, "11", &file, &mode);
120 cfile = StringValuePtr (file);
123 tmp = StringValuePtr (mode);
124 if (!strcmp (tmp, "r+"))
125 m = EET_FILE_MODE_READ_WRITE;
126 else if (!strcmp (tmp, "w"))
127 m = EET_FILE_MODE_WRITE;
128 else if (strcmp (tmp, "r"))
129 rb_raise (rb_eArgError, "illegal access mode %s", tmp);
134 *ef = eet_open (cfile, m);
137 case EET_FILE_MODE_READ_WRITE:
138 case EET_FILE_MODE_WRITE:
139 tmp = "Permission denied - %s";
142 tmp = "File not found - %s";
146 rb_raise (rb_eRuntimeError, tmp, cfile);
156 * Closes _ef_ and flushes any pending writes.
157 * _ef_ is unavailable for any further data operations;
158 * an +IOError+ is raised if such an attempt is made.
160 * Eet::File objects are automatically closed when they
161 * are claimed by the garbage collector.
166 Eet_File **ef = NULL;
168 Data_Get_Struct (self, Eet_File *, ef);
181 * ef.list([glob]) -> array
183 * Returns an Array of entries in _ef_ that match the shell glob
184 * _glob_ (defaulting to "*").
187 c_list (int argc, VALUE *argv, VALUE self)
189 VALUE glob = Qnil, ret;
190 Eet_File **ef = NULL;
191 char **entries, *tmp = "*";
194 Data_Get_Struct (self, Eet_File *, ef);
197 switch (eet_mode_get (*ef)) {
198 case EET_FILE_MODE_READ:
199 case EET_FILE_MODE_READ_WRITE:
202 rb_raise (rb_eIOError, "cannot list entries");
205 rb_scan_args (argc, argv, "01", &glob);
208 tmp = StringValuePtr (glob);
212 entries = eet_list (*ef, tmp, &count);
214 for (i = 0; i < count; i++)
215 rb_ary_push (ret, rb_str_new2 (entries[i]));
224 * ef.delete(key) -> ef
226 * Deletes the entry from _ef_ that is stored as _key_.
227 * If the entry cannot be deleted, an +IOError+ is raised,
228 * otherwise _ef_ is returned.
231 c_delete (VALUE self, VALUE key)
233 Eet_File **ef = NULL;
236 Data_Get_Struct (self, Eet_File *, ef);
239 ckey = StringValuePtr (key);
242 if (!eet_delete (*ef, ckey))
243 rb_raise (rb_eIOError, "cannot delete entry - %s", ckey);
250 * ef.read(key) -> string
252 * Reads an entry from _ef_ that is stored as _key_.
253 * If the data cannot be read, an +IOError+ is raised,
254 * otherwise a String is returned that contains the data.
257 c_read (VALUE self, VALUE key)
260 Eet_File **ef = NULL;
265 Data_Get_Struct (self, Eet_File *, ef);
268 ckey = StringValuePtr (key);
271 data = eet_read (*ef, ckey, &size);
273 rb_raise (rb_eIOError, "cannot read entry - %s", ckey);
275 ret = rb_str_new (data, size);
284 * ef.write(key, data [, compress] ) -> integer
286 * Stores _data_ in _ef_ as _key_.
287 * If _compress_ is true (which is the default), the data will be
289 * If the data cannot be written, an +IOError+ is raised,
290 * otherwise a the number of bytes written is returned.
293 c_write (int argc, VALUE *argv, VALUE self)
295 VALUE key = Qnil, buf = Qnil, comp = Qnil;
296 Eet_File **ef = NULL;
300 Data_Get_Struct (self, Eet_File *, ef);
303 rb_scan_args (argc, argv, "21", &key, &buf, &comp);
308 ckey = StringValuePtr (key);
310 cbuf = StringValuePtr (buf);
312 n = eet_write (*ef, ckey,
313 cbuf, RSTRING (buf)->len,
316 rb_raise (rb_eIOError, "couldn't write to file");
323 * ef.read_image(key) -> array
325 * Reads an image entry from _ef_ that is stored as _key_.
326 * If the data cannot be read, an +IOError+ is raised,
327 * otherwise an Array is returned that contains the image data,
328 * the image width, the image height, a boolean that indicates
329 * whether the image has an alpha channel or not and a hash
330 * that contains the compression options that were used to store
331 * the image (see Eet::File#write_image).
334 c_read_image (VALUE self, VALUE key)
337 Eet_File **ef = NULL;
340 int w = 0, h = 0, has_alpha = 0, level = 0, quality = 0, lossy = 0;
342 Data_Get_Struct (self, Eet_File *, ef);
345 ckey = StringValuePtr (key);
348 data = eet_data_image_read (*ef, ckey, &w, &h,
349 &has_alpha, &level, &quality,
352 rb_raise (rb_eIOError, "cannot read entry - %s", ckey);
354 comp = rb_hash_new ();
355 rb_hash_aset (comp, ID2SYM (rb_intern ("lossy")), INT2FIX (lossy));
356 rb_hash_aset (comp, ID2SYM (rb_intern ("level")), INT2FIX (level));
357 rb_hash_aset (comp, ID2SYM (rb_intern ("quality")), INT2FIX (quality));
359 ret = rb_ary_new3 (5, rb_str_new (data, w * h * 4),
360 INT2FIX (w), INT2FIX (h),
361 has_alpha ? Qtrue : Qfalse, comp);
369 * ef.write_image(key, image_data, w, h [, alpha] [, comp] ) -> integer
371 * Stores _image_data_ with width _w_ and height _h_ in _ef_ as _key_.
372 * Pass true for _alpha_ if the image contains an alpha channel.
373 * You can control how the image is compressed by passing _comp_, which
374 * is a hash whose :lossy entry is true if the image should be
375 * compressed lossily. If :lossy is true, the :quality entry
376 * (0-100, default 100) sets the compression quality.
377 * If :lossy is false, the :level entry (0-9, default 9) sets the
378 * compression level. If _comp_ isn't passed, then the
379 * image is stored losslessly.
380 * If the data cannot be written, an +IOError+ is raised,
381 * otherwise a the number of bytes written is returned.
384 c_write_image (int argc, VALUE *argv, VALUE self)
386 VALUE key = Qnil, buf = Qnil, w = Qnil, h = Qnil, has_alpha = Qnil;
387 VALUE comp = Qnil, tmp;
388 Eet_File **ef = NULL;
390 int n, lossy = 0, level = 9, quality = 100;
392 Data_Get_Struct (self, Eet_File *, ef);
395 rb_scan_args (argc, argv, "42", &key, &buf, &w, &h, &has_alpha,
398 if (NIL_P (has_alpha))
401 ckey = StringValuePtr (key);
403 cbuf = StringValuePtr (buf);
404 Check_Type (w, T_FIXNUM);
405 Check_Type (h, T_FIXNUM);
408 Check_Type (comp, T_HASH);
410 tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("lossy")));
412 lossy = FIX2INT (tmp);
414 tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("level")));
416 level = FIX2INT (tmp);
418 tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("quality")));
420 quality = FIX2INT (tmp);
423 if (!RSTRING (buf)->len)
426 n = eet_data_image_write (*ef, ckey, cbuf,
427 FIX2INT (w), FIX2INT (h),
429 level, quality, lossy);
431 rb_raise (rb_eIOError, "couldn't write to file");
437 stream_serialize (VALUE self)
440 struct RArray *stream;
443 ret = rb_str_new2 ("");
445 stream = RARRAY (self);
449 for (i = 0; i < stream->len; i++) {
450 VALUE str = rb_funcall (stream->ptr[i], id_serialize, 0, NULL);
452 rb_str_append (ret, str);
459 chunk_init (VALUE self, VALUE tag, VALUE data)
466 if (rb_funcall (tag, id_include, 1, INT2FIX (0)) == Qtrue) \
467 rb_raise (rb_eArgError, "tag must not contain binary zeroes");
469 /* libeet uses a signed 32bit integer to store the
470 * chunk size, so make sure we don't overflow it
472 len = RSTRING (tag)->len + 1 + RSTRING (data)->len;
473 if (len < 0 || len >= 2147483647L)
474 rb_raise (rb_eArgError, "tag or data too long");
476 rb_ivar_set (self, id_tag, rb_str_dup_frozen (tag));
477 rb_ivar_set (self, id_data, rb_str_dup_frozen (data));
483 chunk_serialize (VALUE self)
486 unsigned int size, buf_len;
488 struct RString *tag, *data;
490 tmp = rb_ivar_get (self, id_tag);
494 tmp = rb_ivar_get (self, id_data);
496 data = RSTRING (tmp);
498 buf_len = 9 + tag->len + data->len;
499 ret = rb_str_buf_new (buf_len);
501 buf = RSTRING (ret)->ptr;
502 RSTRING (ret)->len = buf_len;
504 memcpy (buf, "CHnK", 4);
507 size = tag->len + data->len + 1;
508 size = BSWAP32 (size);
509 memcpy (buf, &size, 4);
512 memcpy (buf, tag->ptr, tag->len);
517 memcpy (buf, data->ptr, data->len);
523 for_each_prop (VALUE tag, VALUE arg, VALUE stream)
525 VALUE value, type, chunks;
528 if (rb_obj_is_kind_of (arg, rb_cArray) == Qfalse)
529 rb_raise (ePropError, "hash value is not an array");
531 value = rb_ary_entry (arg, 0);
535 type = rb_ary_entry (arg, 1);
536 chunks = rb_funcall (value, id_to_eet_chunks, 2, tag, type);
538 len = RARRAY (chunks)->len;
540 for (i = 0; i < len; i++)
541 rb_funcall (stream, id_push, 1, rb_ary_entry (chunks, i));
548 * object.to_eet -> string
550 * Serializes the receiver to EET format.
553 c_to_eet (VALUE self)
555 VALUE props, name, stream, chunk, args[2];
557 props = rb_funcall (self, id_to_eet_properties, 0);
559 if (rb_obj_is_kind_of (props, rb_cHash) == Qfalse ||
560 rb_funcall (props, id_empty, 0) == Qtrue)
561 rb_raise (ePropError, "invalid EET properties");
563 name = rb_funcall (self, id_to_eet_name, 0);
566 if (rb_funcall (name, id_include, 1, INT2FIX (0)) == Qtrue ||
567 rb_funcall (name, id_empty, 0) == Qtrue)
568 rb_raise (eNameError, "invalid EET name");
570 stream = rb_class_new_instance (0, NULL, cStream);
572 rb_hash_foreach (props, for_each_prop, stream);
575 args[1] = rb_funcall (stream, id_serialize, 0);
576 chunk = rb_class_new_instance (2, args, cChunk);
578 stream = rb_class_new_instance (1, &chunk, cStream);
580 return rb_funcall (stream, id_serialize, 0);
588 m = rb_define_module ("Eet");
590 c = rb_define_class_under (m, "File", rb_cObject);
591 rb_define_alloc_func (c, c_alloc);
592 rb_define_singleton_method (c, "open", c_open, -1);
593 rb_define_method (c, "initialize", c_init, -1);
594 rb_define_method (c, "close", c_close, 0);
595 rb_define_method (c, "list", c_list, -1);
596 rb_define_method (c, "delete", c_delete, 1);
597 rb_define_method (c, "read", c_read, 1);
598 rb_define_method (c, "write", c_write, -1);
599 rb_define_method (c, "read_image", c_read_image, 1);
600 rb_define_method (c, "write_image", c_write_image, -1);
602 cStream = rb_define_class_under (m, "Stream", rb_cArray);
603 rb_define_method (cStream, "serialize", stream_serialize, 0);
605 cChunk = rb_define_class_under (m, "Chunk", rb_cObject);
606 rb_define_method (cChunk, "initialize", chunk_init, 2);
607 rb_define_method (cChunk, "serialize", chunk_serialize, 0);
609 rb_define_attr (cChunk, "tag", 1, 0);
610 rb_define_attr (cChunk, "data", 1, 0);
612 rb_define_method (rb_cObject, "to_eet", c_to_eet, 0);
614 eEetError = rb_define_class_under (m, "EetError", rb_eStandardError);
615 eNameError = rb_define_class_under (m, "NameError", eEetError);
616 ePropError = rb_define_class_under (m, "PropertyError", eEetError);
618 id_include = rb_intern ("include?");
619 id_serialize = rb_intern ("serialize");
620 id_push = rb_intern ("push");
621 id_empty = rb_intern ("empty?");
622 id_to_eet_chunks = rb_intern ("to_eet_chunks");
623 id_to_eet_name = rb_intern ("to_eet_name");
624 id_to_eet_properties = rb_intern ("to_eet_properties");
625 id_tag = rb_intern ("@tag");
626 id_data = rb_intern ("@data");