Skip to content

Commit

Permalink
Re-write and enable new core renderer
Browse files Browse the repository at this point in the history
This also fixes the position of the ascii column was the hex column was incomplete

Last commit where the old renderer will be available
  • Loading branch information
dd86k committed Aug 12, 2020
1 parent 85b4034 commit 5ee328a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
74 changes: 69 additions & 5 deletions src/ddhx.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module ddhx;

import std.stdio : write, writeln, writef, writefln;
import std.mmfile;
import core.stdc.stdio : printf;
import core.stdc.stdio : printf, fflush, puts, snprintf;
import core.stdc.string : memset;
import menu, ddcon;
import utils : formatsize, unformat;

Expand All @@ -17,7 +18,7 @@ enum APP_VERSION = "0.2.0";

/// Offset type (hex, dec, etc.)
enum OffsetType : size_t {
Hexadecimal, Decimal, Octal
Hex, Decimal, Octal
}

///
Expand Down Expand Up @@ -282,8 +283,74 @@ uint ddhx_render() {
return ddhx_render_raw;
}

version = NewRenderer;

/// Update display from buffer without setting cursor
/// Returns: The number of lines printed on screen
version (NewRenderer)
uint ddhx_render_raw() {
__gshared char[] hexTable = [
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
];

uint linesp; /// Lines printed
char[2048] buf = void;

size_t viewpos = cast(size_t)fpos;
size_t viewend = viewpos + screenl; /// window length
ubyte *filebuf = cast(ubyte*)CFile[viewpos..viewend].ptr;

const(char) *fposfmt = void;
with (OffsetType)
final switch (CurrentOffsetType) {
case Hex: fposfmt = "%8zX "; break;
case Octal: fposfmt = "%8zo "; break;
case Decimal: fposfmt = "%8zd "; break;
}

uint hexlen = BytesPerRow * 3; // hex part length

// vi: view index
for (size_t vi; vi < screenl; viewpos += BytesPerRow) {
// Offset column: Cannot be negative since the buffer will
// always be large enough
size_t bufindex = snprintf(buf.ptr, 32, fposfmt, viewpos);

// data bytes left to be treated for the row
size_t left = screenl - vi;

if (left >= BytesPerRow) {
left = BytesPerRow;
} else { // left < BytesPerRow
memset(buf.ptr + bufindex, ' ', 2048);
}

// Data buffering (hexadecimal and ascii)
// hi: hex buffer offset
// ai: ascii buffer offset
size_t hi = bufindex;
size_t ai = bufindex + hexlen;// + 2;
buf[ai] = ' ';
buf[ai+1] = ' ';
for (ai += 2; left > 0; --left, hi += 3, ++ai) {
ubyte b = filebuf[vi++];
buf[hi] = ' ';
buf[hi+1] = hexTable[b >> 4];
buf[hi+2] = hexTable[b & 15];
buf[ai] = b > 0x7E || b < 0x20 ? DEFAULT_CHAR : b;
}

// null terminator
buf[ai] = 0;

// Output
puts(buf.ptr);
}

return linesp;
}
else
uint ddhx_render_raw() {
__gshared char[] hexTable = [
'0', '1', '2', '3', '4', '5', '6', '7',
Expand All @@ -302,7 +369,6 @@ uint ddhx_render_raw() {
char[18] bytef = cast(char[18])"%8zX %.*s %.*s\n";
bytef[3] = formatTable[CurrentOffsetType];

//TODO: Rework loop
uint ld; /// number of lines printed
bool over = void;
ubyte b = void;
Expand All @@ -323,8 +389,6 @@ uint ddhx_render_raw() {
a[ai] = b > 0x7E || b < 0x20 ? DEFAULT_CHAR : b;
}

//TODO: Row remainder

printf(cast(char*)bytef, p, minw, cast(char*)d, brow, cast(char*)a);

++ld;
Expand Down
2 changes: 1 addition & 1 deletion src/settings.d
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void HandleOffset(string val, bool cli = false) {
switch (val[0]) {
case 'o','O': CurrentOffsetType = OffsetType.Octal; break;
case 'd','D': CurrentOffsetType = OffsetType.Decimal; break;
case 'h','H': CurrentOffsetType = OffsetType.Hexadecimal; break;
case 'h','H': CurrentOffsetType = OffsetType.Hex; break;
default:
if (cli) {
writef("Unknown mode parameter: %s", val);
Expand Down

0 comments on commit 5ee328a

Please sign in to comment.