image.c: Move image tag functions to separate file

This commit is contained in:
Kim Woelders 2019-12-23 20:21:52 +01:00
parent 0c7464f0db
commit 02e85ee069
3 changed files with 103 additions and 96 deletions

View File

@ -45,6 +45,7 @@ grad.c \
grad.h \
image.c \
image.h \
image_tags.c \
line.c \
modules.c \
polygon.c \

View File

@ -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)

102
src/lib/image_tags.c Normal file
View File

@ -0,0 +1,102 @@
#include "common.h"
#include <stdlib.h>
#include <string.h>
#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);
}
}