simplify Extract_filename() and Extract_path()

This commit is contained in:
Thomas Bernard 2021-01-28 22:21:59 +01:00
parent d80b02c971
commit 0fc6aa12f1
No known key found for this signature in database
GPG Key ID: DB511043A31ACAAF
7 changed files with 29 additions and 45 deletions

View File

@ -2385,7 +2385,7 @@ void Run_script(const char *script_subdirectory, const char *script_filename)
// This chdir is for the script's sake. Grafx2 itself will (try to)
// not rely on what is the system's current directory.
path = Extract_path(NULL, Last_run_script);
path = Extract_path(Last_run_script);
Change_directory(path);
free(path);

View File

@ -2265,7 +2265,7 @@ byte Button_Load_or_Save(T_Selector_settings *settings, byte load, T_IO_Context
}
else
{
previous_directory = Extract_filename(NULL, Selector->Directory);
previous_directory = Extract_filename(Selector->Directory);
}
free(Selector->Directory);

View File

@ -326,37 +326,26 @@ char * Filepath_append_to_dir(const char * dir, const char * filename)
}
// Récupère la partie "nom de file seul" d'un chemin
char * Extract_filename(char *dest, const char *source)
char * Extract_filename(const char *source)
{
const char * position = Find_last_separator(source);
if (dest != NULL)
{
if (position)
strcpy(dest,position+1);
else
strcpy(dest,source);
return dest;
}
else
{
if (position)
return strdup(position + 1);
else
return strdup(source);
}
}
// Récupère la partie "répertoire+/" d'un chemin.
char * Extract_path(char *dest, const char *source)
char * Extract_path(const char *source)
{
char * position;
char * path;
path = Realpath(source, dest);
path = Realpath(source, NULL);
if (path == NULL)
{
GFX2_Log(GFX2_ERROR, "Realpath(\"%s\", %p) failed !\n", source, dest);
GFX2_Log(GFX2_ERROR, "Realpath(\"%s\") failed !\n", source);
return NULL;
}
position = Find_last_separator(path);
@ -365,10 +354,6 @@ char * Extract_path(char *dest, const char *source)
else
{
size_t len = strlen(path);
if (dest != NULL)
strcpy(path + len, PATH_SEPARATOR);
else
{
char * tmp = realloc(path, len + strlen(PATH_SEPARATOR) + 1);
if (tmp != NULL)
{
@ -381,7 +366,6 @@ char * Extract_path(char *dest, const char *source)
(unsigned long)(len + strlen(PATH_SEPARATOR) + 1));
}
}
}
return path;
}

View File

@ -90,9 +90,9 @@ unsigned long File_length_file(FILE * file);
/// Construct full file path
char * Filepath_append_to_dir(const char * dir, const char * filename);
/// Extracts the filename part from a full file name.
char * Extract_filename(char *dest, const char *source);
char * Extract_filename(const char *source);
/// Extracts the directory from a full file name.
char * Extract_path(char *dest, const char *source);
char * Extract_path(const char *source);
/// Finds the rightmost path separator in a full filename. Used to separate directory from file.
char * Find_last_separator(const char * str);

View File

@ -1311,9 +1311,9 @@ static void Load_ClipBoard_Image(T_IO_Context * context)
if (File_exists(filename))
{
free(context->File_name);
context->File_name = Extract_filename(NULL, filename);
context->File_name = Extract_filename(filename);
free(context->File_directory);
context->File_directory = Extract_path(NULL, filename);
context->File_directory = Extract_path(filename);
context->Format = DEFAULT_FILEFORMAT;
}
else
@ -1597,9 +1597,9 @@ static void Load_ClipBoard_Image(T_IO_Context * context)
if (File_exists(p))
{
free(context->File_name);
context->File_name = Extract_filename(NULL, p);
context->File_name = Extract_filename(p);
free(context->File_directory);
context->File_directory = Extract_path(NULL, p);
context->File_directory = Extract_path(p);
context->Format = DEFAULT_FILEFORMAT;
}
else

View File

@ -111,7 +111,7 @@ char * Get_program_directory(const char * argv0)
{
path[path_len] = '\0'; // add null terminating char
GFX2_Log(GFX2_DEBUG, "binary path resolved to : %s\n", path);
program_dir = Extract_path(NULL, path);
program_dir = Extract_path(path);
}
else
{
@ -128,7 +128,7 @@ char * Get_program_directory(const char * argv0)
if (tmp != NULL)
{
snprintf(tmp, len, "%s/%s", current_dir, argv0);
program_dir = Extract_path(NULL, tmp);
program_dir = Extract_path(tmp);
free(tmp);
}
free(current_dir);
@ -136,15 +136,15 @@ char * Get_program_directory(const char * argv0)
}
}
else
program_dir = Extract_path(NULL, argv0);
program_dir = Extract_path(argv0);
#elif defined(__HAIKU__)
program_dir = Extract_path(NULL, haiku_get_app_path());
program_dir = Extract_path(haiku_get_app_path());
// Others: The part of argv[0] before the executable name.
// Keep the last \ or /.
// On Windows, Mingw32 already provides the full path in all cases.
#else
program_dir = Extract_path(NULL, argv0);
program_dir = Extract_path(argv0);
#endif
if (program_dir == NULL)
{

View File

@ -280,10 +280,10 @@ int Test_File_exists(char * errmsg)
return 0;
}
data.result = 0;
data.filename = Extract_filename(NULL, path);
data.filename = Extract_filename(path);
if (data.filename == NULL)
{
snprintf(errmsg, ERRMSG_LENGTH, "Extract_filename(NULL, \"%s\") FAILED", path);
snprintf(errmsg, ERRMSG_LENGTH, "Extract_filename(\"%s\") FAILED", path);
return 0;
}
GFX2_Log(GFX2_DEBUG, "Listing directory with For_each_directory_entry(\"%s\"):\n", tmpdir);