latest.diff

Roger Pack, 04/30/2010 02:32 AM

Download (7.49 KB)

View differences:

io.c (working copy)
7779 7779
};
7780 7780

  
7781 7781
static void
7782
open_key_args(int argc, VALUE *argv, struct foreach_arg *arg)
7782
open_key_args_with_opt(int argc, VALUE *argv, struct foreach_arg *arg, int mandatory_argc, int default_mode, int default_perm)
7783 7783
{
7784 7784
    VALUE opt, v;
7785 7785

  
......
7787 7787
    arg->io = 0;
7788 7788
    arg->argc = argc - 1;
7789 7789
    arg->argv = argv + 1;
7790
    if (argc == 1) {
7790
    if (argc == mandatory_argc) {
7791 7791
      no_key:
7792
	arg->io = rb_io_open(argv[0], INT2NUM(O_RDONLY), INT2FIX(0666), Qnil);
7792
	arg->io = rb_io_open(argv[0], INT2NUM(default_mode), INT2FIX(default_perm), Qnil);
7793 7793
	return;
7794 7794
    }
7795 7795
    opt = pop_last_hash(&arg->argc, arg->argv);
......
7814 7814
	rb_ary_clear(args);	/* prevent from GC */
7815 7815
	return;
7816 7816
    }
7817
    if (default_mode != O_RDONLY && NIL_P(rb_hash_aref(opt, sym_mode))) {
7818
	opt = rb_hash_dup(opt);
7819
	rb_hash_aset(opt, sym_mode, INT2NUM(default_mode));
7820
    }
7821
    if (default_perm != 0666 && NIL_P(rb_hash_aref(opt, sym_perm))) {
7822
	opt = rb_hash_dup(opt);
7823
	rb_hash_aset(opt, sym_perm, INT2FIX(default_perm));
7824
    }
7817 7825
    arg->io = rb_io_open(argv[0], Qnil, Qnil, opt);
7818 7826
}
7819 7827

  
7828
static void
7829
open_key_args(int argc, VALUE *argv, struct foreach_arg *arg)
7830
{
7831
    open_key_args_with_opt(argc, argv, arg, 1, O_RDONLY, 0666);
7832
}
7833

  
7820 7834
static VALUE
7821 7835
io_s_foreach(struct foreach_arg *arg)
7822 7836
{
......
7904 7918
    return io_read(arg->argc, arg->argv, arg->io);
7905 7919
}
7906 7920

  
7921
struct write_arg {
7922
    VALUE io, str;
7923
};
7924

  
7925
static VALUE
7926
io_s_write(struct write_arg *arg)
7927
{
7928
    return io_write(arg->io, arg->str, 0);
7929
}
7930

  
7907 7931
struct seek_arg {
7908 7932
    VALUE io;
7909 7933
    VALUE offset;
......
8006 8030
    return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
8007 8031
}
8008 8032

  
8033
static VALUE rb_io_s_write_helper(int argc, VALUE *argv, VALUE io, int use_binary);
8034

  
8035
/*
8036
 *  call-seq:
8037
 *     IO.write(name, string, [offset] )   => fixnum
8038
 *     IO.write(name, string, [offset], open_args )   => fixnum
8039
 *
8040
 *  Opens the file, optionally seeks to the given <i>offset</i>, writes
8041
 *  <i>string</i>, then returns the length written.
8042
 *  <code>write</code> ensures the file is closed before returning.
8043
 *  If <i>offset</i> is not given, the file is truncated.  Otherwise,
8044
 *  it is not truncated.
8045
 *
8046
 *  If the last argument is a hash, it specifies option for internal
8047
 *  open().  The key would be the following.  open_args: is exclusive
8048
 *  to others.
8049
 *
8050
 *   encoding: string or encoding
8051
 *
8052
 *    specifies encoding of the read string.  encoding will be ignored
8053
 *    if length is specified.
8054
 *
8055
 *   mode: string
8056
 *
8057
 *    specifies mode argument for open().  it should start with "w" or "a" or "r+"
8058
 *    otherwise it would cause error.
8059
 *
8060
 *   perm: fixnum
8061
 *
8062
 *    specifies perm argument for open().
8063
 *
8064
 *   open_args: array of strings
8065
 *
8066
 *    specifies arguments for open() as an array.
8067
 *
8068
 *     IO.write("testfile", "0123456789")      #=> "0123456789"
8069
 *     IO.write("testfile", "0123456789", 20)  #=> "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n"
8070
 */
