factory.c: remove static selected_file[] string buffer

This commit is contained in:
Thomas Bernard 2019-02-20 11:08:13 +01:00
parent 05175292a6
commit b5d61472e5
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C
3 changed files with 36 additions and 9 deletions

View File

@ -2685,7 +2685,7 @@ void Reload_scripts_list(void)
void Button_Brush_Factory(void)
{
static char selected_file[MAX_PATH_CHARACTERS]="";
static char * selected_file = NULL; // currently selected file in factory file selector
short clicked_button;
T_List_button* scriptlist;
@ -2728,9 +2728,10 @@ void Button_Brush_Factory(void)
Window_redraw_list(scriptlist);
// Display current path:
q = strlen(Config.Scripts_directory);
q = Config.Scripts_directory == NULL ? 0 : strlen(Config.Scripts_directory);
if (q<=DESC_WIDTH)
{
if (q > 0)
strcpy(displayed_path, Config.Scripts_directory);
for (; q < DESC_WIDTH; q++)
displayed_path[q]=' ';
@ -2819,7 +2820,8 @@ void Button_Brush_Factory(void)
if (item->Type == FSOBJECT_FILE)
{
strcpy(selected_file, item->Full_name);
free(selected_file);
selected_file = strdup(item->Full_name);
break;
}
else if (item->Type == FSOBJECT_DIR || item->Type == FSOBJECT_DRIVE)
@ -2827,7 +2829,8 @@ void Button_Brush_Factory(void)
if (item->Type == FSOBJECT_DRIVE)
{
// Selecting one drive root
strcpy(selected_file, PARENT_DIR);
free(selected_file);
selected_file = strdup(PARENT_DIR);
free(Config.Scripts_directory);
Config.Scripts_directory = strdup(item->Full_name);
}
@ -2835,7 +2838,26 @@ void Button_Brush_Factory(void)
{
// Going down one or up by one directory
if (strcmp(item->Full_name, PARENT_DIR) == 0)
Append_path(Config.Scripts_directory, item->Full_name, selected_file);
{
char * separator_pos = Find_last_separator(Config.Scripts_directory);
if (separator_pos != NULL && separator_pos[1] == '\0')
{
// remove trailing separator
separator_pos[0] = '\0';
separator_pos = Find_last_separator(Config.Scripts_directory);
}
free(selected_file);
if (separator_pos == NULL)
{
selected_file = Config.Scripts_directory; // steal heap buffer
Config.Scripts_directory = NULL;
}
else
{
selected_file = strdup(separator_pos + 1);
separator_pos[0] = '\0';
}
}
else
{
char * new_dir = Filepath_append_to_dir(Config.Scripts_directory, item->Full_name);
@ -2861,7 +2883,7 @@ void Button_Brush_Factory(void)
Close_window();
Unselect_button(BUTTON_BRUSH_EFFECTS);
if (clicked_button == 5) // Run the script
if (clicked_button == 5 && selected_file != NULL) // Run the script
{
Run_script(Config.Scripts_directory, selected_file);
}

View File

@ -1462,6 +1462,8 @@ short Find_file_in_fileselector(T_Fileselector *list, const char * fname)
short index;
short close_match = -1;
if (fname == NULL)
return -1;
index=0;
for (item=list->First; item!=NULL; item=item->Next)
{

View File

@ -262,6 +262,9 @@ int Write_dword_be(FILE *file, dword dw)
char * Find_last_separator(const char * str)
{
const char * position = NULL;
if (str == NULL)
return NULL;
for (; *str != '\0'; str++)
if (*str == PATH_SEPARATOR[0]
#if defined(__WIN32__) || defined(WIN32)
@ -292,7 +295,7 @@ word * Find_last_separator_unicode(const word * str)
char * Filepath_append_to_dir(const char * dir, const char * filename)
{
char * path;
size_t len = strlen(dir);
size_t len = dir == NULL ? 0 : strlen(dir);
if (len == 0) // no directory
return strdup(filename);
if (dir[len-1] == PATH_SEPARATOR[0]