- clean the headers of gettext macros
[platform/upstream/libexif.git] / libexif / exif-content.c
1 /* exif-content.c
2  *
3  * Copyright (C) 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 //#define DEBUG
29
30 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
31
32 struct _ExifContentPrivate
33 {
34         unsigned int ref_count;
35 };
36
37 ExifContent *
38 exif_content_new (void)
39 {
40         ExifContent *content;
41
42         content = malloc (sizeof (ExifContent));
43         if (!content)
44                 return (NULL);
45         memset (content, 0, sizeof (ExifContent));
46         content->priv = malloc (sizeof (ExifContentPrivate));
47         if (!content->priv) {
48                 free (content);
49                 return (NULL);
50         }
51         memset (content->priv, 0, sizeof (ExifContentPrivate));
52
53         content->priv->ref_count = 1;
54
55         return (content);
56 }
57
58 void
59 exif_content_ref (ExifContent *content)
60 {
61         content->priv->ref_count++;
62 }
63
64 void
65 exif_content_unref (ExifContent *content)
66 {
67         content->priv->ref_count--;
68         if (!content->priv->ref_count)
69                 exif_content_free (content);
70 }
71
72 void
73 exif_content_free (ExifContent *content)
74 {
75         unsigned int i;
76
77         for (i = 0; i < content->count; i++)
78                 exif_entry_unref (content->entries[i]);
79         free (content->entries);
80         free (content->priv);
81         free (content);
82 }
83
84 void
85 exif_content_dump (ExifContent *content, unsigned int indent)
86 {
87         char buf[1024];
88         unsigned int i;
89
90         for (i = 0; i < 2 * indent; i++)
91                 buf[i] = ' ';
92         buf[i] = '\0';
93
94         if (!content)
95                 return;
96
97         printf ("%sDumping exif content (%i entries)...\n", buf,
98                 content->count);
99         for (i = 0; i < content->count; i++)
100                 exif_entry_dump (content->entries[i], indent + 1);
101 }
102
103 void
104 exif_content_add_entry (ExifContent *content, ExifEntry *entry)
105 {
106         if (entry->parent)
107                 return;
108
109         entry->parent = content;
110         content->entries = realloc (content->entries,
111                                     sizeof (ExifEntry) * (content->count + 1));
112         content->entries[content->count] = entry;
113         exif_entry_ref (entry);
114         content->count++;
115 }
116
117 void
118 exif_content_remove_entry (ExifContent *content, ExifEntry *entry)
119 {
120         unsigned int i;
121
122         if (entry->parent != content)
123                 return;
124
125         for (i = 0; i < content->count; i++)
126                 if (content->entries[i] == entry)
127                         break;
128         if (i == content->count)
129                 return;
130
131         memmove (&content->entries[i], &content->entries[i + 1],
132                  sizeof (ExifEntry) * (content->count - i - 1));
133         content->count--;
134
135         entry->parent = NULL;
136         exif_entry_unref (entry);
137 }
138
139 ExifEntry *
140 exif_content_get_entry (ExifContent *content, ExifTag tag)
141 {
142         unsigned int i;
143
144         if (!content)
145                 return (NULL);
146
147         for (i = 0; i < content->count; i++)
148                 if (content->entries[i]->tag == tag)
149                         return (content->entries[i]);
150         return (NULL);
151 }
152
153 void
154 exif_content_foreach_entry (ExifContent *content,
155                             ExifContentForeachEntryFunc func, void *data)
156 {
157         unsigned int i;
158
159         if (!content || !func)
160                 return;
161
162         for (i = 0; i < content->count; i++)
163                 func (content->entries[i], data);
164 }