EDIT: Enhanced file_ui() to support directories bigger than io_buf[]

This commit is contained in:
Bobbi Webber-Manners 2020-09-24 00:20:41 -04:00
parent 569e70e7dd
commit 1129ee37e9

View File

@ -2028,7 +2028,9 @@ struct tabent {
uint32_t size;
} *entry;
#define FILELINES 16
// It is best if FILESPERPAGE is a multiple of FILELINES
#define FILELINES 16 // Number of lines displayed in file_ui
#define FILESPERPAGE 160 // Number of files loaded into iobuf[]
/*
* Draw one line in file chooser UI
@ -2133,6 +2135,49 @@ uint16_t online(void) {
return entries;
}
/*
* Read directory entries and populate table in iobuf[]
* for file_ui()
* page - page index, each page has FILESPERPAGE entries
* page is 0, 1, 2, 3 ...
* num_entries - number of entries read
* Returns 0 there are more files, 1 if end of directory reached
*/
uint8_t read_dir(uint8_t page, uint16_t *num_entries) {
DIR *dp;
struct dirent *ent;
struct tabent *entry;
uint8_t rc = 0;
uint16_t entries = 0, i = 0;
entry = (struct tabent*)iobuf;
if (page == 0) {
strcpy(entry->name, ".."); // Add fake '..' entry
entry->type = 0x0f;
++entry;
++entries;
}
dp = opendir(".");
while (1) {
ent = readdir(dp);
if (!ent) {
rc = 1;
break;
}
if (++i < page * FILESPERPAGE)
continue;
if (entries >= FILESPERPAGE)
break;
memcpy(entry->name, ent->d_name, 16);
entry->type = ent->d_type;
entry->size = ent->d_size;
++entry;
++entries;
}
closedir(dp);
*num_entries = entries;
return rc;
}
/*
* File chooser UI
* Leaves file name in userentry[], or empty string if error/cancel
@ -2142,13 +2187,15 @@ uint16_t online(void) {
*/
void file_ui(char *msg1, char *msg2, char *msg3) {
struct tabent *entry;
DIR *dp;
struct dirent *ent;
char c;
uint16_t entries, current, first;
uint8_t toplevel = 0;
uint16_t entries;
uint8_t end_of_dir;
uint16_t first = 0, current = 0;
uint8_t toplevel = 0, page = 0;
cutbuflen = 0;
restart:
clrscr();
cursor(0);
gotoxy(0,0);
revers(1);
cprintf("%s", msg1);
@ -2162,34 +2209,10 @@ restart:
revers(1);
cprintf("%s", (toplevel ? "Volumes" : userentry));
revers(0);
entries = current = first = 0;
cutbuflen = 0;
if (toplevel) {
if (toplevel)
entries = online();
} else {
entry = (struct tabent*)iobuf;
strcpy(entry->name, ".."); // Add fake '..' entry
entry->type = 0x0f;
++entry;
++entries;
cursor(0);
dp = opendir(".");
while (1) {
ent = readdir(dp);
if (!ent)
break;
memcpy(entry->name, ent->d_name, 16);
entry->type = ent->d_type;
entry->size = ent->d_size;
++entry;
++entries;
if ((char*)entry > (char*)iobuf + CUTBUFSZ - 100) {
beep();
break;
}
}
closedir(dp);
}
else
end_of_dir = read_dir(page, &entries);
redraw:
file_ui_draw_all(first, current, entries);
while (1) {
@ -2198,6 +2221,13 @@ redraw:
case 0x0b: // Up
if (current > 0)
--current;
else
if (page > 0) {
--page;
current = FILESPERPAGE - 1;
first = current - FILELINES + 1;
goto restart;
}
if (current < first) {
if (first > FILELINES)
first -= FILELINES;
@ -2211,6 +2241,11 @@ redraw:
case 0x0a: // Down
if (current < entries - 1)
++current;
else if (!end_of_dir) {
++page;
first = current = 0;
goto restart;
}
if (current >= first + FILELINES) {
first += FILELINES;
goto redraw;
@ -2233,6 +2268,7 @@ redraw:
toplevel = 1;
else
chdir(userentry);
first = current = 0;
goto restart;
} else {
if (toplevel) {
@ -2245,6 +2281,7 @@ redraw:
chdir(userentry);
}
toplevel = 0;
first = current = 0;
goto restart;
}
break;