429c0eb6b5d2e5d93cc8ffb8445550896112e624
[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 #include <config.h>
21 #include "exif-content.h"
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 //#define DEBUG
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
36 ExifContent *
37 exif_content_new (void)
38 {
39         ExifContent *content;
40
41         content = malloc (sizeof (ExifContent));
42         if (!content)
43                 return (NULL);
44         memset (content, 0, sizeof (ExifContent));
45         content->priv = malloc (sizeof (ExifContentPrivate));
46         if (!content->priv) {
47                 free (content);
48                 return (NULL);
49         }
50         memset (content->priv, 0, sizeof (ExifContentPrivate));
51
52         content->priv->ref_count = 1;
53
54         return (content);
55 }
56
57 void
58 exif_content_ref (ExifContent *content)
59 {
60         content->priv->ref_count++;
61 }
62
63 void
64 exif_content_unref (ExifContent *content)
65 {
66         content->priv->ref_count--;
67         if (!content->priv->ref_count)
68                 exif_content_free (content);
69 }
70
71 void
72 exif_content_free (ExifContent *content)
73 {
74         unsigned int i;
75
76         for (i = 0; i < content->count; i++)
77                 exif_entry_unref (content->entries[i]);
78         free (content->entries);
79         free (content->priv);
80         free (content);
81 }
82
83 void
84 exif_content_dump (ExifContent *content, unsigned int indent)
85 {
86         char buf[1024];
87         unsigned int i;
88
89         for (i = 0; i < 2 * indent; i++)
90                 buf[i] = ' ';
91         buf[i] = '\0';
92
93         if (!content)
94                 return;
95
96         printf ("%sDumping exif content (%i entries)...\n", buf,
97                 content->count);
98         for (i = 0; i < content->count; i++)
99                 exif_entry_dump (content->entries[i], indent + 1);
100 }
101
102 void
103 exif_content_add_entry (ExifContent *content, ExifEntry *entry)
104 {
105         if (entry->parent)
106                 return;
107
108         entry->parent = content;
109         content->entries = realloc (content->entries,
110                                     sizeof (ExifEntry) * (content->count + 1));
111         content->entries[content->count] = entry;
112         exif_entry_ref (entry);
113         content->count++;
114 }
115
116 void
117 exif_content_remove_entry (ExifContent *content, ExifEntry *entry)
118 {
119         unsigned int i;
120
121         if (entry->parent != content)
122                 return;
123
124         for (i = 0; i < content->count; i++)
125                 if (content->entries[i] == entry)
126                         break;
127         if (i == content->count)
128                 return;
129
130         memmove (&content->entries[i], &content->entries[i + 1],
131                  sizeof (ExifEntry) * (content->count - i - 1));
132         content->count--;
133
134         entry->parent = NULL;
135         exif_entry_unref (entry);
136 }
137
138 ExifEntry *
139 exif_content_get_entry (ExifContent *content, ExifTag tag)
140 {
141         unsigned int i;
142
143         if (!content)
144                 return (NULL);
145
146         for (i = 0; i < content->count; i++)
147                 if (content->entries[i]->tag == tag)
148                         return (content->entries[i]);
149         return (NULL);
150 }