File: io.rb

package info (click to toggle)
ruby-backports 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,460 kB
  • ctags: 703
  • sloc: ruby: 6,195; makefile: 24
file content (57 lines) | stat: -rw-r--r-- 2,541 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require 'backports/tools/arguments'
module Backports
  # Used internally to combine {IO|File} options hash into mode (String or Integer)
  def self.combine_mode_and_option(mode = nil, options = Backports::Undefined)
    # Can't backport autoclose, {internal|external|}encoding
    mode, options = nil, mode if mode.respond_to?(:to_hash) && options == Backports::Undefined
    options = {} if options == nil || options == Backports::Undefined
    options = coerce_to_hash(options)
    if mode && options[:mode]
      raise ArgumentError, "mode specified twice"
    end
    mode ||= options[:mode] || "r"
    mode = try_convert(mode, String, :to_str) || try_convert(mode, Integer, :to_int) || mode
    if options[:textmode] || options[:binmode]
      text = options[:textmode] || (mode.is_a?(String) && mode =~ /t/)
      bin  = options[:binmode]  || (mode.is_a?(String) ? mode =~ /b/ : mode & File::Constants::BINARY != 0)
      if text && bin
        raise ArgumentError, "both textmode and binmode specified"
      end
      case
        when !options[:binmode]
        when mode.is_a?(String)
          mode.insert(1, "b")
        else
          mode |= File::Constants::BINARY
      end
    end
    mode
  end

  # Used internally to combine {IO|File} options hash into mode (String or Integer) and perm
  def self.combine_mode_perm_and_option(mode = nil, perm = Backports::Undefined, options = Backports::Undefined)
    mode, options = nil, mode if mode.respond_to?(:to_hash) && perm == Backports::Undefined
    perm, options = nil, perm if perm.respond_to?(:to_hash) && options == Backports::Undefined
    perm = nil if perm == Backports::Undefined
    options = {} if options == Backports::Undefined
    options = coerce_to_hash(options)
    if perm && options[:perm]
      raise ArgumentError, "perm specified twice"
    end
    [combine_mode_and_option(mode, options), perm || options[:perm]]
  end

  def self.write(binary, filename, string, offset, options)
    offset, options = nil, offset if offset.respond_to?(:to_hash) && options == Backports::Undefined
    options = {} if options == Backports::Undefined
    options = coerce_to_hash(options)
    File.open(filename, 'a+'){} if offset # insure existence
    options = {:mode => offset.nil? ? "w" : "r+"}.merge(options)
    args = options[:open_args] || [options]
    File.open(filename, *Backports.combine_mode_perm_and_option(*args)) do |f|
      f.binmode if binary && f.respond_to?(:binmode)
      f.seek(offset) unless offset.nil?
      f.write(string)
    end
  end
end