hex mode more more more

--HG--
branch : pmacs2
This commit is contained in:
moculus 2008-04-10 20:23:09 +00:00
parent 8f75c44bc1
commit 9cd9d68b57
2 changed files with 43 additions and 62 deletions

View File

@ -516,39 +516,46 @@ class AesBuffer(FileBuffer):
return aes.encrypt_data(data, self.password)
class Binary32Buffer(FileBuffer):
btype = 'bin32file'
wordsize = 4
numwords = 4
data = None
btype = 'bin32file'
grouppad = 2
groupsize = 8
numgroups = 2
bytepad = 1
data = None
def __init__(self, path, nl='\n', name=None):
'''fb = FileBuffer(path)'''
FileBuffer.__init__(self, path, nl, name)
def cursorx_to_datax(self, cy, cx):
if cx >= 0 and cx < 8:
ix = cx // 2
elif cx >= 9 and cx < 17:
ix = (cx - 1) // 2
elif cx >= 18 and cx < 26:
ix = (cx - 2) // 2
elif cx >= 27 and cx < 35:
ix = (cx - 3) // 2
else:
bytespace = 2 + self.bytepad
groupspace = bytespace * self.groupsize - 1 + self.grouppad
groupmod = (cx + self.grouppad) % groupspace
if groupmod < self.grouppad:
return None
groupdiv = (cx + 2) // groupspace
if groupdiv >= self.numgroups:
return None
bytemod = (cx + self.bytepad) % bytespace
if bytemod == 0:
return None
bytediv = ((cx + self.bytepad) % groupspace) // bytespace
ix = self.groupsize * groupdiv + bytediv
if ix < len(self.rawdata[cy]):
return ix
else:
return None
def datax_to_cursorx(self, ix):
if ix >= 0 and ix < 4:
return ix * 2
elif ix >= 4 and ix < 8:
return ix * 2 + 1
elif ix >= 8 and ix < 12:
return ix * 2 + 2
elif ix >= 12 and ix < 16:
return ix * 2 + 3
groupsize = (((2 + self.bytepad) * self.groupsize) + self.grouppad)
maxsize = groupsize * self.numgroups - self.grouppad
if ix < maxsize:
return (ix // self.groupsize) * self.grouppad + ix * (2 + self.bytepad)
else:
return None
def overwrite_char(self, p, c, act=ACT_NORM, force=False):
ix = self.cursorx_to_datax(p.y, p.x)
if ix is None:
@ -559,41 +566,26 @@ class Binary32Buffer(FileBuffer):
rawline = self.rawdata[p.y]
self.rawdata[p.y] = rawline[0:ix] + c + rawline[ix + 1:]
def read_filter(self, data):
bytepad = ' ' * self.bytepad
grouppad = ' ' * self.grouppad
self.rawdata = []
lines = []
i = 0
lines = []
i = 0
while i < len(data):
self.rawdata.append(data[i:i + self.numwords * self.wordsize])
self.rawdata.append(data[i:i + self.numgroups * self.groupsize])
j = 0
words = []
while j < self.numwords * self.wordsize and i + j < len(data):
nibbles = []
for c in data[i + j:i + j + self.wordsize]:
nibbles.append(string.hexdigits[ord(c) / 16])
nibbles.append(string.hexdigits[ord(c) % 16])
words.append(''.join(nibbles))
j += self.wordsize
lines.append(' '.join(words))
i += self.numwords * self.wordsize
groups = []
while j < self.numgroups * self.groupsize and i + j < len(data):
bytes = []
for c in data[i + j:i + j + self.groupsize]:
bytes.append(string.hexdigits[ord(c) / 16] + string.hexdigits[ord(c) % 16])
groups.append(bytepad.join(bytes))
j += self.groupsize
lines.append(grouppad.join(groups))
i += self.numgroups * self.groupsize
return '\n'.join(lines)
def write_filter(self, data):
bytes = []
lastc = None
for c in data:
if c not in '0123456789abcdefABCDEF':
pass
elif lastc is None:
lastc = c
else:
bytes.append(chr(int(lastc + c, 16)))
lastc = None
if lastc is not None:
bytes.append(chr(int(lastc + '0', 16)))
return ''.join(bytes)
class Binary64Buffer(Binary32Buffer):
wordsize = 8
numwords = 2
return ''.join(self.rawdata)
class DirBuffer(Buffer):
btype = 'dir'

View File

@ -4,19 +4,8 @@ from lex import Grammar, PatternRule, RegionRule
from method import Method, Argument
from point import Point
class HexGrammar(Grammar):
rules = [
PatternRule(r'zero', r"00"),
PatternRule(r'byte', r'[0-f][0-f]'),
]
class Hex(mode.Fundamental):
modename = 'Hex'
grammar = HexGrammar
colors = {
'zero': ('magenta', 'default'),
'byte': ('white', 'default'),
}
lmargin = 12
rmargin = 18
_ctrans = ['.'] * 256