Adding Zlib::GzipFile#path


#1

 Zlib::GzipFile#path ã‚’è¿½åŠ ã™ã‚‹ãƒ‘ãƒƒãƒã§ã™ã€‚zgrep みたいなのを
書くときに使えます。

 テストの後半は StringIO#path の削除が前提なので、そちらの動向
次第で削るか差し替えます。

*    *    *

 ところで、 Document-method: path を書いているのに rdoc/ri に
現れないのは仕様なんでしょうか…。


Akinori MUSHA / http://akinori.org/

Index: ChangeLog

— ChangeLog (revision 23123)
+++ ChangeLog (working copy)
@@ -1,3 +1,8 @@
+Thu Apr 2 21:53:10 2009 Akinori MUSHA removed_email_address@domain.invalid
+

    • ext/zlib/zlib.c: Define Zlib::GzipFile#path if the associated
  • IO-like object responds to #path.

Thu Apr 2 14:50:06 2009 Yukihiro M. removed_email_address@domain.invalid

  • lib/getoptlong.rb: remove unused udoc/usage from example.
    Index: NEWS
    ===================================================================
    — NEWS (revision 23123)
    +++ NEWS (working copy)
    @@ -177,6 +177,10 @@ with all sufficient information, see the
    • Etc::Passwd.each
    • Etc::Group.each

+* zlib

    • new methods:
    • Zlib::GzipFile#path

=== Compatibility issues (excluding feature bug fixes)

  • Enumerator#rewind
    Index: ext/zlib/zlib.c
    ===================================================================
    — ext/zlib/zlib.c (revision 23123)
    +++ ext/zlib/zlib.c (working copy)
    @@ -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, VAL
#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 *
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 *
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);
    Index: ext/zlib/doc/zlib.rd
    ===================================================================
    — ext/zlib/doc/zlib.rd (revision 23123)
    +++ ext/zlib/doc/zlib.rd (working copy)
    @@ -543,6 +543,12 @@ GzipReader should be used with associati
    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

Index: test/zlib/test_zlib.rb

— test/zlib/test_zlib.rb (revision 23123)
+++ test/zlib/test_zlib.rb (working copy)
@@ -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