Blender  V2.59
metadata.c
Go to the documentation of this file.
00001 /*
00002  * $Id: metadata.c 35239 2011-02-27 20:23:21Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2005 Blender Foundation
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): Austin Benesh. Ton Roosendaal.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  */
00029 
00035 #include <stdlib.h>
00036 #include <string.h>
00037 
00038 #include "BLI_blenlib.h"
00039 #include "MEM_guardedalloc.h"
00040 
00041 #include "IMB_imbuf_types.h"
00042 #include "IMB_imbuf.h"
00043 
00044 #include "IMB_metadata.h"
00045 
00046 
00047 
00048 void IMB_metadata_free(struct ImBuf* img)
00049 {
00050         ImMetaData *info;
00051 
00052         if (!img)
00053                 return;
00054         if (!img->metadata) {
00055                 return;
00056         }
00057         info = img->metadata;
00058         while (info) {
00059                 ImMetaData* next = info->next;
00060                 MEM_freeN(info->key);
00061                 MEM_freeN(info->value);
00062                 MEM_freeN(info);
00063                 info = next;
00064         }
00065 }
00066 
00067 int IMB_metadata_get_field(struct ImBuf* img, const char* key, char* field, int len)
00068 {
00069         ImMetaData *info;
00070         int retval = 0;
00071 
00072         if (!img)
00073                 return 0;
00074         if (!img->metadata) {
00075                 return 0;
00076         }
00077         info = img->metadata;
00078         while (info) {
00079                 if (strcmp(key, info->key) == 0) {
00080                         BLI_strncpy(field, info->value, len);
00081                         retval = 1;
00082                         break;
00083                 }
00084                 info = info->next;
00085         }
00086         return retval;
00087 }
00088 
00089 int IMB_metadata_add_field(struct ImBuf* img, const char* key, const char* field)
00090 {
00091         ImMetaData *info;
00092         ImMetaData *last;
00093 
00094         if (!img)
00095                 return 0;
00096 
00097         if (!img->metadata) {
00098                 img->metadata = MEM_callocN(sizeof(ImMetaData), "ImMetaData");
00099                 info = img->metadata;
00100         } else {
00101                 info = img->metadata;
00102                 last = info;
00103                 while (info) {
00104                         last = info;
00105                         info = info->next;
00106                 }
00107                 info = MEM_callocN(sizeof(ImMetaData), "ImMetaData");
00108                 last->next = info;
00109         }
00110         info->key = BLI_strdup(key);
00111         info->value = BLI_strdup(field);
00112         return 1;
00113 }
00114 
00115 int IMB_metadata_del_field(struct ImBuf *img, const char *key)
00116 {
00117         ImMetaData *p, *p1;
00118 
00119         if ((!img) || (!img->metadata))
00120                 return (0);
00121 
00122         p = img->metadata;
00123         p1 = NULL;
00124         while (p) {
00125                 if (!strcmp (key, p->key)) {
00126                         if (p1)
00127                                 p1->next = p->next;
00128                         else
00129                                 img->metadata = p->next;
00130 
00131                         MEM_freeN(p->key);
00132                         MEM_freeN(p->value);
00133                         MEM_freeN(p);
00134                         return (1);
00135                 }
00136                 p1 = p;
00137                 p = p->next;
00138         }
00139         return (0);
00140 }
00141 
00142 int IMB_metadata_change_field(struct ImBuf *img, const char *key, const char *field)
00143 {
00144         ImMetaData *p;
00145 
00146         if (!img)
00147                 return (0);
00148 
00149         if (!img->metadata)
00150                 return (IMB_metadata_add_field (img, key, field));
00151 
00152         p = img->metadata;
00153         while (p) {
00154                 if (!strcmp (key, p->key)) {
00155                         MEM_freeN (p->value);
00156                         p->value = BLI_strdup (field);
00157                         return (1);
00158                 }
00159                 p = p->next;
00160         }
00161 
00162         return (IMB_metadata_add_field (img, key, field));
00163 }
00164