/*
- * $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) | \
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));
}
/*
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);
-# $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"
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
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