Skip to content
Snippets Groups Projects
Commit 25fa822e authored by rswindell's avatar rswindell
Browse files

Make hexdump_lib.js more lib-like and change the functions (methods) to

return arrays rather than calling printf() directly. Also, make the ASCII
portion of the output optional (but enabled by default).
parent 43e88b61
No related branches found
No related tags found
No related merge requests found
// $Id$
load("hexdump_lib.js");
var hexdump = load({}, "hexdump_lib.js");
function main()
{
......@@ -11,7 +11,8 @@ function main()
if(!file.open("rb"))
alert(file.name + " open error " + file.error);
else {
hexdump_file(file);
var output = hexdump.file(file);
print(output.join('\n'));
file.close();
}
}
......
// $Id$
function hexdump(title, val)
// Returns an array
function generate(title, val, include_ascii)
{
var i;
var output = [];
if(include_ascii === undefined)
include_ascii = true;
if(!val) {
printf("%s: (null)\r\n", title);
return false;
return [format("%s: (null)\r\n", title)];
}
printf("%s: %u bytes\r\n", title, val.length);
if(title)
output.push(format("%s: %u bytes", title, val.length));
var line = '';
var ascii = '';
for(i=0; i < val.length; i++) {
var ch = val.charCodeAt(i);
if(i && i%16 == 0) {
printf(" %s\r\n", ascii);
output.push(line + format(" %s", ascii));
ascii = '';
line = '';
}
else if(i && i%8 == 0)
printf("- ");
printf("%02X ", ch);
ascii += format("%c", (ch >= 0x20 && ch < 0x7f) ? ch : '.');
line += "- ";
line += format("%02X ", ch);
if(include_ascii)
ascii += format("%c", (ch >= 0x20 && ch < 0x7f) ? ch : '.');
}
if(ascii) {
var gap = 0;
......@@ -28,15 +36,17 @@ function hexdump(title, val)
if((i%16) < 8)
gap += 2;
}
printf("%*s %s\r\n", gap, "", ascii);
output.push(line + format("%*s %s\r\n", gap, "", ascii));
}
print();
return true;
return output;
}
function hexdump_file(file)
function file(file, include_ascii)
{
var buf = file.read();
hexdump(file.name, buf);
}
\ No newline at end of file
return this.generate(file.name, buf, include_ascii);
}
// Leave as last line, so maybe used as a lib
this;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment