/******************************************************************************
版权所有：  
文件名称：  history_DB.c
文件版本：  01.01
创建作者：  邓良基
创建日期：  2020-5
功能说明：
其它说明：  历史文件读写数据接口
修改记录：
*/

#include "head.h" 
#ifdef LINUX_KERNEL_APP

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/wait.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/ioctls.h>
#include <linux/syscalls.h>
#include <termios.h>

//#include <stdio.h>
//#include <sys/unistd.h>

#else
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/wait.h> 
#include <asm/ioctls.h>
#include <termios.h>

#endif




#define DEBUG_DB rt_printf

#define FILE_NULL 0

//保存list文件信息
static void updataListInfo(fileInfo *list)
{
    BYTE buf[FILE_NAME_NUM];
    struct file * fd;
    int i;
    loff_t pos=0;
    if(list->listMax == 0
       || list->listSize == 0 )
    {
        DEBUG_DB("%s%s fileInfo data  error!\n");
    }
    sprintf(buf,"%s/%s_info",HF_HISTORY_DB_INFO,list->fileName);
    //sprintf(buf,"./app/history/fileInfo/%s_info",list->fileName);
    fd = rt_file_open((char *)&buf,O_RDWR,0777);
    //rt_file_llseek(fd,0,SEEK_SET);
    i = rt_file_write(fd,(char *)list,sizeof(fileInfo),&pos);
    rt_file_close(fd,0);
}

//更新历史记录文件头信息
void updataFileTableData(fileInfo *list)
{

}
WORD getListMax(fileInfo *list)
{
    return list->listMax;
}
void delListInfo(fileInfo *list)
{

}


int DelListFile(fileInfo *list)
{
    BYTE buf[FILE_NAME_NUM];

    sprintf(buf,"%s/%s",list->dirName,list->fileName);
    if(list->fd)
        rt_file_close(list->fd,0);

    list->fd = rt_file_open((char *)&buf,O_CREAT|O_RDWR,0);

    if(IS_ERR(list->fd))
    {
        return -1;
    }

    if(rt_file_del((char *)&buf))
    {
        return -2;
    }

    return 0;

}



