This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project gfxprim.git.
The branch, master has been updated via a960a02a7e253fcdd2ec0cfa64f7d2f513719961 (commit) via 581ccfd2c21886816a54a99edf7e43a443506e5e (commit) from 9f7de83d6395614ad22b2492340900a09f930db8 (commit)
Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below.
- Log ----------------------------------------------------------------- http://repo.or.cz/w/gfxprim.git/commit/a960a02a7e253fcdd2ec0cfa64f7d2f513719...
commit a960a02a7e253fcdd2ec0cfa64f7d2f513719961 Author: Cyril Hrubis metan@ucw.cz Date: Sun Nov 4 01:14:52 2012 +0100
loaders: Add PSP signature matching.
diff --git a/include/loaders/GP_PSP.h b/include/loaders/GP_PSP.h index 4827a98..a44ae9b 100644 --- a/include/loaders/GP_PSP.h +++ b/include/loaders/GP_PSP.h @@ -58,4 +58,9 @@ GP_Context *GP_ReadPSP(FILE *f, GP_ProgressCallback *callback); */ GP_Context *GP_LoadPSP(const char *src_path, GP_ProgressCallback *callback);
+/* + * Match PSP signature. + */ +int GP_MatchPSP(const void *buf); + #endif /* LOADERS_GP_PSP_H */ diff --git a/libs/loaders/GP_Loader.c b/libs/loaders/GP_Loader.c index 057322c..ab374c4 100644 --- a/libs/loaders/GP_Loader.c +++ b/libs/loaders/GP_Loader.c @@ -39,7 +39,7 @@ static GP_Loader psp_image = { .Load = GP_LoadPSP, .Save = NULL, - .Match = NULL, + .Match = GP_MatchPSP, .fmt_name = "Paint Shop Pro Image", .next = NULL, .extensions = {"psp", "pspimage", NULL}, diff --git a/libs/loaders/GP_PSP.c b/libs/loaders/GP_PSP.c index 7ab8835..d77ee29 100644 --- a/libs/loaders/GP_PSP.c +++ b/libs/loaders/GP_PSP.c @@ -61,6 +61,11 @@ struct psp_version { uint16_t minor; };
+int GP_MatchPSP(const void *buf) +{ + return !memcmp(buf, PSP_SIGNATURE, PSP_SIGNATURE_LEN); +} + int GP_OpenPSP(const char *src_path, FILE **f) { int err;
http://repo.or.cz/w/gfxprim.git/commit/581ccfd2c21886816a54a99edf7e43a443506...
commit 581ccfd2c21886816a54a99edf7e43a443506e5e Author: Cyril Hrubis metan@ucw.cz Date: Sun Nov 4 00:56:48 2012 +0100
loaders: Add initial support for loader registration.
diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_Loaders.h index 5292608..d523494 100644 --- a/include/loaders/GP_Loaders.h +++ b/include/loaders/GP_Loaders.h @@ -49,33 +49,6 @@
#include "GP_MetaData.h"
-/* - * Tries to load image accordingly to the file extension. - * - * If operation fails NULL is returned and errno is filled. - */ -GP_Context *GP_LoadImage(const char *src_path, GP_ProgressCallback *callback); - -/* - * Loads image Meta Data (if possible). - */ -int GP_LoadMetaData(const char *src_path, GP_MetaData *data); - -/* - * Simple saving function, the image format is matched by file extension. - * - * Retruns zero on succes. - * - * On failure non-zero is returned. - * - * When file type wasn't recognized by extension or if support for requested - * image format wasn't compiled in non-zero is returned and errno is set to - * ENOSYS. - * - * The resulting errno may also be set to any possible error from fopen(3), open(3), - * write(3), fwrite(3), seek(3), etc.. - */ -int GP_SaveImage(const GP_Context *src, const char *dst_path, - GP_ProgressCallback *callback); +#include "GP_Loader.h"
#endif /* LOADERS_GP_LOADERS_H */ diff --git a/libs/loaders/GP_Loaders.c b/libs/loaders/GP_Loader.c similarity index 70% rename from libs/loaders/GP_Loaders.c rename to libs/loaders/GP_Loader.c index cef1821..057322c 100644 --- a/libs/loaders/GP_Loaders.c +++ b/libs/loaders/GP_Loader.c @@ -34,6 +34,122 @@
#include "GP_Loaders.h"
+#include "GP_Loader.h" + +static GP_Loader psp_image = { + .Load = GP_LoadPSP, + .Save = NULL, + .Match = NULL, + .fmt_name = "Paint Shop Pro Image", + .next = NULL, + .extensions = {"psp", "pspimage", NULL}, +}; + +static GP_Loader *loaders = &psp_image; +static GP_Loader *loaders_last = &psp_image; + +void GP_LoaderRegister(GP_Loader *self) +{ + GP_DEBUG(1, "Registering loader for '%s'", self->fmt_name); + + self->next = NULL; + loaders_last->next = self; +} + +void GP_LoaderUnregister(GP_Loader *self) +{ + struct GP_Loader *i; + + if (self == NULL) + return; + + GP_DEBUG(1, "Unregistering loader for '%s'", self->fmt_name); + + for (i = loaders; i != NULL; i = i->next) { + if (i->next == self) + break; + } + + if (i == NULL) { + GP_WARN("Loader '%s' (%p) wasn't registered", + self->fmt_name, self); + return; + } + + i->next = self->next; +} + +static struct GP_Loader *loader_by_extension(const char *ext) +{ + struct GP_Loader *i; + int j; + + for (i = loaders; i != NULL; i = i->next) { + for (j = 0; i->extensions[j] != NULL; j++) { + if (!strcasecmp(ext, i->extensions[j])) { + GP_DEBUG(1, "Found loader '%s'", i->fmt_name); + return i; + } + } + } + + return NULL; +} + +static struct GP_Loader *loader_by_filename(const char *path) +{ + size_t len = strlen(path); + const char *ext; + int i; + + for (i = len - 1; i >= 0; i--) + if (path[i] == '.') + break; + + ext = path + i + 1; + + GP_DEBUG(1, "Loading file by filename extension '%s'", ext); + + return loader_by_extension(ext); +} + +static struct GP_Loader *loader_by_signature(const char *path) +{ + uint8_t buf[32]; + FILE *f; + int err; + + GP_DEBUG(1, "Trying to match file signature"); + + f = fopen(path, "rb"); + + if (f == NULL) { + err = errno; + GP_DEBUG(1, "Failed to open file '%s'", path); + errno = err; + return NULL; + } + + if (fread(buf, sizeof(buf), 1, f) < 1) { + GP_DEBUG(1, "Failed to read start of the file '%s'", path); + errno = EIO; + return NULL; + } + + fclose(f); + + struct GP_Loader *i; + + for (i = loaders; i != NULL; i = i->next) { + if (i->Match && i->Match(buf)) { + GP_DEBUG(1, "Found loader '%s'", i->fmt_name); + return i; + } + } + + return NULL; +} + enum GP_ImageFmt { GP_FMT_UNKNOWN, GP_FMT_PNG, @@ -178,6 +294,18 @@ GP_Context *GP_LoadImage(const char *src_path, GP_ProgressCallback *callback) break; }
+ struct GP_Loader *l; + + l = loader_by_filename(src_path); + + if (l != NULL) + return l->Load(src_path, callback); + + l = loader_by_signature(src_path); + + if (l != NULL) + return l->Load(src_path, callback); + //TODO file signature based check errno = ENOSYS; return NULL;
-----------------------------------------------------------------------
Summary of changes: include/loaders/GP_Loaders.h | 29 +------ include/loaders/GP_PSP.h | 5 + libs/loaders/{GP_Loaders.c => GP_Loader.c} | 128 ++++++++++++++++++++++++++++ libs/loaders/GP_PSP.c | 5 + 4 files changed, 139 insertions(+), 28 deletions(-) rename libs/loaders/{GP_Loaders.c => GP_Loader.c} (70%)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos@gmail.com if you want to unsubscribe, or site admin admin@repo.or.cz if you receive no reply.