From 925861b3e76d231c7463841ee78a7db477b7dc02 Mon Sep 17 00:00:00 2001 From: d0k3 Date: Mon, 29 Feb 2016 15:47:45 +0100 Subject: [PATCH] Enable dir entry list sorting ... plus some code cosmetics --- source/fs.c | 51 ++++++++++++++++++++++++++++++++++-------------- source/fs.h | 6 +++--- source/godmode.c | 8 ++++---- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/source/fs.c b/source/fs.c index 8c679c8..bbc733c 100644 --- a/source/fs.c +++ b/source/fs.c @@ -5,7 +5,7 @@ // don't use this area for anything else! static FATFS* fs = (FATFS*)0x20316000; -// reserve one MB for this, just to be safe +// reserve one MB for this, just to be safe -> 512kb is more than enough! static DirStruct* curdir_contents = (DirStruct*)0x21000000; // this is the main buffer @@ -16,8 +16,7 @@ static size_t main_buffer_size = 4 * 1024 * 1024; // number of currently open file systems static u32 numfs = 0; -bool InitFS() -{ +bool InitFS() { #ifndef EXEC_GATEWAY // TODO: Magic? *(u32*)0x10000020 = 0; @@ -37,8 +36,7 @@ bool InitFS() return true; } -void DeinitFS() -{ +void DeinitFS() { for (u32 i = 0; i < numfs; i++) { char fsname[8]; snprintf(fsname, 7, "%lu:", numfs); @@ -57,8 +55,7 @@ bool FileCreate(const char* path, u8* data, u32 size) { return (bytes_written == size); } -bool PathCopyWorker(char* dest, char* orig) -{ +bool PathCopyWorker(char* dest, char* orig) { FILINFO fno; bool ret = false; @@ -148,8 +145,7 @@ bool PathCopyWorker(char* dest, char* orig) return ret; } -bool PathCopy(const char* destdir, const char* orig) -{ +bool PathCopy(const char* destdir, const char* orig) { char fdpath[256]; // 256 is the maximum length of a full path char fopath[256]; strncpy(fdpath, destdir, 256); @@ -157,8 +153,7 @@ bool PathCopy(const char* destdir, const char* orig) return PathCopyWorker(fdpath, fopath); } -bool PathDeleteWorker(char* fpath) -{ +bool PathDeleteWorker(char* fpath) { FILINFO fno; bool ret = true; @@ -193,15 +188,13 @@ bool PathDeleteWorker(char* fpath) return ret; } -bool PathDelete(const char* path) -{ +bool PathDelete(const char* path) { char fpath[256]; // 256 is the maximum length of a full path strncpy(fpath, path, 256); return PathDeleteWorker(fpath); } -void Screenshot() -{ +void CreateScreenshot() { const u8 bmp_header[54] = { 0x42, 0x4D, 0x36, 0xCA, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x90, 0x01, 0x00, 0x00, 0xE0, 0x01, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, @@ -230,6 +223,33 @@ void Screenshot() FileCreate(filename, main_buffer, 54 + (400 * 240 * 3 * 2)); } +void SortDirStruct(DirStruct* contents) { + for (u32 s = 0; s < contents->n_entries; s++) { + DirEntry* cmp0 = &(contents->entry[s]); + DirEntry* min0 = cmp0; + for (u32 c = s + 1; c < contents->n_entries; c++) { + DirEntry* cmp1 = &(contents->entry[c]); + if (min0->type != cmp1->type) { + if (min0->type > cmp1->type) + min0 = cmp1; + continue; + } + if (strncasecmp(min0->name, cmp1->name, 256) > 0) + min0 = cmp1; + } + if (min0 != cmp0) { + DirEntry swap; // swap entries and fix names + u32 offset_name_cmp0 = cmp0->name - cmp0->path; + u32 offset_name_min0 = min0->name - min0->path; + memcpy(&swap, cmp0, sizeof(DirEntry)); + memcpy(cmp0, min0, sizeof(DirEntry)); + memcpy(min0, &swap, sizeof(DirEntry)); + cmp0->name = cmp0->path + offset_name_min0; + min0->name = min0->path + offset_name_cmp0; + } + } +} + bool GetRootDirContentsWorker(DirStruct* contents) { static const char* drvname[16] = { "SDCARD", @@ -307,6 +327,7 @@ DirStruct* GetDirContents(const char* path) { strncpy(fpath, path, 256); if (!GetDirContentsWorker(curdir_contents, fpath, 256, false)) curdir_contents->n_entries = 0; + SortDirStruct(curdir_contents); } return curdir_contents; diff --git a/source/fs.h b/source/fs.h index db1cf84..2b158ab 100644 --- a/source/fs.h +++ b/source/fs.h @@ -4,8 +4,8 @@ typedef enum { T_FAT_ROOT, - T_FAT_FILE, - T_FAT_DIR + T_FAT_DIR, + T_FAT_FILE } EntryType; #define MAX_ENTRIES 1024 @@ -36,7 +36,7 @@ bool PathCopy(const char* destdir, const char* orig); bool PathDelete(const char* path); /** Create a screenshot of the current framebuffer **/ -void Screenshot(); +void CreateScreenshot(); /** Get directory content under a given path **/ DirStruct* GetDirContents(const char* path); diff --git a/source/godmode.c b/source/godmode.c index d30e6bd..c64211f 100644 --- a/source/godmode.c +++ b/source/godmode.c @@ -106,7 +106,7 @@ u32 GodMode() { contents = GetDirContents(""); while (true) { // this is the main loop - DrawUserInterface(current_path, &contents->entry[cursor]); + DrawUserInterface(current_path, &contents->entry[cursor]); // no need to fully do this everytime! DrawDirContents(contents, cursor); u32 pad_state = InputWait(); if (pad_state & BUTTON_DOWN) { @@ -127,16 +127,16 @@ u32 GodMode() { strncpy(current_path, contents->entry[cursor].path, 256); contents = GetDirContents(current_path); cursor = 0; - ClearScreenF(true, true, COLOR_STD_BG); + ClearScreenF(true, true, COLOR_STD_BG); // not really required } else if (pad_state & BUTTON_B) { char* last_slash = strrchr(current_path, '/'); if (last_slash) *last_slash = '\0'; else *current_path = '\0'; contents = GetDirContents(current_path); cursor = 0; - ClearScreenF(true, true, COLOR_STD_BG); + ClearScreenF(true, true, COLOR_STD_BG); // not really required } else if (pad_state & BUTTON_X) { - Screenshot(); + CreateScreenshot(); } if (pad_state & BUTTON_START) { exit_mode = (pad_state & BUTTON_LEFT) ? GODMODE_EXIT_POWEROFF : GODMODE_EXIT_REBOOT;