class Pocolog::ExtractConfig

Attributes

streams[RW]

Public Class Methods

new(io) click to toggle source
Calls superclass method Pocolog::BaseCopyConfig.new
# File bin/pocolog, line 146
def initialize(io)
    super
    @streams = Hash.new
end
optname() click to toggle source
# File bin/pocolog, line 142
def self.optname; "--extract" end
single_io?() click to toggle source
# File bin/pocolog, line 144
def self.single_io?; false end

Public Instance Methods

execute() click to toggle source
# File bin/pocolog, line 151
def execute
    buffer = ""
    init do |from, to|
        Logfiles.write_prologue(to, Pocolog.big_endian?)
        
        next_index = 0
        enabled = Hash.new
        sample_count = Array.new
        start_sample = Array.new

        from.each do |file_io|
            from    = Logfiles.new(file_io)
            index_mapping = Array.new
            from.each_block do |info|
                if info.type == STREAM_BLOCK
                    stream = from.read_stream_declaration
                    if streams.empty? || streams.has_key?(stream.name)
                        if !enabled[[stream.name, stream.typename]]
                            enabled[[stream.name, stream.typename]] = next_index
                            next_index += 1
                        end
                        index_mapping[info.index] = [enabled[[stream.name, stream.typename]]].pack("v")
                        start_sample[info.index], sample_count[info.index] = *streams[stream.name]
                    end
                end

                stream_index = index_mapping[info.index]
                if stream_index
                    if (skip = start_sample[info.index]) && info.type == DATA_BLOCK
                        start_sample[info.index] =
                            if skip > 1 then skip - 1
                            else nil
                            end
                        next
                    end

                    Logfiles.copy_block(info, from.rio, to, buffer) do |buffer|
                        buffer[2, 2] = stream_index
                        buffer
                    end
                    remaining    = sample_count[info.index]
                    if remaining && info.type == DATA_BLOCK
                        if remaining > 1
                            sample_count[info.index] = remaining - 1
                        else # reached the max count, disable copying that stream
                            index_mapping[info.index] = nil
                        end
                    end
                end
            end
        end
    end
end