Simplify loader lookup functions

This commit is contained in:
Kim Woelders 2019-10-15 19:21:00 +02:00
parent 7a2769cad7
commit 5d1e984e57
4 changed files with 34 additions and 90 deletions

View File

@ -741,28 +741,13 @@ __imlib_LoadAllLoaders(void)
}
__EXPORT__ ImlibLoader *
__imlib_FindBestLoaderForFile(const char *file, int for_save)
__imlib_FindBestLoaderForFormat(const char *format, int for_save)
{
char *extension, *lower, *rfile;
ImlibLoader *l = NULL;
ImlibLoader *l;
/* use the file extension for a "best guess" as to what loader to try */
/* first at any rate */
rfile = __imlib_FileRealFile(file);
extension = __imlib_FileExtension(rfile);
free(rfile);
/* change the extensiont o all lower case as all "types" are listed as */
/* lower case strings from the loader that represent all the possible */
/* extensions that file format could have */
lower = extension;
while (*lower)
{
*lower = tolower(*lower);
lower++;
}
if (!extension)
if (!format || format[0] == '\0')
return NULL;
/* go through the loaders - first loader that claims to handle that */
/* image type (extension wise) wins as a first guess to use - NOTE */
/* this is an OPTIMISATION - it is possible the file has no extension */
@ -775,8 +760,7 @@ __imlib_FindBestLoaderForFile(const char *file, int for_save)
/* to be used first next time in this search mechanims - this */
/* assumes you tend to laod a few image types and ones generally */
/* of the same format */
l = loaders;
while (l)
for (l = loaders; l; l = l->next)
{
int i;
@ -784,83 +768,44 @@ __imlib_FindBestLoaderForFile(const char *file, int for_save)
for (i = 0; i < l->num_formats; i++)
{
/* does it match ? */
if (!strcmp(l->formats[i], extension))
if (strcasecmp(l->formats[i], format) == 0)
{
/* does it provide the function we need? */
if ((for_save && l->save) || (!for_save && l->load))
{
/* free the memory allocated for the extension */
free(extension);
/* return the loader */
return l;
}
goto done;
}
}
l = l->next;
}
/* free the memory allocated for the extension */
free(extension);
/* return the loader */
done:
return l;
}
ImlibLoader *
__EXPORT__ ImlibLoader *
__imlib_FindBestLoaderForFile(const char *file, int for_save)
{
char *extension;
ImlibLoader *l;
extension = __imlib_FileExtension(file);
l = __imlib_FindBestLoaderForFormat(extension, for_save);
free(extension);
return l;
}
static ImlibLoader *
__imlib_FindBestLoaderForFileFormat(const char *file, char *format,
int for_save)
{
char *extension, *lower;
ImlibLoader *l = NULL;
/* if the format is provided ("png" "jpg" etc.) use that */
/* otherwise us the file extension */
if (format)
extension = strdup(format);
/* otherwise us the extension */
return __imlib_FindBestLoaderForFormat(format, for_save);
else
{
extension = __imlib_FileExtension(file);
/* change the extension to all lower case as all "types" are listed as */
/* lower case strings from the loader that represent all the possible */
/* extensions that file format could have */
if (extension)
{
lower = extension;
while (*lower)
{
*lower = tolower(*lower);
lower++;
}
}
}
if (!extension)
return NULL;
/* look through the loaders one by one to see if one matches that format */
l = loaders;
while (l)
{
int i;
/* go through all the formats that loader supports */
for (i = 0; i < l->num_formats; i++)
{
/* does it match ? */
if (!strcmp(l->formats[i], extension))
{
/* does it provide the function we need? */
if ((for_save && l->save) || (!for_save && l->load))
{
/* free the memory allocated for the extension */
free(extension);
/* return the loader */
return l;
}
}
}
l = l->next;
}
/* free the memory allocated for the extension */
free(extension);
/* return the loader */
return l;
return __imlib_FindBestLoaderForFile(file, for_save);
}
/* set or unset the alpha flag on the umage (alpha = 1 / 0 ) */

View File

@ -143,9 +143,8 @@ void __imlib_CleanupImagePixmapCache(void);
void __imlib_RemoveAllLoaders(void);
ImlibLoader *__imlib_FindBestLoaderForFile(const char *file,
int for_save);
ImlibLoader *__imlib_FindBestLoaderForFileFormat(const char *file,
char *format,
int for_save);
ImlibLoader *__imlib_FindBestLoaderForFormat(const char *format,
int for_save);
void __imlib_SetImageAlphaFlag(ImlibImage * im, char alpha);
ImlibImage *__imlib_CreateImage(int w, int h, DATA32 * data);
ImlibImage *__imlib_LoadImage(const char *file,

View File

@ -62,10 +62,10 @@ load(ImlibImage * im, ImlibProgressFunction progress,
if (!p || p == im->real_file || strcasecmp(p + 1, "bz2") || p == q)
return 0;
if (!(real_ext = strndup(im->real_file, p - im->real_file)))
if (!(real_ext = strndup(q + 1, p - q - 1)))
return 0;
if (!(loader = __imlib_FindBestLoaderForFile(real_ext, 0)))
if (!(loader = __imlib_FindBestLoaderForFormat(real_ext, 0)))
{
free(real_ext);
return 0;

View File

@ -55,10 +55,10 @@ load(ImlibImage * im, ImlibProgressFunction progress,
if (!p || p == im->real_file || strcasecmp(p + 1, "gz") || p == q)
return 0;
if (!(real_ext = strndup(im->real_file, p - im->real_file)))
if (!(real_ext = strndup(q + 1, p - q - 1)))
return 0;
if (!(loader = __imlib_FindBestLoaderForFile(real_ext, 0)))
if (!(loader = __imlib_FindBestLoaderForFormat(real_ext, 0)))
{
free(real_ext);
return 0;