2004-11-10 Lutz Mueller <lutz@users.sourceforge.net>
[platform/upstream/libexif.git] / libexif / exif-content.c
index ad550c0..eca6efe 100644 (file)
@@ -1,16 +1,16 @@
 /* exif-content.c
  *
- * Copyright (C) 2001 Lutz Müller <lutz@users.sourceforge.net>
+ * Copyright © 2001 Lutz Müller <lutz@users.sourceforge.net>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful, 
- * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details. 
+ * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Boston, MA 02111-1307, USA.
  */
 
-#include "config.h"
-#include "exif-content.h"
+#include <config.h>
+
+#include <libexif/exif-content.h>
 
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 
-//#define DEBUG
-
 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
 
 struct _ExifContentPrivate
 {
        unsigned int ref_count;
+
+       ExifMem *mem;
 };
 
 ExifContent *
 exif_content_new (void)
 {
+       ExifMem *mem = exif_mem_new_default ();
+       ExifContent *content = exif_content_new_mem (mem);
+
+       exif_mem_unref (mem);
+
+       return content;
+}
+
+ExifContent *
+exif_content_new_mem (ExifMem *mem)
+{
        ExifContent *content;
 
-       content = malloc (sizeof (ExifContent));
+       if (!mem) return NULL;
+
+       content = exif_mem_alloc (mem, (ExifLong) sizeof (ExifContent));
        if (!content)
-               return (NULL);
-       memset (content, 0, sizeof (ExifContent));
-       content->priv = malloc (sizeof (ExifContentPrivate));
+               return NULL;
+       content->priv = exif_mem_alloc (mem,
+                               (ExifLong) sizeof (ExifContentPrivate));
        if (!content->priv) {
-               free (content);
-               return (NULL);
+               exif_mem_free (mem, content);
+               return NULL;
        }
-       memset (content->priv, 0, sizeof (ExifContentPrivate));
 
        content->priv->ref_count = 1;
 
-       return (content);
+       content->priv->mem = mem;
+       exif_mem_ref (mem);
+
+       return content;
 }
 
 void
@@ -74,11 +90,17 @@ exif_content_free (ExifContent *content)
 {
        unsigned int i;
 
+       if (!content) return;
+
        for (i = 0; i < content->count; i++)
                exif_entry_unref (content->entries[i]);
-       free (content->entries);
-       free (content->priv);
-       free (content);
+       if (content->priv) {
+               ExifMem *mem = content->priv->mem;
+               exif_mem_free (mem, content->entries);
+               exif_mem_free (mem, content->priv);
+               exif_mem_free (mem, content);
+               exif_mem_unref (mem);
+       }
 }
 
 void
@@ -103,37 +125,36 @@ exif_content_dump (ExifContent *content, unsigned int indent)
 void
 exif_content_add_entry (ExifContent *content, ExifEntry *entry)
 {
-       if (entry->parent)
-               return;
+       if (!content || !content->priv || !entry || entry->parent) return;
 
        entry->parent = content;
-       content->entries = realloc (content->entries,
-                                   sizeof (ExifEntry) * (content->count + 1));
+       content->entries = exif_mem_realloc (content->priv->mem,
+               content->entries, sizeof (ExifEntry) * (content->count + 1));
+       if (!content->entries) return;
        content->entries[content->count] = entry;
        exif_entry_ref (entry);
        content->count++;
 }
 
 void
-exif_content_remove_entry (ExifContent *content, ExifEntry *entry)
+exif_content_remove_entry (ExifContent *c, ExifEntry *e)
 {
        unsigned int i;
 
-       if (entry->parent != content)
-               return;
-
-       for (i = 0; i < content->count; i++)
-               if (content->entries[i] == entry)
-                       break;
-       if (i == content->count)
-               return;
+       if (!c || !c->priv || !e || (e->parent != c)) return;
 
-       memmove (&content->entries[i], &content->entries[i + 1],
-                sizeof (ExifEntry) * (content->count - i - 1));
-       content->count--;
+       /* Search the entry */
+       for (i = 0; i < c->count; i++) if (c->entries[i] == e) break;
+       if (i == c->count) return;
 
-       entry->parent = NULL;
-       exif_entry_unref (entry);
+       /* Remove the entry */
+       memmove (&c->entries[i], &c->entries[i + 1],
+                sizeof (ExifEntry) * (c->count - i - 1));
+       c->count--;
+       e->parent = NULL;
+       exif_entry_unref (e);
+       c->entries = exif_mem_realloc (c->priv->mem, c->entries,
+                                       sizeof(ExifEntry) * c->count);
 }
 
 ExifEntry *