From f5403611b8f443aa0f7bf87b6047eacb03e8bd7b Mon Sep 17 00:00:00 2001 From: MCMrARM Date: Wed, 16 May 2018 20:03:47 +0200 Subject: [PATCH] File list improvements --- source/filebrowser.c | 30 +++++++++++++++++------------- source/filebrowser.h | 3 +-- source/main.c | 8 ++++++-- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/source/filebrowser.c b/source/filebrowser.c index ae88ef4..f8b94da 100644 --- a/source/filebrowser.c +++ b/source/filebrowser.c @@ -3,11 +3,11 @@ #include #include -void openMainMenu(); +static MenuItem* currentFileListBuf; +static const char* currentBasePath; +static void (*currentExitCb)(); -MenuItem* currentFileListBuf; - -void freeCurrentFileListBuf() { +static void freeCurrentFileListBuf() { MenuItem* ptr = currentFileListBuf; while (ptr != NULL) { free(ptr->text); @@ -17,20 +17,21 @@ void freeCurrentFileListBuf() { } free(currentFileListBuf); } -void exitFileList() { +static void exitFileList() { freeCurrentFileListBuf(); - openMainMenu(); + currentBasePath = NULL; + currentExitCb(); } -char* getParentDir(const char* path) { +static char* getParentDir(const char* path, const char* basePath) { char* ptr = strrchr(path, '/'); - if (ptr == NULL || ptr == path) // not found or first character + if (ptr == NULL || (ptr - path < strlen(basePath) && strcmp(path, basePath) == 0)) return NULL; char* retval = (char*) malloc(ptr - path + 1); memcpy(retval, path, ptr - path); retval[ptr - path] = '\0'; return retval; } -char* pathJoin(const char* p1, const char* p2) { +static char* pathJoin(const char* p1, const char* p2) { size_t p1s = strlen(p1); if (p1s == 0) return strdup(p2); @@ -41,14 +42,17 @@ char* pathJoin(const char* p1, const char* p2) { memcpy(&retval[p1s + 1], p2, p2s + 1); // copy with null terminator return retval; } -void printFilesInDir(const char* path) { +static void printFilesInDirMenuItem(MenuItem* item); +void printFilesInDir(const char* path, const char* basePath, void (*exitCb)()) { int maxMenuItemCount = 48; MenuItem* buf = (MenuItem*) malloc(sizeof(MenuItem) * (maxMenuItemCount + 1)); currentFileListBuf = buf; + currentBasePath = basePath; + currentExitCb = exitCb; DIR* dir = opendir(path); struct dirent* ent; int bufi = 0; - char* parentDir = getParentDir(path); + char* parentDir = getParentDir(path, basePath); if (parentDir != NULL) { buf[bufi].userdata = parentDir; buf[bufi].text = strdup(".."); @@ -72,6 +76,6 @@ void printFilesInDir(const char* path) { menuSetCurrent(buf, exitFileList); } -void printFilesInDirMenuItem(MenuItem* item) { - printFilesInDir(item->userdata); +static void printFilesInDirMenuItem(MenuItem* item) { + printFilesInDir(item->userdata, currentBasePath, currentExitCb); } diff --git a/source/filebrowser.h b/source/filebrowser.h index 6292515..2133507 100644 --- a/source/filebrowser.h +++ b/source/filebrowser.h @@ -2,5 +2,4 @@ #include "menu.h" -void printFilesInDir(const char* path); -void printFilesInDirMenuItem(MenuItem* item); \ No newline at end of file +void printFilesInDir(const char* path, const char* basePath, void (*exitCb)()); \ No newline at end of file diff --git a/source/main.c b/source/main.c index 5cd4f44..b364c98 100644 --- a/source/main.c +++ b/source/main.c @@ -63,6 +63,11 @@ void dumpPartitionDataMenuItem(MenuItem* item) { menuWaitForAnyButton(); } +void openMainMenu(); +void viewPartitionExitCb() { + fsdevUnmountDevice("view"); + openMainMenu(); +} void viewPartitionMenuItem(MenuItem* item) { u32 partition = (u32) (size_t) item->userdata; @@ -74,13 +79,12 @@ void viewPartitionMenuItem(MenuItem* item) { menuWaitForAnyButton(); return; } - fsdevUnmountDevice("view"); // unmount it if it exists TODO: This should be done when exiting, not here if (fsdevMountDevice("view", fs) == -1) { printf("fsdevMountDevice failed\n"); menuWaitForAnyButton(); return; } - printFilesInDir("view:/"); + printFilesInDir("view://", "view://", viewPartitionExitCb); }