/* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*               National Center for Biotechnology Information
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government have not placed any restriction on its use or reproduction.
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
*  Please cite the author in any work or product based on this material.
*
* ===========================================================================*/
#include <ncbi.h>
#include <gishlib.h>


void _cdecl
AnyDataAssign(any, usertype, datatype, ptr)
	AnyDataPtr	any;
	int			usertype;
	AnyType		datatype;
	VoidPtr		ptr;
{
	any->usertype = usertype;
	any->type = datatype;
	switch (datatype) {
	case ANY_CHAR:
		any->value.c = *(CharPtr)ptr;
		break;
	case ANY_UCHAR:
		any->value.uc = *(UcharPtr)ptr;
		break;
	case ANY_SHORT:
		any->value.s = *(short PNTR)ptr;
		break;
	case ANY_USHORT:
		any->value.us = *(unsigned short PNTR)ptr;
		break;
	case ANY_INT:
		any->value.i = *(int PNTR)ptr;
		break;
	case ANY_UINT:
		any->value.u = *(unsigned PNTR)ptr;
		break;
	case ANY_LONG:
		any->value.l = *(long PNTR)ptr;
		break;
	case ANY_ULONG:
		any->value.ul = *(unsigned long PNTR)ptr;
		break;
	case ANY_FLOAT:
		any->value.f = *(float PNTR)ptr;
		break;
	case ANY_DOUBLE:
		any->value.d = *(double PNTR)ptr;
		break;
	case ANY_CHARPTR:
		any->value.cp = (CharPtr)ptr;
		break;
	case ANY_CSTR:
		any->value.cstr = (CharPtr)ptr;
		break;
	case ANY_VOIDPTR:
		any->value.vp = (VoidPtr)ptr;
		break;
	case ANY_BUFFER:
		Nlm_MemCpy(&any->value.buf, ptr, sizeof(any->value.buf));
		break;
	default:
		break;
	}
}


int _cdecl
AnyDataCmp(any1, any2)
	AnyDataPtr	any1, any2;
{
	if (any1->type != any2->type) {
		if (any1->type > any2->type)
			return 1;
		return -1;
	}
	switch (any1->type) {
	case ANY_CHAR:
		return any1->value.c - any2->value.c;
	case ANY_UCHAR:
		if (any1->value.uc > any2->value.uc)
			return 1;
		if (any1->value.uc < any2->value.uc)
			return -1;
		return 0;
	case ANY_SHORT:
		if (any1->value.s > any2->value.s)
			return 1;
		if (any1->value.s < any2->value.s)
			return -1;
		return 0;
	case ANY_USHORT:
		if (any1->value.us > any2->value.us)
			return 1;
		if (any1->value.us < any2->value.us)
			return -1;
		return 0;
	case ANY_INT:
		return any1->value.i - any2->value.i;
	case ANY_UINT:
		if (any1->value.u > any2->value.u)
			return 1;
		if (any1->value.u < any2->value.u)
			return -1;
		return 0;
	case ANY_LONG:
		if (any1->value.l > any2->value.l)
			return 1;
		if (any1->value.l < any2->value.l)
			return -1;
		return 0;
	case ANY_ULONG:
		if (any1->value.ul > any2->value.ul)
			return 1;
		if (any1->value.ul < any2->value.ul)
			return -1;
		return 0;
	case ANY_FLOAT:
		if (any1->value.f > any2->value.f)
			return 1;
		if (any1->value.f < any2->value.f)
			return -1;
		return 0;
	case ANY_DOUBLE:
		if (any1->value.d > any2->value.d)
			return 1;
		if (any1->value.d < any2->value.d)
			return -1;
		return 0;
	case ANY_CHARPTR:
		if (any1->value.cp > any2->value.cp)
			return 1;
		if (any1->value.cp < any2->value.cp)
			return -1;
		return 0;
	case ANY_VOIDPTR:
		if (any1->value.vp > any2->value.vp)
			return 1;
		if (any1->value.vp < any2->value.vp)
			return -1;
		return 0;
	case ANY_CSTR:
		return strcmp(any1->value.cstr, any2->value.cstr);
	case ANY_BUFFER:
		{
			int	i; size_t	len;

			len = MIN(any1->value.buf.len, any2->value.buf.len);
			i = memcmp(any1->value.buf.ptr, any2->value.buf.ptr, len);
			if (i != 0)
				return i;
			if (any1->value.buf.len < any2->value.buf.len)
				return -1;
			if (any1->value.buf.len > any2->value.buf.len)
				return 1;
			return 0;
		}
	default:
		if (any1 > any2)
			return 1;
		if (any1 < any2)
			return -1;
		return 0;
	}
	/*NOTREACHED*/
}