static void openListFile(fileInfo *list)
{
    BYTE buf[FILE_NAME_NUM]= {0};
    BYTE counter=0;
#ifdef ADD_MULTIPLE_FILE_WR_FUN
    if(list->fileTatal>1&&list->fileWRType==MULTIPLE_FILE_WR_TYPE)
    {
        DataListFileInfoType DataListFileInfo;
        DWORD FilePos=0;
        memset((char*)&DataListFileInfo,0,sizeof(DataListFileInfo));
        GetCurMultipleFileNumPos(list,&FilePos);
        if(GetMultipleFileNameInfo(list,FilePos,&DataListFileInfo)>0)
        {
            if(DataListFileInfo.fileName[0]==0)    //未创建文件则默认保存为
            {
                sprintf(buf,"%s/%s",list->dirName,list->fileName);
                rt_printf("未定义多数据文件\r\n");
            }
            else
            {
                sprintf(buf,"%s/%s",list->dirName,(char*)&DataListFileInfo.fileName[0]);
                //if(list->fd)
                // rt_file_close(list->fd,0);
            }
        }
        else
            return;
    }
    else
#endif
    {
        sprintf(buf,"%s/%s",list->dirName,list->fileName);
        if(list->fd)
            rt_file_close(list->fd,0);
    }

    list->fd = rt_file_open((char *)&buf,O_CREAT|O_RDWR,0);
    while(IS_ERR(list->fd))
    {
        list->fd = rt_file_open((char *)&buf,O_CREAT|O_RDWR,0);
        rt_printf("new list file %s \n",&buf);
        if(++counter>=5)   //防止卡死
            break;
    }
    if( IS_ERR(list->fd))
        rt_printf("openListFile:error /%s \n",buf);



}
int initListData(fileInfo *list)
{
    BYTE buf[FILE_NAME_NUM];
    fileInfo tempList;
    int i;
    loff_t pos=0;
    if(list->fileName[0]==0)
        return FALSE;
    if(list->listSize==0||list->listMax==0)
        return FALSE;
    if(list->dirName[0]==0)
        sprintf(list->dirName,HF_HISTORY_DB_INFO);

    list->fileTableSize = 0;//暂不考虑文件头信息
    rt_file_mkdir(list->dirName);
    rt_file_mkdir(HF_HISTORY_DB_INFO);
    sprintf(buf,"%s/%s_info",HF_HISTORY_DB_INFO,list->fileName);
    list->fd = rt_file_open((char *)&buf,O_RDWR,0777);
    if( IS_ERR(list->fd))
    {
        list->listWp = 0;
        list->listNum = 0;
        list->listNewNum = 0;

        list->fd = rt_file_open((char *)&buf,O_CREAT|O_RDWR|O_TRUNC,0777);
        rt_printf("initListData %s\r\n",buf);
        //sys_lseek(list->fd,(list->listSize*list->listWp) + list->fileTableSize,SEEK_SET);
        i = rt_file_write(list->fd,(char *)list,sizeof(fileInfo),&pos);
        rt_file_close(list->fd,0);
    }
    else
    {
        //rt_file_llseek(list->fd,(list->listSize*list->listWp) + list->fileTableSize,SEEK_SET);
        if( rt_file_read(list->fd,(char *)&tempList,sizeof(fileInfo),&pos) != sizeof(fileInfo) );
        ;//rt_printf("%s数据格式与之前不符 ERROR\r\n",buf);//暂不考虑开机跟之前文件信息不一致
        rt_file_close(list->fd,0);
        memcpy(list,(char*)&tempList,sizeof(fileInfo));
    }
    list->fd =FILE_NULL;
    return TRUE;
}
//读取连续数据，操作以后新产生个数将清空
DWORD readRangeListData(fileInfo *list,DWORD startList,DWORD readCount,BYTE *buf)
{
    list->listNewNum = 0;//读取连续数据将未读数量清空
    updataListInfo(list);
    return 0;
}
//读取单个链表数据，返回读取的数据长度，为0为空数据或者失败
DWORD readListData(fileInfo *list,DWORD readNum,BYTE *buf)
{
    //DWORD retCount=0;
    int ret=0;
    //int i;
    //BYTE *p = buf;
    loff_t pos=0;
    if(readNum > list->listNum || readNum==0)//链表总数处理
        return 0;
    //未初始化，直接返回
    if(list->listSize==0)
        return 0;
    if(list->fileName[0]==0)
        return 0;

    openListFile(list);

    pos = list->listSize*((list->listWp+readNum-1)%list->listNum) + list->fileTableSize;

    ret = rt_file_read(list->fd,buf,list->listSize,&pos);

    if(ret<0)
        return ret;

    //rt_printf("readListData: ");
    //for(i=0;i<list->listSize;i++)
    //rt_printf("0x%X,",p[i]);
    //rt_printf("\n");

    return list->listSize;
}



//写先入先出模式单个链表数据
void writeFifoListData(fileInfo *list,BYTE *buf)
{
    int i;
    //BYTE *p = buf;
    loff_t pos=0;
    //未初始化，直接返回
    if(list->listSize==0)
        return;
    if(list->fileName[0]==0)
        return;
    if(list->listNum++ >= list->listMax)//链表总数处理
        list->listNum = list->listMax;

    if(list->listNewNum++ >= list->listMax)//未读链表数据处理
        list->listNewNum = list->listMax;

    openListFile(list);
    //rewind(list->fd);
    pos = (list->listSize*list->listWp) + list->fileTableSize;

    //rt_file_llseek(list->fd,pos,SEEK_SET);
    i = rt_file_write(list->fd,buf,list->listSize,&pos);

    pos = (list->listSize*list->listWp) + list->fileTableSize;

    //rt_printf("\r\n %s writeWp=%d pos=%d 写成功大小=%d 写数据大小=%d offset=%d ListNew=%d \r\n",list->fileName,list->listWp,pos,i,list->listSize*list->listWp,list->listSize,list->listNewNum);
    list->listWp++;
    if((list->listWp % list->listMax) == 0)//写指针迂回
        list->listWp=0;

    //rt_printf("writeListData: ");
    //for(i=0;i<list->listSize;i++)
    //rt_printf("0x%X,",p[i]);
    //rt_printf("\n");
    rt_file_close(list->fd,0);
    list->fd=FILE_NULL;
    updataListInfo(list);
}

