Class: Lightwave::LightwaveObject::ChunkReader

Inherits:
Object
  • Object
show all
Defined in:
lib/lightwave.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strio) ⇒ ChunkReader

Returns a new instance of ChunkReader.



144
145
146
147
# File 'lib/lightwave.rb', line 144

def initialize(strio)
  process_tags(strio)
  make_defaults
end

Instance Attribute Details

#errorsObject



139
140
141
# File 'lib/lightwave.rb', line 139

def errors
  @errors ||= []
end

Class Method Details

.chunk_map(sym, method = nil, &var) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/lightwave.rb', line 109

def self.chunk_map(sym, method=nil, &var)
  if method and method.respond_to? :to_sym
    pr = Proc.new{|s,c| s.send(method.to_sym, c) }
    write_inheritable_hash(:matched_chunks, {sym => pr})
  else
    write_inheritable_hash(:matched_chunks, {sym => var})
  end
end

.set_chunk_length(len) ⇒ Object



117
118
119
# File 'lib/lightwave.rb', line 117

def self.set_chunk_length(len)
  write_inheritable_attribute(:chunk_length,len)
end

Instance Method Details

#make_defaultsObject



142
143
# File 'lib/lightwave.rb', line 142

def make_defaults
end

#process_tags(strio) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/lightwave.rb', line 121

def process_tags(strio)
  while !strio.eof?
    label, chunk, length = strio.read_chunk(chunk_length || 2)
    #p self.class.to_s + ' reading ' + label
    #p "Contents: " + chunk.string
    if matched_chunks and matched_chunks.has_key?(label) and matched_chunks[label].respond_to?(:call)
      begin
        matched_chunks[label].call(self, chunk)
      rescue
        error_message = "#{label} Tag reading broke, length was " + length.inspect + "\n" + chunk.string.inspect
        errors << ChunkError.new(error_message)
      end
    else 
      error_message = self.class.to_s + ' Unhandled chunk:' + label
      errors << ChunkError.new(error_message)
    end
  end
end