Made the file browser sorting a bit faster.
This commit is contained in:
parent
ac33d98ad5
commit
dac935f66d
|
@ -20,6 +20,13 @@ typedef struct
|
|||
char str[255];
|
||||
} DirListEnt;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u32 num;
|
||||
DirListEnt entries[MAX_DIR_ENTRIES];
|
||||
DirListEnt *ptrs[MAX_DIR_ENTRIES];
|
||||
} DirList;
|
||||
|
||||
|
||||
|
||||
// num including null terminator.
|
||||
|
@ -40,8 +47,8 @@ static size_t safeStrcpy(char *const dst, const char *const src, size_t num)
|
|||
|
||||
int dlistCompare(const void *a, const void *b)
|
||||
{
|
||||
const DirListEnt *const entA = (DirListEnt*)a;
|
||||
const DirListEnt *const entB = (DirListEnt*)b;
|
||||
const DirListEnt *const entA = *(DirListEnt**)a;
|
||||
const DirListEnt *const entB = *(DirListEnt**)b;
|
||||
|
||||
if(entA->type != entB->type) return (int)entB->type - entA->type;
|
||||
|
||||
|
@ -53,12 +60,12 @@ int dlistCompare(const void *a, const void *b)
|
|||
return res;
|
||||
}
|
||||
|
||||
static Result scanDir(const char *const path, DirListEnt *const dList, u32 *num, const char *const filter)
|
||||
static Result scanDir(const char *const path, DirList *const dList, const char *const filter)
|
||||
{
|
||||
FILINFO *const fi = (FILINFO*)malloc(sizeof(FILINFO) * DIR_READ_BLOCKS);
|
||||
if(fi == NULL) return RES_OUT_OF_MEM;
|
||||
|
||||
*num = 0;
|
||||
dList->num = 0;
|
||||
|
||||
Result res;
|
||||
DHandle dh;
|
||||
|
@ -82,35 +89,36 @@ static Result scanDir(const char *const path, DirListEnt *const dList, u32 *num,
|
|||
continue;
|
||||
}
|
||||
|
||||
dList[dListPos].type = isDir;
|
||||
safeStrcpy(dList[dListPos].str, fi[i].fname, 255);
|
||||
dList->entries[dListPos].type = isDir;
|
||||
safeStrcpy(dList->entries[dListPos].str, fi[i].fname, 255);
|
||||
dList->ptrs[dListPos] = &dList->entries[dListPos];
|
||||
dListPos++;
|
||||
}
|
||||
} while(read == DIR_READ_BLOCKS);
|
||||
*num = dListPos;
|
||||
dList->num = dListPos;
|
||||
|
||||
fCloseDir(dh);
|
||||
}
|
||||
|
||||
free(fi);
|
||||
|
||||
qsort(dList, *num, sizeof(DirListEnt), dlistCompare);
|
||||
qsort(dList->ptrs, dList->num, sizeof(DirListEnt*), dlistCompare);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static void showDirList(const DirListEnt *const dList, u32 num, u32 start)
|
||||
static void showDirList(const DirList *const dList, u32 start)
|
||||
{
|
||||
// Clear screen.
|
||||
ee_printf("\x1b[2J");
|
||||
|
||||
const u32 listLength = (num - start > SCREEN_ROWS ? start + SCREEN_ROWS : num);
|
||||
const u32 listLength = (dList->num - start > SCREEN_ROWS ? start + SCREEN_ROWS : dList->num);
|
||||
for(u32 i = start; i < listLength; i++)
|
||||
{
|
||||
const char *const printStr =
|
||||
(dList[i].type == 0 ? "\x1b[%lu;H\x1b[37m %.51s" : "\x1b[%lu;H\x1b[33m %.51s");
|
||||
(dList->ptrs[i]->type == 0 ? "\x1b[%lu;H\x1b[37m %.51s" : "\x1b[%lu;H\x1b[33m %.51s");
|
||||
|
||||
ee_printf(printStr, i - start, dList[i].str);
|
||||
ee_printf(printStr, i - start, dList->ptrs[i]->str);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -123,13 +131,12 @@ Result browseFiles(const char *const basePath, char selected[512])
|
|||
if(curDir == NULL) return RES_OUT_OF_MEM;
|
||||
safeStrcpy(curDir, basePath, 512);
|
||||
|
||||
DirListEnt *const dList = (DirListEnt*)malloc(sizeof(DirListEnt) * MAX_DIR_ENTRIES);
|
||||
DirList *const dList = (DirList*)malloc(sizeof(DirList));
|
||||
if(dList == NULL) return RES_OUT_OF_MEM;
|
||||
|
||||
Result res;
|
||||
u32 num;
|
||||
if((res = scanDir(curDir, dList, &num, ".gba")) != RES_OK) goto end;
|
||||
showDirList(dList, num, 0);
|
||||
if((res = scanDir(curDir, dList, ".gba")) != RES_OK) goto end;
|
||||
showDirList(dList, 0);
|
||||
|
||||
s32 cursorPos = 0; // Within the entire list.
|
||||
u32 windowPos = 0; // Window start position within the list.
|
||||
|
@ -149,6 +156,7 @@ Result browseFiles(const char *const basePath, char selected[512])
|
|||
kDown = hidKeysDown();
|
||||
} while(kDown == 0);
|
||||
|
||||
const u32 num = dList->num;
|
||||
if(num != 0)
|
||||
{
|
||||
oldCursorPos = cursorPos;
|
||||
|
@ -172,12 +180,12 @@ Result browseFiles(const char *const basePath, char selected[512])
|
|||
if((u32)cursorPos < windowPos)
|
||||
{
|
||||
windowPos = cursorPos;
|
||||
showDirList(dList, num, windowPos);
|
||||
showDirList(dList, windowPos);
|
||||
}
|
||||
if((u32)cursorPos >= windowPos + SCREEN_ROWS)
|
||||
{
|
||||
windowPos = cursorPos - (SCREEN_ROWS - 1);
|
||||
showDirList(dList, num, windowPos);
|
||||
showDirList(dList, windowPos);
|
||||
}
|
||||
|
||||
if(kDown & (KEY_A | KEY_B))
|
||||
|
@ -188,9 +196,9 @@ Result browseFiles(const char *const basePath, char selected[512])
|
|||
{
|
||||
// TODO: !!! Insecure !!!
|
||||
if(curDir[pathLen - 1] != '/') curDir[pathLen++] = '/';
|
||||
safeStrcpy(curDir + pathLen, dList[cursorPos].str, 255);
|
||||
safeStrcpy(curDir + pathLen, dList->ptrs[cursorPos]->str, 255);
|
||||
|
||||
if(dList[cursorPos].type == 0)
|
||||
if(dList->ptrs[cursorPos]->type == 0)
|
||||
{
|
||||
safeStrcpy(selected, curDir, 512);
|
||||
break;
|
||||
|
@ -204,10 +212,10 @@ Result browseFiles(const char *const basePath, char selected[512])
|
|||
*tmpPathPtr = '\0';
|
||||
}
|
||||
|
||||
if((res = scanDir(curDir, dList, &num, ".gba")) != RES_OK) break;
|
||||
if((res = scanDir(curDir, dList, ".gba")) != RES_OK) break;
|
||||
cursorPos = 0;
|
||||
windowPos = 0;
|
||||
showDirList(dList, num, 0);
|
||||
showDirList(dList, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue