Deprecated Eet::File#list.
authorTilman Sauerbeck <tilman@code-monkey.de>
Sat, 13 May 2006 10:34:36 +0000 (10:34 +0000)
committerTilman Sauerbeck <tilman@code-monkey.de>
Sat, 13 May 2006 10:34:36 +0000 (10:34 +0000)
Introduced Eet::File#entries and Eet::File#[] as replacements.

ChangeLog
ext/ext.c
lib/eet.rb
test/test_misc.rb

index 0cf4671fc66385981f2a62fe3aee597329fec834..a68b9b846c3be88513f047cc9d0f392a85710df9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,7 +1,12 @@
 --
-$Id: ChangeLog 73 2005-08-25 20:36:00Z tilman $
+$Id: ChangeLog 74 2006-05-13 10:34:36Z tilman $
 ++
 
+2006-05-13 Tilman Sauerbeck (tilman at code-monkey de)
+        * lib/eet.rb, ext/ext.c, test/test_misc.rb: Deprecated
+          Eet::File#list and introduced Eet::File#entries and
+          Eet::File#[] instead.
+
 2005-08-25 Tilman Sauerbeck (tilman at code-monkey de)
         * README: Fixed bad link to project homepage
 
index 4fb3f1e45895a2873f0ed3ee115fa320a6448eef..0ab91b3ad66c6525c3c464723c0961c09f7e2765 100644 (file)
--- a/ext/ext.c
+++ b/ext/ext.c
@@ -1,5 +1,5 @@
 /*
- * $Id: ext.c 72 2005-07-16 13:15:42Z tilman $
+ * $Id: ext.c 74 2006-05-13 10:34:36Z tilman $
  *
  * Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
  *
        if (!*(ef)) \
                rb_raise (rb_eIOError, "closed stream");
 
+#define CHECK_READABLE(ef) \
+       switch (eet_mode_get (*ef)) { \
+               case EET_FILE_MODE_READ: \
+               case EET_FILE_MODE_READ_WRITE: \
+                       break; \
+               default: \
+                       rb_raise (rb_eIOError, "permission denied"); \
+       }
+
 #ifdef WORDS_BIGENDIAN
 # define BSWAP32(x) \
        ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
@@ -179,47 +188,61 @@ c_close (VALUE self)
        return self;
 }
 
+static VALUE
+get_keys (Eet_File *ef, char *glob)
+{
+       VALUE ret;
+       char **keys;
+       int i, count = 0;
+
+       keys = eet_list (ef, glob, &count);
+       ret = rb_ary_new2 (count);
+
+       for (i = 0; i < count; i++)
+               rb_ary_store (ret, i, rb_str_new2 (keys[i]));
+
+       free (keys);
+
+       return ret;
+}
+
 /*
  * call-seq:
- *  ef.list([glob]) -> array
+ *  ef.entries -> array
  *
- * Returns an Array of entries in _ef_ that match the shell glob
- * _glob_ (defaulting to "*").
+ * Returns an Array with the keys of the entries in _ef_.
+ * If the keys cannot be retrieved, an +IOError+ is raised.
  */
 static VALUE
-c_list (int argc, VALUE *argv, VALUE self)
+c_entries (VALUE self)
 {
-       VALUE glob = Qnil, ret;
        Eet_File **ef = NULL;
-       char **entries, *tmp = "*";
-       int i, count = 0;
 
        Data_Get_Struct (self, Eet_File *, ef);
        CHECK_CLOSED (ef);
+       CHECK_READABLE (ef);
 
-       switch (eet_mode_get (*ef)) {
-               case EET_FILE_MODE_READ:
-               case EET_FILE_MODE_READ_WRITE:
-                       break;
-               default:
-                       rb_raise (rb_eIOError, "cannot list entries");
-       }
-
-       rb_scan_args (argc, argv, "01", &glob);
-
-       if (!NIL_P (glob))
-               tmp = StringValuePtr (glob);
-
-       ret = rb_ary_new ();
-
-       entries = eet_list (*ef, tmp, &count);
+       return get_keys (*ef, "*");
+}
 
