improved hex mode

--HG--
branch : pmacs2
This commit is contained in:
moculus 2007-10-15 19:35:06 +00:00
parent 100947dd36
commit c9998e1b19
4 changed files with 29 additions and 13 deletions

View File

@ -280,7 +280,7 @@ class Application(object):
if cipher is None: if cipher is None:
if not os.path.exists(path) or os.path.isfile(path): if not os.path.exists(path) or os.path.isfile(path):
if binary: if binary:
b = buffer2.BinaryBuffer(path, name=name) b = buffer2.Binary32Buffer(path, name=name)
else: else:
b = buffer2.FileBuffer(path, name=name) b = buffer2.FileBuffer(path, name=name)
elif os.path.isdir(path): elif os.path.isdir(path):
@ -795,7 +795,7 @@ def open_aes_file(path, nl, name=None):
def open_plain_file(path, nl, name=None, binary=False): def open_plain_file(path, nl, name=None, binary=False):
if os.path.isfile(path) or not os.path.exists(path): if os.path.isfile(path) or not os.path.exists(path):
if binary: if binary:
return buffer2.BinaryBuffer(path, nl, name) return buffer2.Binary32Buffer(path, nl, name)
else: else:
return buffer2.FileBuffer(path, nl, name) return buffer2.FileBuffer(path, nl, name)
elif os.path.isdir(path): elif os.path.isdir(path):

View File

@ -484,27 +484,28 @@ class AesBuffer(FileBuffer):
def write_filter(self, data): def write_filter(self, data):
return aes.encrypt_data(data, self.password) return aes.encrypt_data(data, self.password)
class BinaryBuffer(FileBuffer): class Binary32Buffer(FileBuffer):
btype = 'binfile' btype = 'bin32file'
def __init__(self, path, nl='\n', name=None, numbytes=2): wordsize = 4
numwords = 4
def __init__(self, path, nl='\n', name=None):
'''fb = FileBuffer(path)''' '''fb = FileBuffer(path)'''
FileBuffer.__init__(self, path, nl, name) FileBuffer.__init__(self, path, nl, name)
self.numbytes = numbytes
def read_filter(self, data): def read_filter(self, data):
lines = [] lines = []
i = 0 i = 0
while i < len(data): while i < len(data):
j = 0 j = 0
words = [] words = []
while j < self.numbytes * 8 and i + j < len(data): while j < self.numwords * self.wordsize and i + j < len(data):
nibbles = [] nibbles = []
for c in data[i + j:i + j + 8]: for c in data[i + j:i + j + self.wordsize]:
nibbles.append(string.hexdigits[ord(c) / 16]) nibbles.append(string.hexdigits[ord(c) / 16])
nibbles.append(string.hexdigits[ord(c) % 16]) nibbles.append(string.hexdigits[ord(c) % 16])
words.append(''.join(nibbles)) words.append(''.join(nibbles))
j += 8 j += self.wordsize
lines.append(' '.join(words)) lines.append(' '.join(words))
i += self.numbytes * 8 i += self.numwords * self.wordsize
return '\n'.join(lines) return '\n'.join(lines)
def write_filter(self, data): def write_filter(self, data):
bytes = [] bytes = []

View File

@ -34,7 +34,7 @@ class Argument:
def coerce_to_type(self, value): def coerce_to_type(self, value):
if self.type == type(0): if self.type == type(0):
try: try:
return int(value) return int(value, 0)
except: except:
raise Exception, "expected int; got %s" % (repr(value)) raise Exception, "expected int; got %s" % (repr(value))
else: else:

View File

@ -1,11 +1,12 @@
import string import string
import color, mode2 import color, mode2
from lex3 import Grammar, PatternRule, RegionRule from lex3 import Grammar, PatternRule, RegionRule
from method import Method, Argument
from point2 import Point
class HexGrammar(Grammar): class HexGrammar(Grammar):
rules = [ rules = [
PatternRule(r'zero', r"00"), PatternRule(r'zero', r"00"),
PatternRule(r'printable', r"[2-7][0-9a-f]"),
PatternRule(r'byte', r'[0-f][0-f]'), PatternRule(r'byte', r'[0-f][0-f]'),
] ]
@ -13,12 +14,13 @@ class Hex(mode2.Fundamental):
grammar = HexGrammar grammar = HexGrammar
colors = { colors = {
'zero': ('magenta', 'default'), 'zero': ('magenta', 'default'),
'printable': ('green', 'default'),
'byte': ('white', 'default'), 'byte': ('white', 'default'),
} }
def __init__(self, w): def __init__(self, w):
mode2.Fundamental.__init__(self, w) mode2.Fundamental.__init__(self, w)
self.add_action_and_bindings(GotoWord(), ('M-g',))
# create all the insert actions for the basic text input # create all the insert actions for the basic text input
for c in string.letters + string.digits + string.punctuation: for c in string.letters + string.digits + string.punctuation:
if c in string.hexdigits: if c in string.hexdigits:
@ -28,3 +30,16 @@ class Hex(mode2.Fundamental):
def name(self): def name(self):
return "Hex" return "Hex"
class GotoWord(Method):
'''Jump to the specified line number'''
args = [Argument("wordno", type=type(0), prompt="Goto word: ")]
def _execute(self, w, **vargs):
n = vargs["wordno"]
if n < 0:
w.set_error("Negative word counts not supported.")
try:
p = Point((n % 4) * 9, n / 4)
w.goto(p)
except:
w.goto_end()