/*	XDataUtil.h
 *
 *		The data manipulation utilities. I wrote my own because I'm
 *	one of those obnoxious types who likes to know what's going on.
 */

/*  YAAF - Yet another application framework
 *  Copyright (C) 1997, 1998 William Edward Woody
 *  
 *  This library is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Library
 *  General Public License as published by the Free Software
 *  Foundation; either version 2 of the License, or any
 *  later version.
 *  
 *  This library is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied
 *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
 *  PURPOSE. See the GNU Library General Public License for
 *  more details.
 *  
 *  You should have received a copy of the GNU Library General
 *  Public License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *  
 *  To contact the author, either e-mail me at woody@pandawave.com, 
 *	or write to
 *  
 *          William Edward Woody
 *          In Phase Consulting
 *          1545 Ard Eevin Avenue
 *          Glendale, CA 91202
 *
 *	Or visit us on the web at http://www.pandawave.com
 */

#ifndef PandaWave_XDATAUTIL
#define PandaWave_XDATAUTIL

#include <XConfig.h>
#include <stdarg.h>

#ifdef __cplusplus
extern "C" {
#endif


/************************************************************************/
/*																		*/
/*	short/long compression												*/
/*																		*/
/************************************************************************/

/*	@F"MAKEDWORD"
 *	@U"MAKEDWORD(highword,loword)"
 *	@H"XDataUtil.h"
 *	@I
 *		This macro concatenates two word-sized objects into a long-word
 *	sized object.
 */

#define MAKEDWORD(x,y)	(((unsigned long)(unsigned short)(x)) << 16 |	\
						 ((unsigned long)(unsigned short)(y)))

/*	@F"GETLOWORD"
 *	@U"GETLOWORD(longword)"
 *	@H"XDataUtil.h"
 *	@I
 *		This macro returns the low order word from a long-word, and is
 *	the inverse of the MAKEDWORD macro.
 */

#define GETLOWORD(x)	((unsigned short)((x) & 0xFFFF))


/*	@F"GETHIWORD"
 *	@U"GETHIWORD(longword)"
 *	@H"XDataUtil.h"
 *	@I
 *		This macro returns the high order word from a long-word, and is
 *	the inverse of the MAKEDWORD macro.
 */

#define GETHIWORD(x)	((unsigned short)((x) >> 16))

/************************************************************************/
/*																		*/
/*	String Conversion													*/
/*																		*/
/************************************************************************/

extern unsigned char	*cvtc2p(unsigned char *, const char *);
extern char				*cvtp2c(char *, const unsigned char *);

/************************************************************************/
/*																		*/
/*	Pascal string manipulation											*/
/*																		*/
/************************************************************************/

extern unsigned char	*pstrcpy(unsigned char *, const unsigned char *);
extern unsigned char	*pstrcat(unsigned char *, const unsigned char *);
extern long				pstrcmp(const unsigned char *, const unsigned char *);

/************************************************************************/
/*																		*/
/*	Endian byte order utilities											*/
/*																		*/
/************************************************************************/

/*
 *	Byte order swap
 */

extern unsigned short	SwapWord(unsigned short);
extern unsigned long	SwapLong(unsigned long);
extern float			SwapFloat(float);
extern double			SwapDouble(double);


/*
 *	Conversion to/from network order
 */

#if OPT_NATIVEORDER == 1

/*	@F"ESwapWord"
 *	@U"ESwapWord(word)"
 *	@H"XDataUtil.h"
 *	@I
 *		This macro swaps the word order for a non-network order word
 *	to the native order of the target environment. Depending on the
 *	setting of OPT_NATIVEORDER, this either swaps or does not swap
 *	the bytes in word.
 */

#define	ESwapWord(x)	SwapWord(x)

/*	@F"ESwapLong"
 *	@U"ESwapLong(word)"
 *	@H"XDataUtil.h"
 *	@I
 *		This macro swaps the word order for a non-network order word
 *	to the native order of the target environment. Depending on the
 *	setting of OPT_NATIVEORDER, this either swaps or does not swap
 *	the bytes in word.
 */

#define	ESwapLong(x)	SwapLong(x)

/*	@F"ESwapFloat"
 *	@U"ESwapFloat(word)"
 *	@H"XDataUtil.h"
 *	@I
 *		This macro swaps the word order for a non-network order word
 *	to the native order of the target environment. Depending on the
 *	setting of OPT_NATIVEORDER, this either swaps or does not swap
 *	the bytes in word.
 * 
 *		This is reliant on the size of a float being 4 bytes. This
 *	also relies on the double being in IEEE format; use with caution.
 */

