www.pudn.com > tvctrl.rar > chdb.c


/*  
	描述:	频道数据库管理模块  
			  
*/  
#include   
#include   
#include   
  
#include "atvdtv.h"  
#include "channeldb.h"  
#include "tvmisc.h"				/* 共享内存管理 */  
#include "httpreq.h"  
  
#define CH_CTRL_ALLOC_NUMS	10	//每次分配的项数  
  
#define CH_DB_FILE 		"/extra/database/ch.db"  
#define CHTYPE_DB_FILE 	"/extra/database/chtype.db"  
#define CHNAME_DB_FILE 	"/extra/database/chname.db"  
#define FUNCDB_FILE 	"/etc/funcdb.ini"	//只在初始化将其读入内存数据库中,并不需要更新  
  
#define KEY_CH_DB_SHMEM_CTRLSTRUCT		0x1000  
#define KEY_CH_DB_SHMEM_PHYSCHINFO		0x1001  
#define KEY_CH_DB_SHMEM_LOCALCHINFO		0x1002  
#define KEY_CH_DB_SHMEM_TVINFO			0x1003  
  
#define CH_DB_MAX_ITEM					500  
  
#define GLH_DEBUG(x...) fprintf(stderr,x)  
  
typedef struct tagTVChannelCtrl  
{  
	int 				itemCount ;  
	int 				mallocCount ;  
	  
} TVChannelCtrl_t, *pTVChannelCtrl_t ;  
  
typedef struct tagTVChannelMgr  
{  
	int 				hShmCtrl ; 		/* 共享内存句柄 */  
	  
	int 				hShmPhys ; 		/* 共享内存句柄 */  
	int 				hShmLocal ; 	/* 共享内存句柄 */  
	int 				hShmTvinfo ; 	/* 共享内存句柄 */  
	  
	TVChannelCtrl_t		*pChCtrl ;		/* 控制结构 */  
	TVChannelInfo 		*physChInfo ;  
	LocalChannelRecord	*localChInfo ;  
	TVINFO				*tvInfo ;  
	int					*filterArr ;  
	int					filterArrCount ;  
} TVChannelMgr_t, *pTVChannelMgr_t ;  
  
//static TVChannelCtrl_t *pChCtrl = NULL ;  
static TVChannelMgr_t *pChMgr = NULL ;  
  
  
static int GetFuncTVItem(FUNCITEM *pprog, char* pSection) ;  
static int InitFuncTVData(TVChannelInfo *physChInfo, LocalChannelRecord *localChInfo,  
							TVINFO *tvInfo, int maxitem) ;  
static int GetNewChannelid(int tvtype, int *newid) ;  
  
#if 0  
//初始化频道数据库;  
int InitChannelDB(void)  
{  
	int result = -1 ;  
	int r, r0, r1, r2, r3 ;  
	int status0, status1, status2, status3 ;  
	  
	/* 已经打开 */  
	if (pChMgr)  
	{  
		result = 0 ;  
		goto done ;  
	}  
	  
	/* 检查频道数据库控制结构(句柄)是否已经在共享数据区中) */  
	r0 = ShareMemOpen(KEY_CH_DB_SHMEM_CTRLSTRUCT, sizeof(TVChannelCtrl_t), NULL, &status0) ;  
	r1 = ShareMemOpen(KEY_CH_DB_SHMEM_PHYSCHINFO, sizeof(TVChannelInfo)*CH_DB_MAX_ITEM, NULL, &status1) ;  
	r2 = ShareMemOpen(KEY_CH_DB_SHMEM_LOCALCHINFO, sizeof(LocalChannelRecord)*CH_DB_MAX_ITEM, NULL, &status2) ;  
	r3 = ShareMemOpen(KEY_CH_DB_SHMEM_TVINFO, sizeof(TVINFO)*CH_DB_MAX_ITEM, NULL, &status3) ;  
	/* 共享内存已经分配 */  
	if ((r0 != 0 && status0 > 1)  
		&& (r1 != 0 && status1 > 1)  
		&& (r2 != 0 && status2 > 1)  
		&& (r3 != 0 && status3 > 1)  
		)  
	{  
		TVChannelMgr_t *p = (TVChannelMgr_t *)malloc(sizeof(TVChannelMgr_t)) ;  
		  
		if (p)  
		{  
			GLH_DEBUG("chdb, use exist share memory \n") ;  
			memset(p, 0x00, sizeof(TVChannelMgr_t)) ;  
			p->hShmCtrl 	= r0 ;  
			p->hShmPhys 	= r1 ;  
			p->hShmLocal 	= r2 ;  
			p->hShmTvinfo 	= r3 ;  
			  
			p->pChCtrl 		= (TVChannelCtrl_t *)ShareMemGetStartAddr(r0) ;  
			p->physChInfo 	= (TVChannelInfo *)ShareMemGetStartAddr(r1) ;  
			p->localChInfo 	= (LocalChannelRecord *)ShareMemGetStartAddr(r2) ;  
			p->tvInfo 		= (TVINFO *)ShareMemGetStartAddr(r3) ;  
			  
			result = 0 ;  
			pChMgr = p ;  
		}  
	}  
	/* 刚创建 */  
	else if ((r0 != 0 && status0 == 1)  
		&& (r1 != 0 && status1 == 1)  
		&& (r2 != 0 && status2 == 1)  
		&& (r3 != 0 && status3 == 1)  
		)  
	{  
		TVChannelMgr_t *p = (TVChannelMgr_t *)malloc(sizeof(TVChannelMgr_t)) ;  
		  
		if (p)  
		{  
			GLH_DEBUG("chdb, create share memory \n") ;  
			memset(p, 0x00, sizeof(TVChannelMgr_t)) ;  
			p->hShmCtrl 	= r0 ;  
			p->hShmPhys 	= r1 ;  
			p->hShmLocal 	= r2 ;  
			p->hShmTvinfo 	= r3 ;  
			  
			p->pChCtrl 		= (TVChannelCtrl_t *)ShareMemGetStartAddr(r0) ;  
			p->physChInfo 	= (TVChannelInfo *)ShareMemGetStartAddr(r1) ;  
			p->localChInfo 	= (LocalChannelRecord *)ShareMemGetStartAddr(r2) ;  
			p->tvInfo 		= (TVINFO *)ShareMemGetStartAddr(r3) ;  
			  
			if (p->pChCtrl)  
			{  
				memset(p->pChCtrl, 0x00, sizeof(TVChannelCtrl_t)) ;  
				p->pChCtrl->mallocCount = CH_DB_MAX_ITEM ;  
			}  
  
			/* 准备从文件中更新数据 */	  
			if (p->pChCtrl  
				&& p->physChInfo  
				&& p->localChInfo  
				&& p->tvInfo  
				)  
			{  
				FILE *fr = fopen(CH_DB_FILE, "rb") ;  
				  
				if (fr)  
				{  
					int n = 0 ;  
					  
					if (fseek(fr, 0, SEEK_END) == 0)  
						n = (int)ftell(fr) ;  
					fseek(fr, 0, SEEK_SET) ;  
					if (n >= (int)sizeof(TVChannelCtrl_t)  
						&& fread(p->pChCtrl, (size_t)sizeof(TVChannelCtrl_t), 1, fr) == 1  
						&& p->pChCtrl->itemCount > 0  
						)  
					{  
						int itemCount = ((p->pChCtrl->itemCount < CH_DB_MAX_ITEM)? p->pChCtrl->itemCount : CH_DB_MAX_ITEM) ;  
						fread(p->physChInfo, sizeof(TVChannelInfo), itemCount, fr) ;  
						fread(p->localChInfo, sizeof(LocalChannelRecord), itemCount, fr) ;  
						fread(p->tvInfo, sizeof(TVINFO), itemCount, fr) ;  
						  
						/* 准备从.ini文件中读入功能电视项 */  
						r = InitFuncTVData(&(p->physChInfo[itemCount]), &(p->localChInfo[itemCount]), &(p->tvInfo[itemCount]),  
										CH_DB_MAX_ITEM-itemCount) ;  
						  
						/* 更新总项数 */  
						p->pChCtrl->itemCount = itemCount + r ;  
						p->pChCtrl->mallocCount = CH_DB_MAX_ITEM ;  
					}  
					fclose(fr) ;  
					GLH_DEBUG("open datafile:%s ok !!! \n", CH_DB_FILE) ;  
				}  
				else  
				{  
					GLH_DEBUG("open datafile:%s error !!! \n", CH_DB_FILE) ;  
				}  
			}  
			result = 0 ;  
			pChMgr = p ;  
		}  
	}  
	/* 打开共享内存有错误,关闭它 */  
	else  
	{  
		if (r0 != 0) ShareMemClose(r0) ;  
		if (r1 != 0) ShareMemClose(r1) ;  
		if (r2 != 0) ShareMemClose(r2) ;  
		if (r3 != 0) ShareMemClose(r3) ;  
	}  
  
done:  
	return (result) ;  
}  
  
//关闭频道数据库;  
int CloseChannelDB(void)  
{  
	int result = 0 ;  
	  
	if (pChMgr)  
	{  
		/* 关闭共享内存 */  
		if (pChMgr->hShmCtrl)	ShareMemClose(pChMgr->hShmCtrl) ;  
		if (pChMgr->hShmPhys)	ShareMemClose(pChMgr->hShmPhys) ;  
		if (pChMgr->hShmLocal)	ShareMemClose(pChMgr->hShmLocal) ;  
		if (pChMgr->hShmTvinfo) ShareMemClose(pChMgr->hShmTvinfo) ;  
		  
		/* 释放本地管理结构 */  
		free(pChMgr) ;  
		pChMgr = NULL ;  
	}  
	  
	return (result) ;  
}  
#endif  
  
#if 1  
// 不使用共享内存  
  
