From 25c346e0d74d30aca49faa420c2d3858cc3ed9a6 Mon Sep 17 00:00:00 2001 From: d0k3 Date: Sun, 10 Apr 2016 15:55:39 +0200 Subject: [PATCH] Improved hex viewer navigation --- source/fs.c | 16 ++++++++++++++++ source/fs.h | 3 +++ source/godmode.c | 11 ++++++++--- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/source/fs.c b/source/fs.c index d1ba651..8dbd4e6 100644 --- a/source/fs.c +++ b/source/fs.c @@ -188,6 +188,22 @@ size_t FileGetData(const char* path, u8* data, size_t size, size_t foffset) return 0; } +size_t FileGetSize(const char* path) { + if (PathToNumFS(path) >= 0) { + FILINFO fno; + fno.lfname = NULL; + if (f_stat(path, &fno) != FR_OK) + return 0; + return fno.fsize; + } else if (GetVirtualSource(path)) { + VirtualFile vfile; + if (!FindVirtualFile(&vfile, path, 0)) + return 0; + return vfile.size; + } + return 0; +} + bool PathCopyVirtual(const char* destdir, const char* orig) { char dest[256]; // maximum path name length in FAT char* oname = strrchr(orig, '/'); diff --git a/source/fs.h b/source/fs.h index 639c163..7d3a2f4 100644 --- a/source/fs.h +++ b/source/fs.h @@ -44,6 +44,9 @@ bool FileCreateData(const char* path, u8* data, size_t size); /** Read data from file@offset **/ size_t FileGetData(const char* path, u8* data, size_t size, size_t foffset); +/** Get size of file **/ +size_t FileGetSize(const char* path); + /** Recursively copy a file or directory **/ bool PathCopy(const char* destdir, const char* orig); diff --git a/source/godmode.c b/source/godmode.c index daba6f7..e352d42 100644 --- a/source/godmode.c +++ b/source/godmode.c @@ -161,6 +161,7 @@ void DrawDirContents(DirStruct* contents, u32 cursor, u32* scroll) { u32 HexViewer(const char* path) { static u32 mode = 0; u8 data[(SCREEN_HEIGHT / 8) * 16]; // this is the maximum size + u32 fsize = FileGetSize(path); int x_off, x_hex, x_ascii; u32 vpad, hlpad, hrpad; @@ -211,6 +212,9 @@ u32 HexViewer(const char* path) { last_mode = mode; ClearScreenF(true, false, COLOR_STD_BG); } + // fix offset (if required) + if (offset + total_shown > fsize + cols) + offset = (total_shown > fsize) ? 0 : (fsize + cols - total_shown - (fsize % cols)); total_data = FileGetData(path, data, total_shown, offset); // get data // display data on screen @@ -243,8 +247,8 @@ u32 HexViewer(const char* path) { // handle user input u32 pad_state = InputWait(); - u32 step_ud = (pad_state & BUTTON_R1) ? total_shown * 16 : cols; - u32 step_lr = (pad_state & BUTTON_R1) ? total_shown * 256 : total_shown; + u32 step_ud = (pad_state & BUTTON_R1) ? (0x1000 - (0x1000 % cols)) : cols; + u32 step_lr = (pad_state & BUTTON_R1) ? (0x10000 - (0x10000 % cols)) : total_shown; if (pad_state & BUTTON_DOWN) offset += step_ud; else if (pad_state & BUTTON_RIGHT) offset += step_lr; else if (pad_state & BUTTON_UP) offset = (offset > step_ud) ? offset - step_ud : 0; @@ -349,7 +353,8 @@ u32 GodMode() { } if (clipboard->n_entries && (strcspn(clipboard->entry[0].path, IMG_DRV) == 0)) clipboard->n_entries = 0; // remove invalid clipboard stuff - } else if (ShowPrompt(true, "Show HexViewer?\n \nControls:\n\x18\x19\x1A\x1B(+R) - Scroll\nR+Y - Switch view\nB - Exit\n")) { + } else if (FileGetSize(curr_entry->path) && + ShowPrompt(true, "Show HexViewer?\n \nControls:\n\x18\x19\x1A\x1B(+R) - Scroll\nR+Y - Switch view\nB - Exit\n")) { HexViewer(curr_entry->path); } } else if (*current_path && ((pad_state & BUTTON_B) || // one level down