18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/rstruct/base_types/container_type.rb', line 18
def write(dst=nil, pvals=nil)
if dst.is_a?(String)
l = dst.size
dst = StringIO.new(dst)
dst.pos = l
elsif dst.nil?
dst = StringIO.new
end
typ ||= self.rstruct_type
vals = (pvals.respond_to?(:values) ? pvals.values : pvals)
vals ||= self.values
opos = dst.pos if dst.respond_to?(:pos)
typ.fields.each_with_index do |f, i|
fldval = vals[i]
if fldval.respond_to?(:write)
fldval.write(dst, fldval)
else
dst.write(f.typ.pack_value((fldval || 0), self))
end
end
if dst.is_a?(StringIO) and pvals.nil?
dst.pos = opos
return(dst.read)
elsif opos and dst.respond_to?(:pos)
return dst.pos - opos
end
end
|