//初始化频道数据库;  
static int InitChannelDBCount = 0 ;  
int InitChannelDB(void)  
{  
	int result = -1 ;  
	int r ;  
	void *p0, *p1, *p2, *p3 ;  
	  
	/* 已经打开 */  
	if (pChMgr)  
	{  
		result = 0 ;  
		goto done ;  
	}  
	  
	p0 = (void *) malloc(sizeof(TVChannelCtrl_t)) ;  
	p1 = (void *) malloc(sizeof(TVChannelInfo)*CH_DB_MAX_ITEM) ;  
	p2 = (void *) malloc(sizeof(LocalChannelRecord)*CH_DB_MAX_ITEM) ;  
	p3 = (void *) malloc(sizeof(TVINFO)*CH_DB_MAX_ITEM) ;  
	  
	if (p0  
		&& p1  
		&& p2  
		&& p3  
		)  
	{  
		TVChannelMgr_t *p = (TVChannelMgr_t *)malloc(sizeof(TVChannelMgr_t)) ;  
		  
		if (p)  
		{  
			GLH_DEBUG("chdb, create memory \n") ;  
			memset(p, 0x00, sizeof(TVChannelMgr_t)) ;  
			  
			p->pChCtrl 		= (TVChannelCtrl_t *)p0 ;  
			p->physChInfo 	= (TVChannelInfo *)p1 ;  
			p->localChInfo 	= (LocalChannelRecord *)p2 ;  
			p->tvInfo 		= (TVINFO *)p3 ;  
			  
			if (p->pChCtrl)  
			{  
				memset(p->pChCtrl, 0x00, sizeof(TVChannelCtrl_t)) ;  
				p->pChCtrl->mallocCount = CH_DB_MAX_ITEM ;  
			}  
  
			/* 准备从文件中更新数据 */	  
			if (p->pChCtrl  
				&& p->physChInfo  
				&& p->localChInfo  
				&& p->tvInfo  
				)  
			{  
				FILE *fr = fopen(CH_DB_FILE, "rb") ;  
				  
				if (fr)  
				{  
					int n = 0 ;  
					  
					if (fseek(fr, 0, SEEK_END) == 0)  
						n = (int)ftell(fr) ;  
					fseek(fr, 0, SEEK_SET) ;  
					if (n >= (int)sizeof(TVChannelCtrl_t)  
						&& fread(p->pChCtrl, (size_t)sizeof(TVChannelCtrl_t), 1, fr) == 1  
						&& p->pChCtrl->itemCount > 0  
						)  
					{  
						int itemCount = ((p->pChCtrl->itemCount < CH_DB_MAX_ITEM)? p->pChCtrl->itemCount : CH_DB_MAX_ITEM) ;  
						fread(p->physChInfo, sizeof(TVChannelInfo), itemCount, fr) ;  
						fread(p->localChInfo, sizeof(LocalChannelRecord), itemCount, fr) ;  
						fread(p->tvInfo, sizeof(TVINFO), itemCount, fr) ;  
						  
						/* 准备从.ini文件中读入功能电视项 */  
						r = InitFuncTVData(&(p->physChInfo[itemCount]), &(p->localChInfo[itemCount]), &(p->tvInfo[itemCount]),  
										CH_DB_MAX_ITEM-itemCount) ;  
						  
						/* 更新总项数 */  
						p->pChCtrl->itemCount = itemCount + r ;  
						p->pChCtrl->mallocCount = CH_DB_MAX_ITEM ;  
					}  
					fclose(fr) ;  
					GLH_DEBUG("open datafile:%s ok !!! \n", CH_DB_FILE) ;  
					GLH_DEBUG("p->pChCtrl->itemCount=%d  \n", p->pChCtrl->itemCount) ;  
				}  
				else  
				{  
					GLH_DEBUG("open datafile:%s error !!! \n", CH_DB_FILE) ;  
				}  
			}  
			result = 0 ;  
			pChMgr = p ;  
		}  
	}  
  
	if (result != 0)  
	{  
		if (p0)	free(p0) ;  
		if (p1)	free(p1) ;  
		if (p2)	free(p2) ;  
		if (p3)	free(p3) ;  
	}  
  
done:  
	if (result == 0)  
		InitChannelDBCount++ ;  
		  
	return (result) ;  
}  
  
//关闭频道数据库;  
int CloseChannelDB(void)  
{  
	int result = 0 ;  
	  
	if (pChMgr && (--InitChannelDBCount) <= 0)  
	{  
		if (pChMgr->pChCtrl)		free(pChMgr->pChCtrl) ;  
		if (pChMgr->physChInfo) 	free(pChMgr->physChInfo) ;  
		if (pChMgr->localChInfo)	free(pChMgr->localChInfo) ;  
		if (pChMgr->tvInfo)			free(pChMgr->tvInfo) ;  
		  
		free(pChMgr) ;  
		pChMgr = NULL ;  
	}  
	  
	return (result) ;  
}  
#endif  
  
//更新频道数据库;  
int UpdateChannelDB(void)  
{  
	int result = -1 ;  
	  
	if (pChMgr)  
	{  
		if (pChMgr->pChCtrl  
			&& pChMgr->pChCtrl->itemCount > 0  
			&& pChMgr->physChInfo  
			&& pChMgr->localChInfo  
			&& pChMgr->tvInfo  
			)  
		{  
			  
			int i, count ;  
			  
			for (i=0,count=0; ipChCtrl->itemCount; i++)  
			{  
				if (pChMgr->physChInfo[i].tvtype != TVTYPE_FTV)  
					count++ ;  
			}  
			  
			if (count > 0)  
			{  
				TVChannelCtrl_t ch ;  
				FILE *fw ;  
				  
				memset(&ch, 0x00, sizeof(TVChannelCtrl_t)) ;  
				ch.itemCount = count ;  
				  
				fw = fopen(CH_DB_FILE, "wb") ;  
				if (fw)  
				{  
					/* 写文件头 */  
					fwrite(&ch, sizeof(TVChannelCtrl_t), 1, fw) ;  
					for (i=0; ipChCtrl->itemCount; i++)  
					{  
						if (pChMgr->physChInfo[i].tvtype != TVTYPE_FTV)  
							fwrite(&(pChMgr->physChInfo[i]), sizeof(TVChannelInfo), 1, fw) ;  
					}  
					for (i=0; ipChCtrl->itemCount; i++)  
					{  
						if (pChMgr->physChInfo[i].tvtype != TVTYPE_FTV)  
							fwrite(&(pChMgr->localChInfo[i]), sizeof(LocalChannelRecord), 1, fw) ;  
					}  
					for (i=0; ipChCtrl->itemCount; i++)  
					{  
						if (pChMgr->physChInfo[i].tvtype != TVTYPE_FTV)  
							fwrite(&(pChMgr->tvInfo[i]), sizeof(TVINFO), 1, fw) ;  
					}  
					result = 0 ;  
					GLH_DEBUG("create datafile:%s ok !!! \n", CH_DB_FILE) ;  
				}  
				else  
				{  
					GLH_DEBUG("create datafile:%s error \n", CH_DB_FILE) ;  
				}  
				fclose(fw) ;  
			}  
			else /* count <= 0, empty database file */  
			{  
				FILE *fw ;  
				  
				fw = fopen(CH_DB_FILE, "wb") ;  
#ifdef __DEBUG__  
				if (fw)  
					printf("empty datafile:%s ok \n", CH_DB_FILE) ;  
#endif  
				if (fw)  
					fclose(fw) ;  
			}  
		}  
	}  
  
	return (result) ;  
}  
  
int UpdateChannelDBEx(int issave)  
{  
	int result = -1 ;  
	  
	if (issave)  
		result = UpdateChannelDB() ;  
	else  
	if (pChMgr)  
	{  
		InitChannelDBCount = 0;  
		if (pChMgr->pChCtrl)  
			free(pChMgr->pChCtrl) ;  
		if (pChMgr->physChInfo)  
			free(pChMgr->physChInfo) ;  
		if (pChMgr->localChInfo)  
			free(pChMgr->localChInfo) ;  
		if (pChMgr->tvInfo)  
			free(pChMgr->tvInfo) ;  
		free(pChMgr) ;  
		pChMgr = NULL ;  
		result = InitChannelDB() ;  
	}  
	return (result) ;	  
}  
  
int EmptyChannelDB(int tvtype)  
{  
	int result = 0 ;  
	  
	if (pChMgr  
		&& (tvtype == TVTYPE_ATV || tvtype == TVTYPE_DTV || tvtype == TVTYPE_FTV)  
		)  
	{  
		int i, j ;  
		  
		for (j=0; jpChCtrl->itemCount; j++)  
		{  
			if (pChMgr->physChInfo[j].tvtype == tvtype)  
				break ;  
		}  
		if (jpChCtrl->itemCount)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				  
				if (pChMgr->physChInfo[i].tvtype != tvtype  
					&& i > j  
					)  
				{  
					memcpy(&(pChMgr->physChInfo[j]), &(pChMgr->physChInfo[i]), sizeof(TVChannelInfo)) ;  
					memcpy(&(pChMgr->localChInfo[j]), &(pChMgr->localChInfo[i]), sizeof(LocalChannelRecord)) ;  
					memcpy(&(pChMgr->tvInfo[j]), &(pChMgr->tvInfo[i]), sizeof(TVINFO)) ;  
					j++ ;  
				}  
			}  
			pChMgr->pChCtrl->itemCount = j ;  
		}  
		  
	}  
	  
	return (result) ;  
}  
  
  
//从过程服务器刷新数据库  
int NetRefreshChannelDB(int tvtype, int sync)  
{  
	int result = -1 ;  
  
	return (result) ;  
}  
//使用本地数据更新过程服务器  
int NetUpdateChannelDB(int tvtype, int sync)  
{  
	int result = -1 ;  
		  
	return (result) ;	  
}  
  
  
//过滤频道数据库  
/*result:返回的搜索结果,数组中元素为频道号;.  
Return:过滤后的result总数*/  
int FilterChannelDB(LocalChannelRecord * filter,int mask,int **result)  
{  
	int r = 0 ;  
	int *r_arr = NULL ;  
	int r_count = 0 ;  
	  
	if (  
		((filter && mask) || (mask&FILTER_MASK_ALL_ATV) || (mask&FILTER_MASK_ALL_DTV) || (mask&FILTER_MASK_ALL_FTV))  
		&& result  
		&& pChMgr  
		&& pChMgr->pChCtrl  
		)  
	{  
		int i, j, filted_mask = 0 ;  
		  
		r_arr = pChMgr->filterArr ;  
		r_count = pChMgr->filterArrCount ;  
		  
		if (r_arr == NULL  
			|| r_count < pChMgr->pChCtrl->mallocCount)  
		{  
			if (r_arr) free(r_arr) ;  
			r_arr = (int *)malloc(sizeof(int)*pChMgr->pChCtrl->mallocCount) ;  
			r_count = pChMgr->pChCtrl->mallocCount ;  
		}  
		if (r_arr == NULL)  
			goto done ;  
		  
		memset(r_arr, 0x00, sizeof(int)*pChMgr->pChCtrl->itemCount) ;  
		  
		if (mask&FILTER_MASK_UDEFINE)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if ((pChMgr->localChInfo[i].udefine&filter->udefine) == filter->udefine)  
					r_arr[i] |= FILTER_MASK_UDEFINE ;  
			}  
			filted_mask |= FILTER_MASK_UDEFINE ;  
		}  
  
		if (mask&FILTER_MASK_TIME)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if (pChMgr->localChInfo[i].isTiming == filter->isTiming)	  
					r_arr[i] |= FILTER_MASK_TIME ;  
			}  
			filted_mask |= FILTER_MASK_TIME ;  
		}  
  
		if (mask&FILTER_MASK_RANGE)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if (pChMgr->localChInfo[i].rangetype == filter->rangetype)	  
					r_arr[i] |= FILTER_MASK_RANGE ;  
			}  
			filted_mask |= FILTER_MASK_RANGE ;  
		}  
  
		if (mask&FILTER_MASK_PWD)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if (pChMgr->localChInfo[i].needpwd == filter->needpwd)	  
					r_arr[i] |= FILTER_MASK_PWD ;  
			}  
			filted_mask |= FILTER_MASK_PWD ;  
		}  
  
		if (mask&FILTER_MASK_EPG)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if (pChMgr->localChInfo[i].haveEPG == filter->haveEPG)	  
					r_arr[i] |= FILTER_MASK_EPG ;  
			}  
			filted_mask |= FILTER_MASK_EPG ;  
		}  
  
		if (mask&FILTER_MASK_CONTENT)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if (pChMgr->localChInfo[i].contenttype == filter->contenttype)	  
					r_arr[i] |= FILTER_MASK_CONTENT ;  
			}  
			filted_mask |= FILTER_MASK_CONTENT ;  
		}  
  
		if (mask&FILTER_MASK_ALL_ATV)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if (pChMgr->physChInfo[i].tvtype == TVTYPE_ATV)  
					r_arr[i] |= FILTER_MASK_ALL_ATV ;  
			}  
			filted_mask |= FILTER_MASK_ALL_ATV ;  
		}  
  
		if (mask&FILTER_MASK_ALL_DTV)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if (pChMgr->physChInfo[i].tvtype == TVTYPE_DTV)  
					r_arr[i] |= FILTER_MASK_ALL_DTV ;  
			}  
			filted_mask |= FILTER_MASK_ALL_DTV ;  
		}  
  
		if (mask&FILTER_MASK_ALL_FTV)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if (pChMgr->physChInfo[i].tvtype == TVTYPE_FTV)  
					r_arr[i] |= FILTER_MASK_ALL_FTV ;  
			}  
			filted_mask |= FILTER_MASK_ALL_FTV ;  
		}  
/* 目前未能确定 */  
#if 0  
		if (mask&FILTER_MASK_FREQ)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				int freq = 0 ;  
				  
				if (pChMgr->physChInfo[i].type == TVTYPE_ATV)  
					freq = pChMgr->physChInfo[i].info.atv.ndiv * 16 ;  
				else  
				if (pChMgr->physChInfo[i].type == TVTYPE_DTV)  
					freq = pChMgr->physChInfo[i].info.dtv.freq ;  
				else  
					continue ;  
					  
				if (freq == filter->freq)	  
					r_arr[i] |= FILTER_MASK_RANGE ;  
			}  
		}  
  
		if (mask&FILTER_MASK_NAME)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if (filter->name[0] == 0x00)  
				{  
					if (pChMgr->tvInfo[i].name[0] == 0x00)  
						r_arr[i] |= FILTER_MASK_NAME ;  
				}  
				else  
				{  
					if (strcmp(pChMgr->tvInfo[i].name, filter->name) == 0)  
						r_arr[i] |= FILTER_MASK_NAME ;  
				}  
			}  
		}  
  
		if (mask&FILTER_MASK_STREAM)  
		{  
  
		}  
  
		if (mask&FILTER_MASK_FAVORITE)  
		{  
  
		}  
  
		if (mask&FILTER_MASK_REVERSE)  
		{  
  
		}  
#endif  
		  
		for (i=0,j=0; ipChCtrl->itemCount; i++)  
		{  
			if ((r_arr[i]&filted_mask))  
				r_arr[i] = pChMgr->localChInfo[i].channelid ;  
			else  
				r_arr[i] |= 0xFFFFFFFF ;  
		}  
		for (i=0,j=0; ipChCtrl->itemCount; i++)  
		{  
			if (r_arr[i]^0xFFFFFFFF)  
			{  
				if (j != i) r_arr[j] = r_arr[i] ;  
				j++ ;  
			}  
		}  
		  
		r = j ;  
		*result = r_arr ;  
		pChMgr->filterArr = r_arr ;  
		pChMgr->filterArrCount = r_count ;  
	}  
  
done:  
	return (r) ;  
}  
  
//取得指定频道的信息;  
LocalChannelRecord *GetChannelLogicInfo(int channelid)  
{  
	LocalChannelRecord *result = NULL ;  
	  
	if (channelid >= 0  
		&& pChMgr  
		&& pChMgr->pChCtrl  
		&& pChMgr->pChCtrl->itemCount > 0  
		&& pChMgr->localChInfo  
		)  
	{  
		int i ;  
		  
		for (i=0; ipChCtrl->itemCount; i++)  
		{  
			if (pChMgr->localChInfo[i].channelid == channelid)  
			{  
				result = &(pChMgr->localChInfo[i]) ;  
				break ;  
			}  
		}  
	}  
	  
	return (result) ;  
}  
  
//取得指定频道的物理信息;  
TVChannelInfo * GetChannelPhysInfo(int channelid)  
{  
	TVChannelInfo *result = NULL ;  
	  
	  
	GLH_DEBUG("\nGetChannelPhysInfo>>>pChMGr=%08x,chctrl=%08x,phys=%08x.local=%08x.",pChMgr,pChMgr?pChMgr->pChCtrl : NULL,  
			pChMgr?pChMgr->physChInfo : NULL,  
			pChMgr?pChMgr->localChInfo : NULL);  
			  
	GLH_DEBUG("\nGetChannelPhysInfo>>>pchCtrlItems=%d",pChMgr ? (pChMgr->pChCtrl ? pChMgr->pChCtrl->itemCount : -1) : -1);  
	if (channelid >= 0  
		&& pChMgr  
		&& pChMgr->pChCtrl  
		&& pChMgr->pChCtrl->itemCount > 0  
		&& pChMgr->physChInfo  
		&& pChMgr->localChInfo  
		)  
	{  
		int i ;  
		  
		for (i=0; ipChCtrl->itemCount; i++)  
		{  
			if (pChMgr->localChInfo[i].channelid == channelid)  
			{  
				GLH_DEBUG("\nGetChannelPhysInfo>>>find a item=%d",i);  
				result = &(pChMgr->physChInfo[i]) ;  
				break ;  
			}  
		}  
	}  
	  
	return (result) ;  
}  
  
//取得指定频道的名称;  
char * GetChannelName(int channelid)  
{  
	char *result = NULL ;  
	  
	if (channelid >= 0  
		&& pChMgr  
		&& pChMgr->pChCtrl  
		&& pChMgr->pChCtrl->itemCount > 0  
		&& pChMgr->tvInfo  
		&& pChMgr->localChInfo  
		)  
	{  
		int i ;  
		  
		for (i=0; ipChCtrl->itemCount; i++)  
		{  
			if (pChMgr->localChInfo[i].channelid == channelid)  
			{  
				if (pChMgr->localChInfo[i].nameid >= 0  
					&& pChMgr->localChInfo[i].nameid < pChMgr->pChCtrl->itemCount  
					)  
				{  
					result = pChMgr->tvInfo[i].name ;  
				}  
				break ;  
			}  
		}  
	}  
	  
	return (result) ;  
}  
  
//设置指定频道的名称;  
void SetChannelName(int channelid,char * newname)  
{  
	if (channelid >= 0  
		&& newname  
		&& pChMgr  
		&& pChMgr->pChCtrl  
		&& pChMgr->pChCtrl->itemCount > 0  
		&& pChMgr->tvInfo  
		&& pChMgr->localChInfo  
		)  
	{  
		int i ;  
		  
		for (i=0; ipChCtrl->itemCount; i++)  
		{  
			if (pChMgr->localChInfo[i].channelid == channelid)  
			{  
				if (pChMgr->localChInfo[i].nameid >= 0  
					&& pChMgr->localChInfo[i].nameid < pChMgr->pChCtrl->itemCount  
					)  
				{  
					memset(pChMgr->tvInfo[i].name, 0x00, sizeof(pChMgr->tvInfo[0].name)) ;  
					strncpy(pChMgr->tvInfo[i].name, newname, sizeof(pChMgr->tvInfo[0].name)-1) ;  
				}  
				break ;  
			}  
		}  
	}	  
}  
  
//设置指定频道的物理信息;  
void SetChannelPhysInfo(int channelid, TVChannelInfo * info)  
{  
	if (channelid >= 0  
		&& info  
		&& pChMgr  
		&& pChMgr->pChCtrl  
		&& pChMgr->pChCtrl->itemCount > 0  
		&& pChMgr->physChInfo  
		&& pChMgr->localChInfo  
		)  
	{  
		int i ;  
		  
		for (i=0; ipChCtrl->itemCount; i++)  
		{  
			if (pChMgr->localChInfo[i].channelid == channelid)  
			{  
				memcpy(&(pChMgr->physChInfo[i]), info, sizeof(TVChannelInfo)) ;  
				break ;  
			}  
		}  
	}  
}  
  
//设置指定频道的逻辑信息;  
void SetChannelLogicInfo(int channelid, LocalChannelRecord * info)  
{  
	if (channelid >= 0  
		&& info  
		&& pChMgr  
		&& pChMgr->pChCtrl  
		&& pChMgr->pChCtrl->itemCount > 0  
		&& pChMgr->localChInfo  
		)  
	{  
		int i ;  
		  
		for (i=0; ipChCtrl->itemCount; i++)  
		{  
			if (pChMgr->localChInfo[i].channelid == channelid)  
			{  
				memcpy(&(pChMgr->localChInfo[i]), info, sizeof(LocalChannelRecord)) ;  
				pChMgr->localChInfo[i].nameid = (short)i ;  
				break ;  
			}  
		}  
	}  
}  
  
/*增加频道,name:新频道的名称,可以为NULL;  
phys:频道物理信息;  
logic:频道逻辑信息可以为NULL,返回:0:成功,其它:出错;*/  
int AddChannel(char * name, TVChannelInfo* phys, LocalChannelRecord* logic)  
{  
	int result = -1 ;  
	  
	if (phys  
		&& pChMgr  
		&& pChMgr->pChCtrl  
		)  
	{  
		LocalChannelRecord mylogic ;  
		  
		if (logic)  
		{  
			mylogic = *logic ;  
		}  
		else  
		{  
			bzero(&mylogic, sizeof(LocalChannelRecord)) ;  
			mylogic.tvtype = phys->tvtype ;  
		}  
		  
		logic = &mylogic ;  
		  
		if (pChMgr->pChCtrl->mallocCount > 0  
			&& pChMgr->pChCtrl->itemCount < pChMgr->pChCtrl->mallocCount  
			)  
		{  
			int tvid = 0 ;  
			int newid ;  
			  
			GLH_DEBUG("now Add Channel... \n") ;  
			if (phys->tvtype == TVTYPE_ATV)  
				tvid = phys->info.atv.nchid ;  
			else  
			if (phys->tvtype == TVTYPE_DTV)  
				tvid = phys->info.dtv.tvid ;  
			else  
				goto done ;  
  
			if (GetNewChannelid(phys->tvtype, &newid) != 0)  
				goto done ;  
			  
			memcpy(&(pChMgr->physChInfo[pChMgr->pChCtrl->itemCount]), phys, sizeof(pChMgr->physChInfo[pChMgr->pChCtrl->itemCount])) ;  
			memcpy(&(pChMgr->localChInfo[pChMgr->pChCtrl->itemCount]), logic, sizeof(pChMgr->localChInfo[pChMgr->pChCtrl->itemCount])) ;  
			pChMgr->localChInfo[pChMgr->pChCtrl->itemCount].channelid = newid ;  
			pChMgr->localChInfo[pChMgr->pChCtrl->itemCount].nameid = (short)pChMgr->pChCtrl->itemCount ;  
			memset(&(pChMgr->tvInfo[pChMgr->pChCtrl->itemCount]), 0x00, sizeof(pChMgr->tvInfo[pChMgr->pChCtrl->itemCount])) ;  
			pChMgr->tvInfo[pChMgr->pChCtrl->itemCount].tvid = tvid ;  
			if (name  
				&& name[0]  
				)  
				strncpy(pChMgr->tvInfo[pChMgr->pChCtrl->itemCount].name, name, sizeof(pChMgr->tvInfo[pChMgr->pChCtrl->itemCount].name)-1) ;  
			else  
			if (name == NULL)  
				strcpy(pChMgr->tvInfo[pChMgr->pChCtrl->itemCount].name, "未命名") ;  
				  
			pChMgr->pChCtrl->itemCount++ ;  
			  
			result = 0 ;  
		}  
	}  
	  
done:  
	return (result) ;  
}  
  
int AddChannelEx(char * name, TVChannelInfo* phys, LocalChannelRecord* logic, int flags)  
{  
	int result = -1 ;  
	  
	if (phys  
		&& pChMgr  
		&& pChMgr->pChCtrl  
		)  
	{  
		LocalChannelRecord mylogic ;  
		  
		if (logic)  
		{  
			mylogic = *logic ;  
		}  
		else  
		{  
			bzero(&mylogic, sizeof(LocalChannelRecord)) ;  
			mylogic.tvtype = phys->tvtype ;  
		}  
  
		logic = &mylogic ;  
			  
		if (pChMgr->pChCtrl->mallocCount > 0  
			&& pChMgr->pChCtrl->itemCount < pChMgr->pChCtrl->mallocCount  
			)  
		{  
			int i, j ;  
			int tvid = 0 ;  
			int newid ;  
			  
			GLH_DEBUG("now Add ChannelEx... tvtype=%d\n",phys->tvtype) ;  
			if (phys->tvtype == TVTYPE_ATV)  
				tvid = phys->info.atv.nchid ;  
			else  
			if (phys->tvtype == TVTYPE_DTV)  
				tvid = phys->info.dtv.tvid ;  
			else  
				goto done ;  
				  
			if (phys->tvtype == TVTYPE_ATV  
				&& (flags&ADDCHANNEL_FLGAS_AUTOMATCH)  
				)  
			{  
				for (i=0; ipChCtrl->itemCount; i++)  
				{  
					if (pChMgr->physChInfo[i].tvtype == phys->tvtype  
						&& phys->info.atv.ndiv >= (pChMgr->physChInfo[i].info.atv.ndiv - 16)  
						&& phys->info.atv.ndiv <= (pChMgr->physChInfo[i].info.atv.ndiv + 16)  
					)  
					{  
						GLH_DEBUG("get match item ... \n") ;  
						if (flags&ADDCHANNEL_FLGAS_TEST)  
						{  
							result = pChMgr->localChInfo[i].channelid ;  
							goto done ;  
						}  
						tvid = pChMgr->physChInfo[i].info.atv.nchid ;  
						memcpy(&(pChMgr->physChInfo[i]), phys, sizeof(pChMgr->physChInfo[i])) ;  
						pChMgr->physChInfo[i].info.atv.nchid = tvid ;  
						result = pChMgr->localChInfo[i].channelid ;  
						goto done ;  
					}  
				}  
			}  
			else  
			if (phys->tvtype == TVTYPE_DTV  
				&& (flags&ADDCHANNEL_FLGAS_AUTOMATCH)  
				)  
			{  
				  
			}  
			  
			if (GetNewChannelid(phys->tvtype, &newid) != 0)  
				goto done ;  
			  
			if (flags&ADDCHANNEL_FLGAS_TEST)  
			{  
				result = newid ;  
				goto done ;  
			}  
						  
			memcpy(&(pChMgr->physChInfo[pChMgr->pChCtrl->itemCount]), phys, sizeof(pChMgr->physChInfo[pChMgr->pChCtrl->itemCount])) ;  
			memcpy(&(pChMgr->localChInfo[pChMgr->pChCtrl->itemCount]), logic, sizeof(pChMgr->localChInfo[pChMgr->pChCtrl->itemCount])) ;  
			pChMgr->localChInfo[pChMgr->pChCtrl->itemCount].channelid = newid ;  
			pChMgr->localChInfo[pChMgr->pChCtrl->itemCount].nameid = (short)pChMgr->pChCtrl->itemCount ;  
			memset(&(pChMgr->tvInfo[pChMgr->pChCtrl->itemCount]), 0x00, sizeof(pChMgr->tvInfo[pChMgr->pChCtrl->itemCount])) ;  
			pChMgr->tvInfo[pChMgr->pChCtrl->itemCount].tvid = tvid ;  
			if (name  
				&& name[0]  
				)  
				strncpy(pChMgr->tvInfo[pChMgr->pChCtrl->itemCount].name, name, sizeof(pChMgr->tvInfo[pChMgr->pChCtrl->itemCount].name)-1) ;  
			else  
			if (name == NULL)  
				strcpy(pChMgr->tvInfo[pChMgr->pChCtrl->itemCount].name, "未命名") ;  
				  
			pChMgr->pChCtrl->itemCount++ ;  
			  
			result = newid ;  
			GLH_DEBUG("now Add ChannelEx... newid=%d\n",newid) ;  
		}  
	}  
	  
done:  
	return (result) ;  
}  
  
