2004-11-10 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 };
37
38 ExifContent *
39 exif_content_new (void)
40 {
41         ExifMem *mem = exif_mem_new_default ();
42         ExifContent *content = exif_content_new_mem (mem);
43
44         exif_mem_unref (mem);
45
46         return content;
47 }
48
49 ExifContent *
50 exif_content_new_mem (ExifMem *mem)
51 {
52         ExifContent *content;
53
54         if (!mem) return NULL;
55
56         content = exif_mem_alloc (mem, (ExifLong) sizeof (ExifContent));
57         if (!content)
58                 return NULL;
59         content->priv = exif_mem_alloc (mem,
60                                 (ExifLong) sizeof (ExifContentPrivate));
61         if (!content->priv) {
62                 exif_mem_free (mem, content);
63                 return NULL;
64         }
65
66         content->priv->ref_count = 1;
67
68         content->priv->mem = mem;
69         exif_mem_ref (mem);
70
71         return content;
72 }
73
74 void
75 exif_content_ref (ExifContent *content)
76 {
77         content->priv->ref_count++;
78 }
79
80 void
81 exif_content_unref (ExifContent *content)
82 {
83         content->priv->ref_count--;
84         if (!content->priv->ref_count)
85                 exif_content_free (content);
86 }
87
88 void
89 exif_content_free (ExifContent *content)
90 {
91         unsigned int i;
92
93         if (!content) return;
94
95         for (i = 0; i < content->count; i++)
96                 exif_entry_unref (content->entries[i]);
97         if (content->priv) {
98                 ExifMem *mem = content->priv->mem;
99                 exif_mem_free (mem, content->entries);
100                 exif_mem_free (mem, content->priv);
101                 exif_mem_free (mem, content);
102                 exif_mem_unref (mem);
103         }
104 }
105
106 void
107 exif_content_dump (ExifContent *content, unsigned int indent)
108 {
109         char buf[1024];
110         unsigned int i;
111
112         for (i = 0; i < 2 * indent; i++)
113                 buf[i] = ' ';
114         buf[i] = '\0';
115
116         if (!content)
117                 return;
118
119         printf ("%sDumping exif content (%i entries)...\n", buf,
120                 content->count);
121         for (i = 0; i < content->count; i++)
122                 exif_entry_dump (content->entries[i], indent + 1);
123 }
124
125 void
126 exif_content_add_entry (ExifContent *content, ExifEntry *entry)
127 {
128         if (!content || !content->priv || !entry || entry->parent) return;
129
130         entry->parent = content;
131         content->entries = exif_mem_realloc (content->priv->mem,
132                 content->entries, sizeof (ExifEntry) * (content->count + 1));
133         if (!content->entries) return;
134         content->entries[content->count] = entry;
135         exif_entry_ref (entry);
136         content->count++;
137 }
138
139 void
140 exif_content_remove_entry (ExifContent *c, ExifEntry *e)
141 {
142         unsigned int i;
143
144         if (!c || !c->priv || !e || (e->parent != c)) return;
145
146         /* Search the entry */
147         for (i = 0; i < c->count; i++) if (c->entries[i] == e) break;
148         if (i == c->count) return;
149
150         /* Remove the entry */
151         memmove (&c->entries[i], &c->entries[i + 1],
152                  sizeof (ExifEntry) * (c->count - i - 1));
153         c->count--;
154         e->parent = NULL;
155         exif_entry_unref (e);
156         c->entries = exif_mem_realloc (c->priv->mem, c->entries,
157                                         sizeof(ExifEntry) * c->count);
158 }
159
160 ExifEntry *
161 exif_content_get_entry (ExifContent *content, ExifTag tag)
162 {
163         unsigned int i;
164
165         if (!content)
166                 return (NULL);
167
168         for (i = 0; i < content->count; i++)
169                 if (content->entries[i]->tag == tag)
170                         return (content->entries[i]);
171         return (NULL);
172 }
173
174 void
175 exif_content_foreach_entry (ExifContent *content,
176                             ExifContentForeachEntryFunc func, void *data)
177 {
178         unsigned int i;
179
180         if (!content || !func)
181                 return;
182
183         for (i = 0; i < content->count; i++)
184                 func (content->entries[i], data);
185 }