/*
 *	(c) 1998-99 Sassafras Software Inc.
 *
 *	Basic routines for getting strings out of resources.
 */

/**********************************************************************

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

**********************************************************************/

#include <XConfig.h>			// must be first to optimize compile on MSVC++
#include <string.h>
#include <ctype.h>

#include <XStringUtil.h>

/**@name Support--String Utilities
 *
 *		These provide miscellaneous string conversion and processing
 *	routines for handling simple 8-bit strings. 
 */

//@{

/************************************************************************/
/*																		*/
/*	String Support Routines												*/
/*																		*/
/************************************************************************/

/*$	XGGetResString
 *
 *		Get string from the specified resource
 */

uint16 XGGetResString (short res, char *s, uint16 maxlen)
{
#if OPT_MACOS
	StringHandle h;

	if (s)
		*s = 0;
	h = GetString(res);
	if (h == NULL)
		return(0);

	if (!maxlen)
		maxlen = **h;
	else {
		maxlen -= 1;
		if (maxlen > **h)
			maxlen = **h;
	}
	if (s) {
		memcpy(s, *h + 1, maxlen);
		s[maxlen] = 0;
	}
#endif

#if OPT_WINOS
	extern HINSTANCE _GInstance;
	char ss[256];
	uint16 len;

	if (s)
		*s = 0;
	len = LoadString(_GInstance, res, ss, sizeof(ss));
	if (!len)
		return(0);

	if (!maxlen)
		maxlen = len;
	else {
		maxlen -= 1;
		if (maxlen > len)
			maxlen = len;
	}
	if (s) {
		memcpy(s, ss, maxlen);
		s[maxlen] = 0;
	}
#endif

#if OPT_XWIN
	unsigned char *p;

	if (s) *s = 0;
	p = GetResourceData('STR ', res);
	if (p == NULL)
		return(0);

	if (!maxlen)
		maxlen = *p;
	else {
		maxlen -= 1;
		if (maxlen > *p)
			maxlen = *p;
	}
	if (s) {
		memcpy(s, p + 1, maxlen);
		s[maxlen] = 0;
	}
#endif
	return(maxlen);
}

/*$	XGGetIndResString
 *
 *		Get a string from a string list resource
 */

uint16 XGGetIndResString (short res, short ix, char *s, uint16 maxlen)
{
#if OPT_MACOS
	Str255 st;

	if (s)
		*s = 0;
	GetIndString(st, res, ix + 1);
	if (!*st)
		return(0);

	if (!maxlen)
		maxlen = *st;
	else {
		maxlen -= 1;
		if (maxlen > *st)
			maxlen = *st;
	}
	if (s) {
		memcpy(s, st + 1, maxlen);
		s[maxlen] = 0;
	}
	return(maxlen);
#endif

#if OPT_WINOS
	extern HINSTANCE _GInstance;
	HRSRC hrsrc;
	HGLOBAL h;
	char *p;
	int len;

	/* on windows, the format of the resource is:
			uint16 length;
			char strings[length][];
		the strings are all packed together.
	 */
	if (s)
		*s = 0;
	
	hrsrc = FindResource(_GInstance,MAKEINTRESOURCE(res), "STRL");
	if (hrsrc == NULL)
		return(0);
	h = LoadResource(_GInstance,hrsrc);
	if (h == NULL)
		return(0);
	p = (char *)LockResource(h);

	/*
	 *	Now parse the resource pointed to by p
	 */
	
	len = *(short *)p;
	p += sizeof(short);
	if (ix >= 0 || ix < len)  {
		while (--ix >= 0)
			p += strlen(p) + 1;

		len = strlen(p);
		if (!maxlen)
			maxlen = len;
		else {
			maxlen -= 1;
			if (maxlen > len)
				maxlen = len;
		}
		if (s) {
			memcpy(s, p, maxlen);
			s[maxlen] = 0;
		}
	}
	
	UnlockResource(h);
	FreeResource(h);
	return(maxlen);
#endif

#if OPT_XWIN
	/*
	 *	X Windows resource format is the same
	 */
	
	char *p;
	int len;

	if (s) *s = 0;
	p = GetResourceData('STR#', res);
	if (p == NULL)
		return(0);

	len = *(short *)p;
	p += sizeof(short);
	if (ix >= 0 || ix < len)  {
		while (--ix >= 0)
			p += strlen(p) + 1;

		len = strlen(p);
		if (!maxlen)
			maxlen = len;
		else {
			maxlen -= 1;
			if (maxlen > len)
				maxlen = len;
		}
		if (s) {
			memcpy(s, p, maxlen);
			s[maxlen] = 0;
		}
	}
	return(maxlen);
#endif
}

