summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKim Woelders <kim@woelders.dk>2019-10-15 19:21:00 +0200
committerKim Woelders <kim@woelders.dk>2019-11-10 15:11:56 +0100
commit5d1e984e57e23068219d02aefe6a3a4be24b9ade (patch)
tree11432c22d8603e05e82a52149dfd553e5a409c87
parent7a2769cad7181a4ecf844714d0c06a423156b739 (diff)
downloadimlib2-5d1e984e57e23068219d02aefe6a3a4be24b9ade.tar.gz
Simplify loader lookup functions
-rw-r--r--src/lib/image.c111
-rw-r--r--src/lib/image.h5
-rw-r--r--src/modules/loaders/loader_bz2.c4
-rw-r--r--src/modules/loaders/loader_zlib.c4
4 files changed, 34 insertions, 90 deletions
diff --git a/src/lib/image.c b/src/lib/image.c
index 99f05db..d2e0fb9 100644
--- a/src/lib/image.c
+++ b/src/lib/image.c
@@ -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;
-
- /* 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)
+ ImlibLoader *l;
+
+ 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 */
+
+ done:
+ return l;
+}
+
+__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 the loader */
+
return l;
}
-ImlibLoader *
+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 ) */
diff --git a/src/lib/image.h b/src/lib/image.h
index 2c32d2c..37ca713 100644
--- a/src/lib/image.h
+++ b/src/lib/image.h
@@ -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,
diff --git a/src/modules/loaders/loader_bz2.c b/src/modules/loaders/loader_bz2.c
index f047f7b..47f68ab 100644
--- a/src/modules/loaders/loader_bz2.c
+++ b/src/modules/loaders/loader_bz2.c
@@ -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;
diff --git a/src/modules/loaders/loader_zlib.c b/src/modules/loaders/loader_zlib.c
index aba9583..d30725b 100644
--- a/src/modules/loaders/loader_zlib.c
+++ b/src/modules/loaders/loader_zlib.c
@@ -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;