//链表数据替换
int  AllNumReplaceListData(fileInfo *list)
{
    DWORD count;
    int ret=0;
    loff_t pos=0;
    BYTE buf1[FILE_NAME_NUM];
    DWORD ListMax=2*list->listMax;

    if(list->listNum<ListMax)
        return ret;

    openListFile(list);
    for(count=0; count<ListMax/2; count++)
    {
        pos = (list->listSize*(ListMax/2+count)) + list->fileTableSize;   //0->N-1   ==>  N->2N-1
        ret = rt_file_read(list->fd,buf1,list->listSize,&pos);     //get the N+count frame data
        if(ret<0)
        {
            rt_printf("all file data replace error!\r\n");
            return ret;
        }

        pos = (list->listSize*(count)) + list->fileTableSize;
        ret=rt_file_write(list->fd,buf1,list->listSize,&pos);
        if(ret<0)
        {
            rt_printf("all file data replace error!\r\n");
            return ret;
        }

    }

    list->listNum=list->listMax;
    list->listNewNum=list->listMax;
    list->listWp=list->listMax;

    return 0;

}


//写全数替换模式单个链表数据
void writeAllNumReplaceListData(fileInfo *list,BYTE *buf)
{
    int i;
    loff_t pos=0;
    DWORD ListMax=2*list->listMax;


    if(list->listNum++ >= ListMax)//链表总数处理
        list->listNum = ListMax;

    if(list->listNewNum++ >= ListMax)//未读链表数据处理
        list->listNewNum = ListMax;

    openListFile(list);

    pos = (list->listSize*list->listWp) + list->fileTableSize;

    i = rt_file_write(list->fd,buf,list->listSize,&pos);

    //pos = (list->listSize*list->listWp) + list->fileTableSize;

    //rt_printf("\r\n %s writeWp=%d pos=%d 写成功大小=%d 写数据大小=%d offset=%d ListNew=%d \r\n",list->fileName,list->listWp,pos,i,list->listSize*list->listWp,list->listSize,list->listNewNum);
    list->listWp++;
    if((list->listWp % ListMax) == 0)  //若写满2×N时
    {
        AllNumReplaceListData(list);   //搬迁替换
    }

    //rt_printf("writeListData: ");
    //for(i=0;i<list->listSize;i++)
    //rt_printf("0x%X,",p[i]);
    //rt_printf("\n");
    rt_file_close(list->fd,0);
    list->fd=FILE_NULL;
    updataListInfo(list);
}

//覆盖某一条数据
int ReplaceListData(fileInfo *list,int replaceNum,BYTE *buf)
{
    loff_t pos=0;

    if(replaceNum > list->listNum || replaceNum==0)//链表总数处理
        return 0;

    openListFile(list);
    pos = list->listSize*((list->listWp+replaceNum-1)%list->listNum) + list->fileTableSize;

    rt_file_write(list->fd,buf,list->listSize,&pos);     //覆盖某一条数据

    rt_file_close(list->fd,0);
    list->fd=FILE_NULL;
    updataListInfo(list);

    return 1;
}


