libs/bfile: fix the file enumeration
It used to fail, but actually not because of BFile, instead because of shaky variable storage on the stack.
This commit is contained in:
parent
ddb9bc12a2
commit
29fc91d60c
|
@ -11,51 +11,70 @@
|
|||
#include <gintctl/widgets/gtable.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* Names and file informations (allocated on the stack) */
|
||||
static uint16_t (*test_names)[32][32];
|
||||
static struct BFile_FileInfo *test_infos;
|
||||
/* Names and file informations */
|
||||
static char **test_names = NULL;
|
||||
static struct BFile_FileInfo *test_infos = NULL;
|
||||
|
||||
int explore_folder(uint16_t const *search, uint16_t names[32][32],
|
||||
struct BFile_FileInfo infos[32])
|
||||
extern char *fc_to_utf8_alloc(uint16_t const *fc);
|
||||
|
||||
int explore_folder(uint16_t const *search)
|
||||
{
|
||||
/* Search descriptor */
|
||||
int sd = 0;
|
||||
/* Number of files found */
|
||||
int found = 0;
|
||||
struct BFile_FileInfo info;
|
||||
int sd=-1, count=0, allocated=0;
|
||||
|
||||
int rc = BFile_FindFirst(search, &sd, names[found], &infos[found]);
|
||||
if(rc < 0) return 0;
|
||||
uint16_t *fc_path = malloc(512 * sizeof *fc_path);
|
||||
if(!fc_path) return 0;
|
||||
|
||||
while(rc >= 0 && found < 32) {
|
||||
found++;
|
||||
rc = BFile_FindNext(sd, names[found], &infos[found]);
|
||||
int rc = BFile_FindFirst(search, &sd, fc_path, &info);
|
||||
if(rc < 0) goto end;
|
||||
|
||||
do {
|
||||
/* We want one extra space for the terminator in test_names[] */
|
||||
if(count+1 >= allocated) {
|
||||
allocated += 8;
|
||||
|
||||
char **new_test_names = realloc(test_names,
|
||||
sizeof *test_names * allocated);
|
||||
struct BFile_FileInfo *new_test_infos = realloc(test_infos,
|
||||
sizeof *test_infos * allocated);
|
||||
|
||||
if(new_test_names)
|
||||
test_names = new_test_names;
|
||||
if(new_test_infos)
|
||||
test_infos = new_test_infos;
|
||||
|
||||
if(!new_test_names || !new_test_infos)
|
||||
goto end;
|
||||
}
|
||||
|
||||
test_names[count] = fc_to_utf8_alloc(fc_path);
|
||||
test_names[count+1] = NULL;
|
||||
test_infos[count] = info;
|
||||
count++;
|
||||
|
||||
rc = BFile_FindNext(sd, fc_path, &info);
|
||||
}
|
||||
while(rc >= 0);
|
||||
|
||||
BFile_FindClose(sd);
|
||||
return found;
|
||||
end:
|
||||
if(sd >= 0)
|
||||
BFile_FindClose(sd);
|
||||
free(fc_path);
|
||||
return count;
|
||||
}
|
||||
|
||||
static void table_gen(gtable *t, int row)
|
||||
{
|
||||
char c1[48]={0}, c2[16];
|
||||
|
||||
for(int i = 0; i < 32 && (*test_names)[row][i]; i++)
|
||||
c1[i] = (*test_names)[row][i];
|
||||
|
||||
sprintf(c2, "%d", test_infos[row].file_size);
|
||||
char *c1 = test_names ? test_names[row] : "(null)";
|
||||
char c2[16];
|
||||
sprintf(c2, "%d", test_infos ? (int)test_infos[row].file_size : -1);
|
||||
gtable_provide(t, c1, c2);
|
||||
}
|
||||
|
||||
void gintctl_libs_bfile(void)
|
||||
{
|
||||
/* Data for 32 files, each with 32 characters in the name */
|
||||
uint16_t names[32][32];
|
||||
struct BFile_FileInfo infos[32];
|
||||
|
||||
test_names = &names;
|
||||
test_infos = infos;
|
||||
|
||||
gtable *table = gtable_create(2, table_gen, NULL, NULL);
|
||||
gtable_set_rows(table, 0);
|
||||
gtable_set_column_titles(table, "Name", "Size");
|
||||
|
@ -83,13 +102,22 @@ void gintctl_libs_bfile(void)
|
|||
key = e.key.key;
|
||||
|
||||
if(key == KEY_F1) {
|
||||
if(test_names) {
|
||||
for(int i = 0; test_names[i]; i++)
|
||||
free(test_names[i]);
|
||||
free(test_names);
|
||||
test_names = NULL;
|
||||
}
|
||||
if(test_infos) {
|
||||
free(test_infos);
|
||||
test_infos = NULL;
|
||||
}
|
||||
|
||||
int rows = gint_world_switch(GINT_CALL(explore_folder,
|
||||
u"\\\\fls0\\*", (void *)names, (void *)infos));
|
||||
u"\\\\fls0\\*"));
|
||||
gtable_set_rows(table, rows);
|
||||
}
|
||||
}
|
||||
|
||||
test_names = NULL;
|
||||
test_infos = NULL;
|
||||
gscreen_destroy(scr);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue