2004-11-15 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
23 #include <libexif/exif-content.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
30
31 struct _ExifContentPrivate
32 {
33         unsigned int ref_count;
34
35         ExifMem *mem;
36         ExifLog *log;
37 };
38
39 ExifContent *
40 exif_content_new (void)
41 {
42         ExifMem *mem = exif_mem_new_default ();
43         ExifContent *content = exif_content_new_mem (mem);
44
45         exif_mem_unref (mem);
46
47         return content;
48 }
49
50 ExifContent *
51 exif_content_new_mem (ExifMem *mem)
52 {
53         ExifContent *content;
54
55         if (!mem) return NULL;
56
57         content = exif_mem_alloc (mem, (ExifLong) sizeof (ExifContent));
58         if (!content)
59                 return NULL;
60         content->priv = exif_mem_alloc (mem,
61                                 (ExifLong) sizeof (ExifContentPrivate));
62         if (!content->priv) {
63                 exif_mem_free (mem, content);
64                 return NULL;
65         }
66
67         content->priv->ref_count = 1;
68
69         content->priv->mem = mem;
70         exif_mem_ref (mem);
71
72         return content;
73 }
74
75 void
76 exif_content_ref (ExifContent *content)
77 {
78         content->priv->ref_count++;
79 }
80
81 void
82 exif_content_unref (ExifContent *content)
83 {
84         content->priv->ref_count--;
85         if (!content->priv->ref_count)
86                 exif_content_free (content);
87 }
88
89 void
90 exif_content_free (ExifContent *content)
91 {
92         ExifMem *mem = (content && content->priv) ? content->priv->mem : NULL;
93         unsigned int i;
94
95         if (!content) return;
96
97         for (i = 0; i < content->count; i++)
98                 exif_entry_unref (content->entries[i]);
99         exif_mem_free (mem, content->entries);
100
101         if (content->priv) {
102                 exif_log_unref (content->priv->log);
103         }
104
105         exif_mem_free (mem, content->priv);
106         exif_mem_free (mem, content);
107         exif_mem_unref (mem);
108 }
109
110 void
111 exif_content_dump (ExifContent *content, unsigned int indent)
112 {
113         char buf[1024];
114         unsigned int i;
115
116         for (i = 0; i < 2 * indent; i++)
117                 buf[i] = ' ';
118         buf[i] = '\0';
119
120         if (!content)
121                 return;
122
123         printf ("%sDumping exif content (%i entries)...\n", buf,
124                 content->count);
125         for (i = 0; i < content->count; i++)
126                 exif_entry_dump (content->entries[i], indent + 1);
127 }
128
129 void
130 exif_content_add_entry (ExifContent *c, ExifEntry *entry)
131 {
132         if (!c || !c->priv || !entry || entry->parent) return;
133
134         /* One tag can only be added once to an IFD. */
135         if (exif_content_get_entry (c, entry->tag)) {
136                 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "ExifContent",
137                         "An attempt has been made to add "
138                         "the tag '%s' twice to an IFD. This is against "
139                         "specification.", exif_tag_get_name (entry->tag));
140                 return;
141         }
142
143         entry->parent = c;
144         c->entries = exif_mem_realloc (c->priv->mem,
145                 c->entries, sizeof (ExifEntry) * (c->count + 1));
146         if (!c->entries) return;
147         c->entries[c->count] = entry;
148         exif_entry_ref (entry);
149         c->count++;
150 }
151
152 void
153 exif_content_remove_entry (ExifContent *c, ExifEntry *e)
154 {
155         unsigned int i;
156
157         if (!c || !c->priv || !e || (e->parent != c)) return;
158
159         /* Search the entry */
160         for (i = 0; i < c->count; i++) if (c->entries[i] == e) break;
161         if (i == c->count) return;
162
163         /* Remove the entry */
164         memmove (&c->entries[i], &c->entries[i + 1],
165                  sizeof (ExifEntry) * (c->count - i - 1));
166         c->count--;
167         e->parent = NULL;
168         exif_entry_unref (e);
169         c->entries = exif_mem_realloc (c->priv->mem, c->entries,
170                                         sizeof(ExifEntry) * c->count);
171 }
172
173 ExifEntry *
174 exif_content_get_entry (ExifContent *content, ExifTag tag)
175 {
176         unsigned int i;
177
178         if (!content)
179                 return (NULL);
180
181         for (i = 0; i < content->count; i++)
182                 if (content->entries[i]->tag == tag)
183                         return (content->entries[i]);
184         return (NULL);
185 }
186
187 void
188 exif_content_foreach_entry (ExifContent *content,
189                             ExifContentForeachEntryFunc func, void *data)
190 {
191         unsigned int i;
192
193         if (!content || !func)
194                 return;
195
196         for (i = 0; i < content->count; i++)
197                 func (content->entries[i], data);
198 }
199
200 void
201 exif_content_log (ExifContent *content, ExifLog *log)
202 {
203         if (!content || !content->priv || !log || content->priv->log == log)
204                 return;
205
206         if (content->priv->log) exif_log_unref (content->priv->log);
207         content->priv->log = log;
208         exif_log_ref (log);
209 }