//写多文件模式下单个链表数据
void writeMultipleFileListData(fileInfo *list,BYTE *data)
{
    int i;
    //BYTE *p = buf;
    loff_t pos=0;
    //BYTE buf[64*4];
    DataListFileInfoType DataListFileInfo;


    //未初始化，直接返回
    if(list->listSize==0)
        return;
    if(list->fileName[0]==0)
        return;
    if(list->listNum++ >= list->listMax)//链表总数处理
        list->listNum = list->listMax;

    if(list->listNewNum++ >= list->listMax)//未读链表数据处理
        list->listNewNum = list->listMax;


    if(GetMultipleFileNameInfo(list,list->fileWp,&DataListFileInfo)<0)
    {
        return ;
    }

    openListFile(list);

    pos = (list->listSize*list->listWp) + list->fileTableSize;

    //rt_file_llseek(list->fd,pos,SEEK_SET);
    i = rt_file_write(list->fd,data,list->listSize,&pos);

    //pos = (list->listSize*list->listWp) + list->fileTableSize;
    //rt_printf("\r\n %s writeWp=%d pos=%d 写成功大小=%d 写数据大小=%d offset=%d ListNew=%d \r\n",list->fileName,list->listWp,pos,i,list->listSize*list->listWp,list->listSize,list->listNewNum);
    list->listWp++;
    if((list->listWp % list->listMax) == 0)//写指针迂回
        list->listWp=0;

    //rt_printf("writeListData: ");
    //for(i=0;i<list->listSize;i++)
    //rt_printf("0x%X,",p[i]);
    //rt_printf("\n");
    rt_file_close(list->fd,0);
    list->fd=FILE_NULL;
    updataListInfo(list);
}





void writeListData(fileInfo *list,BYTE *buf)
{

    //未初始化，直接返回
    if(list->listSize==0)
        return ;
    if(list->fileName[0]==0)
        return ;


    switch(list->fileWRType)
    {
        case FIRST_INT_FIRST_OUT_TYPE:
            writeFifoListData(list,buf);
            break;
        case ALL_LIST_NUM_REPLACE_TYPE:
            writeAllNumReplaceListData(list,buf);
            break;
        case MULTIPLE_FILE_WR_TYPE:
            writeMultipleFileListData(list,buf);
            break;
        default:
            break;
    }
}




//读取最先产生的未读数据,返回指针写长度
DWORD readNewListData(fileInfo *list,BYTE *buf)
{
    WORD readOffset;
    int i;
    loff_t pos=0;
    if(list->listNewNum==0)
        return 0;
    //未初始化，直接返回
    if(list->listSize==0)
        return 0;
    if(list->fileName[0]==0)
        return 0;
    if(list->listNewNum > list->listWp)
        readOffset = list->listMax-list->listNewNum+list->listWp;
    else
        readOffset = list->listWp-list->listNewNum;
    rt_printf("%s readOffset=%d new=%d wp=%d size=%d\n",list->fileName,readOffset,list->listNewNum,list->listWp,list->listSize);

    openListFile(list);
    //rewind(list->fd);
    pos = (list->listSize*readOffset) + list->fileTableSize;
    //rt_file_llseek(list->fd,(list->listSize*readOffset) + list->fileTableSize,SEEK_SET);


    i =rt_file_read(list->fd,buf,list->listSize,&pos);

    rt_file_close(list->fd,0);
    list->fd=FILE_NULL;
    list->listNewNum--;
    updataListInfo(list);
    return list->listSize;
}


#ifdef ADD_MULTIPLE_FILE_WR_FUN

//获取文件名信息
//fileNum: 0~fileTatal
int GetMultipleFileNameInfo(fileInfo* list,DWORD fileNum,DataListFileInfoType* DataListFileInfo)
{
    BYTE buf[FILE_NAME_NUM];
    int ret,count;
    loff_t pos=0;
    char Sum=0;

    //未初始化，直接返回
    if(list->listSize==0)
        return -1;
    if(list->fileName[0]==0)
        return -1;

    if(list->fileTatal<=1||list->fileWRType!=MULTIPLE_FILE_WR_TYPE)   //非多文件读写
        return -1;

    if(fileNum>=list->fileTatal)
        return -1;

    sprintf(buf,"%s/%s_info",HF_HISTORY_DB_INFO,list->fileName);
    list->fd = rt_file_open((char *)&buf,O_RDWR,0777);
    if( IS_ERR(list->fd))
    {
        rt_file_close(list->fd,0);
        return -1;
    }

    pos = sizeof(fileInfo) + fileNum*sizeof(DataListFileInfoType);

    ret = rt_file_read(list->fd,(char*)DataListFileInfo,sizeof(DataListFileInfoType),&pos);
    if(ret<0)
        return ret;

    if(list->fd)
    {
        if(!IS_ERR(list->fd))     //判断是否为错误的    FD
            rt_file_close(list->fd,0);
    }


    for(count=0; count<(sizeof(DataListFileInfoType)-1); count++)
        Sum+=*((char*)DataListFileInfo+count);

    if(Sum!=DataListFileInfo->SumValue)
        memset((char*)DataListFileInfo,0,sizeof(DataListFileInfoType));

    return 1;
}

