gimpparasite.c 8.15 KB
Newer Older
1
/* LIBGIMP - The GIMP Library
2 3
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
4
 * gimpparasite.c
5
 * Copyright (C) 1998 Jay Cox <jaycox@gimp.org>
6
 *
7
 * This library is free software: you can redistribute it and/or
Marc Lehmann's avatar
Marc Lehmann committed
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 3 of the License, or (at your option) any later version.
11 12 13 14 15 16
 *
 * 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.
 *
Marc Lehmann's avatar
Marc Lehmann committed
17
 * You should have received a copy of the GNU Lesser General Public
18 19
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
20
 */
21

22 23
#include "config.h"

24
#include <stdio.h>
25
#ifdef HAVE_UNISTD_H
26
#include <unistd.h>
27
#endif
28
#include <string.h>
29 30

#include <glib-object.h>
31

32
#ifdef G_OS_WIN32
Sven Neumann's avatar
Sven Neumann committed
33
#include <process.h>                /* For _getpid() */
34 35
#endif

36
#include "gimpbasetypes.h"
37

38 39 40
#include "gimpparasite.h"


41 42 43 44 45 46 47 48 49 50 51 52 53
/**
 * 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.
 **/


54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/*
 * 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
 */

76 77 78 79 80 81 82 83 84
#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;
};

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
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)
    {
100
      const GTypeInfo type_info =
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
      {
        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                          ||
143
           *parasite->name == '\0'                         ||
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
           ! 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);
}


Manish Singh's avatar
Manish Singh committed
188
#ifdef DEBUG
189
static void
190
gimp_parasite_print (GimpParasite *parasite)
191
{
192
  if (parasite == NULL)
193
    {
194
      g_print ("pid %d: attempt to print a null parasite\n", getpid ());
195 196
      return;
    }
197 198 199 200 201

  g_print ("pid %d: parasite: %p\n", getpid (), parasite);

  if (parasite->name)
    g_print ("\tname: %s\n", parasite->name);
202
  else
203 204 205 206 207 208
    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);
209
}
Manish Singh's avatar
Manish Singh committed
210
#endif
211

212
GimpParasite *
213
gimp_parasite_new (const gchar    *name,
Sven Neumann's avatar
Sven Neumann committed
214 215 216
                   guint32         flags,
                   guint32         size,
                   gconstpointer   data)
217
{
218 219
  GimpParasite *parasite;

220
  if (! (name && *name))
221 222
    return NULL;

223
  parasite = g_slice_new (GimpParasite);
224 225 226
  parasite->name  = g_strdup (name);
  parasite->flags = (flags & 0xFF);
  parasite->size  = size;
227

228
  if (size)
229
    parasite->data = g_memdup (data, size);
230
  else
231 232 233
    parasite->data = NULL;

  return parasite;
234 235 236
}

void
237
gimp_parasite_free (GimpParasite *parasite)
238
{
239 240
  if (parasite == NULL)
    return;
241

242
  if (parasite->name)
243
    g_free (parasite->name);
244

245
  if (parasite->data)
246
    g_free (parasite->data);
247

248
  g_slice_free (GimpParasite, parasite);
249 250
}

251
gboolean
252
gimp_parasite_is_type (const GimpParasite *parasite,
Sven Neumann's avatar
Sven Neumann committed
253
                       const gchar        *name)
254
{
255
  if (!parasite || !parasite->name)
256
    return FALSE;
257 258

  return (strcmp (parasite->name, name) == 0);
259 260
}

261 262
GimpParasite *
gimp_parasite_copy (const GimpParasite *parasite)
263 264 265
{
  if (parasite == NULL)
    return NULL;
266 267

  return gimp_parasite_new (parasite->name, parasite->flags,
Sven Neumann's avatar
Sven Neumann committed
268
                            parasite->size, parasite->data);
269 270
}

271
gboolean
272
gimp_parasite_compare (const GimpParasite *a,
Sven Neumann's avatar
Sven Neumann committed
273
                       const GimpParasite *b)
274
{
275 276 277 278 279
  if (a && b &&
      a->name && b->name &&
      strcmp (a->name, b->name) == 0 &&
      a->flags == b->flags &&
      a->size == b->size)
280
    {
281
      if (a->data == NULL && b->data == NULL)
Sven Neumann's avatar
Sven Neumann committed
282
        return TRUE;
283
      else if (a->data && b->data && memcmp (a->data, b->data, a->size) == 0)
Sven Neumann's avatar
Sven Neumann committed
284
        return TRUE;
285
    }
286

287 288 289
  return FALSE;
}

Marc Lehmann's avatar
Marc Lehmann committed
290
gulong
291
gimp_parasite_flags (const GimpParasite *parasite)
Marc Lehmann's avatar
Marc Lehmann committed
292
{
293
  if (parasite == NULL)
Marc Lehmann's avatar
Marc Lehmann committed
294
    return 0;
295 296

  return parasite->flags;
Marc Lehmann's avatar
Marc Lehmann committed
297 298
}

299
gboolean
300
gimp_parasite_is_persistent (const GimpParasite *parasite)
301
{
302
  if (parasite == NULL)
303
    return FALSE;
304 305

  return (parasite->flags & GIMP_PARASITE_PERSISTENT);
306 307
}

308
gboolean
309
gimp_parasite_is_undoable (const GimpParasite *parasite)
310
{
311
  if (parasite == NULL)
312
    return FALSE;
313 314

  return (parasite->flags & GIMP_PARASITE_UNDOABLE);
315 316
}

317
gboolean
318
gimp_parasite_has_flag (const GimpParasite *parasite,
Sven Neumann's avatar
Sven Neumann committed
319
                        gulong              flag)
320
{
321
  if (parasite == NULL)
322
    return FALSE;
323 324

  return (parasite->flags & flag);
325 326
}

327
const gchar *
328
gimp_parasite_name (const GimpParasite *parasite)
329
{
330 331 332
  if (parasite)
    return parasite->name;

333 334 335
  return NULL;
}

336
gconstpointer
337
gimp_parasite_data (const GimpParasite *parasite)
338
{
339 340 341
  if (parasite)
    return parasite->data;

342 343 344
  return NULL;
}

345
glong
346
gimp_parasite_data_size (const GimpParasite *parasite)
347
{
348 349 350
  if (parasite)
    return parasite->size;

351
  return 0;
352
}