Made Ogg::Vorbis::Tagger#close return nil.
[ruby-vorbistagger.git] / ext / ext.c
index 67fa6b14b24811514db40966ca373d1050c9bbf8..46ce0d178d1090296f6a74b16e29b205e692c706 100644 (file)
--- a/ext/ext.c
+++ b/ext/ext.c
 #include "vcedit.h"
 #include "comments.h"
 
+#define CHECK_CLOSED(o) \
+       if (!o->state) \
+               rb_raise (eClosed, "closed stream");
+
 typedef struct {
        vcedit_state *state;
        VALUE comments;
@@ -32,8 +36,8 @@ typedef struct {
 
 static VALUE c_close (VALUE self);
 
-static VALUE cComments, eVT, eOpen, eInvalidData, eInvalidComment,
-             eTempFile, eReopen;
+static VALUE cComments, eVT, eClosed, eOpen, eInvalidData,
+             eInvalidComment, eTempFile, eReopen;
 static ID id_length;
 
 static void
@@ -138,9 +142,11 @@ c_init (VALUE self, VALUE filename)
 
 /*
  * call-seq:
- *  object.close -> object
+ *  object.close -> nil
  *
- * Closes *object* and returns it.
+ * Closes *object*. Further method calls on *object* will raise an
+ * Ogg::Vorbis::Tagger::ClosedStreamError exception.
+ * Returns +nil+.
  */
 static VALUE
 c_close (VALUE self)
@@ -149,10 +155,12 @@ c_close (VALUE self)
 
        Data_Get_Struct (self, RbVorbisTagger, o);
 
+       CHECK_CLOSED (o);
+
        vcedit_state_unref (o->state);
        o->state = NULL;
 
-       return self;
+       return Qnil;
 }
 
 /*
@@ -169,6 +177,8 @@ c_write (VALUE self)
 
        Data_Get_Struct (self, RbVorbisTagger, o);
 
+       CHECK_CLOSED (o);
+
        comments_sync (o->comments, o->state);
 
        switch (vcedit_write (o->state)) {
@@ -199,6 +209,8 @@ c_comments (VALUE self)
 
        Data_Get_Struct (self, RbVorbisTagger, o);
 
+       CHECK_CLOSED (o);
+
        return o->comments;
 }
 
@@ -225,6 +237,7 @@ Init_vorbistagger_ext (void)
        rb_define_method (cVT, "comments", c_comments, 0);
 
        eVT = rb_define_class_under (cVT, "TaggerError", eVorbis);
+       eClosed = rb_define_class_under (cVT, "ClosedStreamError", eVT);
        eOpen = rb_define_class_under (cVT, "OpenError", eVT);
        eInvalidData = rb_define_class_under (cVT, "InvalidDataError", eVT);
        eInvalidComment = rb_define_class_under (cVT, "InvalidCommentError",