Class: Resedit::Changeable::Change
- Inherits:
-
Object
- Object
- Resedit::Changeable::Change
- Defined in:
- lib/resedit/classes/changeable.rb
Instance Attribute Summary collapse
-
#buf ⇒ Object
Returns the value of attribute buf.
-
#n ⇒ Object
Returns the value of attribute n.
-
#nbuf ⇒ Object
Returns the value of attribute nbuf.
-
#obuf ⇒ Object
Returns the value of attribute obuf.
-
#sz ⇒ Object
Returns the value of attribute sz.
Instance Method Summary collapse
- #_updbufs(obuf = nil, nbuf = nil) ⇒ Object
- #all ⇒ Object
- #change(ofs, data, fix = false) ⇒ Object
- #changed?(ofs, size) ⇒ Boolean
- #cload(ofs, bytes, len) ⇒ Object
- #data(ofs, size) ⇒ Object
- #dump ⇒ Object
- #getChanges(ofs = 0) ⇒ Object
- #hex(writer, ofs, size, col) ⇒ Object
-
#initialize(obuf = nil, nbuf = nil) ⇒ Change
constructor
A new instance of Change.
- #insert(ofs, data) ⇒ Object
- #mode(mode = nil) ⇒ Object
- #normalize ⇒ Object
- #revert ⇒ Object
- #size ⇒ Object
- #split(ofs, data, nu = false) ⇒ Object
- #undo(ofs) ⇒ Object
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
#buf ⇒ Object
Returns the value of attribute buf.
16 17 18 |
# File 'lib/resedit/classes/changeable.rb', line 16 def buf @buf end |
#n ⇒ Object
Returns the value of attribute n.
16 17 18 |
# File 'lib/resedit/classes/changeable.rb', line 16 def n @n end |
#nbuf ⇒ Object
Returns the value of attribute nbuf.
16 17 18 |
# File 'lib/resedit/classes/changeable.rb', line 16 def nbuf @nbuf end |
#obuf ⇒ Object
Returns the value of attribute obuf.
16 17 18 |
# File 'lib/resedit/classes/changeable.rb', line 16 def obuf @obuf end |
#sz ⇒ Object
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 |
#all ⇒ Object
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
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 |
#dump ⇒ Object
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 |
#normalize ⇒ Object
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 |
#revert ⇒ Object
106 107 108 109 110 |
# File 'lib/resedit/classes/changeable.rb', line 106 def revert @nbuf = nil mode() @n.revert if @n end |
#size ⇒ Object
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 |