Blender  V2.93
metadata.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2005 Blender Foundation
17  * All rights reserved.
18  */
19 
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "BLI_string.h"
28 #include "BLI_utildefines.h"
29 
30 #include "BKE_idprop.h"
31 
32 #include "DNA_ID.h" /* ID property definitions. */
33 
34 #include "MEM_guardedalloc.h"
35 
36 #include "IMB_imbuf.h"
37 #include "IMB_imbuf_types.h"
38 
39 #include "IMB_metadata.h"
40 
41 void IMB_metadata_ensure(struct IDProperty **metadata)
42 {
43  if (*metadata != NULL) {
44  return;
45  }
46 
48  *metadata = IDP_New(IDP_GROUP, &val, "metadata");
49 }
50 
51 void IMB_metadata_free(struct IDProperty *metadata)
52 {
53  if (metadata == NULL) {
54  return;
55  }
56 
57  IDP_FreeProperty(metadata);
58 }
59 
60 bool IMB_metadata_get_field(struct IDProperty *metadata,
61  const char *key,
62  char *field,
63  const size_t len)
64 {
65  IDProperty *prop;
66 
67  if (metadata == NULL) {
68  return false;
69  }
70 
71  prop = IDP_GetPropertyFromGroup(metadata, key);
72 
73  if (prop && prop->type == IDP_STRING) {
74  BLI_strncpy(field, IDP_String(prop), len);
75  return true;
76  }
77  return false;
78 }
79 
80 void IMB_metadata_copy(struct ImBuf *dimb, struct ImBuf *simb)
81 {
82  BLI_assert(dimb != simb);
83  if (simb->metadata) {
85  dimb->metadata = IDP_CopyProperty(simb->metadata);
86  }
87 }
88 
89 void IMB_metadata_set_field(struct IDProperty *metadata, const char *key, const char *value)
90 {
91  BLI_assert(metadata);
92  IDProperty *prop = IDP_GetPropertyFromGroup(metadata, key);
93 
94  if (prop != NULL && prop->type != IDP_STRING) {
95  IDP_FreeFromGroup(metadata, prop);
96  prop = NULL;
97  }
98 
99  if (prop == NULL) {
100  prop = IDP_NewString(value, key, 0);
101  IDP_AddToGroup(metadata, prop);
102  }
103 
104  IDP_AssignString(prop, value, 0);
105 }
106 
107 void IMB_metadata_foreach(struct ImBuf *ibuf, IMBMetadataForeachCb callback, void *userdata)
108 {
109  if (ibuf->metadata == NULL) {
110  return;
111  }
112  for (IDProperty *prop = ibuf->metadata->data.group.first; prop != NULL; prop = prop->next) {
113  callback(prop->name, IDP_String(prop), userdata);
114  }
115 }
struct IDProperty * IDP_NewString(const char *st, const char *name, int maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition: idprop.c:325
#define IDP_String(prop)
Definition: BKE_idprop.h:181
void IDP_FreeProperty(struct IDProperty *prop)
Definition: idprop.c:1040
bool IDP_AddToGroup(struct IDProperty *group, struct IDProperty *prop) ATTR_NONNULL()
Definition: idprop.c:643
struct IDProperty * IDP_GetPropertyFromGroup(const struct IDProperty *prop, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
struct IDProperty * IDP_New(const char type, const IDPropertyTemplate *val, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: idprop.c:907
void IDP_AssignString(struct IDProperty *prop, const char *st, int maxlen) ATTR_NONNULL()
Definition: idprop.c:369
void IDP_FreeFromGroup(struct IDProperty *group, struct IDProperty *prop) ATTR_NONNULL()
Definition: idprop.c:690
struct IDProperty * IDP_CopyProperty(const struct IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
#define BLI_assert(a)
Definition: BLI_assert.h:58
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
ID and Library types, which are fundamental for sdna.
@ IDP_STRING
Definition: DNA_ID.h:97
@ IDP_GROUP
Definition: DNA_ID.h:101
Contains defines and structs used throughout the imbuf module.
void(* IMBMetadataForeachCb)(const char *field, const char *value, void *userdata)
Definition: IMB_metadata.h:80
Read Guarded memory(de)allocation.
DEGForeachIDComponentCallback callback
void IMB_metadata_set_field(struct IDProperty *metadata, const char *key, const char *value)
Definition: metadata.c:89
void IMB_metadata_foreach(struct ImBuf *ibuf, IMBMetadataForeachCb callback, void *userdata)
Definition: metadata.c:107
bool IMB_metadata_get_field(struct IDProperty *metadata, const char *key, char *field, const size_t len)
Definition: metadata.c:60
void IMB_metadata_copy(struct ImBuf *dimb, struct ImBuf *simb)
Definition: metadata.c:80
void IMB_metadata_ensure(struct IDProperty **metadata)
Definition: metadata.c:41
void IMB_metadata_free(struct IDProperty *metadata)
Definition: metadata.c:51
ListBase group
Definition: DNA_ID.h:64
IDPropertyData data
Definition: DNA_ID.h:80
char type
Definition: DNA_ID.h:71
struct IDProperty * metadata
void * first
Definition: DNA_listBase.h:47
uint len