From 02e85ee0695a079e738545bb8747ed863c384e6b Mon Sep 17 00:00:00 2001 From: Kim Woelders Date: Mon, 23 Dec 2019 20:21:52 +0100 Subject: [PATCH] image.c: Move image tag functions to separate file --- src/lib/Makefile.am | 1 + src/lib/image.c | 96 ---------------------------------------- src/lib/image_tags.c | 102 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 96 deletions(-) create mode 100644 src/lib/image_tags.c diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index ee2064f..2c1dded 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -45,6 +45,7 @@ grad.c \ grad.h \ image.c \ image.h \ +image_tags.c \ line.c \ modules.c \ polygon.c \ diff --git a/src/lib/image.c b/src/lib/image.c index 1a2c8fb..a4f529a 100644 --- a/src/lib/image.c +++ b/src/lib/image.c @@ -73,102 +73,6 @@ __imlib_ReplaceData(ImlibImage * im, unsigned int *new_data) im->data_memory_func = NULL; } -/* attach a string key'd data and/or int value to an image that cna be */ -/* looked up later by its string key */ -__EXPORT__ void -__imlib_AttachTag(ImlibImage * im, const char *key, int val, void *data, - ImlibDataDestructorFunction destructor) -{ - ImlibImageTag *t; - - /* no string key? abort */ - if (!key) - return; - - /* if a tag of that name already exists - remove it and free it */ - if ((t = __imlib_RemoveTag(im, key))) - __imlib_FreeTag(im, t); - /* allocate the struct */ - t = malloc(sizeof(ImlibImageTag)); - /* fill it int */ - t->key = strdup(key); - t->val = val; - t->data = data; - t->destructor = destructor; - t->next = im->tags; - /* prepend it to the list of tags */ - im->tags = t; -} - -/* look up a tage by its key on the image it was attached to */ -__EXPORT__ ImlibImageTag * -__imlib_GetTag(ImlibImage * im, const char *key) -{ - ImlibImageTag *t; - - t = im->tags; - while (t) - { - if (!strcmp(t->key, key)) - return t; - t = t->next; - } - /* no tag found - return NULL */ - return NULL; -} - -/* remove a tag by looking it up by its key and removing it from */ -/* the list of keys */ -ImlibImageTag * -__imlib_RemoveTag(ImlibImage * im, const char *key) -{ - ImlibImageTag *t, *tt; - - tt = NULL; - t = im->tags; - while (t) - { - if (!strcmp(t->key, key)) - { - if (tt) - tt->next = t->next; - else - im->tags = t->next; - return t; - } - tt = t; - t = t->next; - } - /* no tag found - NULL */ - return NULL; -} - -/* free the data struct for the tag and if a destructor function was */ -/* provided call it on the data member */ -void -__imlib_FreeTag(ImlibImage * im, ImlibImageTag * t) -{ - free(t->key); - if (t->destructor) - t->destructor(im, t->data); - free(t); -} - -/* free all the tags attached to an image */ -void -__imlib_FreeAllTags(ImlibImage * im) -{ - ImlibImageTag *t, *tt; - - t = im->tags; - while (t) - { - tt = t; - t = t->next; - __imlib_FreeTag(im, tt); - } -} - /* create an image data struct and fill it in */ static ImlibImage * __imlib_ProduceImage(void) diff --git a/src/lib/image_tags.c b/src/lib/image_tags.c new file mode 100644 index 0000000..e3c7149 --- /dev/null +++ b/src/lib/image_tags.c @@ -0,0 +1,102 @@ +#include "common.h" + +#include +#include + +#include "image.h" + +/* attach a string key'd data and/or int value to an image that cna be */ +/* looked up later by its string key */ +__EXPORT__ void +__imlib_AttachTag(ImlibImage * im, const char *key, int val, void *data, + ImlibDataDestructorFunction destructor) +{ + ImlibImageTag *t; + + /* no string key? abort */ + if (!key) + return; + + /* if a tag of that name already exists - remove it and free it */ + if ((t = __imlib_RemoveTag(im, key))) + __imlib_FreeTag(im, t); + /* allocate the struct */ + t = malloc(sizeof(ImlibImageTag)); + /* fill it int */ + t->key = strdup(key); + t->val = val; + t->data = data; + t->destructor = destructor; + t->next = im->tags; + /* prepend it to the list of tags */ + im->tags = t; +} + +/* look up a tage by its key on the image it was attached to */ +__EXPORT__ ImlibImageTag * +__imlib_GetTag(ImlibImage * im, const char *key) +{ + ImlibImageTag *t; + + t = im->tags; + while (t) + { + if (!strcmp(t->key, key)) + return t; + t = t->next; + } + /* no tag found - return NULL */ + return NULL; +} + +/* remove a tag by looking it up by its key and removing it from */ +/* the list of keys */ +ImlibImageTag * +__imlib_RemoveTag(ImlibImage * im, const char *key) +{ + ImlibImageTag *t, *tt; + + tt = NULL; + t = im->tags; + while (t) + { + if (!strcmp(t->key, key)) + { + if (tt) + tt->next = t->next; + else + im->tags = t->next; + return t; + } + tt = t; + t = t->next; + } + /* no tag found - NULL */ + return NULL; +} + +/* free the data struct for the tag and if a destructor function was */ +/* provided call it on the data member */ +void +__imlib_FreeTag(ImlibImage * im, ImlibImageTag * t) +{ + free(t->key); + if (t->destructor) + t->destructor(im, t->data); + free(t); +} + +/* free all the tags attached to an image */ +void +__imlib_FreeAllTags(ImlibImage * im) +{ + ImlibImageTag *t, *tt; + + t = im->tags; + while (t) + { + tt = t; + t = t->next; + __imlib_FreeTag(im, tt); + } +}