kb_dc_fileproc.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:16k
- //-----------------------------------------------------------------------------
- // Project include files:
- //-----------------------------------------------------------------------------
- #include "kb_dc_pub.h"
- DCMODULEINFO moduleInfo={0,0};
- static unsigned short curMovieCfgVer = 0x100; /*影片配置文件版本号*/
- static unsigned short curServCfgVer = 0x100; /*业务主配置文件版本号*/
- static unsigned char *curCfgName=NULL;
- //-----------------------------------------------------------------------------
- // FUNCTION: Get_firstModID_ByName
- //
- // DESCRIPTION:
- //
- // RETURN:
- //
- // NOTES: None
- //-----------------------------------------------------------------------------
- unsigned short Get_firstModID_ByName(char *name)
- {
- int i=0;
- unsigned short firstMId=0xffff;
- for(i=0;i<moduleInfo.modNum;i++)
- {
- if(strcmp((const char*)moduleInfo.modInf[i].fileName, (const char*)name)==0)
- return moduleInfo.modInf[i].modID;
- }
-
- return firstMId;
- }
- //get module number from a file
- int DCGetModNumByFile(DCFILE *file)
- {
- int i=0;
- int count=0, blocksCnt=0;
- unsigned long fSize;
-
- if(file==NULL)
- {
- return -1;
- }
-
- for(i=0;i<moduleInfo.modNum;i++)
- {
- if(strcmp((const char*)file->fileName, (const char*)moduleInfo.modInf[i].fileName)==0)
- count++;
- }
- //if error, then choose another way to caculate the modulecount
- if(count==0)
- {
- fSize = file->fileLength;
- blocksCnt = (int)fSize/4066+(((int)fSize%4066==0)?0:1);
- count = blocksCnt/256+((blocksCnt%256==0)?0:1);
- }
- return count;
- }
- unsigned int DclsEof(DCFILE *file)
- {
- long position=file->position;
- long len=file->fileLength;
- if (position==(len))
- return 1;
- else
- return 0;
-
- }
- void DcCloseFile(DCFILE *file)
- {
- KBDcFree((void*)file->section);
- KBDcFree((void*)file);
- }
- void DcCloseDirectory(DCDIR *dir)
- {
- KBDcFree(dir->diiSection);
- KBDcFree(dir);
- }
- long DcReadFile(void *buf,int len,DCFILE *file)
- {
- long position=file->position;
- long fileLength=file->fileLength;
- unsigned long readBytes=0;
-
- if(DclsEof(file)==1)
- return readBytes;
-
- if((position+len)<fileLength)
- readBytes=len;
- else
- readBytes=fileLength-position;
- KBDcMemcpy((void*)buf,(const void*)(file->section+position),(size_t)readBytes);
- file->position+=readBytes;
- return readBytes;
- }
- //-----------------------------------------------------------------------------
- // FUNCTION: FreeDCFILE
- //
- // DESCRIPTION: 释放分配的gDCFile
- //
- // RETURN:
- //
- // NOTES: None
- //-----------------------------------------------------------------------------
- void FreeDCFILE(void)
- {
- int i;
-
- if(gDCFile!=NULL)
- {
- if(gDCFile->section!=NULL)
- {
- DCDebug3("n ==free gDCFile->section");
- KBDcFree(gDCFile->section);
- gDCFile->section=NULL;
- }
- gDCFile->fileLength=0;
- //free BLOCK buffer status
- if(gDCFile->BufStatus.modcount>0&&gDCFile->BufStatus.modStat!=NULL)
- {
- for(i=0;i<gDCFile->BufStatus.modcount;i++)
- {
- if(gDCFile->BufStatus.modStat[i].blockCount>0)
- {
- if(gDCFile->BufStatus.modStat[i].blockStat!=NULL)
- {
- DCDebug3("n ==free blockStat of module %d", i);
- KBDcFree(gDCFile->BufStatus.modStat[i].blockStat);
- gDCFile->BufStatus.modStat[i].blockStat= NULL;
- gDCFile->BufStatus.modStat[i].blockCount = 0;
- }
- }
- }
- }
- //free module buffer status
- if(gDCFile->BufStatus.modStat!=NULL)
- {
- DCDebug3("n ==free modStat==");
- KBDcFree(gDCFile->BufStatus.modStat);
- gDCFile->BufStatus.modStat = NULL;
- gDCFile->BufStatus.modcount = 0;
- }
-
- }
- if(gDCFile!=NULL)
- KBDcFree(gDCFile);
- gDCFile=NULL;
- }
- //-----------------------------------------------------------------------------
- // FUNCTION: FreeOneDCFILE
- //
- // DESCRIPTION: 根据参数释放分配的DCFILE内存
- //
- // RETURN:
- //
- // NOTES: None
- //-----------------------------------------------------------------------------
- void FreeOneDCFILE(DCFILE *pfile)
- {
- int i;
-
- if(pfile!=NULL)
- {
- if(pfile->section!=NULL)
- {
- DCDebug3("n ==free gDCFile->section_2");
- KBDcFree(pfile->section);
- pfile->section=NULL;
- }
- pfile->fileLength=0;
- //free BLOCK buffer status
- if(pfile->BufStatus.modcount>0&&pfile->BufStatus.modStat!=NULL)
- {
- for(i=0;i<pfile->BufStatus.modcount;i++)
- {
- if(pfile->BufStatus.modStat[i].blockCount>0)
- {
- if(pfile->BufStatus.modStat[i].blockStat!=NULL)
- {
- DCDebug3("n ==free blockStat of module %d_2", i);
- KBDcFree(pfile->BufStatus.modStat[i].blockStat);
- pfile->BufStatus.modStat[i].blockStat= NULL;
- pfile->BufStatus.modStat[i].blockCount = 0;
- }
- }
- }
- }
- //free module buffer status
- if(pfile->BufStatus.modStat!=NULL)
- {
- DCDebug3("n ==free modStat_2==");
- KBDcFree(pfile->BufStatus.modStat);
- pfile->BufStatus.modStat = NULL;
- pfile->BufStatus.modcount = 0;
- }
-
- }
- if(pfile!=NULL)
- {
- KBDcFree(pfile);
- pfile=NULL;
- }
- }
- //-----------------------------------------------------------------------------
- // FUNCTION: FreeDCDIR
- //
- // DESCRIPTION: 释放分配的gDCFile
- //
- // RETURN:
- //
- // NOTES: None
- //-----------------------------------------------------------------------------
- void FreeDCDIR(void)
- {
- if(gDCDir!=NULL)
- {
- if(gDCDir->diiSection!=NULL)
- {
- KBDcFree(gDCDir->diiSection);
- gDCDir->diiSection=NULL;
- }
- }
- if(gDCDir!=NULL)
- KBDcFree(gDCDir);
- gDCDir=NULL;
- }
- //get file count int DII
- int DcGetFileCount()
- {
- int filecnt=0;
- return filecnt;
- }
- int InitFileModuleInfo(unsigned short modCount)
- {
- if((moduleInfo.modInf!=NULL)||(moduleInfo.modNum!=0))
- {
- if(moduleInfo.modNum != modCount)
- {
- FreeModuleInfo(); /*先释放moduleinfo结构内存*/
- }
- else
- {
- KBDcMemset(moduleInfo.modInf, 0x00, modCount*sizeof(DCModuleInfoArray));
- moduleInfo.modNum = 0;
- return 0;
- }
- }
-
- //init moduleinf struct
- moduleInfo.modInf = (DCModuleInfoArray *)KBDcMalloc(modCount*sizeof(DCModuleInfoArray));
- if(moduleInfo.modInf == NULL)
- {
- DCDebug1("n moduleInfo malloc error!");
- return -1;
- }
- KBDcMemset(moduleInfo.modInf, 0x00, modCount*sizeof(DCModuleInfoArray));
- moduleInfo.modNum = 0;
- return 0;
- }
- void FreeModuleInfo(void)
- {
- if(moduleInfo.modInf!=NULL)
- {
- DCDebug4("n ==free moduleInfo.modInf");
- KBDcFree(moduleInfo.modInf);
- moduleInfo.modInf=NULL;
- moduleInfo.modNum = 0;
- }
- }
- //初始化file结构
- void InitFileStruct(DCFILE *file)
- {
- if(file==NULL)
- return;
- file->position =0;
- file->fileLength =0;
- KBDcMemset(file->fileName,0,KB_DCFILE_NAME_MAX_LEN);
- file->section =NULL;
-
- //init bufstatus
- file->BufStatus.modcount=0;
- file->BufStatus.modStat = NULL;
-
- return;
- }
- //----------初始化buffer status----------
- int kb_dc_bufferStatusBuild(DCFILE *pfile, int blocksCnt)
- {
- int i=0, modulesCnt=0;
- modulesCnt=blocksCnt/256+((blocksCnt%256==0)?0:1);
- if(pfile==NULL)
- {
- DCDebug1("n kb_dc_bufferStatusBuild: pfile is NULL");
- return 0;
- }
-
- //module buffer status
- pfile->BufStatus.modcount = modulesCnt;
- DCDebug3("n kb_dc_bufferStatusBuild: modulesCnt=%d structSize=%d", modulesCnt, sizeof(DCBLOCKBUFSTATUS));
- pfile->BufStatus.modStat = (DCBLOCKBUFSTATUS *)KBDcMalloc(modulesCnt*sizeof(DCBLOCKBUFSTATUS));
- if(pfile->BufStatus.modStat == NULL)
- {
- DCDebug1("n BufStatus malloc error");
- return 0;
- }
- KBDcMemset(pfile->BufStatus.modStat, EMPTY, modulesCnt*sizeof(DCBLOCKBUFSTATUS));
- //block buffer status
- if(pfile->BufStatus.modcount==1)
- {
- DCDebug3("n ---in one module mode----");
- pfile->BufStatus.modStat[0].blockCount = blocksCnt;
- pfile->BufStatus.modStat[0].blockStat = (unsigned char *)KBDcMalloc(blocksCnt*sizeof(unsigned char));
- if(pfile->BufStatus.modStat[0].blockStat==NULL)
- {
- DCDebug1("n blockStat malloc error 1");
- return 0;
- }
- KBDcMemset(pfile->BufStatus.modStat[0].blockStat, EMPTY, blocksCnt*sizeof(unsigned char));
- }
- else if(pfile->BufStatus.modcount > 1)
- {
- int blockCntOfMod =0, blockCntOfLastMod =0;
- DCDebug3("n ---in multi-module mode----");
- blockCntOfMod = 256;
- for(i=0; i<pfile->BufStatus.modcount-1; i++)
- {
- pfile->BufStatus.modStat[i].blockCount = blockCntOfMod;
- pfile->BufStatus.modStat[i].blockStat = (unsigned char *)KBDcMalloc(blockCntOfMod*sizeof(unsigned char));
- if(pfile->BufStatus.modStat[i].blockStat==NULL)
- {
- DCDebug1("n blockStat of mod[%d] malloc error 2", i);
- return 0;
- }
- KBDcMemset(pfile->BufStatus.modStat[i].blockStat, EMPTY, blockCntOfMod*sizeof(unsigned char));
- }
-
- //last module
- blockCntOfLastMod = blocksCnt - (pfile->BufStatus.modcount-1)*256;
- pfile->BufStatus.modStat[pfile->BufStatus.modcount-1].blockCount = blockCntOfLastMod;
- pfile->BufStatus.modStat[pfile->BufStatus.modcount-1].blockStat = (unsigned char *)KBDcMalloc(blockCntOfLastMod*sizeof(unsigned char));
- if(pfile->BufStatus.modStat[pfile->BufStatus.modcount-1].blockStat==NULL)
- {
- DCDebug1("n blockStat of last mod malloc error 3");
- return 0;
- }
- KBDcMemset(pfile->BufStatus.modStat[pfile->BufStatus.modcount-1].blockStat, EMPTY, blockCntOfLastMod*sizeof(unsigned char));
-
- }
- return 1;
- }
- //根据moduleid从diiSection取得该module所承载的有效数据的大小
- unsigned int DCGetModSizeByIdSec(int index)
- {
- DCDIR *dir;
- ModuleInfo modInfo;
-
- // 获得dir 结构
- dir = GetDCDIR();
- GetModuleInfo(dir->diiSection, index, &modInfo);
- return modInfo.moduleSize;
- }
- //根据moduleid从DCFIle取得该module所承载的有效数据的大小
- unsigned int DCGetModSizeByModuleId(int modId)
- {
- int i=0;
- for(i=0;i<moduleInfo.modNum;i++)
- {
- if(moduleInfo.modInf[i].modID == modId)
- return moduleInfo.modInf[i].moduleSize;
- }
- return 0xffff;
- }
- //Fuction:DcOpenFile
- //Description:malloc
- DCFILE* DcOpenFile(DCDIR* dir,const char* fileName)
- {
- DIIHead diiHead;
- ModuleInfo modInfo;
- unsigned char name[255];
- int modCount=0;
- int blockSize=0;
- int blocksCnt=0;
- int i=0, nameLength;
- DCFILE *file;
-
- unsigned char *diiSection=dir->diiSection;
- GetDIIHead(diiSection,&diiHead);
- modCount=diiHead.numberOfModules;
- blockSize=diiHead.blockSize;
-
- file = (DCFILE*)KBDcMalloc(sizeof(DCFILE));
- if(file==NULL)
- {
- DCDebug1("nfile malloc error...n");
- return NULL;
- }
-
- InitFileStruct(file);
- if(InitFileModuleInfo(modCount)!=0)
- {
- DCDebug1("n InitFileModuleInfo error");
- return NULL;
- }
-
- DCDebug3("n modCount=%d...n",modCount);
- for(i=0;i<modCount;i++)
- {
- GetModuleInfo(diiSection,i,&modInfo);
- nameLength=GetModuleName(&modInfo,name);
- DCDebug3("[%s]VS[%s]...n", name, fileName);
- //初始化moduleInfo
- moduleInfo.modInf[moduleInfo.modNum].modID = modInfo.moduleId;
- moduleInfo.modInf[moduleInfo.modNum].moduleSize = modInfo.moduleSize;
- moduleInfo.modInf[moduleInfo.modNum].moduleVersion = modInfo.moduleVersion;
- strncpy((char*)moduleInfo.modInf[moduleInfo.modNum].fileName, (char*)name, strlen((char*)name));
- moduleInfo.modNum++;
-
- if(0==strcmp((const char*)name,(const char*)fileName))
- {
- DCDebug3("open File[%d]=[%s]n",i,name);
- kb_dc_SaveCurCfgName(name);
- strcpy((char*)file->fileName,(char*)name);
- file->position=0;
- file->fileLength+=modInfo.moduleSize;
- }
- }
- //初始化一个配置文件收取 重复判断的结构,以避免解析中数组访问越界
- blocksCnt=(int)(file->fileLength)/4066+(((int)(file->fileLength)%4066==0)?0:1);
- if(kb_dc_bufferStatusBuild(file, blocksCnt)==0)
- {
- DCDebug1("n kb_dc_bufferStatusBuild failed");
- return NULL;
- }
- //初始化section buffer
- file->section = (unsigned char *)KBDcMalloc(file->fileLength);
- if(file->section==NULL)
- {
- DCDebug1("Out of memory[%d]rn",file->fileLength);
- return NULL;
- }
- else
- {
- //KBDcMemset(file->section, EMPTY, file->fileLength);
- return file;
- }
- }
- //--------------------------------------------------------------------------
- //description:
- //--
- DCDIR* OpenDirectory(unsigned char *pData,int len)
- {
- DIIHead diiHead;
- DCDIR *dir;
-
- dir=(DCDIR*)KBDcMalloc(sizeof(DCDIR));
- KBDcMemset(dir, 0x00, sizeof(DCDIR));
-
- dir->diiSection=(unsigned char*)KBDcMalloc(4096);
- KBDcMemset(dir->diiSection, 0x00, 4096);
-
- KBDcMemcpy(dir->diiSection,pData,len);
- GetDIIHead(dir->diiSection,&diiHead);
- dir->blockSize=diiHead.blockSize;
- dir->numberOfModules = diiHead.numberOfModules;
-
- return dir;
- }
- //检查配置文件的版本号是否变更
- //TRUE: changed
- //FALSE: not changed
- BOOL kb_dc_CfgVerCheck(unsigned char *cfg_name, unsigned char type)
- {
- int i=0;
- if(cfg_name==NULL)
- {
- DCDebug1("n -kb_dc_CfgVerCheck-: cfg_name is null");
- return FALSE;
- }
-
- //还没有取到module info数据的情况下,不去激活
- //配置文件搜索
- if(moduleInfo.modNum==0)
- {
- DCDebug1("n -kb_dc_CfgVerCheck-: moduleInfo.modNum==0! ");
- return FALSE;
- }
- //根据配置文件名匹配
- for(i=0;i<moduleInfo.modNum;i++)
- {
- if(strcmp((const char *)cfg_name, (const char *)moduleInfo.modInf[i].fileName)==0)
- {
- if(type==SERV_CFG_ADVERTISE)
- {
- DCDebug2("n -kb_dc_CfgVerCheck-: curCfgVer=%d modInf[%d].moduleVersion=%d", curMovieCfgVer,i, moduleInfo.modInf[i].moduleVersion);
- if((unsigned short)moduleInfo.modInf[i].moduleVersion!=curMovieCfgVer)
- {
- curMovieCfgVer = (unsigned short)moduleInfo.modInf[i].moduleVersion;
- return TRUE;
- }
- }
- else if(type==SERV_CFG_MAIN)
- {
- DCDebug2("n -kb_dc_CfgVerCheck-: curServCfgVer=%d modInf[%d].moduleVersion=%d", curServCfgVer,i, moduleInfo.modInf[i].moduleVersion);
- if((unsigned short)moduleInfo.modInf[i].moduleVersion!=curServCfgVer)
- {
- curServCfgVer = (unsigned short)moduleInfo.modInf[i].moduleVersion;
- return TRUE;
- }
- }
- }
}
- //如果精确到文件, 此段代码应该屏蔽
- #if 0
- if(i>=moduleInfo.modNum)
- {
- int j=0;
-
- for(j=0;j<moduleInfo.modNum;j++)
- {
- if(moduleInfo.modInf[j].moduleVersion!=curMovieCfgVer)
- return TRUE;
- }
- }
- #endif
- return FALSE;
- }
- //-------
- unsigned char kb_dc_GetCurCfgVer()
- {
- return curMovieCfgVer;
- }
- int kb_dc_SaveServCfgVersion( char *cfg_name)
- {
- int i;
-
- for(i=0; i<moduleInfo.modNum; i++)
- {
- if(strcmp((const char *)cfg_name, (const char *)moduleInfo.modInf[i].fileName)==0)
- {
- curServCfgVer = (unsigned short)moduleInfo.modInf[i].moduleVersion;
- return true;
- }
- }
- return false;
- }
- void kb_dc_SaveCurCfgName(unsigned char *name)
- {
- if(curCfgName==NULL)
- {
- curCfgName=(unsigned char*)KBDcMalloc(KB_DCFILE_NAME_MAX_LEN*sizeof(unsigned char));
- if(curCfgName==NULL)
- {
- DCDebug1("n curCfgName malloc error!!!");
- return;
- }
- }
- strcpy((char*)curCfgName, (char*)name);
- return;
- }
- unsigned char * kb_dc_GetCurCfgName(void)
- {
- return curCfgName;
- }
- void kb_dc_moduleInfo_Test(void)
- {
- int i;
- for(i=0;i<moduleInfo.modNum;i++)
- {
- DCDebug3("n mod[%d]: modid=%d, fileName=%s, moduleVersion=%d", i, moduleInfo.modInf[i].modID, moduleInfo.modInf[i].fileName, moduleInfo.modInf[i].moduleVersion);
- }
- }
- void kb_dc_CfgVerSaveTest(void)
- {
- DCDebug3("n ----kb_dc_CfgVerSaveTest-----");
- curMovieCfgVer++;
- }
- /*
- * 判断一个module是否已经收取完毕
- */
- unsigned char kb_dc_moduleIsEnd(DCFILE*pfile, unsigned short index)
- {
- int k;
- if(index >(pfile->BufStatus.modcount-1))
- {
- DCDebug1("n index overflow error");
- index = pfile->BufStatus.modcount-1;
- }
-
- for(k=0; k<pfile->BufStatus.modStat[index].blockCount; k++)
- {
- if(pfile->BufStatus.modStat[index].blockStat[k] == EMPTY)
- break;
- }
- if(k>=pfile->BufStatus.modStat[index].blockCount)
- return 1;
- else
- return 0;
-
- }