Class: Resedit::Changeable::Change

Inherits:
Object
  • Object
show all
Defined in:
lib/resedit/classes/changeable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obuf = nil, nbuf = nil) ⇒ Change

Returns a new instance of Change.



18
19
20
21
22
# File 'lib/resedit/classes/changeable.rb', line 18

def initialize(obuf=nil, nbuf=nil)
    @n = nil
    @mode = HOW_CHANGED
    _updbufs(obuf, nbuf)
end

Instance Attribute Details

#bufObject

Returns the value of attribute buf.



16
17
18
# File 'lib/resedit/classes/changeable.rb', line 16

def buf
  @buf
end

#nObject

Returns the value of attribute n.



16
17
18
# File 'lib/resedit/classes/changeable.rb', line 16

def n
  @n
end

#nbufObject

Returns the value of attribute nbuf.



16
17
18
# File 'lib/resedit/classes/changeable.rb', line 16

def nbuf
  @nbuf
end

#obufObject

Returns the value of attribute obuf.



16
17
18
# File 'lib/resedit/classes/changeable.rb', line 16

def obuf
  @obuf
end

#szObject

Returns the value of attribute sz.



16
17
18
# File 'lib/resedit/classes/changeable.rb', line 16

def sz
  @sz
end

Instance Method Details

#_updbufs(obuf = nil, nbuf = nil) ⇒ Object



24
25
26
27
28
# File 'lib/resedit/classes/changeable.rb', line 24

def _updbufs(obuf=nil, nbuf=nil)
    @obuf = obuf ? obuf : ''
    @nbuf = nbuf
    mode()
end

#allObject



43
# File 'lib/resedit/classes/changeable.rb', line 43

def all; return @buf + (@n ? @n.all() : '') end

#change(ofs, data, fix = false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/resedit/classes/changeable.rb', line 79

def change(ofs, data, fix=false)
    return @sz + @n.change(ofs - @sz, data, fix) if ofs > @sz
    nxt = data.length - @sz + ofs
    size = nxt>0 ? data.length-nxt : data.length
    @n.change(0, data[size..-1], fix) if nxt > 0
    if @nbuf
        @nbuf = @nbuf[0,ofs] + data[0, size] + @nbuf[ofs+size..-1]
        mode()
        return ofs
    end
    if fix
        @obuf = @obuf[0, ofs] + data[0, size] + @obuf[ofs+size..-1]
    else
        split(ofs, data[0, size])
    end
    mode()
    return ofs
end

#changed?(ofs, size) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
# File 'lib/resedit/classes/changeable.rb', line 112

def changed?(ofs, size)
    return @n.changed?(ofs - @sz, size) if ofs >= @sz
    return true if @nbuf
    return @n.changed?(0, size - @sz + ofs) if @sz < ofs+size
    return false
end

#cload(ofs, bytes, len) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/resedit/classes/changeable.rb', line 163

def cload(ofs, bytes, len)
    LOG.debug("load #{ofs} #{bytes} #{len}")
    return @n.cload(ofs - @sz, bytes, len) if ofs > @sz
    change(ofs, bytes.length>0 ? bytes : '*'*len)
    nd = ofs==0 ? self : @n
    nd.nbuf = nd.obuf
    nd.obuf = bytes
end

#data(ofs, size) ⇒ Object



37
38
39
40
41
# File 'lib/resedit/classes/changeable.rb', line 37

def data(ofs, size)
    return @n.data(ofs - @sz, size) if ofs > @sz
    return @buf[ofs .. -1] + @n.data(0, size - @sz + ofs) if @sz < ofs+size
    return @buf[ofs, size]
end

#dumpObject



158
159
160
161
# File 'lib/resedit/classes/changeable.rb', line 158

def dump()
    printf("#{@sz}:O(#{@obuf.length})\t\t%s\n", @nbuf ? "N(#{@nbuf.length if @nbuf})" : "")
    @n.dump() if @n
