lib/bindata/base.rb in bindata-0.5.1 vs lib/bindata/base.rb in bindata-0.6.0

- old
+ new

@@ -81,11 +81,25 @@ end private :register # Returns the class matching a previously registered +name+. def lookup(name) - Registry.instance.lookup(name) + klass = Registry.instance.lookup(name) + if klass.nil? + # lookup failed so attempt endian lookup + if self.respond_to?(:endian) and self.endian != nil + name = name.to_s + if /^u?int\d\d?$/ =~ name + new_name = name + ((self.endian == :little) ? "le" : "be") + klass = Registry.instance.lookup(new_name) + elsif ["float", "double"].include?(name) + new_name = name + ((self.endian == :little) ? "_le" : "_be") + klass = Registry.instance.lookup(new_name) + end + end + end + klass end end # Define the parameters we use in this class. optional_parameters :check_offset, :readwrite @@ -127,9 +141,19 @@ # set up the environment @env = env || LazyEvalEnv.new @env.params = extra @env.data_object = self + end + + # Returns the class matching a previously registered +name+. + def klass_lookup(name) + klass = self.class.lookup(name) + if klass.nil? and @env.parent_data_object != nil + # lookup failed so retry in the context of the parent data object + klass = @env.parent_data_object.klass_lookup(name) + end + klass end # Reads data into this bin object by calling #do_read then #done_read. def read(io) # remember the current position in the IO object