/*	 -- 

Copyright (C) 1998 Ogochan.

This module is part of ***.

	**** is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves
any particular purpose or works at all, unless he says so in writing.
Refer to the GNU General Public License for full details. 

	Everyone is granted permission to copy, modify and redistribute
MACE, but only under the conditions described in the GNU General
Public License.  A copy of this license is supposed to have been given
to you along with MACE so you can know your rights and
responsibilities.  It should be in a file named COPYING.  Among other
things, the copyright notice and this notice must be preserved on all
copies. 
*/

#include	<malloc.h>
#include	<stdio.h>
#include	<ctype.h>
#include	<stdlib.h>
#include	<string.h>
#include	"types.h"
#include	"misc.h"
#include	"debug.h"

#ifndef NO_XMALLOC

extern	void	*
_xmalloc(
	size_t	size,
	char	*fn,
	int		line)
{	void	*ret;

#ifdef	TRACE
	printf("xmalloc %s(%d)\t",fn,line);
#endif
	if		(  ( ret = malloc(size) )  ==  NULL  )	{
		printf("no memory space!! %s(%d)\n",fn,line);
		exit(1);
	}
#ifdef	TRACE
	printf("allocate = %p , size = %ld\n",ret,(long)size);
#endif
	return	(ret);
}

extern	void
_xfree(
	void	*p,
	char	*fn,
	int		line)
{
#ifdef	TRACE
	printf("xfree %s(%d)\t",fn,line);
#endif
	free(p);
}

#endif /* not def NO_XMALLOC */

extern	void
Error(
	char	*msg)
{
	fprintf(stderr,"%s\n",msg);
	exit(1);
}

extern	int
strlicmp(
	char	*s1,
	char	*s2)
{	int		ret;

	ret = 0;
	for	( ; *s2 != 0 ; s1 ++ , s2 ++ )	{
		if		(  ( ret = toupper(*s1) - toupper(*s2) )  !=  0  )
			break;
	}
	return	(ret);
}

extern	int
strlcmp(
	char	*s1,
	char	*s2)
{
	return	(strncmp(s1,s2,strlen(s2)));
}

#ifdef	__GNUC__
extern	int
stricmp(
	char	*s1,
	char	*s2)
{	int		ret;

	ret = 0;
	for	( ; ( *s1  !=  0 ) && (  *s2  !=  0 ) ; s1 ++ , s2 ++ )	{
		if		(  ( ret = toupper(*s1) - toupper(*s2) )  !=  0  )
			break;
	}
	ret = toupper(*s1) - toupper(*s2);
	return	(ret);
}
extern	int
strnicmp(
	char	*s1,
	char	*s2,
	size_t	l)
{	int		ret;

	ret = 0;
	for	( ; l > 0 ; l -- , s1 ++ , s2 ++ )	{
		if		(  ( ret = toupper(*s1) - toupper(*s2) )  !=  0  )
			break;
	}
	return	(ret);
}
#endif

extern	long
StrToInt(
	char	*str,
	size_t	len)
{	long	ret;

	ret = 0L;
	for	( ; len > 0 ; len -- )	{
		if		(  isdigit(*str)  )	{
			ret = ret * 10 + ( *str - '0' );
		}
		str ++;
	}
	return	(ret);
}

extern	long
HexToInt(
	char	*str,
	size_t	len)
{	long	ret;

	ret = 0L;
	for	( ; len > 0 ; len -- )	{
		ret <<= 4;
		if		(  isdigit(*str)  )	{
			ret += ( *str - '0' );
		} else
		if		(  isxdigit(*str)  )	{
			ret += ( toupper(*str) - 'A' + 10 );
		}
		str ++;
	}
	return	(ret);
}

extern	char	*
IntToStr(
	char	*str,
	long	val,
	size_t	len)
{
	str += len;
#if	0
	*str = 0;
#endif
	for	( ; len > 0 ; len -- )	{
		str --;
		*str = (char)(( val % 10 ) + '0');
		val /= 10;
	}
	return	(str);
}

#ifdef	COBOL_COMPATIBLITY
extern	void
MakeCobolX(
	char	*to,
	size_t	len,
	char	*from)
{
	for	( ; len > 0 ; len -- )	{
		if		(  *from  ==  0  )	{
			*to = ' ';
		} else {
			*to = *from ++;
		}
		to ++;
	}
	*to = 0;
}

extern	void
CopyCobol(
	char	*to,
	char	*from)
{
	while	(  !isspace(*from)  )	{
		*to ++ = *from ++ ;
	}
	*to = 0;
}
#endif

extern	void
PrintFixString(
	char	*s,
	int		len)
{	int		i;

	for	( i = 0 ; i < len ; i ++ )	{
		putchar(*s);
		s ++;
	}
}

extern	char	*
StrDup(
	char	*s)
{	char	*ret;
#if 0
	fprintf(stderr, "StrDup: [%s]\n", s);
#endif
	ret = (char *)xmalloc(strlen(s)+1);
	strcpy(ret,s);
	return	(ret);
}	

extern void
StrFree(char *s)
{
#if 0
	fprintf(stderr, "StrFree: [%s]\n", s);
#endif;
	xfree(s);
}

extern	void
AdjustByteOrder(
	void	*to,
	void	*from,
	size_t	size)
#ifndef	BIG_ENDIAN
{
	memcpy(to,from,size);
}
#else
{	byte	*p
	,		*q;

	p = (byte *)to + size - 1;
	for	( q = from ; p >= (byte *)to ; p -- , q ++ )	{
		*p = *q;
	}
}
#endif

