/*	StringUtil.c
 *
 *		String manipulation utilities
 */

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

  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 <XStringUtil.h>
#include <stdio.h>

/**@name Support--Pascal String Processing
 *
 *		These provide miscellaneous string conversion and processing
 *	routines for handling simple 8-bit strings. Many of these routines
 *	provide simple C to Pascal and Pascal to C string conversion (used
 *	by the Macintosh version of YAAF and for extracting length-encoded
 *	strings from resource files), and for other common string operations.
 */
 
//@{

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

/*$	cvtc2p
 *
 *		Convert a string from C to Pascal. Returns the Pascal string.
 */

pchar *cvtc2p(pchar *dest, const char *src)
{
	register pchar *ptr;
	register pchar len = 0;
	
	ptr = dest+1;
	while (*src) {
		*ptr++ = (pchar)*src++;
		len++;
	}
	*dest = len;
	return dest;
}

/*$	ncvtc2p
 *
 *		Convert a string from C to Pascal. Returns the Pascal string.
 */

pchar *ncvtc2p(pchar *dest, const char *src, uint16 maxlen)
{
	register pchar *ptr;
	register pchar len = 0;
	
	ptr = dest+1;
	while (*src && (--maxlen > 0)) {
		*ptr++ = (pchar)*src++;
		len++;
	}
	*dest = len;
	return dest;
}

/*$	cvtp2c
 *
 *		Convert from Pascal to C format string. Returns the C string.
 */

char *cvtp2c(char *dest, const pchar *src)
{
	register char *ptr;
	register pchar len;
	
	ptr = dest;
	len = *src++;
	while (len--) *ptr++ = (char)*src++;
	*ptr = 0;
	
	return dest;
}

/*$	ncvtp2c
 *
 *		Convert from Pascal to C format string. Returns the C string.
 */

char *ncvtp2c(char *dest, const pchar *src, uint16 maxlen)
{
	register char *ptr;
	register pchar len;
	
	ptr = dest;
	len = *src++;
	maxlen--;
	if (len > maxlen) len = maxlen;
	while (len--) *ptr++ = (char)*src++;
	*ptr = 0;
	
	return dest;
}

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

/*$	pstrcpy
 *
 *		Pascal string copy operation.
 */

pchar *pstrcpy(pchar *dest, const pchar *src)
{
	register pchar len;
	register pchar *ptr;
	
	len = *src;
	ptr = dest;
	*ptr++ = *src++;
	while (len--) *ptr++ = *src++;
	
	return dest;
}

/*$	npstrcpy
 *
 *		Pascal string copy operation.
 */

pchar *npstrcpy(pchar *dest, const pchar *src, uint16 maxlen)
{
	register pchar len;
	register pchar *ptr;
	
	len = *src;
	maxlen--;
	if (len > maxlen) len = maxlen;
	ptr = dest;
	*ptr++ = *src++;
	while (len--) *ptr++ = *src++;
	
	return dest;
}

/*$	pstrcat
 *
 *		Concatenate source string to the end of the dest string.
 */

pchar *pstrcat(pchar *dest, const pchar *src)
{
	register pchar len;
	register pchar *ptr;
	
	len = *src++;
	ptr = dest + *dest + 1;
	*dest += len;
	while (len--) *ptr++ = *src++;
	
	return dest;
}

/*$	npstrcat
 *
 *		Concatenate source string to the end of the dest string.
 */

pchar *npstrcat(pchar *dest, const pchar *src, uint16 maxlen)
{
	register pchar len;
	register pchar *ptr;
	uint16 nlen;
	
	/* Limit boundary based on maxlen */
	len = *src++;
	nlen = len + (uint16)*dest;
	if (nlen > 255) nlen = 255;
	maxlen--;
	if (nlen > maxlen) nlen = maxlen;
	len = nlen - *dest;
	
	ptr = dest + *dest + 1;
	*dest += len;
	while (len--) *ptr++ = *src++;
	
	return dest;
}

/*$	pstrcmp
 *
 *		Compare two pascal strings. This works like the C string compare
 */

uint16 pstrcmp(const pchar *a, const pchar *b)
{
	register pchar alen;
	register pchar blen;
	
	alen = *a++;
	blen = *b++;
	while (alen && blen) {
		if (*a < *b) return -1;
		if (*a > *b) return 1;
		alen--;
		blen--;
	}
	if (alen < blen) return 1;
	if (alen > blen) return -1;
	return 0;
}

/*$	nstrcpy
 *
 *		Copy a C string, limiting it's length. This is *not* the
 *	way strncpy works, by the way--this limits the length, but
 *	always creates a well-formed C string.
 */

char *nstrcpy(char *dest, const char *src, uint16 maxlen)
{
	register char *d = dest;
	while (*src && (--maxlen > 0)) *d++ = *src++;
	*d = 0;
	return dest;
}

/*$	nstrcat
 *
 *		Concatenate C string, limiting it's length
 */

char *nstrcat(char *dest, const char *src, uint16 maxlen)
{
	register char *d = dest;
	
	while (*d && (--maxlen > 0)) d++;
	while (*src && (--maxlen > 0)) *d++ = *src++;
	*d = 0;
	return dest;
}

//@}
