simplify Extract_filename() and Extract_path()
This commit is contained in:
parent
d80b02c971
commit
0fc6aa12f1
@ -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)
|
// This chdir is for the script's sake. Grafx2 itself will (try to)
|
||||||
// not rely on what is the system's current directory.
|
// 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);
|
Change_directory(path);
|
||||||
free(path);
|
free(path);
|
||||||
|
|
||||||
|
|||||||
@ -2265,7 +2265,7 @@ byte Button_Load_or_Save(T_Selector_settings *settings, byte load, T_IO_Context
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
previous_directory = Extract_filename(NULL, Selector->Directory);
|
previous_directory = Extract_filename(Selector->Directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(Selector->Directory);
|
free(Selector->Directory);
|
||||||
|
|||||||
44
src/io.c
44
src/io.c
@ -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
|
// 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);
|
const char * position = Find_last_separator(source);
|
||||||
|
|
||||||
if (dest != NULL)
|
if (position)
|
||||||
{
|
return strdup(position + 1);
|
||||||
if (position)
|
|
||||||
strcpy(dest,position+1);
|
|
||||||
else
|
|
||||||
strcpy(dest,source);
|
|
||||||
return dest;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
return strdup(source);
|
||||||
if (position)
|
|
||||||
return strdup(position + 1);
|
|
||||||
else
|
|
||||||
return strdup(source);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Récupère la partie "répertoire+/" d'un chemin.
|
// 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 * position;
|
||||||
char * path;
|
char * path;
|
||||||
|
|
||||||
path = Realpath(source, dest);
|
path = Realpath(source, NULL);
|
||||||
if (path == 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;
|
return NULL;
|
||||||
}
|
}
|
||||||
position = Find_last_separator(path);
|
position = Find_last_separator(path);
|
||||||
@ -365,21 +354,16 @@ char * Extract_path(char *dest, const char *source)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
size_t len = strlen(path);
|
size_t len = strlen(path);
|
||||||
if (dest != NULL)
|
char * tmp = realloc(path, len + strlen(PATH_SEPARATOR) + 1);
|
||||||
|
if (tmp != NULL)
|
||||||
|
{
|
||||||
|
path = tmp;
|
||||||
strcpy(path + len, PATH_SEPARATOR);
|
strcpy(path + len, PATH_SEPARATOR);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char * tmp = realloc(path, len + strlen(PATH_SEPARATOR) + 1);
|
GFX2_Log(GFX2_ERROR, "Extract_path(): Failed to realloc %lu bytes\n",
|
||||||
if (tmp != NULL)
|
(unsigned long)(len + strlen(PATH_SEPARATOR) + 1));
|
||||||
{
|
|
||||||
path = tmp;
|
|
||||||
strcpy(path + len, PATH_SEPARATOR);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
GFX2_Log(GFX2_ERROR, "Extract_path(): Failed to realloc %lu bytes\n",
|
|
||||||
(unsigned long)(len + strlen(PATH_SEPARATOR) + 1));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
|
|||||||
4
src/io.h
4
src/io.h
@ -90,9 +90,9 @@ unsigned long File_length_file(FILE * file);
|
|||||||
/// Construct full file path
|
/// Construct full file path
|
||||||
char * Filepath_append_to_dir(const char * dir, const char * filename);
|
char * Filepath_append_to_dir(const char * dir, const char * filename);
|
||||||
/// Extracts the filename part from a full file name.
|
/// 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.
|
/// 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.
|
/// Finds the rightmost path separator in a full filename. Used to separate directory from file.
|
||||||
char * Find_last_separator(const char * str);
|
char * Find_last_separator(const char * str);
|
||||||
|
|||||||
@ -1311,9 +1311,9 @@ static void Load_ClipBoard_Image(T_IO_Context * context)
|
|||||||
if (File_exists(filename))
|
if (File_exists(filename))
|
||||||
{
|
{
|
||||||
free(context->File_name);
|
free(context->File_name);
|
||||||
context->File_name = Extract_filename(NULL, filename);
|
context->File_name = Extract_filename(filename);
|
||||||
free(context->File_directory);
|
free(context->File_directory);
|
||||||
context->File_directory = Extract_path(NULL, filename);
|
context->File_directory = Extract_path(filename);
|
||||||
context->Format = DEFAULT_FILEFORMAT;
|
context->Format = DEFAULT_FILEFORMAT;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -1597,9 +1597,9 @@ static void Load_ClipBoard_Image(T_IO_Context * context)
|
|||||||
if (File_exists(p))
|
if (File_exists(p))
|
||||||
{
|
{
|
||||||
free(context->File_name);
|
free(context->File_name);
|
||||||
context->File_name = Extract_filename(NULL, p);
|
context->File_name = Extract_filename(p);
|
||||||
free(context->File_directory);
|
free(context->File_directory);
|
||||||
context->File_directory = Extract_path(NULL, p);
|
context->File_directory = Extract_path(p);
|
||||||
context->Format = DEFAULT_FILEFORMAT;
|
context->Format = DEFAULT_FILEFORMAT;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
10
src/setup.c
10
src/setup.c
@ -111,7 +111,7 @@ char * Get_program_directory(const char * argv0)
|
|||||||
{
|
{
|
||||||
path[path_len] = '\0'; // add null terminating char
|
path[path_len] = '\0'; // add null terminating char
|
||||||
GFX2_Log(GFX2_DEBUG, "binary path resolved to : %s\n", path);
|
GFX2_Log(GFX2_DEBUG, "binary path resolved to : %s\n", path);
|
||||||
program_dir = Extract_path(NULL, path);
|
program_dir = Extract_path(path);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -128,7 +128,7 @@ char * Get_program_directory(const char * argv0)
|
|||||||
if (tmp != NULL)
|
if (tmp != NULL)
|
||||||
{
|
{
|
||||||
snprintf(tmp, len, "%s/%s", current_dir, argv0);
|
snprintf(tmp, len, "%s/%s", current_dir, argv0);
|
||||||
program_dir = Extract_path(NULL, tmp);
|
program_dir = Extract_path(tmp);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
free(current_dir);
|
free(current_dir);
|
||||||
@ -136,15 +136,15 @@ char * Get_program_directory(const char * argv0)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
program_dir = Extract_path(NULL, argv0);
|
program_dir = Extract_path(argv0);
|
||||||
#elif defined(__HAIKU__)
|
#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.
|
// Others: The part of argv[0] before the executable name.
|
||||||
// Keep the last \ or /.
|
// Keep the last \ or /.
|
||||||
// On Windows, Mingw32 already provides the full path in all cases.
|
// On Windows, Mingw32 already provides the full path in all cases.
|
||||||
#else
|
#else
|
||||||
program_dir = Extract_path(NULL, argv0);
|
program_dir = Extract_path(argv0);
|
||||||
#endif
|
#endif
|
||||||
if (program_dir == NULL)
|
if (program_dir == NULL)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -280,10 +280,10 @@ int Test_File_exists(char * errmsg)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
data.result = 0;
|
data.result = 0;
|
||||||
data.filename = Extract_filename(NULL, path);
|
data.filename = Extract_filename(path);
|
||||||
if (data.filename == NULL)
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
GFX2_Log(GFX2_DEBUG, "Listing directory with For_each_directory_entry(\"%s\"):\n", tmpdir);
|
GFX2_Log(GFX2_DEBUG, "Listing directory with For_each_directory_entry(\"%s\"):\n", tmpdir);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user