Blame src/fcobjs.c

Packit 352660
/*
Packit 352660
 * fontconfig/src/fclist.c
Packit 352660
 *
Packit 352660
 * Copyright © 2000 Keith Packard
Packit 352660
 *
Packit 352660
 * Permission to use, copy, modify, distribute, and sell this software and its
Packit 352660
 * documentation for any purpose is hereby granted without fee, provided that
Packit 352660
 * the above copyright notice appear in all copies and that both that
Packit 352660
 * copyright notice and this permission notice appear in supporting
Packit 352660
 * documentation, and that the name of the author(s) not be used in
Packit 352660
 * advertising or publicity pertaining to distribution of the software without
Packit 352660
 * specific, written prior permission.  The authors make no
Packit 352660
 * representations about the suitability of this software for any purpose.  It
Packit 352660
 * is provided "as is" without express or implied warranty.
Packit 352660
 *
Packit 352660
 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
Packit 352660
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
Packit 352660
 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
Packit 352660
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
Packit 352660
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
Packit 352660
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
Packit 352660
 * PERFORMANCE OF THIS SOFTWARE.
Packit 352660
 */
Packit 352660
Packit 352660
#include "fcint.h"
Packit 352660
Packit 352660
static unsigned int
Packit 352660
FcObjectTypeHash (register const char *str, register FC_GPERF_SIZE_T len);
Packit 352660
Packit 352660
static const struct FcObjectTypeInfo *
Packit 352660
FcObjectTypeLookup (register const char *str, register FC_GPERF_SIZE_T len);
Packit 352660
Packit 352660
#include "fcobjshash.h"
Packit 352660
Packit 352660
#include <string.h>
Packit 352660
Packit 352660
/* The 1000 is to leave some room for future added internal objects, such
Packit 352660
 * that caches from newer fontconfig can still be used with older fontconfig
Packit 352660
 * without getting confused. */
Packit 352660
static fc_atomic_int_t next_id = FC_MAX_BASE_OBJECT + FC_EXT_OBJ_INDEX;
Packit 352660
struct FcObjectOtherTypeInfo {
Packit 352660
    struct FcObjectOtherTypeInfo *next;
Packit 352660
    FcObjectType object;
Packit 352660
    FcObject id;
Packit 352660
} *other_types;
Packit 352660
Packit 352660
void
Packit 352660
FcObjectFini (void)
Packit 352660
{
Packit 352660
    struct FcObjectOtherTypeInfo *ots, *ot;
Packit 352660
Packit 352660
retry:
Packit 352660
    ots = fc_atomic_ptr_get (&other_types);
Packit 352660
    if (!ots)
Packit 352660
	return;
Packit 352660
    if (!fc_atomic_ptr_cmpexch (&other_types, ots, NULL))
Packit 352660
	goto retry;
Packit 352660
Packit 352660
    while (ots)
Packit 352660
    {
Packit 352660
	ot = ots->next;
Packit 352660
	if (ots->object.object)
Packit 352660
	    free (ots->object.object);
Packit 352660
	free (ots);
Packit 352660
	ots = ot;
Packit 352660
    }
Packit 352660
}
Packit 352660
Packit 352660
static FcObjectType *
Packit 352660
_FcObjectLookupOtherTypeByName (const char *str, FcObject *id)
Packit 352660
{
Packit 352660
    struct FcObjectOtherTypeInfo *ots, *ot;
Packit 352660
Packit 352660
retry:
Packit 352660
    ots = fc_atomic_ptr_get (&other_types);
Packit 352660
Packit 352660
    for (ot = ots; ot; ot = ot->next)
Packit 352660
	if (0 == strcmp (ot->object.object, str))
Packit 352660
	    break;
Packit 352660
Packit 352660
    if (!ot)
Packit 352660
    {
Packit 352660
	ot = malloc (sizeof (*ot));
Packit 352660
	if (!ot)
Packit 352660
	    return NULL;
Packit 352660
Packit 352660
	ot->object.object = (char *) FcStrdup (str);
Packit 352660
	ot->object.type = FcTypeUnknown;
Packit 352660
	ot->id = fc_atomic_int_add (next_id, +1);
Packit 352660
	if (ot->id < (FC_MAX_BASE_OBJECT + FC_EXT_OBJ_INDEX))
Packit 352660
	{
Packit 352660
	    fprintf (stderr, "Fontconfig error: No object ID to assign\n");
Packit 352660
	    abort ();
Packit 352660
	}
Packit 352660
	ot->next = ots;
Packit 352660
Packit 352660
	if (!fc_atomic_ptr_cmpexch (&other_types, ots, ot)) {
Packit 352660
	    if (ot->object.object)
Packit 352660
		free (ot->object.object);
Packit 352660
	    free (ot);
Packit 352660
	    goto retry;
Packit 352660
	}
Packit 352660
    }
Packit 352660
Packit 352660
    if (id)
Packit 352660
      *id = ot->id;
Packit 352660
Packit 352660
    return &ot->object;
Packit 352660
}
Packit 352660
Packit 352660
FcObject
Packit 352660
FcObjectLookupBuiltinIdByName (const char *str)
Packit 352660
{
Packit 352660
    const struct FcObjectTypeInfo *o = FcObjectTypeLookup (str, strlen (str));
Packit 352660
Packit 352660
    if (o)
Packit 352660
	return o->id;
Packit 352660
Packit 352660
    return 0;
Packit 352660
}
Packit 352660
Packit 352660
FcObject
Packit 352660
FcObjectLookupIdByName (const char *str)
Packit 352660
{
Packit 352660
    const struct FcObjectTypeInfo *o = FcObjectTypeLookup (str, strlen (str));
Packit 352660
    FcObject id;
Packit 352660
    if (o)
Packit 352660
	return o->id;
Packit 352660
Packit 352660
    if (_FcObjectLookupOtherTypeByName (str, &id))
Packit 352660
	return id;
Packit 352660
Packit 352660
    return 0;
Packit 352660
}
Packit 352660
Packit 352660
const char *
Packit 352660
FcObjectLookupOtherNameById (FcObject id)
Packit 352660
{
Packit 352660
    struct FcObjectOtherTypeInfo *ot;
Packit 352660
Packit 352660
    for (ot = fc_atomic_ptr_get (&other_types); ot; ot = ot->next)
Packit 352660
	if (ot->id == id)
Packit 352660
	    return ot->object.object;
Packit 352660
Packit 352660
    return NULL;
Packit 352660
}
Packit 352660
Packit 352660
const FcObjectType *
Packit 352660
FcObjectLookupOtherTypeByName (const char *str)
Packit 352660
{
Packit 352660
    return _FcObjectLookupOtherTypeByName (str, NULL);
Packit 352660
}
Packit 352660
Packit 352660
FcPrivate const FcObjectType *
Packit 352660
FcObjectLookupOtherTypeById (FcObject id)
Packit 352660
{
Packit 352660
    struct FcObjectOtherTypeInfo *ot;
Packit 352660
Packit 352660
    for (ot = fc_atomic_ptr_get (&other_types); ot; ot = ot->next)
Packit 352660
	if (ot->id == id)
Packit 352660
	    return &ot->object;
Packit 352660
Packit 352660
    return NULL;
Packit 352660
}
Packit 352660
Packit 352660
Packit 352660
#include "fcaliastail.h"
Packit 352660
#undef __fcobjs__