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 vc = vcedit_comments (o->state);
108 rb_raise (eVTError, "vcedit_comments() failed - %s",
109 vcedit_error (o->state));
111 /* check whether all comments are well-formed */
112 for (i = 0; i < vc->comments; i++) {
113 char *ptr, *content = vc->user_comments[i];
115 ptr = strchr (content, '=');
116 if (!ptr || ptr == content)
117 rb_raise (eVTError, "malformed comment - %s", content);
120 o->comments = rb_class_new_instance (0, NULL, cComments);
122 comments_init (o->comments, o->state);
129 * object.close -> object
131 * Closes *object* and returns it.
138 Data_Get_Struct (self, RbVorbisTagger, o);
140 vcedit_state_unref (o->state);
148 * object.write -> integer
150 * Writes the comments from *object* back to the IO object and
151 * returns the numbers of comments written.
158 Data_Get_Struct (self, RbVorbisTagger, o);
160 comments_sync (o->comments, o->state);
162 if (vcedit_write (o->state) < 0)
163 rb_raise (rb_eIOError, "write failed - %s",
164 vcedit_error (o->state));
166 return rb_funcall (o->comments, id_length, 0);
171 * object.comments -> comments
173 * Returns the comments collection of *object*, which is an instance of
174 * Ogg::Vorbis::Comments.
177 c_comments (VALUE self)
181 Data_Get_Struct (self, RbVorbisTagger, o);
188 Init_vorbistagger_ext (void)
190 VALUE mOgg, mVorbis, eOgg, eVorbis, cVT;
192 mOgg = rb_define_module ("Ogg");
193 mVorbis = rb_define_module_under (mOgg, "Vorbis");
195 eOgg = rb_define_class_under (mOgg, "OggError", rb_eStandardError);
196 eVorbis = rb_define_class_under (mVorbis, "VorbisError", eOgg);
198 cVT = rb_define_class_under (mVorbis, "Tagger", rb_cObject);
200 rb_define_alloc_func (cVT, c_alloc);
202 rb_define_singleton_method (cVT, "open", c_open, 1);
203 rb_define_method (cVT, "initialize", c_init, 1);
204 rb_define_method (cVT, "write", c_write, 0);
205 rb_define_method (cVT, "comments", c_comments, 0);
207 eVTError = rb_define_class_under (cVT, "TaggerError", eVorbis);
209 id_length = rb_intern ("length");
211 cComments = Init_Comments (mVorbis);