Parent

Files

Bcat::Reader

ARGF style multi-file streaming interface. Input is read with IO#readpartial to avoid buffering.

Attributes

args[R]
fds[R]
is_command[R]

Public Class Methods

new(is_command, args=[]) click to toggle source
# File lib/bcat/reader.rb, line 11
def initialize(is_command, args=[])
  @is_command = is_command
  @args = args
end

Public Instance Methods

each() click to toggle source
# File lib/bcat/reader.rb, line 35
def each
  yield @buf.shift while @buf.any?
  while fd = fds.first
    fds.shift and next if fd.closed?
    fd.sync = true
    begin
      while buf = fd.readpartial(4096)
        yield buf
      end
    rescue EOFError
      fd.close
    end
    fds.shift
  end
end
open() click to toggle source
# File lib/bcat/reader.rb, line 16
def open
  @fds = is_command ? open_command : open_files
  @buf = []
end
open_command() click to toggle source
# File lib/bcat/reader.rb, line 21
def open_command
  [IO.popen(args.join(' '), 'rb')]
end
open_files() click to toggle source
# File lib/bcat/reader.rb, line 25
def open_files
  args.map do |f|
    if f == '-'
      $stdin
    else
      File.open(f, 'rb')
    end
  end
end
sniff() click to toggle source
# File lib/bcat/reader.rb, line 51
def sniff
  @format ||=
    catch :detect do
      each do |chunk|
        @buf << chunk
        case chunk
        when /\A\s*</
          throw :detect, 'html'
        when /\A\s*[^<]/
          throw :detect, 'text'
        end
      end
      throw :detect, 'text'
    end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.