//删除指定频道;  
int DelChannel(int channelid)  
{  
	int result = 0 ;  
  
	if (channelid >= 0  
		&& pChMgr  
		&& pChMgr->pChCtrl  
		&& pChMgr->pChCtrl->itemCount > 0  
		)  
	{  
		int i ;  
		  
		if (pChMgr->localChInfo)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if (pChMgr->localChInfo[i].channelid == channelid)  
				{  
					int copyCount = pChMgr->pChCtrl->itemCount - i - 1 ;  
					  
					if (copyCount > 0)  
						memcpy(&(pChMgr->localChInfo[i]), &(pChMgr->localChInfo[i+1]), copyCount*sizeof(LocalChannelRecord)) ;  
					result |= 0x01 ;  
					if (pChMgr->physChInfo)  
					{  
							if (copyCount > 0)  
								memcpy(&(pChMgr->physChInfo[i]), &(pChMgr->physChInfo[i+1]), copyCount*sizeof(TVChannelInfo)) ;  
							result |= 0x02 ;  
					}  
					if (pChMgr->tvInfo)  
					{  
							if (copyCount > 0)  
								memcpy(&(pChMgr->tvInfo[i]), &(pChMgr->tvInfo[i+1]), copyCount*sizeof(TVINFO)) ;  
							result |= 0x04 ;  
					}  
					pChMgr->pChCtrl->itemCount-- ;  
					  
					break ;  
				}  
			}  
		}  
	}  
	  
	if (result)  
		result = 0 ;  
	else  
		result = -1 ;  
		  
	return (result) ;  
}  
  
  
//设置频道的TvId  
int SetChannelTvId(int channelid, int tvid)  
{  
	int result = -1 ;  
	  
	if (channelid >= 0  
		&& pChMgr  
		&& pChMgr->pChCtrl  
		&& pChMgr->pChCtrl->itemCount > 0  
		&& tvid > 0  
		)  
	{  
		int i ;  
		  
		if (pChMgr->localChInfo)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if (pChMgr->localChInfo[i].channelid == channelid)  
				{  
					int flags = 0 ;  
					  
					if (pChMgr->tvInfo)  
					{  
						pChMgr->tvInfo[i].tvid = tvid ;  
						flags |= 0x10 ;  
					}  
  
#if 1					  
					if (pChMgr->physChInfo)  
					{  
						if (pChMgr->physChInfo[i].tvtype == TVTYPE_ATV)  
						{  
							pChMgr->physChInfo[i].info.atv.nchid = tvid ;  
							flags |= 0x01 ;  
						}  
						else  
						if (pChMgr->physChInfo[i].tvtype == TVTYPE_DTV)  
						{  
							pChMgr->physChInfo[i].info.dtv.tvid = tvid ;  
							flags |= 0x02 ;  
						}  
						else  
						if (pChMgr->physChInfo[i].tvtype == TVTYPE_FTV)  
						{  
							/* 功能电视无tvid这一项 */  
						}  
					}  
#endif  
					  
					if (flags)  
						result = 0 ;  
  
					break ;  
				}  
			}  
		}  
			  
	}  
	  
	return (result) ;	  
}  
  
  
//获取频道的TvId  
int GetChannelTvId(int channelid, int *ptvid)  
{  
	int result = -1 ;  
	  
	if (channelid >= 0  
		&& pChMgr  
		&& pChMgr->pChCtrl  
		&& pChMgr->pChCtrl->itemCount > 0  
		&& ptvid  
		)  
	{  
		int i ;  
		  
		if (pChMgr->localChInfo)  
		{  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if (pChMgr->localChInfo[i].channelid == channelid)  
				{  
					if (pChMgr->tvInfo  
						&& pChMgr->tvInfo[i].tvid > 0  
						)  
					{  
						*ptvid = pChMgr->tvInfo[i].tvid ;  
						result = 0 ;  
						break ;  
					}  
				}  
			}  
		}  
			  
	}  
	  
	return (result) ;  
}  
  
  
//获取TvId对应的频道Id(存在多个频道对就一个TvId的情况)  
/*  
tvid(INPUT)			要查找的tvid  
pchannelid(OUTPUT)	存放channelid的数组  
maxitems(INPUT)		存放channelid的数组的最大长度  
pnums(OUTPUT)		实际取到的项  
*/  
int GetTvChannelId(int tvid, int *pchannelid, int maxitems, int *pnums)  
{  
	int result = -1 ;  
	  
	if (tvid > 0  
		&& pChMgr  
		&& pChMgr->pChCtrl  
		&& pChMgr->pChCtrl->itemCount > 0  
		&& (pchannelid || pnums)  
		)  
	{  
		int i ;  
		  
		if (pChMgr->localChInfo  
			&& pChMgr->tvInfo  
			)  
		{  
			int n = 0 ;  
			int nn = 0 ;  
			  
			for (i=0; ipChCtrl->itemCount; i++)  
			{  
				if (pChMgr->tvInfo[i].tvid == tvid)  
				{  
					if (pchannelid  
						&& n < maxitems  
						)  
					{  
						pchannelid[n] = pChMgr->localChInfo[i].channelid ;  
						n++ ;  
					}  
					nn++ ;  
				}  
			}  
			if (pnums) *pnums = nn ;  
			if (nn > 0)  
				result = 0 ;  
		}  
			  
	}  
	  
	return (result) ;  
}  
  
  
/* 频道互换 */  
int ChannelSwap(int channelid0, int channelid1)  
{  
	int result = -1 ;  
	int i, i0, i1 ;  
	  
	if (pChMgr  
		&& pChMgr->pChCtrl  
		&& pChMgr->pChCtrl->itemCount > 0  
		&& pChMgr->localChInfo  
		)  
	{  
		for (i=0,i0=-1,i1=-1; ipChCtrl->itemCount; i++)  
		{  
			if (pChMgr->localChInfo[i].channelid == channelid0)  
				i0 = i ;  
			else  
			if (pChMgr->localChInfo[i].channelid == channelid1)  
				i1 = i ;  
				  
			if (i0 >= 0  
				&& i1 >= 0  
				)  
			{  
				if (pChMgr->physChInfo[i0].tvtype == pChMgr->physChInfo[i1].tvtype)  
				{  
						TVChannelInfo 		physChInfo ;  
						LocalChannelRecord	localChInfo ;  
						TVINFO				tvInfo ;  
						  
						/* 交换物理存放位置 */  
						if (pChMgr->physChInfo)  
						{  
							memcpy(&physChInfo, &(pChMgr->physChInfo[i0]), sizeof(TVChannelInfo)) ;  
							memcpy(&(pChMgr->physChInfo[i0]), &(pChMgr->physChInfo[i1]), sizeof(TVChannelInfo)) ;  
							memcpy(&(pChMgr->physChInfo[i1]), &physChInfo, sizeof(TVChannelInfo)) ;  
						}  
						memcpy(&localChInfo, &(pChMgr->localChInfo[i0]), sizeof(LocalChannelRecord)) ;  
						memcpy(&(pChMgr->localChInfo[i0]), &(pChMgr->localChInfo[i1]), sizeof(LocalChannelRecord)) ;  
						memcpy(&(pChMgr->localChInfo[i1]), &localChInfo, sizeof(LocalChannelRecord)) ;  
						if (pChMgr->tvInfo)  
						{  
							memcpy(&tvInfo, &(pChMgr->tvInfo[i0]), sizeof(TVINFO)) ;  
							memcpy(&(pChMgr->tvInfo[i0]), &(pChMgr->tvInfo[i1]), sizeof(TVINFO)) ;  
							memcpy(&(pChMgr->tvInfo[i1]), &tvInfo, sizeof(TVINFO)) ;  
						}  
						/* 交换频道号 */  
						pChMgr->localChInfo[i0].channelid = channelid0 ;  
						pChMgr->localChInfo[i1].channelid = channelid1 ;  
						  
						result = 0 ;  
				}  
				break ;	  
			}  
		}  
	}  
	  
	return (result) ;	  
}  
  
  
//==================================================================//  
//频道类型管理s  
typedef struct tagTvtypeCtrl  
{  
	TVTYPE	*pList ;  
	int		bInitialized ;  
	int		itemCount ;  
	int		mallocCount ;  
} TvtypeCtrl_t, *pTvtypeCtrl_t ;  
  
#define CH_TYPE_CTRL_ALLOC_NUMS	10	//每次分配的项数  
  
static TvtypeCtrl_t *pTvTypeCtrl = NULL ;  
  
/*  
名称      ID           使用的标识位  
全部       1             0X00000001  
喜爱       2             0X00000002  
体育       3             0X00000004  
影视       4             0X00000008  
新闻       5             0X00000010  
儿童       6             0X00000020  
音乐       7             0X00000040  
教育       8             0X00000080  
文艺       9             0X00000100  
我的分类   10            0X00000200  
*/  
static TVTYPE tvtypeinfo[] =   
{  
	{0X00000001,	0,	0x00000000,	"全部"},  
	{0X00000002,	1,	0x00000000,	"喜爱"},  
	{0X00000004,	2,	0x00000000,	"体育"},  
	{0X00000008,	3,	0x00000000,	"影视"},  
	{0X00000010,	4,	0x00000000,	"新闻"},  
	{0X00000020,	5,	0x00000000,	"儿童"},  
	{0X00000040,	6,	0x00000000,	"音乐"},  
	{0X00000080,	7,	0x00000000,	"教育"},  
	{0X00000100,	8,	0x00000000,	"文艺"},  
	{0X00000200,	9,	0x00000000,	"我的分类"},  
} ;  
  
// 频道类型数据库的初始化数据  
static int ChannelTypeDataInit()  
{  
	int result = -1 ;  
	  
	if (pTvTypeCtrl  
		&& (!pTvTypeCtrl->bInitialized)  
		)  
	{  
		int count = sizeof(tvtypeinfo)/sizeof(TVTYPE) ;  
		  
		if (count + pTvTypeCtrl->itemCount > pTvTypeCtrl->mallocCount)  
		{  
			TVTYPE *p = (TVTYPE *)malloc((count + pTvTypeCtrl->itemCount)*sizeof(TVTYPE)) ;  
			  
			if (p)  
			{  
				if (pTvTypeCtrl->itemCount > 0  
					&& pTvTypeCtrl->pList  
					)  
				{  
					memcpy(p, pTvTypeCtrl->pList, pTvTypeCtrl->itemCount*sizeof(TVTYPE)) ;  
					free(pTvTypeCtrl->pList) ;  
				}  
				pTvTypeCtrl->pList = p ;  
			}  
			else  
			{  
				goto done ;	  
			}  
			pTvTypeCtrl->mallocCount = count + pTvTypeCtrl->itemCount ;  
		}  
		  
		if (count + pTvTypeCtrl->itemCount <= pTvTypeCtrl->mallocCount)  
		{  
			memcpy(&(pTvTypeCtrl->pList[pTvTypeCtrl->itemCount]), tvtypeinfo, sizeof(tvtypeinfo)) ;  
			pTvTypeCtrl->itemCount += count ;  
			pTvTypeCtrl->bInitialized = 1 ;  
			result = 0 ;  
		}  
	}  
	  
done:  
	return (result) ;  
}  
  
