Class: Block

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

Overview

/usr/bin/env ruby

Defined Under Namespace

Classes: FieldFormat

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Block

Returns a new instance of Block.



131
132
133
134
135
# File 'lib/zip64/structures.rb', line 131

def initialize(options={})
  options.each do |key,val|
    send("#{key}=", val) if respond_to?("#{key}") && respond_to?("#{key}=")
  end
end

Class Method Details

.base_sizeObject



56
57
58
# File 'lib/zip64/structures.rb', line 56

def self.base_size
  fields.inject(0) { |t, f| size_of(f.type) + t }
end

.fields(*args) ⇒ Object

Reader mode: With no arguments returns the fields defined for this class

Writer mode: If args is not empty then each argument is passed to FieldFormat.from(arg) to convert to a FieldFormat object.

Also accessors are defined



42
43
44
45
46
47
48
49
# File 'lib/zip64/structures.rb', line 42

def fields *args
  if args.empty?
    @fields
  else
    @fields = args.map { |arg| FieldFormat.from(arg) }
    @fields.each { |f| attr_accessor(f.name) }
  end
end

.read_from(fp, offset) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/zip64/structures.rb', line 74

def self.read_from(fp, offset)
  o = new()
  fields[offset..-1].each do |field|
    next if o.respond_to?(:skip_read?) && o.skip_read?(field)
    o.read_field_from(fp, field)
  end
  o
end

.size_of(type) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/zip64/structures.rb', line 96

def self.size_of(type)
  case type
  when 'C', 'c'
    1
  when 'v'
    2
  when 'V'
    4
  when 'Q'
    8
  else
    0
  end
end

Instance Method Details

#describe(io) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zip64/structures.rb', line 82

def describe(io)
  io.puts "---- #{self.class.name}"
  fields.each do |field|
    val = send(field.name)
    if val.nil?
      io.puts sprintf("%33s %s", 'NULL', field.name)
    else
      io.puts sprintf("%16s %16i %2i %s", val.to_s(16), val,
              size_of(field.type), field.name)
    end
  end
end

#fieldsObject



52
53
54
# File 'lib/zip64/structures.rb', line 52

def fields
  self.class.fields()
end

#pack_field(field) ⇒ Object



123
124
125
# File 'lib/zip64/structures.rb', line 123

def pack_field(field)
  field.encode(self, send(field.name))
end

#read_field_from(fp, field) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/zip64/structures.rb', line 60

def read_field_from(fp, field)
  if (sz = size_of(field.type)).nonzero?
    val = fp.read(sz).unpack(field.type).first
    send("#{field.name}=", val)
  else
    fn = "#{field.name}_len"
    if field.type == "A*" && o.respond_to?(fn) && o.send(fn)
      send("#{field.name}=", fp.read(o.send(fn)))
    else
      puts "#{field.name} un-fetchable"
    end
  end
end

#sizeObject



127
128
129
# File 'lib/zip64/structures.rb', line 127

def size
  to_string.size
end

#size_of(type) ⇒ Object



110
111
112
# File 'lib/zip64/structures.rb', line 110

def size_of(type)
  self.class.size_of(type)
end

#to_stringObject Also known as: to_s



114
115
116
117
118
119
120
# File 'lib/zip64/structures.rb', line 114

def to_string
  buf = ''.zip64_force_encoding("ASCII-8BIT")
  fields.each do |f|
    buf << pack_field(f)
  end
  buf
end