//更新文件名信息
//fileNum: 0~fileTatal
int UpdateMultipleFileNameInfo(fileInfo* list,DWORD fileNum,DataListFileInfoType* DataListFileInfo)
{
    BYTE buf[FILE_NAME_NUM];
    int ret,count;
    loff_t pos=0;
    char Sum=0;

    //未初始化，直接返回
    if(list->listSize==0)
        return -1;
    if(list->fileName[0]==0)
        return -1;

    if(list->fileTatal<=1||list->fileWRType!=MULTIPLE_FILE_WR_TYPE)   //非多文件读写
        return -1;

    if(fileNum>=list->fileTatal)
        return -1;

    sprintf(buf,"%s/%s_info",HF_HISTORY_DB_INFO,list->fileName);
    list->fd = rt_file_open((char *)&buf,O_RDWR,0777);
    if( IS_ERR(list->fd))
    {
        rt_file_close(list->fd,0);
        return -1;
    }

    for(count=0; count<(sizeof(DataListFileInfoType)-1); count++)
        Sum+=*((char*)DataListFileInfo+count);
    DataListFileInfo->SumValue=Sum;

    pos = sizeof(fileInfo) + fileNum*sizeof(DataListFileInfoType);

    ret = rt_file_write(list->fd,(char*)DataListFileInfo,sizeof(DataListFileInfoType),&pos);
    if(ret<0)
        return ret;

    rt_file_close(list->fd,0);

    updataListInfo(list);
    return 1;
}




//获取Title信息
//fileNum: 0~fileTatal
int GetDataListFileTitleInfo(fileInfo* list,DWORD fileNum,char* TitleData)
{
    BYTE buf[FILE_NAME_NUM];
    int ret;
    loff_t pos=0;


    //未初始化，直接返回
    if(list->listSize==0)
        return -1;
    if(list->fileName[0]==0)
        return -1;

    if(list->fileTatal>1&&list->fileWRType==MULTIPLE_FILE_WR_TYPE)      //多文件读写方式
    {
        DataListFileInfoType  DataListFileInfoData;
        if(GetMultipleFileNameInfo(list,fileNum,&DataListFileInfoData)<0)
            return -1;
        sprintf(buf,"%s/%s",list->dirName,(char*)&DataListFileInfoData.fileName[0]);
    }
    else
    {
        sprintf(buf,"%s/%s",list->dirName,list->fileName);
    }

    if(list->fd)
        rt_file_close(list->fd,0);

    list->fd = rt_file_open((char *)&buf,O_RDWR,0);
    if( IS_ERR(list->fd))
        rt_printf("GetDataListFileTitleInfo:error /%s \n",buf);

    pos=0;
    ret = rt_file_read(list->fd,TitleData,list->fileTableSize,&pos);  //获取TABLE数据
    if(ret<0)
        return ret;

    rt_file_close(list->fd,0);
    return 1;
}

