[前][次][番号順一覧][スレッド一覧]

ruby-changes:11627

From: knu <ko1@a...>
Date: Thu, 23 Apr 2009 01:32:46 +0900 (JST)
Subject: [ruby-changes:11627] Ruby:r23263 (trunk): * ext/zlib/zlib.c (Zlib::GzipFile#path): New method.

knu	2009-04-23 01:31:06 +0900 (Thu, 23 Apr 2009)

  New Revision: 23263

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=23263

  Log:
    * ext/zlib/zlib.c (Zlib::GzipFile#path): New method.

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/ext/zlib/doc/zlib.rd
    trunk/ext/zlib/zlib.c
    trunk/test/zlib/test_zlib.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 23262)
+++ ChangeLog	(revision 23263)
@@ -1,3 +1,7 @@
+Thu Apr 23 01:30:37 2009  Akinori MUSHA  <knu@i...>
+
+	* ext/zlib/zlib.c (Zlib::GzipFile#path): New method.
+
 Wed Apr 22 20:25:24 2009  Nobuyoshi Nakada  <nobu@r...>
 
 	* time.c (time_timespec): check out-of-range.  [ruby-core:23282]
Index: ext/zlib/zlib.c
===================================================================
--- ext/zlib/zlib.c	(revision 23262)
+++ ext/zlib/zlib.c	(revision 23263)
@@ -163,6 +163,7 @@
 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 @@
 #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 @@
     int ecflags;
     VALUE ecopts;
     char *cbuf;
+    VALUE path;
 };
 #define GZFILE_CBUF_CAPA 10
 
@@ -1699,6 +1701,7 @@
     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 @@
     gz->ecflags = 0;
     gz->ecopts = Qnil;
     gz->cbuf = 0;
+    gz->path = Qnil;
 
     return obj;
 }
@@ -2673,6 +2677,21 @@
     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 @@
     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 @@
     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 @@
     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);
Index: ext/zlib/doc/zlib.rd
===================================================================
--- ext/zlib/doc/zlib.rd	(revision 23262)
+++ ext/zlib/doc/zlib.rd	(revision 23263)
@@ -543,7 +543,13 @@
     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
 
 The superclass for all exceptions raised during processing a gzip
Index: NEWS
===================================================================
--- NEWS	(revision 23262)
+++ NEWS	(revision 23263)
@@ -192,6 +192,10 @@
     * Etc::Passwd.each
     * Etc::Group.each
 
+* zlib
+  * new methods:
+    * Zlib::GzipFile#path
+
 === Compatibility issues (excluding feature bug fixes)
 
   * Enumerator#rewind
Index: test/zlib/test_zlib.rb
===================================================================
--- test/zlib/test_zlib.rb	(revision 23262)
+++ test/zlib/test_zlib.rb	(revision 23263)
@@ -363,6 +363,34 @@
         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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]