#define	ESwapFloat(x)	SwapFloat(x)

/*	@F"ESwapDouble"
 *	@U"ESwapDouble(word)"
 *	@H"XDataUtil.h"
 *	@I
 *		This macro swaps the word order for a non-network order word
 *	to the native order of the target environment. Depending on the
 *	setting of OPT_NATIVEORDER, this either swaps or does not swap
 *	the bytes in word.
 *
 *		This is reliant on the size of a double being 8 bytes. This
 *	also relies on the double being in IEEE format; use with caution.
 */

#define	ESwapDouble(x)	SwapDouble(x)

/*	@F"NSwapWord"
 *	@U"NSwapWord(word)"
 *	@H"XDataUtil.h"
 *	@I
 *		This macro swaps the word order for a network order word
 *	to the native order of the target environment. Depending on the
 *	setting of OPT_NATIVEORDER, this either swaps or does not swap
 *	the bytes in word.
 */

#define	NSwapWord(x)	(x)

/*	@F"NSwapLong"
 *	@U"NSwapLong(word)"
 *	@H"XDataUtil.h"
 *	@I
 *		This macro swaps the word order for a network order word
 *	to the native order of the target environment. Depending on the
 *	setting of OPT_NATIVEORDER, this either swaps or does not swap
 *	the bytes in word.
 */

#define	NSwapLong(x)	(x)

/*	@F"NSwapFloat"
 *	@U"NSwapFloat(word)"
 *	@H"XDataUtil.h"
 *	@I
 *		This macro swaps the word order for a network order word
 *	to the native order of the target environment. Depending on the
 *	setting of OPT_NATIVEORDER, this either swaps or does not swap
 *	the bytes in word.
 * 
 *		This is reliant on the size of a float being 4 bytes. This
 *	also relies on the double being in IEEE format; use with caution.
 */

#define	NSwapFloat(x)	(x)

/*	@F"NSwapDouble"
 *	@U"NSwapDouble(word)"
 *	@H"XDataUtil.h"
 *	@I
 *		This macro swaps the word order for a network order word
 *	to the native order of the target environment. Depending on the
 *	setting of OPT_NATIVEORDER, this either swaps or does not swap
 *	the bytes in word.
 *
 *		This is reliant on the size of a double being 8 bytes. This
 *	also relies on the double being in IEEE format; use with caution.
 */

#define	NSwapDouble(x)	(x)

#else

#define	ESwapWord(x)	(x)
#define	ESwapLong(x)	(x)
#define	ESwapFloat(x)	(x)
#define	ESwapDouble(x)	(x)

#define	NSwapWord(x)	SwapWord(x)
#define	NSwapLong(x)	SwapLong(x)
#define	NSwapFloat(x)	SwapFloat(x)
#define	NSwapDouble(x)	SwapDouble(x)

#endif

/************************************************************************/
/*																		*/
/*	Resource Support													*/
/*																		*/
/************************************************************************/

#if OPT_XWIN

/*	@F"GetResourceData"
 *	@H"XDataUtil.h"
 *	@I
 *
 *		This is an X specific routine used for getting resources.
 *
 *		A note about this routine. While I am declaring it in this header,
 *	the GetResourceData routine itself is actually generated by the
 *	RCompile tool, along with the resource data that is associated with
 *	this program.
 *
 *		If you fail to compile the resources into a resource.c file and
 *	link it in with the rest of your YAAF program, this routine will show
 *	up as missing. Likewise, if there is a problem with your resources
 *	(like duplicate resources), you will have problems compiling the
 *	resource.c file.
 */

extern void *GetResourceData(long resType, short resID);

#endif // OPT_XWIN

/************************************************************************/
/*																		*/
/*	Internationalization												*/
/*																		*/
/************************************************************************/

extern short GetResourceMessage(short res, short index, char *smsg, char *lmsg);

/************************************************************************/
/*																		*/
/*	Complex formatting													*/
/*																		*/
/************************************************************************/

extern void yformat(char *out, const char *msg, ...);
extern void vyformat(char *out, const char *msg, va_list vlist);


#ifdef __cplusplus
}
#endif

#endif /* PandaWave_XDATAUTIL */