//更新Title信息
//fileNum: 0~fileTatal
int UpdateDataListFileTitleInfo(fileInfo* list,DWORD fileNum,char* TitleData)
{
    BYTE buf[FILE_NAME_NUM];
    int ret;
    loff_t pos=0;


    //未初始化，直接返回
    if(list->listSize==0)
        return -1;
    if(list->fileName[0]==0)
        return -1;

    if(list->fileTatal>1&&list->fileWRType==MULTIPLE_FILE_WR_TYPE)      //多文件读写方式
    {
        DataListFileInfoType  DataListFileInfoData;
        if(GetMultipleFileNameInfo(list,fileNum,&DataListFileInfoData)<0)
            return -1;
        sprintf(buf,"%s/%s",list->dirName,(char*)&DataListFileInfoData.fileName[0]);
    }
    else
    {
        sprintf(buf,"%s/%s",list->dirName,list->fileName);
    }

    if(list->fd)
    {
        if(!IS_ERR(list->fd))     //判断是否为错误的    FD
            rt_file_close(list->fd,0);
    }


    list->fd = rt_file_open((char *)&buf,O_RDWR,0);
    if( IS_ERR(list->fd))
        rt_printf("GetDataListFileTitleInfo:error /%s \n",buf);

    pos=0;
    ret = rt_file_write(list->fd,TitleData,list->fileTableSize,&pos);  //更新TABLE数据
    if(ret<0)
        return ret;

    rt_file_close(list->fd,0);

    return 1;
}





int CreateNewMultipleFileDataList(fileInfo *list,char* DataListName)
{
    BYTE buf[FILE_NAME_NUM];
    DataListFileInfoType DataListFileInfo;
    struct file *fd;

    //未初始化，直接返回
    if(list->listSize==0)
        return -1;
    if(list->fileName[0]==0)
        return -1;
    if(DataListName==NULL)
        return -1;

    if(list->fileTatal<=1||list->fileWRType!=MULTIPLE_FILE_WR_TYPE)   //非多文件读写
        return -1;

    if(GetMultipleFileNameInfo(list,list->fileWp,&DataListFileInfo)<0)  //获取当前需覆盖的文件名称
    {
        rt_printf("get multiple file name error!!\r\n");
        return -1;
    }

    sprintf(buf,"%s/%s",list->dirName,(char*)&DataListFileInfo.fileName[0]);   //当前文件的路径与名称
    fd = rt_file_open((char *)&buf,O_RDWR,0777);
    if( IS_ERR(fd))
    {
        //TODO: 该文件名未创建数据文件
        ;
    }
    else
    {

        rt_file_del(buf);  //删除之前被替换文件
    }


    //保存文件名列表
    memset((char*)&DataListFileInfo,0,sizeof(DataListFileInfoType));
    memcpy((char*)&DataListFileInfo.fileName[0],DataListName,64);
    if(UpdateMultipleFileNameInfo(list,list->fileWp,&DataListFileInfo)<0)  //替换当前需覆盖的文件名称
    {
        rt_printf("update multiple file name error!!\r\n");
        return -1;
    }

    //创建新文件
    openListFile(list);

    list->listWp = 0;    //初始化链表参数
    list->listNum = 0;
    list->listNewNum = 0;

    if(++list->fileWp >=list->fileTatal)
        list->fileWp=0;

    updataListInfo(list);   //保存基础链表数据
    rt_printf("cearte multiple file:%s!!\r\n",DataListName);
    return 1;
}



int DelMultipleListFile(fileInfo *list,char* FileName)
{
    BYTE buf[FILE_NAME_NUM];
    DataListFileInfoType DataListFileInfo;
    DWORD count,CurFileNum=0;

    //未初始化，直接返回
    if(list->listSize==0)
        return -1;
    if(list->fileName[0]==0)
        return -1;
    if(FileName==NULL)
        return -1;
    if(list->fileTatal<=1||list->fileWRType!=MULTIPLE_FILE_WR_TYPE)   //非多文件读写
        return -1;

    for(count=0; count<list->fileTatal; count++)
    {
        if(GetMultipleFileNameInfo(list,count,&DataListFileInfo)<0)
        {
            rt_printf("get multiple file name error!!\r\n");
            return -1;
        }
        if(strcmp(FileName,DataListFileInfo.fileName)==0)
            break;

    }
    if(count==list->fileTatal)
    {
        rt_printf("del file no vaild!!\r\n");
        return -1;
    }

    GetCurMultipleFileNumPos(list,&CurFileNum);
    if(CurFileNum==count)   //若删除当前的文件则不成功报非法
    {
        rt_printf("del file unlaw!!\r\n");
        return -1;
    }



    sprintf(buf,"%s/%s",list->dirName,(char*)&DataListFileInfo.fileName[0]);

    if(list->fd)
    {
        if(!IS_ERR(list->fd))     //判断是否为错误的    FD
            rt_file_close(list->fd,0);
    }

    list->fd = rt_file_open((char *)&buf,O_RDWR,0);
    if(IS_ERR(list->fd))
    {
        return -1;
    }
    if(rt_file_del((char *)&buf))
    {
        return -2;
    }

    //保存文件名列表
    memset((char*)&DataListFileInfo,0,sizeof(DataListFileInfoType));
    if(UpdateMultipleFileNameInfo(list,count,&DataListFileInfo)<0)  //替换当前需覆盖的文件名称
    {
        rt_printf("update multiple file name error!!\r\n");
        return -1;
    }

    rt_printf("del flie:%s ok!!\r\n",buf);


    return 1;

}


