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

ruby-changes:30159

From: glass <ko1@a...>
Date: Sun, 28 Jul 2013 13:06:25 +0900 (JST)
Subject: [ruby-changes:30159] glass:r42211 (trunk): * io.c (interpret_seek_whence): support SEEK_DATA and SEEK_HOLE.

glass	2013-07-28 13:06:14 +0900 (Sun, 28 Jul 2013)

  New Revision: 42211

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

  Log:
    * io.c (interpret_seek_whence): support SEEK_DATA and SEEK_HOLE.
      These are whences for lseek(2) supported by Linux since version 3.1.
      [ruby-core:56123] [Feature #8671]
    
    * test/ruby/test_io.rb: Add tests for above.

  Modified files:
    trunk/ChangeLog
    trunk/io.c
    trunk/test/ruby/test_io.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42210)
+++ ChangeLog	(revision 42211)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Jul 28 13:04:39 2013  Masaki Matsushita  <glass.saga@g...>
+
+	* io.c (interpret_seek_whence): support SEEK_DATA and SEEK_HOLE.
+	  These are whences for lseek(2) supported by Linux since version 3.1.
+	  [ruby-core:56123] [Feature #8671]
+
+	* test/ruby/test_io.rb: Add tests for above.
+
 Sun Jul 28 12:41:39 2013  Tanaka Akira  <akr@f...>
 
 	* bignum.c (absint_numwords_generic): The char_bit variable changed
Index: io.c
===================================================================
--- io.c	(revision 42210)
+++ io.c	(revision 42211)
@@ -161,6 +161,12 @@ static ID id_write, id_read, id_getc, id https://github.com/ruby/ruby/blob/trunk/io.c#L161
 static VALUE sym_mode, sym_perm, sym_extenc, sym_intenc, sym_encoding, sym_open_args;
 static VALUE sym_textmode, sym_binmode, sym_autoclose;
 static VALUE sym_SET, sym_CUR, sym_END;
+#ifdef SEEK_DATA
+static VALUE sym_DATA;
+#endif
+#ifdef SEEK_HOLE
+static VALUE sym_HOLE;
+#endif
 
 struct argf {
     VALUE filename, current_file;
@@ -1551,6 +1557,14 @@ interpret_seek_whence(VALUE vwhence) https://github.com/ruby/ruby/blob/trunk/io.c#L1557
         return SEEK_CUR;
     if (vwhence == sym_END)
         return SEEK_END;
+#ifdef SEEK_DATA
+    if (vwhence == sym_DATA)
+        return SEEK_DATA;
+#endif
+#ifdef SEEK_HOLE
+    if (vwhence == sym_HOLE)
+        return SEEK_HOLE;
+#endif
     return NUM2INT(vwhence);
 }
 
@@ -11820,6 +11834,14 @@ Init_IO(void) https://github.com/ruby/ruby/blob/trunk/io.c#L11834
     rb_define_const(rb_cIO, "SEEK_CUR", INT2FIX(SEEK_CUR));
     /* Set I/O position from the end */
     rb_define_const(rb_cIO, "SEEK_END", INT2FIX(SEEK_END));
+#ifdef SEEK_DATA
+    /* Set I/O position to the next location containing data */
+    rb_define_const(rb_cIO, "SEEK_DATA", INT2FIX(SEEK_DATA));
+#endif
+#ifdef SEEK_HOLE
+    /* Set I/O position to the next hole */
+    rb_define_const(rb_cIO, "SEEK_HOLE", INT2FIX(SEEK_HOLE));
+#endif
     rb_define_method(rb_cIO, "rewind", rb_io_rewind, 0);
     rb_define_method(rb_cIO, "pos", rb_io_tell, 0);
     rb_define_method(rb_cIO, "pos=", rb_io_set_pos, 1);
@@ -11989,4 +12011,10 @@ Init_IO(void) https://github.com/ruby/ruby/blob/trunk/io.c#L12011
     sym_SET = ID2SYM(rb_intern("SET"));
     sym_CUR = ID2SYM(rb_intern("CUR"));
     sym_END = ID2SYM(rb_intern("END"));
+#ifdef SEEK_DATA
+    sym_DATA = ID2SYM(rb_intern("DATA"));
+#endif
+#ifdef SEEK_HOLE
+    sym_HOLE = ID2SYM(rb_intern("HOLE"));
+#endif
 }
Index: test/ruby/test_io.rb
===================================================================
--- test/ruby/test_io.rb	(revision 42210)
+++ test/ruby/test_io.rb	(revision 42211)
@@ -1565,6 +1565,22 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L1565
         f.seek(2, IO::SEEK_CUR)
         assert_equal("r\nbaz\n", f.read)
       }
+
+      if defined?(IO::SEEK_DATA)
+        open(t.path) { |f|
+          assert_equal("foo\n", f.gets)
+          f.seek(0, IO::SEEK_DATA)
+          assert_equal("foo\nbar\nbaz\n", f.read)
+        }
+      end
+
+      if defined?(IO::SEEK_HOLE)
+        open(t.path) { |f|2
+          assert_equal("foo\n", f.gets)
+          f.seek(0, IO::SEEK_HOLE)
+          assert_equal("", f.read)
+        }
+      end
     }
   end
 
@@ -1585,6 +1601,22 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L1601
         f.seek(2, :CUR)
         assert_equal("r\nbaz\n", f.read)
       }
+
+      if defined?(IO::SEEK_DATA)
+        open(t.path) { |f|
+          assert_equal("foo\n", f.gets)
+          f.seek(0, :DATA)
+          assert_equal("foo\nbar\nbaz\n", f.read)
+        }
+      end
+
+      if defined?(IO::SEEK_HOLE)
+        open(t.path) { |f|
+          assert_equal("foo\n", f.gets)
+          f.seek(0, :HOLE)
+          assert_equal("", f.read)
+        }
+      end
     }
   end
 

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

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