/* 初始化频道类型数据库 */  
int InitChannelTypeDB()  
{  
	int result = -1 ;  
	FILE *fr ;  
	  
	if (pTvTypeCtrl)  
	{  
		if (pTvTypeCtrl->pList)	free(pTvTypeCtrl->pList);  
		free(pTvTypeCtrl) ;  
		pTvTypeCtrl = NULL ;  
	}  
	  
	pTvTypeCtrl = (TvtypeCtrl_t *)malloc(sizeof(TvtypeCtrl_t)) ;  
	if (pTvTypeCtrl == NULL)  
		goto done ;  
		  
	memset(pTvTypeCtrl, 0x00, sizeof(TvtypeCtrl_t)) ;  
	  
	/* 从存储器装载,并初化pTvTypeCtrl指向的结构 */  
	fr = fopen(CHTYPE_DB_FILE, "rb") ;  
	if (fr)  
	{  
		int n = 0 ;  
		  
		if (fseek(fr, 0, SEEK_END) == 0)  
			n = (int)ftell(fr) ;  
		fseek(fr, 0, SEEK_SET) ;  
		  
		if (n >= (int)sizeof(TvtypeCtrl_t)  
			&& fread(pTvTypeCtrl, (size_t)sizeof(TvtypeCtrl_t), 1, fr) == 1  
			&& pTvTypeCtrl->itemCount > 0  
			)  
		{  
			size_t size0 = (size_t)(sizeof(TVTYPE)*pTvTypeCtrl->itemCount) ;  
			  
			TVTYPE *pList = (TVTYPE *)malloc(size0) ;  
			if (pList  
				&& n >= (sizeof(TvtypeCtrl_t) + size0)  
				&& (fread(pList, sizeof(TVTYPE), pTvTypeCtrl->itemCount, fr) == pTvTypeCtrl->itemCount)  
				)  
			{  
				pTvTypeCtrl->pList = pList ;  
				pTvTypeCtrl->mallocCount = pTvTypeCtrl->itemCount ;  
				result = 0 ;  
			}  
			else  
			{  
				GLH_DEBUG("open database %s error \n", CHTYPE_DB_FILE) ;  
				if (pList) free(pList) ;  
				memset(pTvTypeCtrl, 0x00, sizeof(TvtypeCtrl_t)) ;  
			}  
		}  
		else  
		if (n <= 0)  
		{  
			result = 0 ;  
			GLH_DEBUG("open database %s(empty) ok \n", CHTYPE_DB_FILE) ;  
		}  
		fclose(fr) ;  
	}  
	  
	/* 初始情况下,初始化数据库,加入固定数据 */  
	if (!pTvTypeCtrl->bInitialized)  
		ChannelTypeDataInit() ;  
		  
done:  
	return (result) ;  
}  
  
/* 关闭频道类型数据库 */  
int CloseChannelTypeDB()  
{  
	int result = -1 ;  
	FILE *fw ;  
	  
	if (pTvTypeCtrl)  
	{		  
		/* 释放资源 */  
		if (pTvTypeCtrl->pList) free(pTvTypeCtrl->pList) ;  
		free(pTvTypeCtrl) ;  
		pTvTypeCtrl = NULL ;  
	}  
	result = 0 ;  
		  
	return (result) ;  
}  
  
/* 更新频道类型数据库 */  
int UpdateChannelTypeDB()  
{  
	int result = -1 ;  
	FILE *fw ;  
	  
	if (pTvTypeCtrl)  
	{  
		/* 写入数据到存储器中 */  
		if (pTvTypeCtrl->itemCount >= 0)  
		{  
			fw = fopen(CHTYPE_DB_FILE, "wb") ;  
			if (fw)  
			{  
				if (pTvTypeCtrl->itemCount > 0)  
				{  
					fwrite(pTvTypeCtrl, sizeof(TvtypeCtrl_t), 1, fw) ;  
					fwrite(pTvTypeCtrl->pList, sizeof(TVTYPE)*pTvTypeCtrl->itemCount, 1, fw) ;  
				}  
				fclose(fw) ;  
				result = 0 ;  
			}  
		}	  
	}  
		  
	return (result) ;  
}  
  
/* 清空频道类型数据库 */  
int EmptyChannelTypeDB(void)  
{  
	int result = -1 ;  
	  
	if (pTvTypeCtrl)  
	{  
		if (pTvTypeCtrl->pList)	free(pTvTypeCtrl->pList) ;  
		pTvTypeCtrl->pList = NULL ;  
		pTvTypeCtrl->itemCount = 0 ;  
		pTvTypeCtrl->mallocCount = 0 ;  
		result = 0 ;  
	}  
	  
	return (result) ;  
}  
  
/* 从过程服务器刷新数据库 */  
int NetRefreshChannelTypeDB(int tvtype, int sync)  
{  
	int result = -1 ;  
	int r ;  
	TVTYPE *tvtype = NULL ;  
	int nums = 0 ;  
	  
	if (pTvTypeCtrl)  
	{  
		r = GetTvTypeTblData(&tvtype, &nums) ;  
		if (r == 0  
			&& tvtype  
			&& nums > 0  
			)  
		{  
			EmptyChannelTypeDB() ;  
			pTvTypeCtrl->pList = (TVTYPE *)malloc(sizeof(TVTYPE)*nums) ;  
			if (pTvTypeCtrl->pList)  
			{  
				memcpy(pTvTypeCtrl->pList, tvtype, sizeof(TVTYPE)*nums) ;  
				pTvTypeCtrl->itemCount = nums ;  
				pTvTypeCtrl->mallocCount = nums ;  
				  
				UpdateChannelTypeDB() ;  
				result = 0 ;  
			}  
		}  
		else  
		{  
			GLH_DEBUG("NetRefreshChannelTypeDB() Error! \n") ;  
		}  
		FreeTvTypeTblData() ;  
	}  
	  
	return (result) ;  
}  
  
/* 使用本地数据更新过程服务器 */  
int NetUpdateChannelTypeDB(int tvtype, int sync)  
{  
	int result = -1 ;  
	  
	return (result) ;  
}  
  
