aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog4
-rw-r--r--NEWS4
-rw-r--r--ext/zlib/doc/zlib.rd6
-rw-r--r--ext/zlib/zlib.c32
-rw-r--r--test/zlib/test_zlib.rb28
5 files changed, 73 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 67b17cc684..fb11d7a192 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Thu Apr 23 01:30:37 2009 Akinori MUSHA <knu@iDaemons.org>
+
+ * ext/zlib/zlib.c (Zlib::GzipFile#path): New method.
+
Wed Apr 22 20:25:24 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* time.c (time_timespec): check out-of-range. [ruby-core:23282]
diff --git a/NEWS b/NEWS
index 5d30429344..2a2d73e9f9 100644
--- a/NEWS
+++ b/NEWS
@@ -192,6 +192,10 @@ with all sufficient information, see the ChangeLog file.
* Etc::Passwd.each
* Etc::Group.each
+* zlib
+ * new methods:
+ * Zlib::GzipFile#path
+
=== Compatibility issues (excluding feature bug fixes)
* Enumerator#rewind
diff --git a/ext/zlib/doc/zlib.rd b/ext/zlib/doc/zlib.rd
index 6a36dc7fed..0a5e2d0abf 100644
--- a/ext/zlib/doc/zlib.rd
+++ b/ext/zlib/doc/zlib.rd
@@ -543,6 +543,12 @@ GzipReader should be used with associating an instance of IO class
must respond to flush method. While `sync' mode is true,
the compression ratio decreases sharply.
+--- Zlib::GzipFile#path
+
+ Returns the path string of the associated IO-like object. This
+ method is only defined when the IO-like object responds to
+ #path().
+
== Zlib::GzipFile::Error
diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c
index c7ca55cc1b..288b13b728 100644
--- a/ext/zlib/zlib.c
+++ b/ext/zlib/zlib.c
@@ -163,6 +163,7 @@ static VALUE rb_gzfile_sync(VALUE);
static VALUE rb_gzfile_set_sync(VALUE, VALUE);
static VALUE rb_gzfile_total_in(VALUE);
static VALUE rb_gzfile_total_out(VALUE);
+static VALUE rb_gzfile_path(VALUE);
static VALUE rb_gzwriter_s_allocate(VALUE);
static VALUE rb_gzwriter_s_open(int, VALUE*, VALUE);
@@ -1653,7 +1654,7 @@ rb_inflate_set_dictionary(VALUE obj, VALUE dic)
#define OS_CODE OS_UNIX
#endif
-static ID id_write, id_read, id_readpartial, id_flush, id_seek, id_close;
+static ID id_write, id_read, id_readpartial, id_flush, id_seek, id_close, id_path;
static VALUE cGzError, cNoFooter, cCRCError, cLengthError;
@@ -1678,6 +1679,7 @@ struct gzfile {
int ecflags;
VALUE ecopts;
char *cbuf;
+ VALUE path;
};
#define GZFILE_CBUF_CAPA 10
@@ -1699,6 +1701,7 @@ gzfile_mark(struct gzfile *gz)
rb_gc_mark(gz->comment);
zstream_mark(&gz->z);
rb_gc_mark(gz->ecopts);
+ rb_gc_mark(gz->path);
}
static void
@@ -1745,6 +1748,7 @@ gzfile_new(klass, funcs, endfunc)
gz->ecflags = 0;
gz->ecopts = Qnil;
gz->cbuf = 0;
+ gz->path = Qnil;
return obj;
}
@@ -2673,6 +2677,21 @@ rb_gzfile_total_out(VALUE obj)
return rb_uint2inum(gz->z.stream.total_out - gz->z.buf_filled);
}
+/*
+ * Document-method: path
+ *
+ * call-seq: path
+ *
+ * Returns the path string of the associated IO-like object. This
+ * method is only defined when the IO-like object responds to #path().
+ */
+static VALUE
+rb_gzfile_path(VALUE obj)
+{
+ struct gzfile *gz;
+ Data_Get_Struct(obj, struct gzfile, gz);
+ return gz->path;
+}
static void
rb_gzfile_ecopts(struct gzfile *gz, VALUE opts)
@@ -2770,6 +2789,11 @@ rb_gzwriter_initialize(int argc, VALUE *argv, VALUE obj)
ZSTREAM_READY(&gz->z);
rb_gzfile_ecopts(gz, opt);
+ if (rb_respond_to(io, id_path)) {
+ gz->path = rb_funcall(gz->io, id_path, 0);
+ rb_define_singleton_method(obj, "path", rb_gzfile_path, 0);
+ }
+
return obj;
}
@@ -2965,6 +2989,11 @@ rb_gzreader_initialize(int argc, VALUE *argv, VALUE obj)
gzfile_read_header(gz);
rb_gzfile_ecopts(gz, opt);
+ if (rb_respond_to(io, id_path)) {
+ gz->path = rb_funcall(gz->io, id_path, 0);
+ rb_define_singleton_method(obj, "path", rb_gzfile_path, 0);
+ }
+
return obj;
}
@@ -3516,6 +3545,7 @@ Init_zlib()
id_flush = rb_intern("flush");
id_seek = rb_intern("seek");
id_close = rb_intern("close");
+ id_path = rb_intern("path");
cGzipFile = rb_define_class_under(mZlib, "GzipFile", rb_cObject);
cGzError = rb_define_class_under(cGzipFile, "Error", cZError);
diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb
index 9e4cf80033..aa3059d5b1 100644
--- a/test/zlib/test_zlib.rb
+++ b/test/zlib/test_zlib.rb
@@ -363,6 +363,34 @@ if defined? Zlib
assert_equal(3, gz.tell)
end
end
+
+ def test_path
+ t = Tempfile.new("test_zlib_gzip_file")
+ t.close
+
+ gz = Zlib::GzipWriter.open(t.path)
+ gz.print("foo")
+ assert_equal(t.path, gz.path)
+ gz.close
+ assert_equal(t.path, gz.path)
+
+ f = Zlib::GzipReader.open(t.path)
+ assert_equal(t.path, f.path)
+ f.close
+ assert_equal(t.path, f.path)
+
+ s = ""
+ sio = StringIO.new(s)
+ gz = Zlib::GzipWriter.new(sio)
+ gz.print("foo")
+ assert_raise(NoMethodError) { gz.path }
+ gz.close
+
+ sio = StringIO.new(s)
+ f = Zlib::GzipReader.new(sio)
+ assert_raise(NoMethodError) { f.path }
+ f.close
+ end
end
class TestZlibGzipReader < Test::Unit::TestCase