/*$	XGCompareStrings
 *
 *		Simplified version to compare two strings.
 */

sint16 XGCompareStrings (const char *s1, const char *s2)
{
	return XGCompareStringsEx(s1, s2, false, false);
}

/*$	XGCompareStringsEx
 *
 *		Compare two strings. Two option flags determine how those
 *	strings are compared:
 *
 *		csens			Compare strings case sensitive
 *		loose			If case insensitive compare doesn't work,
 *						compare case sensitive
 */

sint16 XGCompareStringsEx (const char *s1, const char *s2, boolean csens, boolean loose)
{
#if 0
#if OPT_MACOS
	char buf1[256], buf2[256];
	long len1, len2;

	if (csens)
		return(strcmp(s1, s2));
	len1 = strlen(s1);
	len2 = strlen(s2);
	if (loose) {
		if (len1 < 256 && len2 < 256) {
			BlockMoveData(s1, buf1, len1 + 1);
			BlockMoveData(s2, buf2, len2 + 1);
			UppercaseText(buf1, len1, smCurrentScript);
			UppercaseText(buf2, len2, smCurrentScript);
			s1 = buf1;
			s2 = buf2;
		}
	}
	return(CompareText(s1, s2, len1, len2, NULL));
#endif

#if OPT_WINOS
	if (!csens) {
		int res;
		if (0 != (res = lstrcmpi(s1, s2)))
			return(res);
		if (loose)
			return(res);
	}
	return(strcmp(s1, s2));
#endif

#if OPT_XWIN
#endif
#else
	if (!csens) {
		const char *a;
		const char *b;
		
		a = s1;
		b = s2;
		while ((toupper(*a) == toupper(*b)) && (*a)) a++, b++;
		if (*a == *b) {
			if (loose) return strcmp(s1, s2);
			else return 0;
		}
		if (toupper(*a) < toupper(*b)) return -1;
		else return 1;
	} else {
		return strcmp(s1, s2);
	}
#endif
}

/*$	XGCompareNumbers
 *
 *		Cmpare two numbers.
 */

sint16 XGCompareNumbers (sint32 num1, sint32 num2)
{
	if (num1 < num2)
		return(-1);
	if (num1 > num2)
		return(1);
	return(0);
}

/*$	XGReadableSpaceString
 *
 *		Given the size of something in bytes, return it's
 *	size as a string
 */

void XGReadableSpaceString (char *buf, uint32 space)
{
	int i;                  
	static char *units[3] = { " bytes", "K", " MB" };
	char *ptr;
	char scratch[32];
	int j;
	
	/*
	 *	Handle special cases
	 */
	
	if (space == 0) {
		strcpy(buf,"0 bytes");
		return;
	}
	if (space == 1) {
		strcpy(buf,"1 byte");
		return;
	}

	/*
	 *	Figure out my scale
	 */
	
	for (i = 0; i < 2; i++) {
		if (!(space >> 10))
			break;
		space >>= 10;
	}
	
	/*
	 *	Now do the conversion without using sprintf
	 */
	
	j = 0;
	while (space) {
		scratch[j++] = (space % 10) + '0';
		space /= 10;
	}
	while (j--) *buf++ = scratch[j];
	ptr = units[i];
	while (0 != (*buf++ = *ptr++)) ;
	*buf = 0;
}

//@}
