2004-10-05 Lutz Mueller <lutz@users.sourceforge.net>
[platform/upstream/libexif.git] / libexif / exif-content.c
1 /* exif-content.c
2  *
3  * Copyright © 2001 Lutz Müller <lutz@users.sourceforge.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22 #include "exif-content.h"
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
29
30 struct _ExifContentPrivate
31 {
32         unsigned int ref_count;
33
34         ExifMem *mem;
35 };
36
37 ExifContent *
38 exif_content_new (void)
39 {
40         ExifMem *mem = exif_mem_new_default ();
41         ExifContent *content = exif_content_new_mem (mem);
42
43         exif_mem_unref (mem);
44
45         return content;
46 }
47
48 ExifContent *
49 exif_content_new_mem (ExifMem *mem)
50 {
51         ExifContent *content;
52
53         if (!mem) return NULL;
54
55         content = exif_mem_alloc (mem, (ExifLong) sizeof (ExifContent));
56         if (!content)
57                 return NULL;
58         content->priv = exif_mem_alloc (mem,
59                                 (ExifLong) sizeof (ExifContentPrivate));
60         if (!content->priv) {
61                 exif_mem_free (mem, content);
62                 return NULL;
63         }
64
65         content->priv->ref_count = 1;
66
67         content->priv->mem = mem;
68         exif_mem_ref (mem);
69
70         return content;
71 }
72
73 void
74 exif_content_ref (ExifContent *content)
75 {
76         content->priv->ref_count++;
77 }
78
79 void
80 exif_content_unref (ExifContent *content)
81 {
82         content->priv->ref_count--;
83         if (!content->priv->ref_count)
84                 exif_content_free (content);
85 }
86
87 void
88 exif_content_free (ExifContent *content)
89 {
90         unsigned int i;
91
92         if (!content) return;
93
94         for (i = 0; i < content->count; i++)
95                 exif_entry_unref (content->entries[i]);
96         if (content->priv) {
97                 ExifMem *mem = content->priv->mem;
98                 exif_mem_free (mem, content->entries);
99                 exif_mem_free (mem, content->priv);
100                 exif_mem_free (mem, content);
101                 exif_mem_unref (mem);
102         }
103 }
104
105 void
106 exif_content_dump (ExifContent *content, unsigned int indent)
107 {
108         char buf[1024];
109         unsigned int i;
110
111         for (i = 0; i < 2 * indent; i++)
112                 buf[i] = ' ';
113         buf[i] = '\0';
114
115         if (!content)
116                 return;
117
118         printf ("%sDumping exif content (%i entries)...\n", buf,
119                 content->count);
120         for (i = 0; i < content->count; i++)
121                 exif_entry_dump (content->entries[i], indent + 1);
122 }
123
124 void
125 exif_content_add_entry (ExifContent *content, ExifEntry *entry)
126 {
127         if (!content || !content->priv || !entry || entry->parent) return;
128
129         entry->parent = content;
130         content->entries = exif_mem_realloc (content->priv->mem,
131                 content->entries, sizeof (ExifEntry) * (content->count + 1));
132         if (!content->entries) return;
133         content->entries[content->count] = entry;
134         exif_entry_ref (entry);
135         content->count++;
136 }
137
138 void
139 exif_content_remove_entry (ExifContent *c, ExifEntry *e)
140 {
141         unsigned int i;
142
143         if (!c || !c->priv || !e || (e->parent != c)) return;
144
145         /* Search the entry */
146         for (i = 0; i < c->count; i++) if (c->entries[i] == e) break;
147         if (i == c->count) return;
148
149         /* Remove the entry */
150         memmove (&c->entries[i], &c->entries[i + 1],
151                  sizeof (ExifEntry) * (c->count - i - 1));
152         c->count--;
153         e->parent = NULL;
154         exif_entry_unref (e);
155         c->entries = exif_mem_realloc (c->priv->mem, c->entries,
156                                         sizeof(ExifEntry) * c->count);
157 }
158
159 ExifEntry *
160 exif_content_get_entry (ExifContent *content, ExifTag tag)
161 {
162         unsigned int i;
163
164         if (!content)
165                 return (NULL);
166
167         for (i = 0; i < content->count; i++)
168                 if (content->entries[i]->tag == tag)
169                         return (content->entries[i]);
170         return (NULL);
171 }
172
173 void
174 exif_content_foreach_entry (ExifContent *content,
175                             ExifContentForeachEntryFunc func, void *data)
176 {
177         unsigned int i;
178
179         if (!content || !func)
180                 return;
181
182         for (i = 0; i < content->count; i++)
183                 func (content->entries[i], data);
184 }