2003-12-05 23:55:15 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
2000-02-26 11:41:06 +08:00
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
|
|
|
*
|
2000-05-27 06:28:40 +08:00
|
|
|
* gimpparasite.c
|
2006-01-12 08:50:44 +08:00
|
|
|
* Copyright (C) 1998 Jay Cox <jaycox@gimp.org>
|
1998-10-08 16:15:21 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This library is free software: you can redistribute it and/or
|
1999-11-18 05:13:50 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
1998-10-08 16:15:21 +08:00
|
|
|
* License as published by the Free Software Foundation; either
|
2009-01-18 06:28:01 +08:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
1998-10-08 16:15:21 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
1999-11-18 05:13:50 +08:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-01-18 06:28:01 +08:00
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
1998-10-08 16:15:21 +08:00
|
|
|
*/
|
2000-12-29 23:22:01 +08:00
|
|
|
|
1999-03-07 20:56:03 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
1998-11-06 08:51:39 +08:00
|
|
|
#include <stdio.h>
|
1999-03-07 20:56:03 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1998-11-06 08:51:39 +08:00
|
|
|
#include <unistd.h>
|
1999-03-07 20:56:03 +08:00
|
|
|
#endif
|
1998-10-08 16:15:21 +08:00
|
|
|
#include <string.h>
|
2004-07-29 20:33:15 +08:00
|
|
|
|
|
|
|
#include <glib-object.h>
|
1998-10-08 16:15:21 +08:00
|
|
|
|
1999-10-05 03:26:07 +08:00
|
|
|
#ifdef G_OS_WIN32
|
2006-04-12 18:53:28 +08:00
|
|
|
#include <process.h> /* For _getpid() */
|
1999-03-07 20:56:03 +08:00
|
|
|
#endif
|
|
|
|
|
2001-05-21 21:58:46 +08:00
|
|
|
#include "gimpbasetypes.h"
|
2000-12-29 23:22:01 +08:00
|
|
|
|
2000-05-27 09:30:21 +08:00
|
|
|
#include "gimpparasite.h"
|
|
|
|
|
|
|
|
|
2010-06-30 01:13:40 +08:00
|
|
|
/**
|
|
|
|
* SECTION: gimpparasite
|
|
|
|
* @title: gimpparasite
|
|
|
|
* @short_description: Arbitrary pieces of data which can be attached
|
|
|
|
* to various GIMP objects.
|
|
|
|
* @see_also: gimp_image_parasite_attach(),
|
|
|
|
* gimp_drawable_parasite_attach(), gimp_parasite_attach()
|
|
|
|
* and their related functions.
|
|
|
|
*
|
|
|
|
* Arbitrary pieces of data which can be attached to various GIMP objects.
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
2006-04-02 17:39:46 +08:00
|
|
|
/*
|
|
|
|
* GIMP_TYPE_PARASITE
|
|
|
|
*/
|
|
|
|
|
|
|
|
GType
|
|
|
|
gimp_parasite_get_type (void)
|
|
|
|
{
|
|
|
|
static GType type = 0;
|
|
|
|
|
|
|
|
if (! type)
|
|
|
|
type = g_boxed_type_register_static ("GimpParasite",
|
|
|
|
(GBoxedCopyFunc) gimp_parasite_copy,
|
|
|
|
(GBoxedFreeFunc) gimp_parasite_free);
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GIMP_TYPE_PARAM_PARASITE
|
|
|
|
*/
|
|
|
|
|
2006-06-02 20:53:06 +08:00
|
|
|
#define GIMP_PARAM_SPEC_PARASITE(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), GIMP_TYPE_PARAM_PARASITE, GimpParamSpecParasite))
|
|
|
|
|
|
|
|
typedef struct _GimpParamSpecParasite GimpParamSpecParasite;
|
|
|
|
|
|
|
|
struct _GimpParamSpecParasite
|
|
|
|
{
|
|
|
|
GParamSpecBoxed parent_instance;
|
|
|
|
};
|
|
|
|
|
2006-04-02 17:39:46 +08:00
|
|
|
static void gimp_param_parasite_class_init (GParamSpecClass *class);
|
|
|
|
static void gimp_param_parasite_init (GParamSpec *pspec);
|
|
|
|
static gboolean gimp_param_parasite_validate (GParamSpec *pspec,
|
|
|
|
GValue *value);
|
|
|
|
static gint gimp_param_parasite_values_cmp (GParamSpec *pspec,
|
|
|
|
const GValue *value1,
|
|
|
|
const GValue *value2);
|
|
|
|
|
|
|
|
GType
|
|
|
|
gimp_param_parasite_get_type (void)
|
|
|
|
{
|
|
|
|
static GType type = 0;
|
|
|
|
|
|
|
|
if (! type)
|
|
|
|
{
|
2006-10-18 21:17:50 +08:00
|
|
|
const GTypeInfo type_info =
|
2006-04-02 17:39:46 +08:00
|
|
|
{
|
|
|
|
sizeof (GParamSpecClass),
|
|
|
|
NULL, NULL,
|
|
|
|
(GClassInitFunc) gimp_param_parasite_class_init,
|
|
|
|
NULL, NULL,
|
|
|
|
sizeof (GimpParamSpecParasite),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gimp_param_parasite_init
|
|
|
|
};
|
|
|
|
|
|
|
|
type = g_type_register_static (G_TYPE_PARAM_BOXED,
|
|
|
|
"GimpParamParasite",
|
|
|
|
&type_info, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_param_parasite_class_init (GParamSpecClass *class)
|
|
|
|
{
|
|
|
|
class->value_type = GIMP_TYPE_PARASITE;
|
|
|
|
class->value_validate = gimp_param_parasite_validate;
|
|
|
|
class->values_cmp = gimp_param_parasite_values_cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_param_parasite_init (GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gimp_param_parasite_validate (GParamSpec *pspec,
|
|
|
|
GValue *value)
|
|
|
|
{
|
|
|
|
GimpParasite *parasite = value->data[0].v_pointer;
|
|
|
|
|
|
|
|
if (! parasite)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (parasite->name == NULL ||
|
2014-03-17 16:34:56 +08:00
|
|
|
*parasite->name == '\0' ||
|
2006-04-02 17:39:46 +08:00
|
|
|
! g_utf8_validate (parasite->name, -1, NULL) ||
|
|
|
|
(parasite->size == 0 && parasite->data != NULL) ||
|
|
|
|
(parasite->size > 0 && parasite->data == NULL))
|
|
|
|
{
|
|
|
|
g_value_set_boxed (value, NULL);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
gimp_param_parasite_values_cmp (GParamSpec *pspec,
|
|
|
|
const GValue *value1,
|
|
|
|
const GValue *value2)
|
|
|
|
{
|
|
|
|
GimpParasite *parasite1 = value1->data[0].v_pointer;
|
|
|
|
GimpParasite *parasite2 = value2->data[0].v_pointer;
|
|
|
|
|
|
|
|
/* try to return at least *something*, it's useless anyway... */
|
|
|
|
|
|
|
|
if (! parasite1)
|
|
|
|
return parasite2 != NULL ? -1 : 0;
|
|
|
|
else if (! parasite2)
|
|
|
|
return parasite1 != NULL;
|
|
|
|
else
|
|
|
|
return gimp_parasite_compare (parasite1, parasite2);
|
|
|
|
}
|
|
|
|
|
|
|
|
GParamSpec *
|
|
|
|
gimp_param_spec_parasite (const gchar *name,
|
|
|
|
const gchar *nick,
|
|
|
|
const gchar *blurb,
|
|
|
|
GParamFlags flags)
|
|
|
|
{
|
|
|
|
GimpParamSpecParasite *parasite_spec;
|
|
|
|
|
|
|
|
parasite_spec = g_param_spec_internal (GIMP_TYPE_PARAM_PARASITE,
|
|
|
|
name, nick, blurb, flags);
|
|
|
|
|
|
|
|
return G_PARAM_SPEC (parasite_spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-22 14:03:53 +08:00
|
|
|
#ifdef DEBUG
|
2003-12-05 23:55:15 +08:00
|
|
|
static void
|
2000-05-27 06:28:40 +08:00
|
|
|
gimp_parasite_print (GimpParasite *parasite)
|
1998-10-30 18:21:33 +08:00
|
|
|
{
|
2000-05-27 06:28:40 +08:00
|
|
|
if (parasite == NULL)
|
2000-02-12 10:16:59 +08:00
|
|
|
{
|
2000-05-27 06:28:40 +08:00
|
|
|
g_print ("pid %d: attempt to print a null parasite\n", getpid ());
|
2000-02-12 10:16:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2000-05-27 06:28:40 +08:00
|
|
|
|
|
|
|
g_print ("pid %d: parasite: %p\n", getpid (), parasite);
|
|
|
|
|
|
|
|
if (parasite->name)
|
|
|
|
g_print ("\tname: %s\n", parasite->name);
|
1998-10-30 18:21:33 +08:00
|
|
|
else
|
2000-05-27 06:28:40 +08:00
|
|
|
g_print ("\tname: NULL\n");
|
|
|
|
|
|
|
|
g_print ("\tflags: %d\n", parasite->flags);
|
|
|
|
g_print ("\tsize: %d\n", parasite->size);
|
|
|
|
if (parasite->size > 0)
|
|
|
|
g_print ("\tdata: %p\n", parasite->data);
|
1998-10-30 18:21:33 +08:00
|
|
|
}
|
1998-12-22 14:03:53 +08:00
|
|
|
#endif
|
1998-10-30 18:21:33 +08:00
|
|
|
|
2000-05-27 06:28:40 +08:00
|
|
|
GimpParasite *
|
2003-12-05 23:55:15 +08:00
|
|
|
gimp_parasite_new (const gchar *name,
|
2006-04-12 18:53:28 +08:00
|
|
|
guint32 flags,
|
|
|
|
guint32 size,
|
|
|
|
gconstpointer data)
|
1998-10-08 16:15:21 +08:00
|
|
|
{
|
2000-05-27 06:28:40 +08:00
|
|
|
GimpParasite *parasite;
|
|
|
|
|
2014-03-17 16:34:56 +08:00
|
|
|
if (! (name && *name))
|
2000-05-27 06:28:40 +08:00
|
|
|
return NULL;
|
|
|
|
|
2007-05-25 19:31:18 +08:00
|
|
|
parasite = g_slice_new (GimpParasite);
|
2000-05-27 06:28:40 +08:00
|
|
|
parasite->name = g_strdup (name);
|
|
|
|
parasite->flags = (flags & 0xFF);
|
|
|
|
parasite->size = size;
|
2007-05-25 19:31:18 +08:00
|
|
|
|
1998-10-08 16:15:21 +08:00
|
|
|
if (size)
|
2000-05-27 06:28:40 +08:00
|
|
|
parasite->data = g_memdup (data, size);
|
1998-10-08 16:15:21 +08:00
|
|
|
else
|
2000-05-27 06:28:40 +08:00
|
|
|
parasite->data = NULL;
|
|
|
|
|
|
|
|
return parasite;
|
1998-10-08 16:15:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-05-27 06:28:40 +08:00
|
|
|
gimp_parasite_free (GimpParasite *parasite)
|
1998-10-08 16:15:21 +08:00
|
|
|
{
|
1998-11-13 12:00:54 +08:00
|
|
|
if (parasite == NULL)
|
|
|
|
return;
|
2000-05-27 06:28:40 +08:00
|
|
|
|
1998-10-14 10:54:02 +08:00
|
|
|
if (parasite->name)
|
2000-01-11 07:27:25 +08:00
|
|
|
g_free (parasite->name);
|
2007-05-25 19:31:18 +08:00
|
|
|
|
1998-10-08 16:15:21 +08:00
|
|
|
if (parasite->data)
|
2000-01-11 07:27:25 +08:00
|
|
|
g_free (parasite->data);
|
2000-05-27 06:28:40 +08:00
|
|
|
|
2007-05-25 19:31:18 +08:00
|
|
|
g_slice_free (GimpParasite, parasite);
|
1998-10-08 16:15:21 +08:00
|
|
|
}
|
|
|
|
|
2000-05-27 06:28:40 +08:00
|
|
|
gboolean
|
2003-12-05 23:55:15 +08:00
|
|
|
gimp_parasite_is_type (const GimpParasite *parasite,
|
2006-04-12 18:53:28 +08:00
|
|
|
const gchar *name)
|
1998-10-08 16:15:21 +08:00
|
|
|
{
|
1998-10-30 18:21:33 +08:00
|
|
|
if (!parasite || !parasite->name)
|
1998-10-08 16:15:21 +08:00
|
|
|
return FALSE;
|
2000-05-27 06:28:40 +08:00
|
|
|
|
|
|
|
return (strcmp (parasite->name, name) == 0);
|
1998-10-08 16:15:21 +08:00
|
|
|
}
|
|
|
|
|
2000-05-27 06:28:40 +08:00
|
|
|
GimpParasite *
|
|
|
|
gimp_parasite_copy (const GimpParasite *parasite)
|
1998-10-08 16:15:21 +08:00
|
|
|
{
|
|
|
|
if (parasite == NULL)
|
|
|
|
return NULL;
|
2000-05-27 06:28:40 +08:00
|
|
|
|
|
|
|
return gimp_parasite_new (parasite->name, parasite->flags,
|
2006-04-12 18:53:28 +08:00
|
|
|
parasite->size, parasite->data);
|
1998-10-08 16:15:21 +08:00
|
|
|
}
|
|
|
|
|
2000-02-12 10:16:59 +08:00
|
|
|
gboolean
|
2003-12-05 23:55:15 +08:00
|
|
|
gimp_parasite_compare (const GimpParasite *a,
|
2006-04-12 18:53:28 +08:00
|
|
|
const GimpParasite *b)
|
1999-02-16 16:53:54 +08:00
|
|
|
{
|
2000-05-27 06:28:40 +08:00
|
|
|
if (a && b &&
|
|
|
|
a->name && b->name &&
|
|
|
|
strcmp (a->name, b->name) == 0 &&
|
|
|
|
a->flags == b->flags &&
|
|
|
|
a->size == b->size)
|
1999-03-20 12:41:59 +08:00
|
|
|
{
|
2003-12-05 23:55:15 +08:00
|
|
|
if (a->data == NULL && b->data == NULL)
|
2006-04-12 18:53:28 +08:00
|
|
|
return TRUE;
|
2000-05-27 06:28:40 +08:00
|
|
|
else if (a->data && b->data && memcmp (a->data, b->data, a->size) == 0)
|
2006-04-12 18:53:28 +08:00
|
|
|
return TRUE;
|
1999-03-20 12:41:59 +08:00
|
|
|
}
|
2000-05-27 06:28:40 +08:00
|
|
|
|
1999-02-16 16:53:54 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
1999-04-12 06:28:51 +08:00
|
|
|
gulong
|
2000-05-27 06:28:40 +08:00
|
|
|
gimp_parasite_flags (const GimpParasite *parasite)
|
1999-04-12 06:28:51 +08:00
|
|
|
{
|
2000-05-27 06:28:40 +08:00
|
|
|
if (parasite == NULL)
|
1999-04-12 06:28:51 +08:00
|
|
|
return 0;
|
2000-05-27 06:28:40 +08:00
|
|
|
|
|
|
|
return parasite->flags;
|
1999-04-12 06:28:51 +08:00
|
|
|
}
|
|
|
|
|
2000-02-12 10:16:59 +08:00
|
|
|
gboolean
|
2000-05-27 06:28:40 +08:00
|
|
|
gimp_parasite_is_persistent (const GimpParasite *parasite)
|
1998-10-08 16:15:21 +08:00
|
|
|
{
|
2000-05-27 06:28:40 +08:00
|
|
|
if (parasite == NULL)
|
1998-10-08 16:15:21 +08:00
|
|
|
return FALSE;
|
2000-05-27 06:28:40 +08:00
|
|
|
|
|
|
|
return (parasite->flags & GIMP_PARASITE_PERSISTENT);
|
1998-12-16 19:23:30 +08:00
|
|
|
}
|
|
|
|
|
2000-02-12 10:16:59 +08:00
|
|
|
gboolean
|
2000-05-27 06:28:40 +08:00
|
|
|
gimp_parasite_is_undoable (const GimpParasite *parasite)
|
1999-04-23 14:07:09 +08:00
|
|
|
{
|
2000-05-27 06:28:40 +08:00
|
|
|
if (parasite == NULL)
|
1999-04-23 14:07:09 +08:00
|
|
|
return FALSE;
|
2000-05-27 06:28:40 +08:00
|
|
|
|
|
|
|
return (parasite->flags & GIMP_PARASITE_UNDOABLE);
|
1999-04-23 14:07:09 +08:00
|
|
|
}
|
|
|
|
|
2000-02-12 10:16:59 +08:00
|
|
|
gboolean
|
2003-12-05 23:55:15 +08:00
|
|
|
gimp_parasite_has_flag (const GimpParasite *parasite,
|
2006-04-12 18:53:28 +08:00
|
|
|
gulong flag)
|
1998-12-16 19:23:30 +08:00
|
|
|
{
|
2000-05-27 06:28:40 +08:00
|
|
|
if (parasite == NULL)
|
1998-12-16 19:23:30 +08:00
|
|
|
return FALSE;
|
2000-05-27 06:28:40 +08:00
|
|
|
|
|
|
|
return (parasite->flags & flag);
|
1998-12-16 19:23:30 +08:00
|
|
|
}
|
|
|
|
|
2000-02-12 10:16:59 +08:00
|
|
|
const gchar *
|
2000-05-27 06:28:40 +08:00
|
|
|
gimp_parasite_name (const GimpParasite *parasite)
|
1999-02-16 16:53:54 +08:00
|
|
|
{
|
2000-05-27 06:28:40 +08:00
|
|
|
if (parasite)
|
|
|
|
return parasite->name;
|
|
|
|
|
1999-02-16 16:53:54 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-06-24 06:02:56 +08:00
|
|
|
gconstpointer
|
2000-05-27 06:28:40 +08:00
|
|
|
gimp_parasite_data (const GimpParasite *parasite)
|
1998-12-16 19:23:30 +08:00
|
|
|
{
|
2000-05-27 06:28:40 +08:00
|
|
|
if (parasite)
|
|
|
|
return parasite->data;
|
|
|
|
|
1998-12-16 19:23:30 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-12-05 23:55:15 +08:00
|
|
|
glong
|
2000-05-27 06:28:40 +08:00
|
|
|
gimp_parasite_data_size (const GimpParasite *parasite)
|
1998-12-16 19:23:30 +08:00
|
|
|
{
|
2000-05-27 06:28:40 +08:00
|
|
|
if (parasite)
|
|
|
|
return parasite->size;
|
|
|
|
|
1998-12-16 19:23:30 +08:00
|
|
|
return 0;
|
1998-10-08 16:15:21 +08:00
|
|
|
}
|