Start of rewrite
Based on lockpick_rcm 1.9.0
This commit is contained in:
38
source/fs/fsutils.c
Normal file
38
source/fs/fsutils.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <mem/heap.h>
|
||||
#include <string.h>
|
||||
#include "fsutils.h"
|
||||
#include "../utils/utils.h"
|
||||
#include <utils/sprintf.h>
|
||||
#include <libs/fatfs/ff.h>
|
||||
|
||||
char *CombinePaths(const char *current, const char *add){
|
||||
char *ret;
|
||||
|
||||
size_t size = strlen(current) + strlen(add) + 2;
|
||||
ret = (char*) malloc (size);
|
||||
|
||||
sprintf(ret, (current[strlen(current) - 1] == '/') ? "%s%s" : "%s/%s", current, add);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *EscapeFolder(char *current){
|
||||
char *ret;
|
||||
char *temp;
|
||||
|
||||
ret = CpyStr(current);
|
||||
temp = strrchr(ret, '/');
|
||||
|
||||
if (*(temp - 1) == ':')
|
||||
temp++;
|
||||
|
||||
*temp = '\0';
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
u64 GetFileSize(char *path){
|
||||
FILINFO fno;
|
||||
f_stat(path, &fno);
|
||||
return fno.fsize;
|
||||
}
|
||||
6
source/fs/fsutils.h
Normal file
6
source/fs/fsutils.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include <utils/types.h>
|
||||
|
||||
u64 GetFileSize(char *path);
|
||||
char *EscapeFolder(char *current);
|
||||
char *CombinePaths(const char *current, const char *add);
|
||||
88
source/fs/menus/explorer.c
Normal file
88
source/fs/menus/explorer.c
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "explorer.h"
|
||||
#include "../../utils/vector.h"
|
||||
#include "../readers/folderReader.h"
|
||||
#include "../../gfx/menu.h"
|
||||
#include "../fsutils.h"
|
||||
#include "../../gfx/gfx.h"
|
||||
#include "../../gfx/gfxutils.h"
|
||||
#include "../../utils/utils.h"
|
||||
#include <string.h>
|
||||
#include <mem/heap.h>
|
||||
|
||||
MenuEntry_t topEntries[] = {
|
||||
{.optionUnion = COLORTORGB(COLOR_ORANGE), .name = "Back"},
|
||||
{.optionUnion = COLORTORGB(COLOR_ORANGE), .name = "Option2"},
|
||||
{.optionUnion = COLORTORGB(COLOR_ORANGE), .name = "Option3"}
|
||||
};
|
||||
|
||||
MenuEntry_t MakeMenuOutFSEntry(FSEntry_t entry){
|
||||
MenuEntry_t out = {.name = entry.name, .sizeUnion = entry.sizeUnion};
|
||||
out.optionUnion = (entry.isDir) ? COLORTORGB(COLOR_WHITE) : COLORTORGB(COLOR_VIOLET);
|
||||
out.icon = (entry.isDir) ? 127 : 128;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
#define maxYOnScreen 35
|
||||
|
||||
void ExpandFileList(Vector_t *vec, void* data){
|
||||
Vector_t *fileVec = (Vector_t*)data;
|
||||
vecPDefArray(FSEntry_t*, fsEntries, fileVec);
|
||||
|
||||
int start = vec->count - 3;
|
||||
for (int i = start; i < MIN(fileVec->count, start + maxYOnScreen); i++){
|
||||
MenuEntry_t a = MakeMenuOutFSEntry(fsEntries[i]);
|
||||
vecAddElem(vec, a);
|
||||
}
|
||||
}
|
||||
|
||||
void clearFileVector(Vector_t *v){
|
||||
vecPDefArray(FSEntry_t*, entries, v);
|
||||
for (int i = 0; i < v->count; i++)
|
||||
free(entries[i].name);
|
||||
|
||||
free(v->data);
|
||||
}
|
||||
|
||||
void FileExplorer(char *path){
|
||||
char *storedPath = path;
|
||||
int res = 0;
|
||||
|
||||
while (1){
|
||||
gfx_clearscreen();
|
||||
gfx_printf("Loading...\r");
|
||||
Vector_t fileVec = ReadFolder(storedPath);
|
||||
vecDefArray(FSEntry_t*, fsEntries, fileVec);
|
||||
|
||||
Vector_t entries = newVec(sizeof(MenuEntry_t), maxYOnScreen);
|
||||
entries.count = 3;
|
||||
memcpy(entries.data, topEntries, sizeof(MenuEntry_t) * 3);
|
||||
|
||||
for (int i = 0; i < fileVec.count; i++){
|
||||
MenuEntry_t a = MakeMenuOutFSEntry(fsEntries[i]);
|
||||
vecAddElem(&entries, a);
|
||||
}
|
||||
|
||||
// ExpandFileList was first in NULL, but now we don't need it as we aren't loading the file size dynamically
|
||||
res = newMenu(&entries, res, 50, maxYOnScreen, NULL, &fileVec, true, fileVec.count);
|
||||
|
||||
if (res < 3) {
|
||||
if (!strcmp(storedPath, path)){
|
||||
clearFileVector(&fileVec);
|
||||
return;
|
||||
}
|
||||
|
||||
char *copy = CpyStr(storedPath);
|
||||
storedPath = EscapeFolder(copy);
|
||||
free(copy);
|
||||
}
|
||||
else if (fsEntries[res - 3].isDir) {
|
||||
char *copy = CpyStr(storedPath);
|
||||
storedPath = CombinePaths(copy, fsEntries[res - 3].name);
|
||||
free(copy);
|
||||
}
|
||||
|
||||
res = 0;
|
||||
clearFileVector(&fileVec);
|
||||
}
|
||||
}
|
||||
9
source/fs/menus/explorer.h
Normal file
9
source/fs/menus/explorer.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include "../../utils/vector.h"
|
||||
|
||||
typedef struct {
|
||||
char *curPath;
|
||||
Vector_t *files;
|
||||
} ExplorerCtx_t;
|
||||
|
||||
void FileExplorer(char *path);
|
||||
37
source/fs/readers/folderReader.c
Normal file
37
source/fs/readers/folderReader.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "folderReader.h"
|
||||
#include <libs/fatfs/ff.h>
|
||||
#include "../../utils/utils.h"
|
||||
|
||||
Vector_t /* of type FSEntry_t */ ReadFolder(char *path){
|
||||
Vector_t out = newVec(sizeof(FSEntry_t), 16); // we may want to prealloc with the same size as the folder
|
||||
DIR dir;
|
||||
FILINFO fno;
|
||||
int res;
|
||||
|
||||
if ((res = f_opendir(&dir, path))){
|
||||
// Err!
|
||||
return out;
|
||||
}
|
||||
|
||||
while (!f_readdir(&dir, &fno) && fno.fname[0]) {
|
||||
FSEntry_t newEntry = {.isDir = (fno.fattrib & AM_DIR) ? 1 : 0, .name = CpyStr(fno.fname)};
|
||||
if (!newEntry.isDir){
|
||||
u64 total = fno.fsize;
|
||||
u8 type = 0;
|
||||
while (total > 1024){
|
||||
total /= 1024;
|
||||
type++;
|
||||
}
|
||||
|
||||
if (type > 3)
|
||||
type = 3;
|
||||
|
||||
newEntry.showSize = 1;
|
||||
newEntry.size = total;
|
||||
newEntry.sizeDef = type;
|
||||
}
|
||||
vecAddElem(&out, newEntry);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
23
source/fs/readers/folderReader.h
Normal file
23
source/fs/readers/folderReader.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include <utils/types.h>
|
||||
#include "../../utils/vector.h"
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
union {
|
||||
struct {
|
||||
u8 isDir:1;
|
||||
};
|
||||
u8 optionUnion;
|
||||
};
|
||||
union {
|
||||
struct {
|
||||
u16 size:12;
|
||||
u16 showSize:1;
|
||||
u16 sizeDef:3;
|
||||
};
|
||||
u16 sizeUnion;
|
||||
};
|
||||
} FSEntry_t;
|
||||
|
||||
Vector_t /* of type FSEntry_t */ ReadFolder(char *path);
|
||||
Reference in New Issue
Block a user