int  GetCurMultipleFileNumPos(fileInfo *list,DWORD* FilePos           )
{
    //未初始化，直接返回
    if(list->listSize==0)
        return -1;
    if(list->fileName[0]==0)
        return -1;

    if(list->fileTatal<=1||list->fileWRType!=MULTIPLE_FILE_WR_TYPE)   //非多文件读写
        return -1;

    if(list->fileWp<1)
        *FilePos=list->fileTatal-1;
    else
        *FilePos=list->fileWp-1;


    return 1;


}

//删除文件的数据 包括链表与存储数据
int DelAllListDataFile(fileInfo *list)
{
    BYTE buf[FILE_NAME_NUM];
    DWORD count;

    //未初始化，直接返回
    if(list->listSize==0)
        return -1;
    if(list->fileName[0]==0)
        return -2;

    if(list->fileTatal>1&&list->fileWRType==MULTIPLE_FILE_WR_TYPE)   //多文件读写
    {
        DataListFileInfoType DataListFileInfo;
        for(count=0; count<list->fileTatal; count++)    //循环删除多文件数据
        {
            if(GetMultipleFileNameInfo(list,count,&DataListFileInfo)<0)
            {
                rt_printf("get multiple file name error!!\r\n");
                continue;
            }
            sprintf(buf,"%s/%s",list->dirName,(char*)&DataListFileInfo.fileName[0]);
            if(list->fd)
            {
                if(!IS_ERR(list->fd))     //判断是否为错误的    FD
                    rt_file_close(list->fd,0);
            }
            list->fd = rt_file_open((char *)&buf,O_RDWR,0);
            if(IS_ERR(list->fd))
            {
                continue;
            }
            if(rt_file_del((char *)&buf))
            {
                continue;
            }

        }

    }


    sprintf(buf,"%s/%s",list->dirName,list->fileName);   //删除基础数据文件
    if(list->fd)
    {
        if(!IS_ERR(list->fd))     //判断是否为错误的    FD
            rt_file_close(list->fd,0);
    }
    list->fd = rt_file_open((char *)&buf,O_RDWR,0);
    if(!IS_ERR(list->fd))
    {
        if(rt_file_del((char *)&buf))
        {
            rt_printf("del base list data error!!\r\n");
        }
       // else
        //    rt_printf("del file：%s\r\n",buf);

    }




    sprintf(buf,"%s/%s_info",HF_HISTORY_DB_INFO,list->fileName); //删除链表文件
    if(list->fd)
    {
        if(!IS_ERR(list->fd))     //判断是否为错误的    FD
            rt_file_close(list->fd,0);
    }

    list->fd = rt_file_open((char *)&buf,O_RDWR,0);
    if(!IS_ERR(list->fd))
    {
        if(rt_file_del((char *)&buf))
        {
            rt_printf("del base list data error!!\r\n");
        }
        //else
           // rt_printf("del file：%s\r\n",buf);
    }

    memset((char*)list,0,sizeof(fileInfo));

    return 0;
}





#endif