8071

  
8072
static VALUE
8073
rb_io_s_write(int argc, VALUE *argv, VALUE io)
8074
{
8075
    return rb_io_s_write_helper(argc, argv, io, 0);
8076
}
8077

  
8078
static VALUE
8079
rb_io_s_write_helper(int argc, VALUE *argv, VALUE io, int use_binary)
8080
{
8081
    VALUE offset;
8082
    struct foreach_arg arg;
8083
    struct write_arg warg;
8084
    int mode = O_WRONLY | O_CREAT, mandatory_argc;
8085
#ifdef O_BINARY
8086
    if(use_binary)
8087
        mode |= O_BINARY;
8088
#endif
8089

  
8090
    rb_scan_args(argc, argv, "22", NULL, &warg.str, &offset, NULL);
8091
    if (!NIL_P(offset) && FIXNUM_P(offset)) {
8092
	mandatory_argc = 3;
8093
    }
8094
    else {
8095
	mode |= O_TRUNC;
8096
	mandatory_argc = 2;
8097
    }
8098
    open_key_args_with_opt(argc, argv, &arg, mandatory_argc, mode, 0666);
8099
    if (NIL_P(arg.io)) return Qnil;
8100
    if (!NIL_P(offset) && FIXNUM_P(offset)) {
8101
	struct seek_arg sarg;
8102
	int state = 0;
8103
	sarg.io = arg.io;
8104
	sarg.offset = offset;
8105
	sarg.mode = SEEK_SET;
8106
	rb_protect((VALUE (*)(VALUE))seek_before_access, (VALUE)&sarg, &state);
8107
	if (state) {
8108
	    rb_io_close(arg.io);
8109
	    rb_jump_tag(state);
8110
	}
8111
	if (arg.argc == 2) arg.argc = 1;
8112
    }
8113
    warg.io = arg.io;
8114
    return rb_ensure(io_s_write, (VALUE)&warg, rb_io_close, arg.io);
8115
}
8116

  
8117
/*
8118
 *  call-seq:
8119
 *     IO.binwrite(name, string, [offset] )   => fixnum
8120
 *
8121
 *  Opens the file, optionally seeks to the given <i>offset</i>, write
8122
 *  <i>string</i> then returns the length written.
8123
 *  <code>binwrite</code> ensures the file is closed before returning.
8124
 *  The open mode would be "wb:ASCII-8BIT".
8125
 *  If <i>offset</i> is not given, the file is truncated.  Otherwise,
8126
 *  it is not truncated.
8127
 *
8128
 *     IO.binwrite("testfile", "0123456789")      #=> "0123456789"
8129
 *     IO.binwrite("testfile", "0123456789", 20)  #=> "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n"
8130
 */
8131
static VALUE
8132
rb_io_s_binwrite(int argc, VALUE *argv, VALUE io)
8133
{
8134
    return rb_io_s_write_helper(argc, argv, io, 1);
8135
}
8136

  
8009 8137
struct copy_stream_struct {
8010 8138
    VALUE src;
8011 8139
    VALUE dst;
......
9809 9937
    rb_define_singleton_method(rb_cIO, "readlines", rb_io_s_readlines, -1);
9810 9938
    rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1);
9811 9939
    rb_define_singleton_method(rb_cIO, "binread", rb_io_s_binread, -1);
9940
    rb_define_singleton_method(rb_cIO, "write", rb_io_s_write, -1);
9941
    rb_define_singleton_method(rb_cIO, "binwrite", rb_io_s_binwrite, -1);
9812 9942
    rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1);
9813 9943
    rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, -1);
9814 9944
    rb_define_singleton_method(rb_cIO, "try_convert", rb_io_s_try_convert, 1);
test/ruby/test_io.rb (working copy)
1582 1582
    t.close
1583 1583
    assert_raise(IOError) {t.binmode}
1584 1584
  end
1585

  
1586
  def test_s_write
1587
    t = Tempfile.new("foo")
1588
    path = t.path
1589
    t.close(false)
1590
    File.write(path, "foo\nbar\nbaz")
1591
    assert_equal("foo\nbar\nbaz", File.read(path))
1592
    File.write(path, "FOO", 0)
1593
    assert_equal("FOO\nbar\nbaz", File.read(path))
1594
    File.write(path, "BAR")
1595
    assert_equal("BAR", File.read(path))
1596
    File.write(path, "\u{3042}", mode: "w", encoding: "EUC-JP")
1597
    assert_equal("\u{3042}".encode("EUC-JP"), File.read(path, encoding: "EUC-JP"))
1598
    File.delete t
1599
    assert_equal(6, File.write(path,'string',2))
1600
    File.delete t
1601
    assert_raise(Errno::EINVAL) { File.write('/tmp/nonexisting','string',-2) }
1602
    assert_equal(6, File.write(path, 'string'))
1603
    assert_equal(3, File.write(path, 'sub', 1))
1604
    assert_equal("ssubng", File.read(path))
1605
    t.unlink
1606
  end
1607

  
1608
  def test_s_binwrite
1609
    t = Tempfile.new("foo")
1610
    path = t.path
1611
    t.close(false)
1612
    File.binwrite(path, "foo\nbar\nbaz")
1613
    assert_equal("foo\nbar\nbaz", File.read(path))
1614
    File.binwrite(path, "FOO", 0)
1615
    assert_equal("FOO\nbar\nbaz", File.read(path))
1616
    File.binwrite(path, "BAR")
1617
    assert_equal("BAR", File.read(path))
1618
    File.binwrite(path, "\u{3042}")
1619
    assert_equal("\u{3042}".force_encoding("ASCII-8BIT"), File.binread(path))
1620
    File.delete t
1621
    assert_equal(6, File.binwrite(path,'string',2))
1622
    File.delete t
1623
    assert_equal(6, File.binwrite(path, 'string'))
1624
    assert_equal(3, File.binwrite(path, 'sub', 1))
1625
    assert_equal("ssubng", File.binread(path))
1626
    assert_equal(6, File.size(path))
1627
    assert_raise(Errno::EINVAL) { File.binwrite('/tmp/nonexisting','string',-2) }
1628
    t.unlink
1629
  end
1585 1630
end