-       for (i = 0; i < count; i++)
-               rb_ary_push (ret, rb_str_new2 (entries[i]));
+/*
+ * call-seq:
+ *  ef[glob] -> array
+ *
+ * Returns an Array with the keys of entries in _ef_ that match the
+ * shell glob _glob_.
+ * If the keys cannot be retrieved, an +IOError+ is raised.
+ */
+static VALUE
+c_glob (VALUE self, VALUE glob)
+{
+       Eet_File **ef = NULL;
 
-       free (entries);
+       Data_Get_Struct (self, Eet_File *, ef);
+       CHECK_CLOSED (ef);
+       CHECK_READABLE (ef);
 
-       return ret;
+       return get_keys (*ef, StringValuePtr (glob));
 }
 
 /*
@@ -638,7 +661,8 @@ Init_eet_ext ()
        rb_define_singleton_method (c, "open", c_open, -1);
        rb_define_method (c, "initialize", c_init, -1);
        rb_define_method (c, "close", c_close, 0);
-       rb_define_method (c, "list", c_list, -1);
+       rb_define_method (c, "entries", c_entries, 0);
+       rb_define_method (c, "[]", c_glob, 1);
        rb_define_method (c, "delete", c_delete, 1);
        rb_define_method (c, "read", c_read, 1);
        rb_define_method (c, "write", c_write, -1);
index 95e1e469dd21307d61447169ed92536b877059d6..f69212952c143755715aebfce1ac37c2b2c8103e 100644 (file)
@@ -1,5 +1,5 @@
 #--
-# $Id: eet.rb 60 2005-06-08 16:18:32Z tilman $
+# $Id: eet.rb 74 2006-05-13 10:34:36Z tilman $
 #
 # Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
 #
@@ -98,6 +98,15 @@ module Eet
 
        class ChunkError < EetError; end
 
+       class File
+               def list(glob = "*")
+                       warn "Eet::File#list is deprecated, " +
+                            "use Eet::File#entries or Eet::File#[] instead"
+
+                       self[glob]
+               end
+       end
+
        class Stream # :nodoc:
                def initialize(chunk = nil)
                        super(chunk.nil? ? 0 : 1, chunk)
index e97634a1d45721ab74eddb0bf82a5a436396f353..80660b509fdc29b447a1c09b6dc2788815b70817 100644 (file)
@@ -1,4 +1,4 @@
-# $Id: test_misc.rb 1 2005-03-26 01:45:38Z tilman $
+# $Id: test_misc.rb 74 2006-05-13 10:34:36Z tilman $
 
 require "eet"
 require "test/unit"
@@ -129,18 +129,18 @@ class MiscTest < Test::Unit::TestCase
                end
        end
 
-       def test_list
+       def test_entries_glob
                ["r", "r+"].each do |mode|
                        Eet::File.open(@dest, mode) do |ef|
-                               assert_equal(@keys, ef.list.sort)
-                               assert_equal(@keys, ef.list("*").sort, @keys)
-                               assert_equal([@keys[0]], ef.list(@keys[0]))
+                               assert_equal(@keys, ef.entries.sort)
+                               assert_equal(@keys, ef["*"].sort)
+                               assert_equal([@keys[0]], ef[@keys[0]])
                        end
                end
 
                assert_raise(IOError) do
                        Eet::File.open(@dest, "w") do |ef|
-                               ef.list
+                               ef.entries
                        end
                end
        end
@@ -154,7 +154,7 @@ class MiscTest < Test::Unit::TestCase
 
                Eet::File.open(@dest) do |ef|
                        tmp = @keys.shift
-                       assert_equal(@keys, ef.list.sort)
+                       assert_equal(@keys, ef.entries.sort)
                        @keys.unshift(tmp)
                end
        end