From 25e39f5a0fe9ed2b96ba69f0b78f8b5b79e22421 Mon Sep 17 00:00:00 2001 From: Angel Ortega Date: Mon, 2 May 2011 10:23:05 +0200 Subject: [PATCH] New helper function for mp.hex_view(). --- mp_file.mpsl | 93 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/mp_file.mpsl b/mp_file.mpsl index 81e8b2d..e257949 100644 --- a/mp_file.mpsl +++ b/mp_file.mpsl @@ -549,60 +549,71 @@ sub mp.open(filename) } -sub mp.hex_view(filename) -/* shows a file as an hex dump */ +sub mp.hex_view_th(f, d) +/* mp.hex_view() helper */ { - local f, c; - local d = NULL; + local c; + local l = []; + local offset = 0; - if ((f = open(filename, "rb")) != NULL) { - local lines = []; - local l = []; - local offset = 0; - - d = mp.new('<' ~ filename ~ ' hex view>', lines); - d.read_only = 1; - d.syntax = mp.syntax.hex_view; + local lines = d.txt.lines; - while (1) { - if ((c = getchar(f)) != NULL) - push(l, c); + while (1) { + if ((c = getchar(f)) != NULL) + push(l, c); - if (size(l) == 16 || c == NULL) { - local h = ''; - local a = ' '; + if (size(l) == 16 || c == NULL) { + local h = ''; + local a = ' '; - /* add hex view */ - foreach (v, l) { - h = h ~ sprintf(' %02X', ord(v)); + /* add hex view */ + foreach (v, l) { + h = h ~ sprintf(' %02X', ord(v)); - if (ord(v) == 0x0a) - v = "\x{00b6}"; - else - if (ord(v) < 32 || ord(v) > 126) - v = "\x{00b7}"; + if (ord(v) == 0x0a) + v = "\x{00b6}"; + else + if (ord(v) < 32 || ord(v) > 126) + v = "\x{00b7}"; - a = a ~ v; - } + a = a ~ v; + } - local n = 16 - size(l); + local n = 16 - size(l); - /* fill up to 16 */ - while (n--) { - h = h ~ ' '; - a = a ~ ' '; - } + /* fill up to 16 */ + while (n--) { + h = h ~ ' '; + a = a ~ ' '; + } - push(lines, join([ sprintf('| %06X', offset), h, a, ''], ' |')); - offset += 16; - l = []; + push(lines, join([ sprintf('| %06X', offset), h, a, ''], ' |')); + offset += 16; + l = []; - if (c == NULL) - break; - } + if (c == NULL) + break; } + } + + close(f); +} + + +sub mp.hex_view(filename) +/* shows a file as an hex dump */ +{ + local f; + local d = NULL; + + if ((f = open(filename, "rb")) != NULL) { + local lines = []; + + d = mp.new('<' ~ filename ~ ' hex view>', lines); + d.read_only = 1; + d.syntax = mp.syntax.hex_view; - close(f); + mp.hex_view_th(f, d); } return d; -- 2.11.4.GIT