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,
33 static VALUE c_close (VALUE self);
35 static VALUE cComments, eVT, eOpen, eInvalidData, eInvalidComment,
40 c_free (RbVorbisTagger *o)
42 /* just in case the user forgot to call #close himself */
44 vcedit_state_unref (o->state);
50 c_mark (RbVorbisTagger *o)
52 rb_gc_mark (o->comments);
60 return Data_Make_Struct (klass, RbVorbisTagger, c_mark, c_free, o);
65 * Ogg::Vorbis::Tagger.open(arg) -> object
66 * Ogg::Vorbis::Tagger.open(arg) { |object| block } -> object
68 * If a block isn't specified, Ogg::Vorbis::Tagger.open is a synonym
69 * for Ogg::Vorbis::Tagger.new.
70 * If a block is given, it will be invoked with the * Ogg::Vorbis::Tagger
71 * object as a parameter.
74 c_open (VALUE klass, VALUE arg)
76 VALUE obj = rb_class_new_instance (1, &arg, klass);
78 if (rb_block_given_p ())
79 return rb_ensure (rb_yield, obj, c_close, obj);
86 * Ogg::Vorbis::Tagger.new(filename) -> object
88 * Returns a new Ogg::Vorbis::Tagger object for the specified file.
90 * FIXME: add optional mode argument (read-only or read-write)
93 c_init (VALUE self, VALUE filename)
99 Data_Get_Struct (self, RbVorbisTagger, o);
101 StringValue (filename);
103 o->state = vcedit_state_new (StringValuePtr (filename));
105 rb_raise (rb_eNoMemError, "Out of Memory");
107 switch (vcedit_open (o->state)) {
108 case VCEDIT_ERR_OPEN:
109 rb_raise (eOpen, "Cannot open file");
110 case VCEDIT_ERR_INVAL:
111 rb_raise (eInvalidData, "Invalid data");
116 vc = vcedit_comments (o->state);
118 /* vcedit_open() succeeded, so vcedit_comments() cannot
123 /* check whether all comments are well-formed */
124 for (i = 0; i < vc->comments; i++) {
125 char *ptr, *content = vc->user_comments[i];
127 ptr = strchr (content, '=');
128 if (!ptr || ptr == content)
129 rb_raise (eInvalidComment, "invalid comment - %s", content);
132 o->comments = rb_class_new_instance (0, NULL, cComments);
134 comments_init (o->comments, o->state);
141 * object.close -> object
143 * Closes *object* and returns it.
150 Data_Get_Struct (self, RbVorbisTagger, o);
152 vcedit_state_unref (o->state);
160 * object.write -> integer
162 * Writes the comments from *object* back to the IO object and
163 * returns the numbers of comments written.
170 Data_Get_Struct (self, RbVorbisTagger, o);
172 comments_sync (o->comments, o->state);
174 switch (vcedit_write (o->state)) {
175 case VCEDIT_ERR_INVAL:
176 rb_raise (eInvalidData, "Invalid data");
177 case VCEDIT_ERR_TMPFILE:
178 rb_raise (eTempFile, "Cannot create temporary file");
179 case VCEDIT_ERR_REOPEN:
180 rb_raise (eReopen, "Cannot reopen file");
185 return rb_funcall (o->comments, id_length, 0);
190 * object.comments -> comments
192 * Returns the comments collection of *object*, which is an instance of
193 * Ogg::Vorbis::Comments.
196 c_comments (VALUE self)
200 Data_Get_Struct (self, RbVorbisTagger, o);
207 Init_vorbistagger_ext (void)
209 VALUE mOgg, mVorbis, eOgg, eVorbis, cVT;
211 mOgg = rb_define_module ("Ogg");
212 mVorbis = rb_define_module_under (mOgg, "Vorbis");
214 eOgg = rb_define_class_under (mOgg, "OggError", rb_eStandardError);
215 eVorbis = rb_define_class_under (mVorbis, "VorbisError", eOgg);
217 cVT = rb_define_class_under (mVorbis, "Tagger", rb_cObject);
219 rb_define_alloc_func (cVT, c_alloc);
221 rb_define_singleton_method (cVT, "open", c_open, 1);
222 rb_define_method (cVT, "initialize", c_init, 1);
223 rb_define_method (cVT, "close", c_close, 0);
224 rb_define_method (cVT, "write", c_write, 0);
225 rb_define_method (cVT, "comments", c_comments, 0);
227 eVT = rb_define_class_under (cVT, "TaggerError", eVorbis);
228 eOpen = rb_define_class_under (cVT, "OpenError", eVT);
229 eInvalidData = rb_define_class_under (cVT, "InvalidDataError", eVT);
230 eInvalidComment = rb_define_class_under (cVT, "InvalidCommentError",
232 eTempFile = rb_define_class_under (cVT, "TempFileError", eVT);
233 eReopen = rb_define_class_under (cVT, "ReopenError", eVT);
235 id_length = rb_intern ("length");
237 cComments = Init_Comments (mVorbis);