Made the Ogg::Vorbis::Tagger object inaccessable after #close was called.
authorTilman Sauerbeck <tilman@code-monkey.de>
Thu, 17 Aug 2006 19:23:07 +0000 (21:23 +0200)
committerTilman Sauerbeck <tilman@code-monkey.de>
Wed, 23 Aug 2006 17:39:15 +0000 (19:39 +0200)
ext/ext.c
test/test_main.rb

index 67fa6b14b24811514db40966ca373d1050c9bbf8..6a45359772e325ff0ca6c75f8a265917cb77df76 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
@@ -149,6 +153,8 @@ c_close (VALUE self)
 
        Data_Get_Struct (self, RbVorbisTagger, o);
 
+       CHECK_CLOSED (o);
+
        vcedit_state_unref (o->state);
        o->state = NULL;
 
@@ -169,6 +175,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 +207,8 @@ c_comments (VALUE self)
 
        Data_Get_Struct (self, RbVorbisTagger, o);
 
+       CHECK_CLOSED (o);
+
        return o->comments;
 }
 
@@ -225,6 +235,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",
index 6da6d6dd556d90920819c3fa914d3032b89f428c..3b577196032c72dff3769a74edb109588e2ef82c 100644 (file)
@@ -253,6 +253,18 @@ EOF
                Ogg::Vorbis::Tagger.new(OGG_FILE).close
        end
 
+       def test_close2
+               assert_raise(Ogg::Vorbis::Tagger::ClosedStreamError) do
+                       Ogg::Vorbis::Tagger.new(OGG_FILE).close.close
+               end
+
+               assert_raise(Ogg::Vorbis::Tagger::ClosedStreamError) do
+                       Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
+                               t.close
+                       end
+               end
+       end
+
        def test_open_non_existing_file
                assert_raises(Ogg::Vorbis::Tagger::OpenError) do
                        Ogg::Vorbis::Tagger.new("foo.bar")