add_file_write_prelude.diff

Roger Pack, 03/19/2011 04:41 AM

Download (4.66 KB)

View differences:

prelude.rb (working copy)
29 29
    }
30 30
  end
31 31
end
32

  
33

  
34
class IO
35
#
36
#  call-seq:
37
#     IO.write(name, string, [offset] )   => fixnum
38
#     IO.write(name, string, [offset], open_args )   => fixnum
39
#
40
#  Opens the file, optionally seeks to the given <i>offset</i>, writes
41
#  <i>string</i>, then returns the length written.
42
#  <code>write</code> ensures the file is closed before returning.
43
#  If <i>offset</i> is not given, the file is truncated.  Otherwise,
44
#  it is not truncated.
45
#
46
#  If the last argument is a hash, it specifies option for internal
47
#  open().  The key would be the following.  open_args: is exclusive
48
#  to others.
49
#
50
#   encoding: string or encoding
51
#
52
#    specifies encoding of the read string.  encoding will be ignored
53
#    if length is specified.
54
#
55
#   mode: string
56
#
57
#    specifies mode argument for open().  it should start with "w" or "a" or "r+"
58
#    otherwise it would cause error.
59
#
60
#   perm: fixnum
61
#
62
#    specifies perm argument for open().
63
#
64
#   open_args: array of strings
65
#
66
#    specifies arguments for open() as an array.
67
#
68
#     IO.write("testfile", "0123456789")      #=> "0123456789"
69
#     IO.write("testfile", "0123456789", 20)  #=> "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n"
70
#
71
  def self.write name, string, offset=nil, open_args = nil
72
    # 3rd param could be offset or open_args
73
    if !open_args && offset.is_a?(Hash)
74
      open_args = offset
75
      offset = nil
76
    end
77
    if offset
78
      open_args ||= (File::RDWR|File::CREAT) 
79
    else
80
      open_args ||= 'w'
81
    end
82
    open(name, open_args || 'w') do |f|
83
      if offset
84
        f.seek offset
85
      end
86
      f.write string
87
    end
88
  end
89
  
90
#  call-seq:
91
#     IO.binwrite(name, string, [offset] )   => fixnum
92
#
93
#  Opens the file, optionally seeks to the given <i>offset</i>, write
94
#  <i>string</i> then returns the length written.
95
#  <code>binwrite</code> ensures the file is closed before returning.
96
#  The open mode would be "wb:ASCII-8BIT".
97
#  If <i>offset</i> is not given, the file is truncated.  Otherwise,
98
#  it is not truncated.
99
#
100
#     IO.binwrite("testfile", "0123456789")      #=> "0123456789"
101
#     IO.binwrite("testfile", "0123456789", 20)  #=> "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n"
102
#
103
  def self.binwrite name, string, offset = nil
104
    open_args = offset ? (File::RDWR|File::CREAT|File::BINARY) : 'wb'
105
    open(name, open_args) do |f|
106
      if offset
107
        f.seek offset
108
      end
109
      f.write string
110
    end
111
  end
112
end
test/ruby/test_io.rb (working copy)
1809 1809
      Process.waitpid2(pid)
1810 1810
    end
1811 1811
  end
1812
  
1813
  
1814
  def test_s_write
1815
    t = Tempfile.new("foo")
1816
    path = t.path
1817
    t.close(false)
1818
    File.write(path, "foo\nbar\nbaz")
1819
    assert_equal("foo\nbar\nbaz", File.read(path))
1820
    File.write(path, "FOO", 0)
1821
    assert_equal("FOO\nbar\nbaz", File.read(path))
1822
    File.write(path, "BAR")
1823
    assert_equal("BAR", File.read(path))
1824
    File.write(path, "\u{3042}", mode: "w", encoding: "EUC-JP")
1825
    assert_equal("\u{3042}".encode("EUC-JP"), File.read(path, encoding: "EUC-JP"))
1826
    File.delete t
1827
    assert_equal(6, File.write(path,'string',2))
1828
    File.delete t
1829
    assert_raise(Errno::EINVAL) { File.write('/tmp/nonexisting','string',-2) }
1830
    assert_equal(6, File.write(path, 'string'))
1831
    assert_equal(3, File.write(path, 'sub', 1))
1832
    assert_equal("ssubng", File.read(path))
1833
    t.unlink
1834
  end
1835

  
1836
  def test_s_binwrite
1837
    t = Tempfile.new("foo")
1838
    path = t.path
1839
    t.close(false)
1840
    File.binwrite(path, "foo\nbar\nbaz")
1841
    assert_equal("foo\nbar\nbaz", File.read(path))
1842
    File.binwrite(path, "FOO", 0)
1843
    assert_equal("FOO\nbar\nbaz", File.read(path))
1844
    File.binwrite(path, "BAR")
1845
    assert_equal("BAR", File.read(path))
1846
    File.binwrite(path, "\u{3042}")
1847
    assert_equal("\u{3042}".force_encoding("ASCII-8BIT"), File.binread(path))
1848
    File.delete t
1849
    assert_equal(6, File.binwrite(path,'string',2))
1850
    File.delete t
1851
    assert_equal(6, File.binwrite(path, 'string'))
1852
    assert_equal(3, File.binwrite(path, 'sub', 1))
1853
    assert_equal("ssubng", File.binread(path))
1854
    assert_equal(6, File.size(path))
1855
    assert_raise(Errno::EINVAL) { File.binwrite('/tmp/nonexisting','string',-2) }
1856
    assert_raise(TypeError) { File.binwrite(path, "string", mode: "w", encoding: "EUC-JP") }
1857
    t.unlink
1858
  end
1859

  
1812 1860
end