fsctl/src/utils/fs_table.c

124 lines
2.6 KiB
C

#include <stdlib.h>
#include <string.h>
#include <gint/display.h>
#include "fsctl/utils/fs_table.h"
//---
// Internals
//---
/* fs_table_list : FS table list */
static struct {
fs_table_t *table;
int idx;
int slots;
} fs_table;
/* fs_table_append() : append the new vbr information */
static int fs_table_append(fugue_fs_t *fs)
{
fs_table_t *check;
check = reallocarray(
fs_table.table,
fs_table.slots + 1,
sizeof(fs_table_t)
);
if (check == NULL)
return -1;
memcpy(&check[fs_table.slots].fs, fs, sizeof(fugue_fs_t));
fs_table.table = check;
fs_table.slots = fs_table.slots + 1;
if (fs_table.idx < 0)
fs_table.idx = 0;
return 0;
}
//---
// Public
//---
/* fs_table_init() : initialize FS table */
void fs_table_init(void)
{
fs_table.table = NULL;
fs_table.idx = -1,
fs_table.slots = 0;
}
/* fs_table_update() : try to update internal fs table */
int fs_table_update(fugue_fs_t *fs)
{
for (int i = 0 ; i < fs_table.slots ; i++)
{
if (fs_table.table[i].fs._private.vbr == fs->_private.vbr)
return 0;
}
return fs_table_append(fs);
}
/* fs_table_info() : get fs table information */
int fs_table_info(fugue_fs_t *fs, int *fs_idx, int *nb_fs)
{
if (fs_table.table == NULL)
return -1;
if (fs != NULL)
memcpy(fs, &fs_table.table[fs_table.idx].fs, sizeof(fugue_fs_t));
if (fs_idx != NULL)
*fs_idx = fs_table.idx;
if (nb_fs != NULL)
*nb_fs = fs_table.slots;
return 0;
}
/* fs_table_dtitle() : display FS table title menu */
void fs_table_dtitle(void)
{
fugue_fs_t *fs;
if (fs_table.table == NULL)
{
dprint(2, 2, C_BLACK, "No File System found");
drect(0, 0, DWIDTH, 14, C_INVERT);
return;
}
fs = &fs_table.table[fs_table.idx].fs;
dprint(2, 2, C_BLACK, "VBR: %p", fs->_private.vbr);
dprint_opt(
DWIDTH, 2,
C_BLACK, C_NONE,
DTEXT_RIGHT, DTEXT_TOP,
"%d/%d",
fs_table.idx + 1,
fs_table.slots
);
drect(0, 0, DWIDTH, 13, C_INVERT);
}
/* fs_table_select() : select a FS if available */
int fs_table_select(int idx)
{
if (fs_table.table == NULL)
return -1;
if (idx >= fs_table.slots)
return -2;
fs_table.idx = idx;
return 0;
}
/* fs_table_select_left() : try to change to the idx - 1 */
int fs_table_select_left(void)
{
return fs_table_select(fs_table.idx - 1);
}
/* fs_table_select_right() : try to change to the idx + 1 */
int fs_table_select_right(void)
{
return fs_table_select(fs_table.idx + 1);
}