| Class | Ogg::Vorbis::Comments |
| In: |
ext/ext.c
|
| Parent: | Object |
Creates an Ogg::Vorbis::Comments object.
/*
* call-seq:
* Ogg::Vorbis::Comments.new -> object
*
* Creates an Ogg::Vorbis::Comments object.
*/
static VALUE
c_init (VALUE self)
{
rb_iv_set (self, "items", rb_ary_new ());
return self;
}
Compares object to other and returns -1, 0 or 1 if object is less than, equal or greater than other.
/*
* call-seq:
* object <=> other -> -1, 0 or 1
*
* Compares *object* to *other* and returns -1, 0 or 1 if
* *object* is less than, equal or greater than *other*.
*/
static VALUE
c_compare (VALUE self, VALUE other)
{
struct RArray *a, *b;
int i, j;
if (rb_obj_is_kind_of (other, CLASS_OF (self)) != Qtrue)
rb_raise (rb_eArgError, "invalid argument");
a = RARRAY (rb_iv_get (self, "items"));
b = RARRAY (rb_iv_get (other, "items"));
if (a->len < b->len)
return -1;
if (b->len < a->len)
return 1;
for (i = 0; i < a->len; i++) {
struct RArray *aa = RARRAY (a->ptr[i]);
struct RArray *bb = RARRAY (b->ptr[i]);
for (j = 0; j < 2; j++) {
VALUE tmp;
tmp = rb_funcall (aa->ptr[j], id_compare, 1, bb->ptr[j]);
if (FIX2INT (tmp) != 0)
return tmp;
}
}
return INT2FIX (0);
}
Returns the value of the tag with the key key or nil if the tag cannot be found.
/*
* call-seq:
* object[key] -> string
*
* Returns the value of the tag with the key *key* or +nil+ if the
* tag cannot be found.
*/
static VALUE
c_aref (VALUE self, VALUE key)
{
struct RArray *items;
int i;
items = RARRAY (rb_iv_get (self, "items"));
for (i = 0; i < items->len; i++) {
struct RArray *pair = RARRAY (items->ptr[i]);
VALUE tmp;
tmp = rb_funcall (pair->ptr[0], id_casecmp, 1, key);
if (tmp == INT2FIX (0))
return pair->ptr[1];
}
return Qnil;
}
Sets the value of the tag with the key key to string.
/*
* call-seq:
* object[key] = string
*
* Sets the value of the tag with the key *key* to *string*.
*/
static VALUE
c_aset (VALUE self, VALUE key, VALUE value)
{
VALUE tmp;
struct RArray *items;
int i;
items = RARRAY (rb_iv_get (self, "items"));
for (i = 0; i < items->len; i++) {
struct RArray *pair = RARRAY (items->ptr[i]);
VALUE tmp;
tmp = rb_funcall (pair->ptr[0], id_casecmp, 1, key);
if (tmp == INT2FIX (0)) {
rb_funcall (pair->ptr[1], id_replace, 1, value);
return pair->ptr[1];
}
}
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;
}
Removes all elements from object and returns it.
/*
* call-seq:
* object.clear -> object
*
* Removes all elements from *object* and returns it.
*/
static VALUE
c_clear (VALUE self)
{
rb_ary_clear (rb_iv_get (self, "items"));
return self;
}
If a tag with the specified key exists, that tag is deleted and the tag‘s value is returned. Otherwise, nil is returned.
/*
* call-seq:
* object.delete(key) -> string or nil
*
* If a tag with the specified key exists, that tag is deleted and
* the tag's value is returned. Otherwise, +nil+ is returned.
*/
static VALUE
c_delete (VALUE self, VALUE key)
{
VALUE ret = Qnil;
struct RArray *items;
int i, pos = -1;
items = RARRAY (rb_iv_get (self, "items"));
for (i = 0; i < items->len; i++) {
struct RArray *pair = RARRAY (items->ptr[i]);
VALUE tmp;
tmp = rb_funcall (pair->ptr[0], id_casecmp, 1, key);
if (tmp == INT2FIX (0)) {
ret = pair->ptr[1];
pos = i;
break;
}
}
if (pos != -1)
rb_ary_delete_at (rb_iv_get (self, "items"), pos);
return ret;
}
Calls block once for each tag in object, passing the key and value of the tag. Returns object.
/*
* call-seq:
* object.each { |key, value| block } -> object
*
* Calls _block_ once for each tag in *object*, passing the key and
* value of the tag.
* Returns *object*.
*/
static VALUE
c_each (VALUE self)
{
struct RArray *items;
int i;
items = RARRAY (rb_iv_get (self, "items"));
for (i = 0; i < items->len; i++) {
struct RArray *pair = RARRAY (items->ptr[i]);
rb_yield_values (2, pair->ptr[0], pair->ptr[1]);
}
return self;
}
Calls block once for each tag in object, passing the key of the tag. Returns object.
/*
* call-seq:
* object.each_key { |key| block } -> object
*
* Calls _block_ once for each tag in *object*, passing the key
* of the tag.
* Returns *object*.
*/
static VALUE
c_each_key (VALUE self)
{
struct RArray *items;
int i;
items = RARRAY (rb_iv_get (self, "items"));
for (i = 0; i < items->len; i++) {
struct RArray *pair = RARRAY (items->ptr[i]);
rb_yield (pair->ptr[0]);
}
return self;
}
Calls block once for each tag in object, passing the value of the tag. Returns object.
/*
* call-seq:
* object.each_value { |value| block } -> object
*
* Calls _block_ once for each tag in *object*, passing the value
* of the tag. Returns *object*.
*/
static VALUE
c_each_value (VALUE self)
{
struct RArray *items;
int i;
items = RARRAY (rb_iv_get (self, "items"));
for (i = 0; i < items->len; i++) {
struct RArray *pair = RARRAY (items->ptr[i]);
rb_yield (pair->ptr[1]);
}
return self;
}
Returns true if object is empty or false otherwise.
/*
* call-seq:
* object.empty? -> true or false
*
* Returns true if *object* is empty or false otherwise.
*/
static VALUE
c_get_empty (VALUE self)
{
struct RArray *items;
items = RARRAY (rb_iv_get (self, "items"));
return items->len ? Qfalse : Qtrue;
}
Returns true if a tag exists with the specified key or false otherwise.
/*
* call-seq:
* object.has_key?(key) -> true or false
*
* Returns true if a tag exists with the specified key or false
* otherwise.
*/
static VALUE
c_has_key (VALUE self, VALUE key)
{
struct RArray *items;
int i;
items = RARRAY (rb_iv_get (self, "items"));
for (i = 0; i < items->len; i++) {
struct RArray *pair = RARRAY (items->ptr[i]);
VALUE tmp;
tmp = rb_funcall (pair->ptr[0], id_casecmp, 1, key);
if (tmp == INT2FIX (0))
return Qtrue;
}
return Qfalse;
}
Returns the contents of object as a string.
/*
* call-seq:
* object.inspect -> string
*
* Returns the contents of *object* as a string.
*/
static VALUE
c_inspect (VALUE self)
{
VALUE ret;
struct RArray *items;
int i;
items = RARRAY (rb_iv_get (self, "items"));
ret = rb_str_buf_new (128);
rb_str_buf_cat (ret, "{", 1);
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]));
}
rb_str_buf_cat (ret, "}", 1);
return ret;
}
Returns an array that contains the keys of the tags in object.
/*
* call-seq:
* object.keys -> array
*
* Returns an array that contains the keys of the tags in *object*.
*/
static VALUE
c_keys (VALUE self)
{
VALUE ret;
struct RArray *items;
int i;
items = RARRAY (rb_iv_get (self, "items"));
ret = rb_ary_new2 (items->len);
for (i = 0; i < items->len; i++) {
struct RArray *pair = RARRAY (items->ptr[i]);
rb_ary_store (ret, i, pair->ptr[0]);
}
return ret;
}
Returns the number of tags in object.
/*
* call-seq:
* object.length -> integer
*
* Returns the number of tags in *object*.
*/
static VALUE
c_length (VALUE self)
{
struct RArray *items;
items = RARRAY (rb_iv_get (self, "items"));
return LONG2NUM (items->len);
}
Adds the key-value pairs from arg to object, overwriting existing values if a key already existed in object.
Note that arg‘s each method needs to yield key-value pairs for this to work. This means that e.g. hashes and Ogg::Vorbis::Comments objects are supported as arguments.
/*
* call-seq:
* object.merge!(arg) -> object
*
* Adds the key-value pairs from *arg* to *object*, overwriting existing
* values if a key already existed in *object*.
*
* Note that *arg*'s each method needs to yield key-value pairs for this
* to work. This means that e.g. hashes and Ogg::Vorbis::Comments objects
* are supported as arguments.
*/
static VALUE
c_merge (VALUE self, VALUE arg)
{
if (!rb_respond_to (arg, id_each))
rb_raise (rb_eArgError, "invalid argument");
rb_iterate (rb_each, arg, merge_cb, self);
return self;
}
Removes the first key-value pair from object and returns it as the two-item array [key, value]. If object is empty, nil is returned.
/*
* call-seq:
* object.shift(hash) -> array or nil
*
* Removes the first key-value pair from *object* and returns it
* as the two-item array [key, value].
* If *object* is empty, +nil+ is returned.
*/
static VALUE
c_shift (VALUE self)
{
return rb_ary_shift (rb_iv_get (self, "items"));
}
Returns an array that contains the values of the tags in object.
/*
* call-seq:
* object.values -> array
*
* Returns an array that contains the values of the tags in *object*.
*/
static VALUE
c_values (VALUE self)
{
VALUE ret;
struct RArray *items;
int i;
items = RARRAY (rb_iv_get (self, "items"));
ret = rb_ary_new2 (items->len);
for (i = 0; i < items->len; i++) {
struct RArray *pair = RARRAY (items->ptr[i]);
rb_ary_store (ret, i, pair->ptr[1]);
}
return ret;
}