/*****************************************************************************
 *
 * File ..................: common/strutil.c
 * Purpose ...............: Common string functions
 * Last modification date : 06-Feb-1999
 *
 *****************************************************************************
 * Copyright (C) 1997-1999
 *   
 * Michiel Broek		FIDO:		2:2801/16
 * Beekmansbos 10		Internet:	mbroek@ux123.pttnwb.nl
 * 1971 BV IJmuiden
 * the Netherlands
 *
 * This file is part of MBSE BBS.
 *
 * This BBS is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * MB BBS is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MB BBS; see the file COPYING.  If not, write to the Free
 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *****************************************************************************/

#include "../include/libs.h"
#include "../include/structs.h"
#include "common.h"



char *xmalloc(size_t size)
{
	char	*tmp;

	tmp = malloc(size);
	if (!tmp) 
		abort();
	
	return tmp;
}



char *xstrcpy(char *src)
{
	char	*tmp;

	if (src == NULL) 
		return(NULL);
	tmp = xmalloc(strlen(src)+1);
	strcpy(tmp, src);
	return tmp;
}



char *xstrcat(char *src, char *add)
{
	char	*tmp;
	size_t	size = 0;

	if ((add == NULL) || (strlen(add) == 0))
		return src;
	if (src)
		size = strlen(src);
	size += strlen(add);
	tmp = xmalloc(size + 1);
	*tmp = '\0';
	if (src) {
		strcpy(tmp, src);
		free(src);
	}
	strcat(tmp, add);
	return tmp;
}



char *padleft(char *str, int size, char pad)
{
	static char stri[256];
	static char temp[256];

	strcpy(stri, str);
	memset(temp, pad, (long)size);
	temp[size] = '\0';
	if (strlen(stri) <= size)
		memmove(temp, stri, (long)strlen(stri));
	else
		memmove(temp, stri, (long)size);
	return temp;
}



/*
 * Small function to convert String to LowerCase
 */
char *tl(char *str)
{
	char	*p = str;

	while (*p != '\0') {
		*p = (char)tolower(*p);
		p++;
	}

	return str;
}




void Striplf(char *String)
{
	int i;

	for(i = 0; i < strlen(String); i++) {
		if(*(String + i) == '\0')
			break;
		if(*(String + i) == '\n')
			*(String + i) = '\0';
	}
}



/*
 * Converts first letter to UpperCase
 */
void tlf(char *str)
{
	*(str) = toupper(*(str));
}



/*
 * Small function to convert String to UpperCase
 */
char *tu(char *str)
{
	char	*p = str;

	while (*p != '\0') {
		*p = (char)toupper(*p);
		p++;
	}

	return str;
}



/*
 * Converts the first letter in every word in a string to uppercase,
 * all other character will be lowercase. Will handle the notation
 * Bob Ten.Dolle as well
 */
char *tlcap(char *String)
{
	static char	stri[256];
	int	Loop, Strlen;
	
	strcpy(stri, String);
	Strlen = strlen(stri);

	/*
	 * Automatic do the first character
	 */
	stri[0] = toupper(stri[0]);

	for(Loop = 1; Loop < Strlen; Loop++) {
		stri[Loop] = tolower(stri[Loop]);
		if (( stri[Loop] == ' ') || (stri[Loop] == '.')) {
			/* 
			 * This is a space charracter, increase the counter
			 * and convert the next character to uppercase.
			 */
			Loop++;
			stri[Loop] = toupper(stri[Loop]);
		}
	}
	return stri;
}



/*
 * Hilite "Word" in string, this is done by inserting ANSI
 * Hilite characters in the string.
 */
char *Hilite(char *str, char *Word)
{
	char *pos;
	char *new;
	char *old;
	int t;

	new = strdup(str);
	old = strdup(str);
	tl(new);

	if ((pos = strstr(new,Word)) == NULL)
		return(str);

	str = realloc(str,strlen(new)+200);
	strcpy(str,"\0");

	while (( pos = strstr(new,Word)) != NULL) {
		*pos = '\0';
		t = strlen(new);
		strncat(str,old,t);
		strcat(str,"[1;37m");
		old+=t;
		strncat(str,old,strlen(Word));
		strcat(str,"[0;37m");
		old+=strlen(Word);
		new = new+t+strlen(Word);
	}
	strcat(str,old);
	return(str);
}



