Improved hex viewer navigation

This commit is contained in:
d0k3 2016-04-10 15:55:39 +02:00
parent 655d56c7bd
commit 25c346e0d7
3 changed files with 27 additions and 3 deletions

View File

@ -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, '/');

View File

@ -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);

View File

@ -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