Fmenu configuration data are now loaded from ROM every time they are needed => less global heap usage

This commit is contained in:
Nemh 2015-03-21 21:35:46 +01:00
parent fad0640f55
commit 37d629fcc9
4 changed files with 100 additions and 65 deletions

Binary file not shown.

Binary file not shown.

158
console.c
View File

@ -7,7 +7,7 @@
extern void run(char *s);
static struct line Line[LINE_MAX];
static struct FMenu FMenu_entries[6];
static char FMenu_entries_name[6*FMENU_TITLE_LENGHT+1] = {'\0'};
static struct location Cursor;
static unsigned char *Edit_Line;
static int Start_Line, Last_Line;
@ -550,15 +550,74 @@ int Console_GetKey()
int Console_FMenu(int key)
{
if(FMenu_entries[key-KEY_CTRL_F1].count > 0)
return Console_Draw_FMenu(key, &FMenu_entries[key-KEY_CTRL_F1]);
int i, handle, ret, matched = 0, number=0;
unsigned int error_key;
struct FMenu entry = {0};
char* tmp_realloc = NULL;
char temp[30] = {'\0'};
char* original_cfg;
char* cfg = memory_load("\\\\fls0\\FMENU.cfg");
original_cfg = cfg;
while(*cfg) {
//Get each line
for(i=0; i<20, *cfg && *cfg!='\r' && *cfg!='\n'; i++, cfg++) {
temp[i] = *cfg;
}
//If starting by 'F' followed by the right number, start filling the structure.
if(temp[0] == 'F' && temp[1]==(key-KEY_CTRL_F1)+'1') matched = 1;
else if(temp[0] == 'F' && temp[1]!=(key-KEY_CTRL_F1)+'0') matched = 0;
//Fill the structure
else if(matched && temp[0] && entry.count < MAX_FMENU_ITEMS) {
//Alloc a new string a copy current data into it
tmp_realloc=(char*)entry.str;
entry.str = realloc(tmp_realloc, sizeof(unsigned char*)*(entry.count+1));
if(entry.str != NULL) {
entry.str[entry.count] = (char*)malloc(strlen(temp)+1);
if(entry.str[entry.count] == NULL) {
PrintMini(10,40, (unsigned char*)"Error realloc bis -> Console_FMenu()", MINI_OVER);
GetKey(&error_key);
entry.str = (char*)realloc(entry.str, entry.count); //May never fail.
entry.count--;
}
else strcpy(entry.str[entry.count], temp);
entry.count++;
}
else {
PrintMini(50,40, (unsigned char*)"Error realloc -> Console_FMenu()", MINI_OVER);
GetKey(&error_key);
entry.str = tmp_realloc;
}
}
memset(temp, '\0', 30);
cfg++;
}
free(original_cfg);
if(entry.count > 0) {
ret = Console_Draw_FMenu(key, &entry);
for(i=0; i < entry.count; i++) {
free(entry.str[i]);
}
free(entry.str);
free(entry.name);
}
else return 0;
}
unsigned char *Console_Make_Entry(const unsigned char* str)
char *Console_Make_Entry(const unsigned char* str)
{
unsigned char* entry = NULL;
entry = calloc((strlen(str)+1), sizeof(unsigned char*));
char* entry = NULL;
entry = (char*)calloc((strlen(str)+1), sizeof(unsigned char*));
if(entry) memcpy(entry, str, strlen(str)+1);
return entry;
@ -568,9 +627,10 @@ unsigned char *Console_Make_Entry(const unsigned char* str)
int Console_Draw_FMenu(int key, struct FMenu* menu)
{
int i, nb_entries = 0, selector = 0, position_number, position_x, ret, longest = 0;
unsigned char quick[] = "*: ";
unsigned int input_key;
char quick[] = "*: ";
int quick_len = 2;
unsigned char **entries;
char **entries;
DISPBOX box;
position_number = key - KEY_CTRL_F1;
@ -598,28 +658,28 @@ int Console_Draw_FMenu(int key, struct FMenu* menu)
if(((Cursor.x*(128/21)<box.right && Cursor.x*(128/21)>box.left))
&& ((Cursor.y*(64/8)<box.bottom) && (Cursor.y*(64/8)>box.top))) Cursor_SetFlashOff();
while(key != KEY_CTRL_EXE || key != KEY_CTRL_EXIT) {
while(input_key != KEY_CTRL_EXE || input_key != KEY_CTRL_EXIT) {
for(i=0; i<nb_entries; i++) {
quick[0] = '0'+(i+1);
PrintMini(3+position_x, box.bottom-7*(i+1), quick, MINI_OVER);
PrintMini(3+position_x+quick_len*4, box.bottom-7*(i+1), entries[i], MINI_OVER);
PrintMini(3+position_x, box.bottom-7*(i+1), (unsigned char*)quick, MINI_OVER);
PrintMini(3+position_x+quick_len*4, box.bottom-7*(i+1), (unsigned char*)entries[i], MINI_OVER);
}
PrintMini(3+position_x+quick_len*4,box.bottom-7*(selector+1), entries[selector], MINI_REV);
GetKey(&key);
PrintMini(3+position_x+quick_len*4,box.bottom-7*(selector+1), (unsigned char*)entries[selector], MINI_REV);
GetKey(&input_key);
if (key == KEY_CTRL_UP && selector < nb_entries-1) selector++;
if (key == KEY_CTRL_DOWN && selector > 0) selector--;
if (input_key == KEY_CTRL_UP && selector < nb_entries-1) selector++;
if (input_key == KEY_CTRL_DOWN && selector > 0) selector--;
if (key == KEY_CTRL_EXE) return Console_Input(entries[selector]);
if (input_key == KEY_CTRL_EXE) return Console_Input((unsigned char *)entries[selector]);
if (key >= KEY_CHAR_1 && key < KEY_CHAR_1 + nb_entries) return Console_Input(entries[key-KEY_CHAR_1]);
if (input_key >= KEY_CHAR_1 && input_key < KEY_CHAR_1 + nb_entries) return Console_Input((unsigned char*)entries[input_key-KEY_CHAR_1]);
if (key == KEY_CTRL_EXIT) return Console_Input((const unsigned char *)"");
if (input_key == KEY_CTRL_EXIT) return Console_Input((const unsigned char *)"");
if (key >= KEY_CTRL_F1 && key <= KEY_CTRL_F6) {
if (input_key >= KEY_CTRL_F1 && input_key <= KEY_CTRL_F6) {
Console_Input((const unsigned char *)"");
Console_Disp();
return Console_FMenu(key);
return Console_FMenu(input_key);
}
}
}
@ -652,10 +712,10 @@ int Console_Init()
Case = LOWER_CASE;
for(i = 0; i < 6; i++) {
/*for(i = 0; i < 6; i++) {
FMenu_entries[i].name = NULL;
FMenu_entries[i].count = 0;
}
}*/
Console_FMenu_Init();
@ -675,11 +735,11 @@ void Console_FMenu_Init()
// Does the file exists ?
// Todo : check the error codes...
if(!cfg) {
unsigned char conf_standard[] = {"F1 alge\nabs(\ndet(\nadj(\nunit(\nF2 trig\ncosh(\narccosh(\nsinh(\narcsinh(\ntanh(\narctanh\nF3 cplx\narg(\nconj(\nimag(\nmag(\npolar(\nreal(\nrect(\nF4 calc\nd(\neval(\nintegral(\ntaylor(\nsum(\nsimplify(\nF5 poly\ncoeff(\nquotient(\ndeg(\nexpand(\nfactor(\nroots(\nnroots(\nF6 misc\nf(\ny\nsimplify("};
unsigned char conf_standard[] = {"F1 alge\nabs(\ndet(\nadj(\nunit(\nF2 trig\ncosh(\narccosh(\nsinh(\narcsinh(\ntanh(\narctanh(\nF3 cplx\narg(\nconj(\nimag(\nmag(\npolar(\nreal(\nrect(\nF4 calc\nd(\neval(\nintegral(\ntaylor(\nsum(\nsimplify(\nF5 poly\ncoeff(\nquotient(\ndeg(\nexpand(\nfactor(\nroots(\nnroots(\nF6 misc\nf(\ny\nsimplify("};
//unsigned char conf_standard[] = {"F2 trig\ncos(\nsin(\ntan(\nF1\ntest(\ntest(\nF4\nje(\nsuis(\nvivant(\nF5\nabdibol(\ngeorges(\ngali(\npom(\npom(\nde(\natol(\nF6\nreturn(\nsolve(\nF3\nsolve(\nfactor(\nsimplify(\nmatrix(\nd("};
memory_createfile("\\\\fls0\\FMENU.cfg", strlen(conf_standard)+1);
memory_createfile("\\\\fls0\\FMENU.cfg", strlen((char*)conf_standard)+1);
handle = memory_openfile("\\\\fls0\\FMENU.cfg", _OPENMODE_READWRITE);
memory_writefile(handle, conf_standard, strlen(conf_standard)+1);
memory_writefile(handle, conf_standard, strlen((char*)conf_standard)+1);
memory_closefile(handle);
cfg = memory_load("\\\\fls0\\FMENU.cfg");
@ -696,44 +756,16 @@ void Console_FMenu_Init()
//If starting by 'F', adjust the number and eventually set the name of the menu
if(temp[0] == 'F' && temp[1]>='1' && temp[1]<='6') {
number = temp[1]-'0' - 1;
if(temp[3] && FMenu_entries[number].name == NULL) {
FMenu_entries[number].name = malloc(sizeof(unsigned char)*(4+1));
if(FMenu_entries[number].name != NULL) {
strncpy(FMenu_entries[number].name, temp+3, 4);
FMenu_entries[number].name[4] = '\0';
}
if(temp[3]) {
strncpy((char*)FMenu_entries_name+(number*FMENU_TITLE_LENGHT), (char*)temp+3, FMENU_TITLE_LENGHT);
//FMenu_entries[number].name[4] = '\0';
}
}
//Else, fill the current menu
else if(temp[0] && FMenu_entries[number].count < MAX_FMENU_ITEMS) {
//Alloc a new string a copy current data into it
tmp_realloc=FMenu_entries[number].str;
FMenu_entries[number].str = realloc(tmp_realloc, sizeof(unsigned char*)*(FMenu_entries[number].count+1));
if(FMenu_entries[number].str != NULL) {
FMenu_entries[number].str[FMenu_entries[number].count] = malloc(strlen(temp)+1);
if(FMenu_entries[number].str[FMenu_entries[number].count] == NULL) {
PrintMini(10,40, "Error realloc bis -> FMenu_init()", MINI_OVER);
GetKey(&key);
FMenu_entries[number].str = realloc(FMenu_entries[number].str, FMenu_entries[number].count); //May never fail.
FMenu_entries[number].count--;
}
else strcpy(FMenu_entries[number].str[FMenu_entries[number].count], temp);
FMenu_entries[number].count++;
}
else {
PrintMini(50,40, "Error realloc -> FMenu_init()", MINI_OVER);
GetKey(&key);
FMenu_entries[number].str = tmp_realloc;
}
}
memset(temp, '\0', 20);
cfg++;
}
FMenu_entries_name[6*FMENU_TITLE_LENGHT] = '\0';
free(original_cfg);
}
@ -745,6 +777,7 @@ void Console_FMenu_Init()
int Console_Disp()
{
unsigned int* pBitmap;
char temp_fkey[FMENU_TITLE_LENGHT] = {'\0'};
int i, alpha_shift_status;
DISPBOX ficon;
@ -879,15 +912,16 @@ int Console_Disp()
for(i=0; i<6; i++) {
ficon.bottom = 64;
ficon.top = 64-8;
if (FMenu_entries[i].count > 0) {
if (FMenu_entries_name[i*FMENU_TITLE_LENGHT]!= '\0') {
ficon.left = 1+i*21;
ficon.right = ficon.left + (127-2)/6 - 1;
Bdisp_AreaClr_VRAM(&ficon);
Bdisp_AreaReverseVRAM(ficon.left, ficon.top, ficon.right, ficon.bottom);
if(FMenu_entries[i].name != NULL) {
PrintMini(ficon.left + 2, ficon.top +2, FMenu_entries[i].name, MINI_REV);
}
memcpy(temp_fkey, FMenu_entries_name+i*FMENU_TITLE_LENGHT, FMENU_TITLE_LENGHT*sizeof(char));
//if(FMenu_entries[i].name != NULL) {
PrintMini(ficon.left + 2, ficon.top +2, (unsigned char*)temp_fkey, MINI_REV);
//}
memset(temp_fkey, '\0', FMENU_TITLE_LENGHT*sizeof(char));
}
}

View File

@ -55,8 +55,8 @@ extern "C"{
};
struct FMenu{
unsigned char* name;
unsigned char** str;
char* name;
char** str;
unsigned char count;
};
@ -66,6 +66,7 @@ extern "C"{
};
#define MAX_FMENU_ITEMS 7
#define FMENU_TITLE_LENGHT 4
#define is_wchar(c) ((c == 0x7F) || (c == 0xF7) || (c == 0xF9) || (c == 0xE5) || (c == 0xE6) || (c == 0xE7))
#define printf(s) Console_Output((const unsigned char *)s);
@ -85,7 +86,7 @@ extern "C"{
int Console_FMenu(int key);
void Console_FMenu_Init(void);
int Console_Draw_FMenu(int key, struct FMenu* menu);
unsigned char *Console_Make_Entry(const unsigned char* str);
char *Console_Make_Entry(const unsigned char* str);
unsigned char *Console_GetLine(void);
#ifdef __cplusplus