From 9cd9d68b575b28b684f3f2fa7b86809e977d28dc Mon Sep 17 00:00:00 2001 From: moculus Date: Thu, 10 Apr 2008 20:23:09 +0000 Subject: [PATCH] hex mode more more more --HG-- branch : pmacs2 --- buffer.py | 94 ++++++++++++++++++++++++----------------------------- mode/hex.py | 11 ------- 2 files changed, 43 insertions(+), 62 deletions(-) diff --git a/buffer.py b/buffer.py index 32f6fe5..fd2a640 100644 --- a/buffer.py +++ b/buffer.py @@ -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' diff --git a/mode/hex.py b/mode/hex.py index e86c25b..8ef345c 100644 --- a/mode/hex.py +++ b/mode/hex.py @@ -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