Blender  V3.3
metadata.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2005 Blender Foundation. All rights reserved. */
3 
8 #include <stdlib.h>
9 #include <string.h>
10 
11 #include "BLI_string.h"
12 #include "BLI_utildefines.h"
13 
14 #include "BKE_idprop.h"
15 
16 #include "DNA_ID.h" /* ID property definitions. */
17 
18 #include "MEM_guardedalloc.h"
19 
20 #include "IMB_imbuf.h"
21 #include "IMB_imbuf_types.h"
22 
23 #include "IMB_metadata.h"
24 
25 void IMB_metadata_ensure(struct IDProperty **metadata)
26 {
27  if (*metadata != NULL) {
28  return;
29  }
30 
31  IDPropertyTemplate val = {0};
32  *metadata = IDP_New(IDP_GROUP, &val, "metadata");
33 }
34 
35 void IMB_metadata_free(struct IDProperty *metadata)
36 {
37  if (metadata == NULL) {
38  return;
39  }
40 
41  IDP_FreeProperty(metadata);
42 }
43 
44 bool IMB_metadata_get_field(struct IDProperty *metadata,
45  const char *key,
46  char *field,
47  const size_t len)
48 {
49  IDProperty *prop;
50 
51  if (metadata == NULL) {
52  return false;
53  }
54 
55  prop = IDP_GetPropertyFromGroup(metadata, key);
56 
57  if (prop && prop->type == IDP_STRING) {
58  BLI_strncpy(field, IDP_String(prop), len);
59  return true;
60  }
61  return false;
62 }
63 
64 void IMB_metadata_copy(struct ImBuf *dimb, struct ImBuf *simb)
65 {
66  BLI_assert(dimb != simb);
67  if (simb->metadata) {
69  dimb->metadata = IDP_CopyProperty(simb->metadata);
70  }
71 }
72 
73 void IMB_metadata_set_field(struct IDProperty *metadata, const char *key, const char *value)
74 {
75  BLI_assert(metadata);
76  IDProperty *prop = IDP_GetPropertyFromGroup(metadata, key);
77 
78  if (prop != NULL && prop->type != IDP_STRING) {
79  IDP_FreeFromGroup(metadata, prop);
80  prop = NULL;
81  }
82 
83  if (prop == NULL) {
84  prop = IDP_NewString(value, key, 0);
85  IDP_AddToGroup(metadata, prop);
86  }
87 
88  IDP_AssignString(prop, value, 0);
89 }
90 
91 void IMB_metadata_foreach(struct ImBuf *ibuf, IMBMetadataForeachCb callback, void *userdata)
92 {
93  if (ibuf->metadata == NULL) {
94  return;
95  }
96  for (IDProperty *prop = ibuf->metadata->data.group.first; prop != NULL; prop = prop->next) {
97  callback(prop->name, IDP_String(prop), userdata);
98  }
99 }
struct IDProperty * IDP_New(char type, const IDPropertyTemplate *val, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: idprop.c:887
struct IDProperty * IDP_NewString(const char *st, const char *name, int maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition: idprop.c:339
#define IDP_String(prop)
Definition: BKE_idprop.h:271
void IDP_FreeProperty(struct IDProperty *prop)
Definition: idprop.c:1093
bool IDP_AddToGroup(struct IDProperty *group, struct IDProperty *prop) ATTR_NONNULL()
Definition: idprop.c:631
struct IDProperty * IDP_GetPropertyFromGroup(const struct IDProperty *prop, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void IDP_AssignString(struct IDProperty *prop, const char *st, int maxlen) ATTR_NONNULL()
Definition: idprop.c:383
void IDP_FreeFromGroup(struct IDProperty *group, struct IDProperty *prop) ATTR_NONNULL()
Definition: idprop.c:666
struct IDProperty * IDP_CopyProperty(const struct IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
#define BLI_assert(a)
Definition: BLI_assert.h:46
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
ID and Library types, which are fundamental for sdna.
@ IDP_STRING
Definition: DNA_ID.h:136
@ IDP_GROUP
Definition: DNA_ID.h:141
Contains defines and structs used throughout the imbuf module.
void(* IMBMetadataForeachCb)(const char *field, const char *value, void *userdata)
Definition: IMB_metadata.h:61
Read Guarded memory(de)allocation.
DEGForeachIDComponentCallback callback
int len
Definition: draw_manager.c:108
void IMB_metadata_set_field(struct IDProperty *metadata, const char *key, const char *value)
Definition: metadata.c:73
void IMB_metadata_foreach(struct ImBuf *ibuf, IMBMetadataForeachCb callback, void *userdata)
Definition: metadata.c:91
bool IMB_metadata_get_field(struct IDProperty *metadata, const char *key, char *field, const size_t len)
Definition: metadata.c:44
void IMB_metadata_copy(struct ImBuf *dimb, struct ImBuf *simb)
Definition: metadata.c:64
void IMB_metadata_ensure(struct IDProperty **metadata)
Definition: metadata.c:25
void IMB_metadata_free(struct IDProperty *metadata)
Definition: metadata.c:35
ListBase group
Definition: DNA_ID.h:101
IDPropertyData data
Definition: DNA_ID.h:117
char type
Definition: DNA_ID.h:108
struct IDProperty * metadata
void * first
Definition: DNA_listBase.h:31