Refactor menu_action_sdfile

This commit is contained in:
Guðni Már Gilbert 2023-10-09 18:40:45 +00:00 committed by gudnimg
parent 82cece95aa
commit dec5e2c1b4
1 changed files with 14 additions and 20 deletions

View File

@ -6984,44 +6984,38 @@ static void menu_action_sdfile(const char* filename)
{ {
if(eFilamentAction != FilamentAction::None) return; if(eFilamentAction != FilamentAction::None) return;
char cmd[30]; // Create a copy of card.filename on the stack since card.filename pointer
char* c; // will be modified by the SD card library when searching for the file
bool result = true; char selected_filename[FILENAME_LENGTH];
sprintf_P(cmd, MSG_M23, filename); strcpy(selected_filename, filename);
for (c = &cmd[4]; *c; c++)
*c = tolower(*c);
const char end[5] = ".gco"; bool result = true;
//we are storing just first 8 characters of 8.3 filename assuming that extension is always ".gco" //we are storing just first 8 characters of 8.3 filename assuming that extension is always ".gco"
for (uint_least8_t i = 0; i < 8; i++) { for (uint_least8_t i = 0; i < 8; i++) {
if (strcmp((cmd + i + 4), end) == 0) { if (selected_filename[i] == '\0' || selected_filename[i] == '.') {
//filename is shorter then 8.3, store '\0' character on position where ".gco" string was found to terminate stored string properly //filename is shorter then 8.3, store '\0' character on position where ".gco" string was found to terminate stored string properly
eeprom_write_byte((uint8_t*)EEPROM_FILENAME + i, '\0'); eeprom_write_byte((uint8_t*)EEPROM_FILENAME + i, '\0');
break; break;
} }
else { else {
eeprom_write_byte((uint8_t*)EEPROM_FILENAME + i, cmd[i + 4]); eeprom_write_byte((uint8_t*)EEPROM_FILENAME + i, selected_filename[i]);
} }
} }
uint8_t depth = card.getWorkDirDepth(); const uint8_t depth = card.getWorkDirDepth();
eeprom_write_byte((uint8_t*)EEPROM_DIR_DEPTH, depth); eeprom_write_byte((uint8_t*)EEPROM_DIR_DEPTH, depth);
for (uint_least8_t i = 0; i < depth; i++) { for (uint_least8_t i = 0; i < depth; i++) {
for (uint_least8_t j = 0; j < 8; j++) { eeprom_update_block(card.dir_names[i], (uint8_t*)EEPROM_DIRS + 8 * i, 8);
eeprom_write_byte((uint8_t*)EEPROM_DIRS + j + 8 * i, card.dir_names[i][j]); }
}
}
//filename is just a pointer to card.filename, which changes everytime you try to open a file by filename. So you can't use filename directly if (!check_file(selected_filename)) {
//to open a file. Instead, the cached filename in cmd is used as that one is static for the whole lifetime of this function.
if (!check_file(cmd + 4)) {
result = !lcd_show_fullscreen_message_yes_no_and_wait_P(_i("File incomplete. Continue anyway?"), false);////MSG_FILE_INCOMPLETE c=20 r=3 result = !lcd_show_fullscreen_message_yes_no_and_wait_P(_i("File incomplete. Continue anyway?"), false);////MSG_FILE_INCOMPLETE c=20 r=3
lcd_update_enable(true); lcd_update_enable(true);
} }
if (result) { if (result) {
enquecommand(cmd); enquecommandf_P(MSG_M23, selected_filename);
enquecommand_P(MSG_M24); enquecommand_P(MSG_M24);
} }