end

#getChanges(ofs = 0) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/resedit/classes/changeable.rb', line 131

def getChanges(ofs=0)
    ch = {}
    if @nbuf
        ch[ofs] = [@obuf, @nbuf]
    end
    ch = ch.merge(@n.getChanges(ofs+@sz)) if @n
    return ch
end

#hex(writer, ofs, size, col) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/resedit/classes/changeable.rb', line 119

def hex(writer, ofs, size, col)
    if ofs > @sz
        return size if !n
        return n.hex(writer, ofs - @sz, size, col)
    end
    sz = @sz < ofs+size ? @sz-ofs : size
    writer.addBytes(@buf[ofs, sz], @nbuf ? col : nil)
    return 0 if sz==size
    return n.hex(writer, 0, size-sz, col) if n
    return size-sz
end

#insert(ofs, data) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/resedit/classes/changeable.rb', line 66

def insert(ofs, data)
    return @sz + @n.insert(ofs - @sz, data) if ofs > @sz
    LOG.debug("inserting #{data} @#{ofs} of #{@buf} #{@sz}")
    if @nbuf
        @nbuf = @nbuf[0, ofs] + data + @nbuf[ofs..-1]
        mode()
        return ofs
    end
    split(ofs, data, true)
    mode()
    return ofs
end

#mode(mode = nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/resedit/classes/changeable.rb', line 30

def mode(mode=nil)
    @mode = mode if mode
    @buf = (@mode == HOW_CHANGED && @nbuf) ? @nbuf : @obuf
    @sz = @buf.length
    @n.mode(mode) if @n && mode
end

#normalizeObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/resedit/classes/changeable.rb', line 140

def normalize()
    while @n && !@nbuf && @obuf.length==0
        @obuf = @n.obuf
        @nbuf = @n.nbuf
        @n = @n.n
    end
    @n=@n.n while @n && !@n.nbuf && @n.obuf.length==0
    while @n &&
            ((@obuf.length>0 && @n.obuf.length>0) || (@obuf.length == 0 && @n.obuf.length==0)) &&
            ((@nbuf && @n.nbuf) || (!@nbuf && !@n.nbuf))
        @obuf += @n.obuf
        @nbuf = @nbuf ? @nbuf+@n.nbuf : nil
        @n=@n.n
    end
    mode()
    @n.normalize() if @n
end

#revertObject



106
107
108
109
110
# File 'lib/resedit/classes/changeable.rb', line 106

def revert
    @nbuf = nil
    mode()
    @n.revert if @n
end

#sizeObject



44
# File 'lib/resedit/classes/changeable.rb', line 44

def size; return @sz + (@n ? @n.size() : 0) end

#split(ofs, data, nu = false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/resedit/classes/changeable.rb', line 46

def split(ofs, data, nu=false)
    if ofs+(nu ? 0 : data.length) == @sz
        tail = @n
    else
        ntail = @nbuf ? @nbuf[ofs + (nu ? 0 : data.length) .. -1] : nil
        tail = Change.new(@obuf[ofs + (nu ? 0 : data.length) .. -1], ntail)
        tail.n = @n
    end
    if ofs != 0
        body = Change.new(nu ? nil : @obuf[ofs, data.length], data)
        body.n = tail
        _updbufs(@buf[0, ofs], @nbuf ? @nbuf[0, ofs] : nil)
        @n = body
    else
        _updbufs(nu ? nil : @obuf[0, data.length], data)
        @n = tail
    end
    mode(@mode)
end

#undo(ofs) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/resedit/classes/changeable.rb', line 98

def undo(ofs)
    LOG.debug("undo #{ofs} #{@buf}")
    return @n.undo(ofs - @sz) if ofs >= @sz
    raise "Change not found @#{ofs}" if ofs != 0 || !@nbuf
    @nbuf = nil
    mode()
end