X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=ext%2Fcomments.c;h=a2569866a62766062a292d9719f0233fd75a1185;hb=0ebbf4f00aa8b375b4a781b0a0397d0fce5466e1;hp=98dd73c4e04b34ccf1876102bab67f2be59bcacc;hpb=310e91e26bebf168b6081e91da123b4a8cfb9b67;p=ruby-vorbistagger.git diff --git a/ext/comments.c b/ext/comments.c index 98dd73c..a256986 100644 --- a/ext/comments.c +++ b/ext/comments.c @@ -23,32 +23,22 @@ #include "vcedit.h" -typedef struct { - vcedit_state *state; - - VALUE items; -} RbVorbisComments; - static ID id_casecmp, id_replace, id_compare; void comments_init (VALUE self, vcedit_state *state) { - RbVorbisComments *o; + VALUE items; vorbis_comment *vc; int i; - Data_Get_Struct (self, RbVorbisComments, o); - - o->state = state; - vcedit_state_ref (state); - - vc = vcedit_comments (o->state); + vc = vcedit_comments (state); - o->items = rb_ary_new2 (vc->comments); + items = rb_ary_new2 (vc->comments); + rb_iv_set (self, "items", items); for (i = 0; i < vc->comments; i++) { - VALUE k, v; + VALUE k, v, pair; char *ptr, *content = vc->user_comments[i]; ptr = strchr (content, '='); @@ -59,29 +49,26 @@ comments_init (VALUE self, vcedit_state *state) v = rb_str_new2 (ptr + 1); - rb_ary_store (o->items, i, - rb_ary_new3 (2, k, v)); - } + pair = rb_ary_new3 (2, k, v); + OBJ_FREEZE (pair); - rb_iv_set (self, "@items", o->items); + rb_ary_store (items, i, pair); + } } void -comments_sync (VALUE self) +comments_sync (VALUE self, vcedit_state *state) { - RbVorbisComments *o; vorbis_comment *vc; struct RArray *items; int i; - Data_Get_Struct (self, RbVorbisComments, o); - - vc = vcedit_comments (o->state); + vc = vcedit_comments (state); vorbis_comment_clear (vc); vorbis_comment_init (vc); - items = RARRAY (o->items); + items = RARRAY (rb_iv_get (self, "items")); for (i = 0; i < items->len; i++) { struct RArray *pair = RARRAY (items->ptr[i]); @@ -92,28 +79,6 @@ comments_sync (VALUE self) } } -static void -c_mark (RbVorbisComments *o) -{ - rb_gc_mark (o->items); -} - -static void -c_free (RbVorbisComments *o) -{ - vcedit_state_unref (o->state); - - ruby_xfree (o); -} - -static VALUE -c_alloc (VALUE klass) -{ - RbVorbisComments *o; - - return Data_Make_Struct (klass, RbVorbisComments, c_mark, c_free, o); -} - /* * call-seq: * object.inspect -> string @@ -124,13 +89,10 @@ static VALUE c_inspect (VALUE self) { VALUE ret; - RbVorbisComments *o; struct RArray *items; int i; - Data_Get_Struct (self, RbVorbisComments, o); - - items = RARRAY (o->items); + items = RARRAY (rb_iv_get (self, "items")); ret = rb_str_buf_new (128); rb_str_buf_cat (ret, "{", 1); @@ -138,12 +100,12 @@ c_inspect (VALUE self) for (i = 0; i < items->len; i++) { struct RArray *pair = RARRAY (items->ptr[i]); + if (i) + rb_str_buf_cat (ret, ", ", 2); + rb_str_buf_append (ret, rb_inspect (pair->ptr[0])); rb_str_buf_cat (ret, "=>", 2); rb_str_buf_append (ret, rb_inspect (pair->ptr[1])); - - if (i < items->len - 1) - rb_str_buf_cat (ret, ", ", 2); } rb_str_buf_cat (ret, "}", 1); @@ -160,11 +122,7 @@ c_inspect (VALUE self) static VALUE c_clear (VALUE self) { - RbVorbisComments *o; - - Data_Get_Struct (self, RbVorbisComments, o); - - rb_ary_clear (o->items); + rb_ary_clear (rb_iv_get (self, "items")); return self; } @@ -180,13 +138,10 @@ static VALUE c_delete (VALUE self, VALUE key) { VALUE ret = Qnil; - RbVorbisComments *o; struct RArray *items; int i, pos = -1; - Data_Get_Struct (self, RbVorbisComments, o); - - items = RARRAY (o->items); + items = RARRAY (rb_iv_get (self, "items")); for (i = 0; i < items->len; i++) { struct RArray *pair = RARRAY (items->ptr[i]); @@ -201,7 +156,7 @@ c_delete (VALUE self, VALUE key) } if (pos != -1) - rb_ary_delete_at (o->items, pos); + rb_ary_delete_at (rb_iv_get (self, "items"), pos); return ret; } @@ -216,13 +171,10 @@ static VALUE c_keys (VALUE self) { VALUE ret; - RbVorbisComments *o; struct RArray *items; int i; - Data_Get_Struct (self, RbVorbisComments, o); - - items = RARRAY (o->items); + items = RARRAY (rb_iv_get (self, "items")); ret = rb_ary_new2 (items->len); for (i = 0; i < items->len; i++) { @@ -244,13 +196,10 @@ static VALUE c_values (VALUE self) { VALUE ret; - RbVorbisComments *o; struct RArray *items; int i; - Data_Get_Struct (self, RbVorbisComments, o); - - items = RARRAY (o->items); + items = RARRAY (rb_iv_get (self, "items")); ret = rb_ary_new2 (items->len); for (i = 0; i < items->len; i++) { @@ -271,11 +220,11 @@ c_values (VALUE self) static VALUE c_length (VALUE self) { - RbVorbisComments *o; + struct RArray *items; - Data_Get_Struct (self, RbVorbisComments, o); + items = RARRAY (rb_iv_get (self, "items")); - return LONG2NUM (RARRAY (o->items)->len); + return LONG2NUM (items->len); } /* @@ -287,11 +236,11 @@ c_length (VALUE self) static VALUE c_get_empty (VALUE self) { - RbVorbisComments *o; + struct RArray *items; - Data_Get_Struct (self, RbVorbisComments, o); + items = RARRAY (rb_iv_get (self, "items")); - return !RARRAY(o->items)->len; + return items->len ? Qfalse : Qtrue; } /* @@ -304,13 +253,10 @@ c_get_empty (VALUE self) static VALUE c_aref (VALUE self, VALUE key) { - RbVorbisComments *o; struct RArray *items; int i; - Data_Get_Struct (self, RbVorbisComments, o); - - items = RARRAY (o->items); + items = RARRAY (rb_iv_get (self, "items")); for (i = 0; i < items->len; i++) { struct RArray *pair = RARRAY (items->ptr[i]); @@ -333,13 +279,11 @@ c_aref (VALUE self, VALUE key) static VALUE c_aset (VALUE self, VALUE key, VALUE value) { - RbVorbisComments *o; + VALUE tmp; struct RArray *items; int i; - Data_Get_Struct (self, RbVorbisComments, o); - - items = RARRAY (o->items); + items = RARRAY (rb_iv_get (self, "items")); for (i = 0; i < items->len; i++) { struct RArray *pair = RARRAY (items->ptr[i]); @@ -352,7 +296,10 @@ c_aset (VALUE self, VALUE key, VALUE value) } } - rb_ary_push (o->items, rb_ary_new3 (2, key, value)); + tmp = rb_ary_new3 (2, rb_str_dup_frozen (key), value); + OBJ_FREEZE (tmp); + + rb_ary_push (rb_iv_get (self, "items"), tmp); return value; } @@ -367,13 +314,10 @@ c_aset (VALUE self, VALUE key, VALUE value) static VALUE c_has_key (VALUE self, VALUE key) { - RbVorbisComments *o; struct RArray *items; int i; - Data_Get_Struct (self, RbVorbisComments, o); - - items = RARRAY (o->items); + items = RARRAY (rb_iv_get (self, "items")); for (i = 0; i < items->len; i++) { struct RArray *pair = RARRAY (items->ptr[i]); @@ -397,18 +341,14 @@ c_has_key (VALUE self, VALUE key) static VALUE c_compare (VALUE self, VALUE other) { - RbVorbisComments *o, *o2; struct RArray *a, *b; int i, j; if (rb_obj_is_kind_of (other, CLASS_OF (self)) != Qtrue) rb_raise (rb_eArgError, "invalid argument"); - Data_Get_Struct (self, RbVorbisComments, o); - Data_Get_Struct (other, RbVorbisComments, o2); - - a = RARRAY (o->items); - b = RARRAY (o2->items); + a = RARRAY (rb_iv_get (self, "items")); + b = RARRAY (rb_iv_get (other, "items")); if (a->len < b->len) return -1; @@ -443,13 +383,10 @@ c_compare (VALUE self, VALUE other) static VALUE c_each (VALUE self) { - RbVorbisComments *o; struct RArray *items; int i; - Data_Get_Struct (self, RbVorbisComments, o); - - items = RARRAY (o->items); + items = RARRAY (rb_iv_get (self, "items")); for (i = 0; i < items->len; i++) { struct RArray *pair = RARRAY (items->ptr[i]); @@ -471,13 +408,10 @@ c_each (VALUE self) static VALUE c_each_key (VALUE self) { - RbVorbisComments *o; struct RArray *items; int i; - Data_Get_Struct (self, RbVorbisComments, o); - - items = RARRAY (o->items); + items = RARRAY (rb_iv_get (self, "items")); for (i = 0; i < items->len; i++) { struct RArray *pair = RARRAY (items->ptr[i]); @@ -498,13 +432,10 @@ c_each_key (VALUE self) static VALUE c_each_value (VALUE self) { - RbVorbisComments *o; struct RArray *items; int i; - Data_Get_Struct (self, RbVorbisComments, o); - - items = RARRAY (o->items); + items = RARRAY (rb_iv_get (self, "items")); for (i = 0; i < items->len; i++) { struct RArray *pair = RARRAY (items->ptr[i]); @@ -522,8 +453,6 @@ Init_Comments (VALUE mVorbis) c = rb_define_class_under (mVorbis, "Comments", rb_cObject); - rb_define_alloc_func (c, c_alloc); - rb_define_method (c, "inspect", c_inspect, 0); rb_define_method (c, "clear", c_clear, 0); rb_define_method (c, "delete", c_delete, 1); @@ -545,6 +474,9 @@ Init_Comments (VALUE mVorbis) rb_define_alias (c, "size", "length"); rb_define_alias (c, "each_pair", "each"); + rb_define_alias (c, "key?", "has_key?"); + rb_define_alias (c, "include?", "has_key?"); + rb_define_alias (c, "member?", "has_key?"); id_casecmp = rb_intern ("casecmp"); id_replace = rb_intern ("replace");