#include	<stdlib.h>
#include	<stdarg.h>
#include	<stdio.h>
#include	<string.h>
#include	<ctype.h>
#include	<time.h>

#include	"tb_lib.h"

LISTCTRL	*tb_listctrl_open(void)
{
	LISTCTRL	   *listctrl;

	if ( (listctrl = MALLOC(sizeof(LISTCTRL))) == NULL )
		return (NULL);
	listctrl->datTop  = NULL;
	listctrl->datLast = NULL;
	listctrl->numDat  = 0;
	listctrl->sizDat  = 0;

	return (listctrl);
}

void	tb_listctrl_close( LISTCTRL *listctrl)
{
	if ( listctrl == NULL )
		return;
	tb_listctrl_clear(listctrl);
	FREE(listctrl);
}

void	tb_listctrl_clear( LISTCTRL *listctrl)
{
	LISTDATA	   *listdata, *next;

	if ( listctrl == NULL )
		return;
	listdata = listctrl->datTop;
	while ( listdata != NULL )
	{
		next = listdata->next;
		FREE(listdata);
		listdata = next;
	}
	listctrl->datTop  = NULL;
	listctrl->datLast = NULL;
	listctrl->numDat  = 0;
	listctrl->sizDat  = 0;
}

LISTDATA	*tb_list_allocData(size_t size)
{
	LISTDATA	*listdata;

	if ( (listdata = MALLOC(sizeof(LISTDATA) + size)) == NULL )
		return (NULL);
	listdata->next = NULL;
	listdata->siz  = size;
	listdata->ptr  = (void *)(listdata + 1);
	return (listdata);
}

LISTDATA	   *tb_list_allocName(const char *name)
{
	LISTDATA	*listdata;

	if ( (listdata = tb_list_allocData(strlen(name)+1)) == NULL )
		return (NULL);
	strcpy( (char *)listdata->ptr, name );
	return (listdata);
}

void	tb_list_linkData( LISTCTRL *listctrl, LISTDATA *listdata )
{
	if ( listctrl == NULL || listdata == NULL )
		return;
	if ( listctrl->datLast != NULL )
	{
		listctrl->datLast->next = listdata;
		listctrl->datLast       = listdata;
	} else
		listctrl->datTop = listctrl->datLast = listdata;
	++listctrl->numDat;
	listctrl->sizDat += listdata->siz;
}

int		tb_list_putf( LISTCTRL *listctrl, const char *form, ... )
{
	char			tmp[BUFSIZ];
	va_list			ap;
	LISTDATA	   *listdata;

	va_start(ap,form);
	vsprintf(tmp,form,ap);
	va_end(ap);

	if ( (listdata = tb_list_allocName(tmp)) == NULL )
		return (-1);	/* error */

	tb_list_linkData( listctrl, listdata );

	return (0);
}

int		tb_search_key( const char *s, SEARCHKEYTBL *tbl, size_t tblnum )
{
	SEARCHKEYTBL	   *tblptr;
	unsigned int		n;

	for ( n = 0; n < tblnum; ++n )
	{
		tblptr = tbl + n;
		if ( strcmpi(s, tblptr->name) == 0)
			return (tblptr->no);
	}
	return (-1);
}

DATETIME	*tb_getNowDate( DATETIME *date )
{
	static DATETIME		datebuf;
	struct tm		   *tmptr;

	{
		time_t		timer;

		time(&timer);
		tmptr = localtime(&timer);
	}
	if ( date == NULL )
		date = &datebuf;

	date->year   = tmptr->tm_year + 1900;
	date->month  = tmptr->tm_mon  + 1;
	date->day    = tmptr->tm_mday;
	date->week   = tmptr->tm_wday;
	date->hour   = tmptr->tm_hour;
	date->minute = tmptr->tm_min;
	date->second = tmptr->tm_sec;

	return (date);
}

char	*tb_date2asc(char *buf, DATETIME *date)
{
	sprintf(buf,"%04d/%02d/%02d %02d:%02d:%02d",
	    date->year  ,
	    date->month ,
	    date->day   ,
	    date->hour  ,
	    date->minute,
	    date->second );

	return (buf);
}