/* 增加频道类型,返回分配给其的台名分类ID(>0) */  
int AddChannelType(TVTYPE *tvtype)  
{  
	int result = -1 ;  
	  
	if (pTvTypeCtrl  
		&& tvtype  
		)  
	{  
		if (pTvTypeCtrl->mallocCount <= pTvTypeCtrl->itemCount)  
		{  
			TVTYPE *p ;  
			int count = (((pTvTypeCtrl->itemCount + 1)/CH_TYPE_CTRL_ALLOC_NUMS) + 1) * CH_TYPE_CTRL_ALLOC_NUMS ;  
			  
			p = (TVTYPE *)malloc(count * sizeof(TVTYPE)) ;  
			if (p)  
			{  
				memcpy(p, pTvTypeCtrl->pList, pTvTypeCtrl->itemCount*sizeof(TVTYPE)) ;  
				free(pTvTypeCtrl->pList) ;  
				pTvTypeCtrl->pList = p ;  
				pTvTypeCtrl->mallocCount = count ;  
			}  
			else  
			{  
				goto done ;  
			}  
		}  
		  
		if (pTvTypeCtrl->mallocCount > pTvTypeCtrl->itemCount)  
		{  
			TVTYPE *p = &(pTvTypeCtrl->pList[pTvTypeCtrl->itemCount]) ;  
			  
			/* id 分配规则, 低16位为"固定项",高16位为"用户自定义项",  
			  通过AddChannelType()加入的为全部为"用户自定义项" */  
			memcpy(p, tvtype, sizeof(TVTYPE)) ;  
			/* 查找空白(未使用)分类 */  
			{  
				int id = 0 ;  
				int i ;  
				  
				for (i=0; iitemCount; i++)  
				{  
					id ^= pTvTypeCtrl->pList[i].type_id ;  
				}  
				for (i=16; i<32; i++)  
				{  
					if (((id>>i)&1) == 0)  
					{  
						p->type_id = (1<= 32)  
					goto done ;  
			}  
			p->sortindex = pTvTypeCtrl->itemCount ;  
			pTvTypeCtrl->itemCount++ ;  
			result = 0 ;  
		}  
	}  
  
done:  
	return (result) ;  
}  
  
/* 删除频道分配类型,成功返回0 */  
int DelChannelType(int type_id)  
{  
	int result = -1 ;  
	  
	if (pTvTypeCtrl)  
	{  
		int i, j ;  
		  
		for (i=0; iitemCount; i++)  
		{  
			if (pTvTypeCtrl->pList  
				&& pTvTypeCtrl->pList[i].type_id == type_id)  
			{  
				int sortindex = pTvTypeCtrl->pList[i].sortindex ;  
				  
				if ((pTvTypeCtrl->itemCount-i-1) > 0)  
					memcpy(&(pTvTypeCtrl->pList[i]), &(pTvTypeCtrl->pList[i+1]), (pTvTypeCtrl->itemCount-i-1)*sizeof(TVTYPE)) ;  
				pTvTypeCtrl->itemCount-- ;  
				for (j=0; jitemCount; j++)  
				{  
					if (pTvTypeCtrl->pList[j].sortindex > sortindex)  
						pTvTypeCtrl->pList[j].sortindex-- ;  
				}  
				result = 0 ;  
				break ;  
			}  
		}  
	}  
	  
	return (result) ;  
}  
  
/* 修改频道分配类型,成功返回0 */  
int ModifyChannelType(TVTYPE *tvtype)  
{  
	int result = -1 ;  
  
	if (pTvTypeCtrl  
		&& tvtype  
		)  
	{  
		int i ;  
		  
		for (i=0; iitemCount; i++)  
		{  
			if (pTvTypeCtrl->pList[i].type_id == tvtype->type_id)  
			{  
				TVTYPE *p = &(pTvTypeCtrl->pList[i]) ;  
				int sortindex = pTvTypeCtrl->pList[i].sortindex ;  
				  
				memcpy(p, tvtype, sizeof(TVTYPE)) ;  
				p->sortindex = sortindex ;  
				  
				result = 0 ;  
				break ;  
			}  
		}  
	}  
	  
	return (result) ;  
}  
  
/* 取得频道类型的种数 */  
int GetChannelTypeNums()  
{  
	int result = 0 ;  
	  
	if (pTvTypeCtrl)  
	{  
		result = pTvTypeCtrl->itemCount ;  
	}  
	  
	return (result) ;  
}  
  
/* 取得频道类型表中的第“n”条记录(已排序),索引从0开始 */  
int GetChannelTypeRec(int sortindex, TVTYPE *tvtype)  
{  
	int result = -1 ;  
  
	if (pTvTypeCtrl  
		&& tvtype  
		)  
	{  
		if (sortindex >= 0  
			&& sortindex < pTvTypeCtrl->itemCount  
			)  
		{  
			int i ;  
			  
			for (i=0; i< pTvTypeCtrl->itemCount; i++)  
			{  
				if (pTvTypeCtrl->pList[i].sortindex == sortindex)  
				{  
					memcpy(tvtype, &(pTvTypeCtrl->pList[i]), sizeof(TVTYPE)) ;  
					result = 0 ;  
					break ;	  
				}  
			}  
		}  
	}  
	  
	return (result) ;  
}  
  
/* 更改排序,成功则返回0 */  
int ModifyChannelTypeSort(int srcIndex, int destIndex)  
{  
	int result = -1 ;  
	  
	if (pTvTypeCtrl  
		&& (srcIndex >= 0 && srcIndex < pTvTypeCtrl->itemCount)  
		&& (destIndex >= 0 && destIndex < pTvTypeCtrl->itemCount)  
		&& srcIndex != destIndex  
		)  
	{  
		int i, j ;  
		  
		for (i=0; iitemCount; i++)  
		{  
			if (pTvTypeCtrl->pList[i].sortindex == srcIndex)  
			{  
				j = i ;  
				break ;  
			}  
		}  
		  
		if (srcIndex > destIndex)  
		{  
			for (i=0; iitemCount; i++)  
			{  
				if (pTvTypeCtrl->pList[i].sortindex >= destIndex  
					&& pTvTypeCtrl->pList[i].sortindex < srcIndex  
					&& i != j  
					)  
				{  
					pTvTypeCtrl->pList[i].sortindex++ ;  
				}  
			}  
		}  
		else  
		{  
			for (i=0; iitemCount; i++)  
			{  
				if (pTvTypeCtrl->pList[i].sortindex > srcIndex  
					&& pTvTypeCtrl->pList[i].sortindex <= destIndex  
					&& i != j  
					)  
				{  
					pTvTypeCtrl->pList[i].sortindex-- ;  
				}  
			}  
		}  
		pTvTypeCtrl->pList[j].sortindex = destIndex ;  
		  
		result = 0 ;	  
	}  
	  
	return (result) ;  
}  
  
//==================================================================//  
//频道名称管理  
  
typedef struct tagTvnameCtrl  
{  
	TVINFO	*pList ;  
	int		bInitialized ;  
	int		itemCount ;  
	int		mallocCount ;  
} TvnameCtrl_t, *pTvnameCtrl_t ;  
  
#define CH_NAME_CTRL_ALLOC_NUMS	10	//每次分配的项数  
  
static TvnameCtrl_t *pTvNameCtrl = NULL ;  
  
  
/* 初始化频道名称数据库 */  
int InitChannelNameDB()  
{  
	int result = -1 ;  
	FILE *fr ;  
	  
	if (pTvNameCtrl)  
	{  
		if (pTvNameCtrl->pList)	free(pTvNameCtrl->pList);  
		free(pTvNameCtrl) ;  
		pTvNameCtrl = NULL ;  
	}  
	  
	pTvNameCtrl = (TvnameCtrl_t *)malloc(sizeof(TvnameCtrl_t)) ;  
	if (pTvNameCtrl == NULL)  
		goto done ;  
		  
	memset(pTvNameCtrl, 0x00, sizeof(TvnameCtrl_t)) ;  
	  
	/* 从存储器装载,并初化pTvNameCtrl指向的结构 */  
	fr = fopen(CHNAME_DB_FILE, "rb") ;  
	if (fr)  
	{  
		int n = 0 ;  
		  
		if (fseek(fr, 0, SEEK_END) == 0)  
			n = (int)ftell(fr) ;  
		fseek(fr, 0, SEEK_SET) ;  
		  
		if (n >= (int)sizeof(TvnameCtrl_t)  
			&& fread(pTvNameCtrl, (size_t)sizeof(TvnameCtrl_t), 1, fr) == 1  
			&& pTvNameCtrl->itemCount > 0  
			)  
		{  
			size_t size0 = (size_t)(sizeof(TVINFO)*pTvNameCtrl->itemCount) ;  
			  
			TVINFO *pList = (TVINFO *)malloc(size0) ;  
			if (pList  
				&& n >= (sizeof(TvnameCtrl_t) + size0)  
				&& (fread(pList, (size_t)sizeof(TVINFO), pTvNameCtrl->itemCount, fr) == pTvNameCtrl->itemCount)  
				)  
			{  
				pTvNameCtrl->pList = pList ;  
				pTvNameCtrl->mallocCount = pTvNameCtrl->itemCount ;  
				result = 0 ;  
			}  
			else  
			{  
				GLH_DEBUG("open database %s error \n", CHNAME_DB_FILE) ;  
				if (pList) free(pList) ;  
				memset(pTvNameCtrl, 0x00, sizeof(TvnameCtrl_t)) ;  
			}  
		}  
		else  
		if (n <= 0)  
		{  
			result = 0 ;  
			GLH_DEBUG("open database %s(empty) ok \n", CHNAME_DB_FILE) ;  
		}  
		fclose(fr) ;  
	}  
		  
done:  
	return (result) ;  
}  
  
/* 关闭频道名称数据库 */  
int CloseChannelNameDB()  
{  
	int result = -1 ;  
	FILE *fw ;  
	  
	if (pTvNameCtrl)  
	{		  
		/* 释放资源 */  
		if (pTvNameCtrl->pList) free(pTvNameCtrl->pList) ;  
		free(pTvNameCtrl) ;  
		pTvNameCtrl = NULL ;  
	}  
	result = 0 ;  
		  
	return (result) ;  
}  
  
/* 更新频道名称数据库 */  
int UpdateChannelNameDB()  
{  
	int result = -1 ;  
	FILE *fw ;  
	  
	if (pTvNameCtrl)  
	{  
		/* 写入数据到存储器中 */  
		if (pTvNameCtrl->itemCount >= 0)  
		{  
			fw = fopen(CHNAME_DB_FILE, "wb") ;  
			if (fw)  
			{  
				if (pTvNameCtrl->itemCount > 0)  
				{  
					fwrite(pTvNameCtrl, sizeof(TvnameCtrl_t), 1, fw) ;  
					fwrite(pTvNameCtrl->pList, sizeof(TVINFO)*pTvNameCtrl->itemCount, 1, fw) ;  
				}  
				fclose(fw) ;  
				result = 0 ;  
			}  
		}	  
	}  
		  
	return (result) ;  
}  
  
/* 清空频道名称数据库 */  
int EmptyChannelNameDB(void)  
{  
	int result = -1 ;  
	  
	if (pTvNameCtrl)  
	{  
		if (pTvNameCtrl->pList) free(pTvNameCtrl->pList) ;  
		pTvNameCtrl->itemCount = 0 ;  
		pTvNameCtrl->mallocCount = 0 ;  
		result = 0 ;  
	}  
	  
	return (result) ;  
}  
  
/* 从过程服务器刷新数据库 */  
int NetRefreshChannelNameDB(int tvtype, int sync)  
{  
	int result = -1 ;  
	int r ;  
	TVINFO *tvinfo = NULL ;  
	int nums = 0 ;  
	  
	if (pTvNameCtrl)  
	{  
		r = GetTvNameTblData(&tvinfo, &nums) ;  
		if (r == 0  
			&& tvinfo  
			&& nums > 0  
			)  
		{  
			EmptyChannelNameDB() ;  
			pTvNameCtrl->pList = (TVINFO *)malloc(sizeof(TVINFO)*nums) ;  
			if (pTvNameCtrl->pList)  
			{  
				memcpy(pTvNameCtrl->pList, tvinfo, sizeof(TVINFO)*nums) ;  
				pTvNameCtrl->itemCount = nums ;  
				pTvNameCtrl->mallocCount = nums ;  
				  
				UpdateChannelNameDB() ;  
				result = 0 ;  
			}  
		}  
		else  
		{  
			GLH_DEBUG("NetRefreshChannelNameDB() Error! \n") ;  
		}  
		FreeTvNameTblData() ;  
	}  
	  
	return (result) ;  
}  
  
/* 使用本地数据更新过程服务器 */  
int NetUpdateChannelNameDB(int tvtype, int sync)  
{  
	int result = -1 ;  
	  
	return (result) ;  
}  
  
/* 增加频道名记录,返回分配给其的台名ID(>0) */  
int AddChannelName(TVINFO *tvinfo)  
{  
	int result = -1 ;  
	  
	if (pTvNameCtrl  
		&& tvinfo  
		)  
	{  
		if (pTvNameCtrl->mallocCount <= pTvNameCtrl->itemCount)  
		{  
			TVINFO *p ;  
			int count = (((pTvNameCtrl->itemCount + 1)/CH_NAME_CTRL_ALLOC_NUMS) + 1) * CH_NAME_CTRL_ALLOC_NUMS ;  
			  
			p = (TVINFO *)malloc(count * sizeof(TVINFO)) ;  
			if (p)  
			{  
				memcpy(p, pTvNameCtrl->pList, pTvNameCtrl->itemCount*sizeof(TVINFO)) ;  
				free(pTvNameCtrl->pList) ;  
				pTvNameCtrl->pList = p ;  
				pTvNameCtrl->mallocCount = count ;  
			}  
			else  
			{  
				goto done ;  
			}  
		}  
		  
		if (pTvNameCtrl->mallocCount > pTvNameCtrl->itemCount)  
		{  
			TVINFO *p = &(pTvNameCtrl->pList[pTvNameCtrl->itemCount]) ;  
			  
			memcpy(p, tvinfo, sizeof(TVINFO)) ;  
			  
			pTvNameCtrl->itemCount++ ;  
			result = 0 ;  
		}  
	}  
  
done:  
	return (result) ;	  
}  
  
  
/* 删除频道名记录,成功返回0 */  
int DelChannelName(int tvid)  
{  
	int result = -1 ;  
	  
	if (pTvNameCtrl)  
	{  
		int i, j ;  
		  
		for (i=0; iitemCount; i++)  
		{  
			if (pTvNameCtrl->pList  
				&& pTvNameCtrl->pList[i].tvid == tvid)  
			{  
				if ((pTvNameCtrl->itemCount-i-1) > 0)  
					memcpy(&(pTvNameCtrl->pList[i]), &(pTvNameCtrl->pList[i+1]), (pTvNameCtrl->itemCount-i-1)*sizeof(TVINFO)) ;  
				pTvNameCtrl->itemCount-- ;  
				result = 0 ;  
				break ;  
			}  
		}  
	}  
	  
	return (result) ;	  
}  
  
  
/* 修改频道名记录,成功返回0 */  
int ModifyChannelName(TVINFO *tvinfo)  
{  
	int result = -1 ;  
  
	if (pTvNameCtrl  
		&& tvinfo  
		)  
	{  
		int i ;  
		  
		for (i=0; iitemCount; i++)  
		{  
			if (pTvNameCtrl->pList[i].tvid == tvinfo->tvid)  
			{  
				TVINFO *p = &(pTvNameCtrl->pList[i]) ;  
				  
				memcpy(p, tvinfo, sizeof(TVINFO)) ;  
				  
				result = 0 ;  
				break ;  
			}  
		}  
	}  
	  
	return (result) ;	  
}  
  
  
/* 取得频道名的总数 */  
int GetChannelNameNums()  
{  
	int result = 0 ;  
	  
	if (pTvNameCtrl)  
	{  
		result = pTvNameCtrl->itemCount ;  
	}  
	  
	return (result) ;	  
}  
  
  
/* 取得频道名表中的第“n”条记录,索引从0开始 */  
int GetChannelNameRec(int index, TVINFO *tvinfo)  
{  
	int result = -1 ;  
  
	if (pTvNameCtrl  
		&& tvinfo  
		&& index >= 0  
		&& index < pTvNameCtrl->itemCount  
		)  
	{  
		memcpy(tvinfo, &(pTvNameCtrl->pList[index]), sizeof(TVINFO)) ;  
		result = 0 ;  
	}  
	  
	return (result) ;  
}  
  
/* 按照TvId取得完整的TVINFO信息 */  
int GetChannelByTvId(int tvid, TVINFO *tvinfo)  
{  
	int result = -1 ;  
  
	if (pTvNameCtrl  
		&& tvid > 0  
		&& tvinfo  
		)  
	{  
		int i ;  
		  
		for (i=0; i< pTvNameCtrl->itemCount; i++)  
		{  
			if (pTvNameCtrl->pList[i].tvid == tvid)	  
			{  
				memcpy(tvinfo, &(pTvNameCtrl->pList[i]), sizeof(TVINFO)) ;  
				result = 0 ;  
				break ;	  
			}  
		}  
	}  
	  
	return (result) ;  
}  
  
  
static int GetFuncTVItem(FUNCITEM *pprog, char* pSection)  
{  
	int result = -1 ;  
	int tmp;  
	char sztime[20];  
	  
	if (GetIntValueFromIniFile(FUNCDB_FILE, pSection, "version", &(pprog->version)) != 0)  
		return (result) ;  
	GetIntValueFromIniFile(FUNCDB_FILE, pSection, "lastversion", &(pprog->lastversion));  
  
	GetValueFromIniFile(FUNCDB_FILE, pSection, "url", pprog->url, MAX_PATH+MAX_NAME);  
	  
	GetValueFromIniFile(FUNCDB_FILE, pSection, "port", pprog->port, MAX_NAME);  
	  
	GetIntValueFromIniFile(FUNCDB_FILE, pSection, "exist", &tmp);  
	if (tmp == 1)  
		pprog->exist = 1;  
	else  
		pprog->exist = 0;  
	  
	GetIntValueFromIniFile(FUNCDB_FILE, pSection, "ziptype", &(pprog->ziptype));  
	GetValueFromIniFile(FUNCDB_FILE, pSection, "icon", pprog->bkbmpfile, MAX_PATH);  
	  
	GetValueFromIniFile(FUNCDB_FILE, pSection, "name", pprog->caption, MAX_PATH);  
	GetValueFromIniFile(FUNCDB_FILE, pSection, "execfile", pprog->execfile, MAX_PATH);  
	GetValueFromIniFile(FUNCDB_FILE, pSection, "root", pprog->root, MAX_PATH);  
	  
	/*  
	if(pprog->exist &&  
		(access(pprog->root,R_OK) != 0 ||  
		access(pprog->execfile,R_OK) != 0))  
	{  
		pprog->exist = 0;  
	}  
	*/  
	  
	if(GetValueFromIniFile(FUNCDB_FILE, pSection, "layer", pprog->layer, MAX_PATH) != 0)  
	{  
		strcpy(pprog->layer,pSection);  
	}  
	  
	GetValueFromIniFile(FUNCDB_FILE, pSection, "lastNoticetime", sztime, 20);  
	memset(&(pprog->LastNoticeTime), 0, sizeof(struct tm));  
	strptime(sztime, "%D", &(pprog->LastNoticeTime));  
	  
	GetValueFromIniFile(FUNCDB_FILE, pSection, "nextNoticetime", sztime, 20);  
	memset(&(pprog->NextNoticeTime), 0, sizeof(struct tm));  
	strptime(sztime, "%D", &(pprog->NextNoticeTime));  
	  
	result = 0 ;  
	  
	return (result);  
}  
  
static int InitFuncTVData(TVChannelInfo *physChInfo, LocalChannelRecord *localChInfo,  
							TVINFO *tvInfo, int maxitem)  
{  
	int result = 0 ;  
	int funcnum = 0 ;  
	char funcname[MAX_NAME] ;  
	char secname[MAX_NAME] ;  
	  
	if (!(physChInfo && localChInfo && tvInfo && maxitem > 0))  
		goto done ;  
	  
	/* 取得功能电视的项数 */  
	if (GetIntValueFromIniFile(FUNCDB_FILE, "FUNCTV", "FUNCNUM", &funcnum) == 0  
		&& funcnum > 0  
		)  
	{  
		int i ;  
		int count = 0 ;  
		  
		for (i=0; i 0)  
				{  
					memset(physChInfo, 0x00, sizeof(TVChannelInfo)) ;  
					memset(localChInfo, 0x00, sizeof(LocalChannelRecord)) ;  
					memset(tvInfo, 0x00, sizeof(TVINFO)) ;  
					physChInfo->tvtype = TVTYPE_FTV ;  
					localChInfo->channelid = FTV_CHANNELID_START + i ;  
					if (GetFuncTVItem(&(physChInfo->info.ftv), secname) == 0)  
					{  
						physChInfo++ ;  
						localChInfo++ ;  
						tvInfo++ ;  
						maxitem-- ;  
						  
						count++ ;  
					}  
				}  
				else  
				{  
					GLH_DEBUG("In InitFuncTVData(), Space Full \n") ;  
					result = 0 ;  
					break ;	  
				}  
				  
			}  
		}  
		result = count ;  
		GLH_DEBUG("add function channel nums=%d \n", count) ;  
	}  
	  
done:  
	return (result) ;	  
}  
  
