diff --git a/src/buttons.c b/src/buttons.c index b6ceb25f..503f0489 100644 --- a/src/buttons.c +++ b/src/buttons.c @@ -3415,25 +3415,24 @@ void Button_Reload(int btn) } -static void Backup_filename(const char * fname, char * backup_name) +static void Backup_existing_file(const char * filename) { + char new_filename[MAX_PATH_CHARACTERS]; // full filename of the backup file + char * p; int i; - strcpy(backup_name,fname); - for (i=strlen(fname)-strlen(Main.backups->Pages->Filename); backup_name[i]!='.' && backup_name[i]!='\0'; i++); - backup_name[i+1]='\0'; - strcat(backup_name,"BAK"); -} - - -static void Backup_existing_file(void) -{ - char filename[MAX_PATH_CHARACTERS]; // Nom complet du fichier - char new_filename[MAX_PATH_CHARACTERS]; // Nom complet du fichier backup - - Get_full_filename(filename, Main.backups->Pages->Filename, Main.backups->Pages->File_directory); - // Calcul du nom complet du fichier backup - Backup_filename(filename,new_filename); + strncpy(new_filename, filename, sizeof(new_filename)); + new_filename[sizeof(new_filename) - 1] = '\0'; + p = Find_last_separator(new_filename); // pointer to the filename part (after directory) + if (p == NULL) + p = new_filename; + else + p++; + i = Position_last_dot(p); + if (i >= 0) + strcpy(p + i + 1, "BAK"); + else + strcat(p, ".BAK"); File_error=0; @@ -3512,7 +3511,9 @@ void Save_picture(enum CONTEXT_TYPE type) confirm=Confirmation_box("Erase old file ?"); if (confirm && (Config.Backup)) { - Backup_existing_file(); + char full_filename[MAX_PATH_CHARACTERS]; + Get_full_filename(full_filename, filename, directory); + Backup_existing_file(full_filename); if (File_error) { confirm=0; @@ -3578,7 +3579,7 @@ void Button_Autosave(int btn) if ( (!file_already_exists) || Confirmation_box("Erase old file ?") ) { if ((file_already_exists) && (Config.Backup)) - Backup_existing_file(); + Backup_existing_file(filename); else File_error=0; diff --git a/src/filesel.c b/src/filesel.c index e9ec1eb6..c45fb977 100644 --- a/src/filesel.c +++ b/src/filesel.c @@ -226,28 +226,6 @@ void Free_fileselector_list(T_Fileselector *list) Recount_files(list); } -static int Position_last_dot(const char * fname) -{ - int pos_last_dot = -1; - int c = 0; - - for (c = 0; fname[c]!='\0'; c++) - if (fname[c]=='.') - pos_last_dot = c; - return pos_last_dot; -} - -static int Position_last_dot_unicode(const word * fname) -{ - int pos_last_dot = -1; - int c = 0; - - for (c = 0; fname[c]!='\0'; c++) - if (fname[c]=='.') - pos_last_dot = c; - return pos_last_dot; -} - word * Format_filename_unicode(const word * fname, word max_length, int type) { static word result[40]; diff --git a/src/io.c b/src/io.c index 40247859..ef1c11a2 100644 --- a/src/io.c +++ b/src/io.c @@ -292,6 +292,28 @@ void Append_path(char *path, const char *filename, char *reverse_path) } } +int Position_last_dot(const char * fname) +{ + int pos_last_dot = -1; + int c = 0; + + for (c = 0; fname[c] != '\0'; c++) + if (fname[c] == '.') + pos_last_dot = c; + return pos_last_dot; +} + +int Position_last_dot_unicode(const word * fname) +{ + int pos_last_dot = -1; + int c = 0; + + for (c = 0; fname[c] != '\0'; c++) + if (fname[c] == '.') + pos_last_dot = c; + return pos_last_dot; +} + int File_exists(const char * fname) // Détermine si un file passé en paramètre existe ou non dans le // répertoire courant. diff --git a/src/io.h b/src/io.h index e4b959bb..172765d7 100644 --- a/src/io.h +++ b/src/io.h @@ -83,6 +83,12 @@ char * Find_last_separator(const char * str); #define PATH_SEPARATOR "/" #endif +/// finds the rightmost '.' character in fullname. Used to find file extension. returns -1 if not found +int Position_last_dot(const char * fname); + +/// finds the rightmost '.' character in fullname. Used to find file extension. returns -1 if not found +int Position_last_dot_unicode(const word * fname); + /// Size of a file, in bytes. Returns 0 in case of error. unsigned long File_length(const char *fname);