2 * Copyright (C) 2006 Tilman Sauerbeck (tilman at code-monkey de)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation, version 2.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
32 static VALUE c_close (VALUE self);
34 static VALUE cComments, eVTError;
38 c_free (RbVorbisTagger *o)
40 /* just in case the user forgot to call #close himself */
42 vcedit_state_unref (o->state);
48 c_mark (RbVorbisTagger *o)
50 rb_gc_mark (o->comments);
58 return Data_Make_Struct (klass, RbVorbisTagger, c_mark, c_free, o);
63 * Ogg::Vorbis::Tagger.open(arg) -> object
64 * Ogg::Vorbis::Tagger.open(arg) { |object| block } -> object
66 * If a block isn't specified, Ogg::Vorbis::Tagger.open is a synonym
67 * for Ogg::Vorbis::Tagger.new.
68 * If a block is given, it will be invoked with the * Ogg::Vorbis::Tagger
69 * object as a parameter.
72 c_open (VALUE klass, VALUE arg)
74 VALUE obj = rb_class_new_instance (1, &arg, klass);
76 if (rb_block_given_p ())
77 return rb_ensure (rb_yield, obj, c_close, obj);
84 * Ogg::Vorbis::Tagger.new(filename) -> object
86 * Returns a new Ogg::Vorbis::Tagger object for the specified file.
88 * FIXME: add optional mode argument (read-only or read-write)
91 c_init (VALUE self, VALUE filename)
97 Data_Get_Struct (self, RbVorbisTagger, o);
99 StringValue (filename);
101 o->state = vcedit_state_new (StringValuePtr (filename));
103 rb_raise (eVTError, "vcedit_new_state() failed - %s",
104 vcedit_error (o->state));
106 if (vcedit_open (o->state) < 0)
107 rb_raise (eVTError, "vcedit_open() failed - %s",
108 vcedit_error (o->state));
110 vc = vcedit_comments (o->state);
112 rb_raise (eVTError, "vcedit_comments() failed - %s",
113 vcedit_error (o->state));
115 /* check whether all comments are well-formed */
116 for (i = 0; i < vc->comments; i++) {
117 char *ptr, *content = vc->user_comments[i];
119 ptr = strchr (content, '=');
120 if (!ptr || ptr == content)
121 rb_raise (eVTError, "malformed comment - %s", content);
124 o->comments = rb_class_new_instance (0, NULL, cComments);
126 comments_init (o->comments, o->state);
133 * object.close -> object
135 * Closes *object* and returns it.
142 Data_Get_Struct (self, RbVorbisTagger, o);
144 vcedit_state_unref (o->state);
152 * object.write -> integer
154 * Writes the comments from *object* back to the IO object and
155 * returns the numbers of comments written.
162 Data_Get_Struct (self, RbVorbisTagger, o);
164 comments_sync (o->comments, o->state);
166 if (vcedit_write (o->state) < 0)
167 rb_raise (rb_eIOError, "write failed - %s",
168 vcedit_error (o->state));
170 return rb_funcall (o->comments, id_length, 0);
175 * object.comments -> comments
177 * Returns the comments collection of *object*, which is an instance of
178 * Ogg::Vorbis::Comments.
181 c_comments (VALUE self)
185 Data_Get_Struct (self, RbVorbisTagger, o);
192 Init_vorbistagger_ext (void)
194 VALUE mOgg, mVorbis, eOgg, eVorbis, cVT;
196 mOgg = rb_define_module ("Ogg");
197 mVorbis = rb_define_module_under (mOgg, "Vorbis");
199 eOgg = rb_define_class_under (mOgg, "OggError", rb_eStandardError);
200 eVorbis = rb_define_class_under (mVorbis, "VorbisError", eOgg);
202 cVT = rb_define_class_under (mVorbis, "Tagger", rb_cObject);
204 rb_define_alloc_func (cVT, c_alloc);
206 rb_define_singleton_method (cVT, "open", c_open, 1);
207 rb_define_method (cVT, "initialize", c_init, 1);
208 rb_define_method (cVT, "close", c_close, 0);
209 rb_define_method (cVT, "write", c_write, 0);
210 rb_define_method (cVT, "comments", c_comments, 0);
212 eVTError = rb_define_class_under (cVT, "TaggerError", eVorbis);
214 id_length = rb_intern ("length");
216 cComments = Init_Comments (mVorbis);