static int GetNewChannelid(int tvtype, int *newid)  
{  
	int result = -1 ;  
	  
	if (pChMgr  
		&& pChMgr->pChCtrl  
		&& pChMgr->localChInfo  
		&& newid  
		)  
	{  
		int i, j, start, end, maxchid ;  
		  
		if (tvtype == TVTYPE_ATV)  
		{  
			start = ATV_CHANNELID_START ;  
			end = ATV_CHANNELID_END ;  
		}  
		else  
		if (tvtype == TVTYPE_DTV)  
		{  
			start = DTV_CHANNELID_START ;  
			end = DTV_CHANNELID_END ;  
		}  
		else  
		if (tvtype == TVTYPE_FTV)  
		{  
			start = FTV_CHANNELID_START ;  
			end = FTV_CHANNELID_END ;  
		}  
		  
		/* 在最大的channelid加1 */  
		for (i=0,maxchid=-1; ipChCtrl->itemCount; i++)  
		{  
			if (tvtype == pChMgr->localChInfo[i].tvtype  
				&& pChMgr->localChInfo[i].channelid > maxchid  
				)  
			{  
				maxchid = pChMgr->localChInfo[i].channelid ;  
			}  
		}  
		if (maxchid >= start  
			&& maxchid < end  
			)  
		{  
			*newid = maxchid+1 ;  
			result = 0 ;  
			goto done ;  
		}  
		else  
		if (maxchid == -1)  
		{  
			*newid = start ;  
			result = 0 ;  
			goto done ;  
		}  
		  
		/* 查找未使用项 */  
		if (maxchid >= 0)  
		{  
			for (i=start; i<=end; i++)  
			{  
				int used = 0 ;  
				  
				for (j=0; jpChCtrl->itemCount; j++)  
				{  
					if (tvtype == pChMgr->localChInfo[j].tvtype  
						&& i == pChMgr->localChInfo[j].channelid  
						)  
					{  
						used = 1 ;  
						break ;  
					}  
				}  
				if (!used)  
				{  
					*newid = i ;  
					result = 0 ;  
					goto done ;  
				}  
			}  
		}  
	}  
	  
done:  
	return (result) ;  
}  
  
#ifdef _TEST_  
	int main(int argc, char *argv[])  
	{  
		int result = 0xffff ;  
		TVChannelInfo phys ;  
		LocalChannelRecord logic ;  
		  
		GLH_DEBUG("\n") ;  
		  
		result = InitChannelDB() ;  
		GLH_DEBUG("InitChannelDB(), result=%d \n", result) ;  
		  
		phys.tvtype = TVTYPE_ATV ;  
		phys.info.atv.nchid = 0x01 ;  
		logic.channelid = 0x01 ;  
		result = AddChannel("中内一", &phys, &logic) ;  
		GLH_DEBUG("AddChannel(), result=%d \n", result) ;  
		phys.tvtype = TVTYPE_ATV ;  
		phys.info.atv.nchid = 0x03 ;  
		logic.channelid = 0x03 ;  
		result = AddChannel("中内二", &phys, &logic) ;  
		GLH_DEBUG("AddChannel(), result=%d \n", result) ;  
		  
		result = CloseChannelDB() ;  
		GLH_DEBUG("CloseChannelDB(), result=%d \n", result) ;  
		  
		result = InitChannelTypeDB() ;  
		GLH_DEBUG("InitChannelTypeDB(), result=%d \n", result) ;  
		  
		result = GetChannelTypeNums() ;  
		GLH_DEBUG("GetChannelTypeNums(), result=%d \n", result) ;  
		  
		if (1)  
		{  
			int i, j ;  
			TVTYPE tvtype ;  
			  
			j = result ;  
			for (i=0; i