home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / libgimp / gimpparasite.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-06  |  5.3 KB  |  243 lines

  1. /* LIBGIMP - The GIMP Library 
  2.  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
  3.  *
  4.  * gimpparasite.c
  5.  * Copyright (C) 1998 Jay Cox <jaycox@earthlink.net>
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Library General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the
  19.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20.  * Boston, MA 02111-1307, USA.
  21.  */
  22. #include "config.h"
  23.  
  24. #include <stdio.h>
  25. #ifdef HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #include <string.h>
  29. #include <glib.h>
  30.  
  31. #ifdef G_OS_WIN32
  32. #include <process.h>        /* For _getpid() */
  33. #endif
  34.  
  35. #include "gimpparasite.h"
  36. #include "gimpparasite_pdb.h"
  37.  
  38.  
  39. #ifdef DEBUG
  40. static void 
  41. gimp_parasite_print (GimpParasite *parasite)
  42. {
  43.   if (parasite == NULL)
  44.     {
  45.       g_print ("pid %d: attempt to print a null parasite\n", getpid ());
  46.       return;
  47.     }
  48.  
  49.   g_print ("pid %d: parasite: %p\n", getpid (), parasite);
  50.  
  51.   if (parasite->name)
  52.     g_print ("\tname: %s\n", parasite->name);
  53.   else
  54.     g_print ("\tname: NULL\n");
  55.  
  56.   g_print ("\tflags: %d\n", parasite->flags);
  57.   g_print ("\tsize: %d\n", parasite->size);
  58.   if (parasite->size > 0)
  59.     g_print ("\tdata: %p\n", parasite->data);
  60. }
  61. #endif
  62.  
  63. GimpParasite *
  64. gimp_parasite_new (const gchar    *name, 
  65.            guint32         flags,
  66.            guint32         size, 
  67.            const gpointer  data)
  68. {
  69.   GimpParasite *parasite;
  70.  
  71.   if (!name)
  72.     return NULL;
  73.  
  74.   parasite = g_new (GimpParasite, 1);
  75.   parasite->name  = g_strdup (name);
  76.   parasite->flags = (flags & 0xFF);
  77.   parasite->size  = size;
  78.   if (size)
  79.     parasite->data = g_memdup (data, size);
  80.   else
  81.     parasite->data = NULL;
  82.  
  83.   return parasite;
  84. }
  85.  
  86. void
  87. gimp_parasite_free (GimpParasite *parasite)
  88. {
  89.   if (parasite == NULL)
  90.     return;
  91.  
  92.   if (parasite->name)
  93.     g_free (parasite->name);
  94.   if (parasite->data)
  95.     g_free (parasite->data);
  96.  
  97.   g_free (parasite);
  98. }
  99.  
  100. gboolean
  101. gimp_parasite_is_type (const GimpParasite *parasite, 
  102.                const gchar        *name)
  103. {
  104.   if (!parasite || !parasite->name)
  105.     return FALSE;
  106.  
  107.   return (strcmp (parasite->name, name) == 0);
  108. }
  109.  
  110. GimpParasite *
  111. gimp_parasite_copy (const GimpParasite *parasite)
  112. {
  113.   if (parasite == NULL)
  114.     return NULL;
  115.  
  116.   return gimp_parasite_new (parasite->name, parasite->flags,
  117.                 parasite->size, parasite->data);
  118. }
  119.  
  120. gboolean
  121. gimp_parasite_compare (const GimpParasite *a, 
  122.                const GimpParasite *b)
  123. {
  124.   if (a && b &&
  125.       a->name && b->name &&
  126.       strcmp (a->name, b->name) == 0 &&
  127.       a->flags == b->flags &&
  128.       a->size == b->size)
  129.     {
  130.       if (a->data == NULL && b->data == NULL)  
  131.     return TRUE;
  132.       else if (a->data && b->data && memcmp (a->data, b->data, a->size) == 0)
  133.     return TRUE;
  134.     }
  135.  
  136.   return FALSE;
  137. }
  138.  
  139. gulong
  140. gimp_parasite_flags (const GimpParasite *parasite)
  141. {
  142.   if (parasite == NULL)
  143.     return 0;
  144.  
  145.   return parasite->flags;
  146. }
  147.  
  148. gboolean
  149. gimp_parasite_is_persistent (const GimpParasite *parasite)
  150. {
  151.   if (parasite == NULL)
  152.     return FALSE;
  153.  
  154.   return (parasite->flags & GIMP_PARASITE_PERSISTENT);
  155. }
  156.  
  157. gboolean
  158. gimp_parasite_is_undoable (const GimpParasite *parasite)
  159. {
  160.   if (parasite == NULL)
  161.     return FALSE;
  162.  
  163.   return (parasite->flags & GIMP_PARASITE_UNDOABLE);
  164. }
  165.  
  166. gboolean
  167. gimp_parasite_has_flag (const GimpParasite *parasite, 
  168.             gulong              flag)
  169. {
  170.   if (parasite == NULL)
  171.     return FALSE;
  172.  
  173.   return (parasite->flags & flag);
  174. }
  175.  
  176. const gchar *
  177. gimp_parasite_name (const GimpParasite *parasite)
  178. {
  179.   if (parasite)
  180.     return parasite->name;
  181.  
  182.   return NULL;
  183. }
  184.  
  185. gpointer
  186. gimp_parasite_data (const GimpParasite *parasite)
  187. {
  188.   if (parasite)
  189.     return parasite->data;
  190.  
  191.   return NULL;
  192. }
  193.  
  194. glong 
  195. gimp_parasite_data_size (const GimpParasite *parasite)
  196. {
  197.   if (parasite)
  198.     return parasite->size;
  199.  
  200.   return 0;
  201. }
  202.  
  203. void
  204. gimp_attach_new_parasite (const gchar    *name, 
  205.               gint            flags,
  206.               gint            size, 
  207.               const gpointer  data)
  208. {
  209.   GimpParasite *parasite = gimp_parasite_new (name, flags, size, data);
  210.  
  211.   gimp_parasite_attach (parasite);
  212.  
  213.   gimp_parasite_free (parasite);
  214. }
  215.  
  216. void
  217. gimp_drawable_attach_new_parasite (gint32          drawable, 
  218.                    const gchar    *name, 
  219.                    gint            flags,
  220.                    gint            size, 
  221.                    const gpointer  data)
  222. {
  223.   GimpParasite *parasite = gimp_parasite_new (name, flags, size, data);
  224.  
  225.   gimp_drawable_parasite_attach (drawable, parasite);
  226.  
  227.   gimp_parasite_free (parasite);
  228. }
  229.  
  230. void
  231. gimp_image_attach_new_parasite (gint32          image_ID, 
  232.                 const gchar    *name, 
  233.                 gint            flags,
  234.                 gint            size, 
  235.                 const gpointer  data)
  236. {
  237.   GimpParasite *parasite = gimp_parasite_new (name, flags, size, data);
  238.  
  239.   gimp_image_parasite_attach (image_ID, parasite);
  240.  
  241.   gimp_parasite_free (parasite);
  242. }
  243.