/*
 * Replace spaces is a string with underscore characters.
 */
void Addunderscore(char *temp)
{
	int i;

	for(i = 0; i < strlen(temp); i++) {
		if (*(temp + i) == '\0')
			break;
		if (*(temp + i) == ' ')
			*(temp + i) = '_';
	}
}



/*
 * Find & Replace string in a string
 */
void strreplace(char *sStr, char *sFind, char *sReplace)
{
	char sNewstr[81]="";
	char *posStr, *posFind;
	int iPos, iLen, iCounter;

	posStr=sStr;
	if(( posFind = strstr(sStr, sFind)) != NULL) {
		iPos = (int)(posFind - posStr);
		strncpy(sNewstr, sStr, iPos);
		strcat(sNewstr, sReplace);
		iPos+= strlen(sFind);
		iLen = strlen(sNewstr);
		for (iCounter=0; iCounter < (strlen(sStr) - iPos); iCounter++)
			sNewstr[iCounter + iLen] = sStr[iCounter + iPos]; 
		sNewstr[iCounter+1+iLen] = '\0';
		strcpy(sStr, sNewstr);
	}
}



/*
 * Converts to HH:MM
 */
char *StrTimeHM(time_t date)
{
	static char	ttime[6];
	struct tm	*l_d;

	l_d = localtime(&date);
	sprintf(ttime, "%02d:%02d", l_d->tm_hour, l_d->tm_min);
	return ttime;
}



/*
 * Returns HH:MM:SS
 */
char *StrTimeHMS(time_t date)
{
	static char	ttime[9];
	struct tm	*l_d;

	l_d = localtime(&date);
	sprintf(ttime, "%02d:%02d:%02d", l_d->tm_hour, l_d->tm_min, l_d->tm_sec);
	return ttime;
}



/*
 * Get the current local time, returns HH:MM
 */
char *GetLocalHM() 
{
	static char	gettime[15];
	time_t		T_Now;

	time(&T_Now);
	sprintf(gettime,"%s", StrTimeHM(T_Now));
	return(gettime);
}




/*
 * Get the current local time, returns HH:MM:SS
 */
char *GetLocalHMS() 
{
	static char	gettime[15];
	time_t		T_Now;

	time(&T_Now);
	sprintf(gettime,"%s", StrTimeHMS(T_Now));
	return(gettime);
}



/* 
 * Returns date as MM-DD-YYYY
 */
char *StrDateMDY(time_t *Clock)
{
	struct tm *tm;
	static char cdate[12];
	
	tm = localtime(Clock);
	sprintf(cdate,"%02d-%02d-%04d", tm->tm_mon+1, tm->tm_mday, tm->tm_year+1900);
	return(cdate);
}



/*
 * Returns DD-MM-YYYY
 */
char *StrDateDMY(time_t date)
{
	static char	tdate[15];
	struct tm	*l_d;

	l_d = localtime(&date);
	sprintf(tdate, "%02d-%02d-%04d", l_d->tm_mday, l_d->tm_mon+1, l_d->tm_year+1900);
	return tdate;
}




/*
 * This function returns the date for today, to test against other functions
 *                 DD-MM-YYYY (DAY-MONTH-YEAR)
 */
char *GetDateDMY()
{
	static char	tdate[15];
	struct tm	*l_d;
	time_t		T_Now;

	time(&T_Now);
	l_d = localtime(&T_Now);
 	sprintf(tdate, "%02d-%02d-%04d", l_d->tm_mday,l_d->tm_mon+1,l_d->tm_year+1900);
	return(tdate);
}



/*
 * Returns current date in DDMMYYY
 */
/*
char *tDate1()
{
	static char	tdate1[15];
	struct tm	*l_d;
	time_t		T_Now;

	time(&T_Now);
	l_d = localtime(&T_Now);
 	sprintf(tdate1, "%02d%02d%02d",
		 l_d->tm_mday,l_d->tm_mon+1,l_d->tm_year+1900);

	return(tdate1);
}

*/
