gintctl/src/libs/bfile.c

124 lines
2.8 KiB
C

#include <gint/gint.h>
#include <gint/bfile.h>
#include <gint/display.h>
#include <gint/keyboard.h>
#include <gintctl/libs.h>
#include <gintctl/util.h>
#include <gintctl/assets.h>
#include <gintctl/widgets/gscreen.h>
#include <gintctl/widgets/gtable.h>
#include <stdio.h>
#include <stdlib.h>
/* Names and file informations */
static char **test_names = NULL;
static struct BFile_FileInfo *test_infos = NULL;
extern char *fc_to_utf8_alloc(uint16_t const *fc);
int explore_folder(uint16_t const *search)
{
struct BFile_FileInfo info;
int sd=-1, count=0, allocated=0;
uint16_t *fc_path = malloc(512 * sizeof *fc_path);
if(!fc_path) return 0;
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);
end:
if(sd >= 0)
BFile_FindClose(sd);
free(fc_path);
return count;
}
static void table_gen(gtable *t, int row)
{
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)
{
gtable *table = gtable_create(2, table_gen, NULL, NULL);
gtable_set_rows(table, 0);
gtable_set_column_titles(table, "Name", "Size");
gtable_set_column_sizes(table, 3, 1);
gtable_set_font(table, _(&font_mini, dfont_default()));
jwidget_set_margin(table, 0, 2, 1, 2);
gscreen *scr = gscreen_create2("BFile filesystem", &img_opt_libs_bfile,
"BFile access to storage memory", "@LIST;;;;;");
gscreen_add_tabs(scr, table, table);
jscene_set_focused_widget(scr->scene, table);
int key = 0;
while(key != KEY_EXIT) {
jevent e = jscene_run(scr->scene);
if(e.type == JSCENE_PAINT) {
dclear(C_WHITE);
jscene_render(scr->scene);
dupdate();
}
key = 0;
if(e.type == JSCENE_KEY && e.key.type == KEYEV_DOWN)
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\\*"));
gtable_set_rows(table, rows);
}
}
gscreen_destroy(scr);
}