/************************************************************************************
* 文件名称:LoopFile.h
* 循环文件控制结构
*
*
*
*
************************************************************************************
*/
struct LF_ctrl {
unsigned int startpos; /*记录开始位置,从1~ */
unsigned int endpos; /*记录结束位置 */
unsigned int total; /*当前的总记录数*/
unsigned int sum; /*循环文件中规定的记录上限*/
unsigned int synflag; /*最后一条记录同步标志*/
unsigned int nouse1;
unsigned int nouse2;
};
/*************************************************************************************************
* 文件名称:LoopFile.c
* 功能:循环文件访问
* 底层函数需要PackageFileSystem.c
* 日期:2007-11-30
*
*
*
*
*************************************************************************************************
*/
#include <stdio.h>
#include "LoopFile.h"
/*=================================================================================================
* 函数名称:InitLoopFile()
* 输入参数:int number 循环文件最高容纳记录数
* 输出参数:void
*
*
*=================================================================================================
*/
void InitLoopFile(int number, char *path)
{
FILE *lf_fd;
struct LF_ctrl lfc;
memset(&lfc, 0x00, sizeof(struct LF_ctrl));
lfc.sum = number;
if((lf_fd = Fopen(path, "w")) == NULL)
{
#if DEBUG_ALL || DEBUG_MIX
DEBUG_printf("OpenOSFile() failed\r\n");
#endif
Fclose(lf_fd);
return;
}
/*=======写入循环文件头结构=====================================*/
if(!Fwrite((void *)&lfc, sizeof(struct LF_ctrl), 1, lf_fd))
{
#if DEBUG_ALL || DEBUG_MIX
DEBUG_printf("WriteOSFile() failed \r\n");
#endif
return;
}
Fclose(lf_fd);
#if DEBUG_ALL || DEBUG_MIX
DEBUG_printf("InitLoopFile OK!\r\n");
#endif
return;
}
/*===================================================================================
* 函数名称:AppendLoopFile()
* 输入描述:
unsigned char *ptr //数据,
int len //数据长度,
char * path //文件路径
* 输出描述:>= 0 成功; < 0 失败
*
*
*====================================================================================
*/
int AppendLoopFile(
unsigned char *ptr, int len,
char *path)
{
FILE *lf_fd;
struct LF_ctrl lfc;
int ilfc;
ilfc = sizeof(struct LF_ctrl);
if((lf_fd = Fopen(path, "r")) == NULL)
{
return(-1);
}
if(Fread((unsigned char *)&lfc, ilfc, 1, lf_fd) != ilfc)
{
#if DEBUG_ALL || DEBUG_MIX
DEBUG_printf("AppendLoopFile: ReadOSFile() failed \r\n");
#endif
Fclose(lf_fd);
return(-1);
}
Fclose(lf_fd);
#if DEBUG_ALL || DEBUG_MIX
DEBUG_printf("APPENDLOOPFILE startpos = [%d] ", lfc.startpos);
DEBUG_printf(" endpos = [%d] ", lfc.endpos);
DEBUG_printf(" total = [%d] ", lfc.total);
DEBUG_printf(" sum = [%d] ", lfc.sum);
DEBUG_printf(" synflag = [%d] ", lfc.synflag);
DEBUG_printf(" nouse1 = [%d] ", lfc.nouse1);
DEBUG_printf(" nouse2 = [%d] \r\n", lfc.nouse2);
#endif
if((lf_fd = Fopen(path, "ab+")) == NULL)
{
return(-1);
}
if(lfc.total < lfc.sum)
{
if(!Fwrite(ptr, len, 1, lf_fd))
{
Fclose(lf_fd);
return(-1);
}
if(lfc.endpos == 0)
{
lfc.endpos = 1;
}
lfc.startpos++;
lfc.total++;
}
else
{
if(Fseek(lf_fd, i(sizeof(struct LF_ctrl) + len * (lfc.endpos - 1)), SEEK_SET) < 0)
{
Fclose(lf_fd);
return(-1);
}
if(!Fwrite(ptr, len, 1, lf_fd))
{
Fclose(lf_fd);
return(-1);
}
lfc.startpos = (++lfc.startpos > lfc.sum) ? (lfc.startpos - lfc.sum) : lfc.startpos;
lfc.endpos = (++lfc.endpos > lfc.sum) ? (lfc.endpos - lfc.sum) : lfc.endpos;
} /* end if */
// lfc->synflag = 1;
if(Fseek(lf_fd, 0, SEEK_SET) < 0)
{
Fclose(lf_fd);
return(-1);
}
if(!Fwrite((unsigned char *)&lfc, ilfc, 1, lf_fd))
{
Fclose(lf_fd);
return(-1);
}
#if DEBUG_ALL || DEBUG_MIX
DEBUG_printf("AppendLoopFile: lfc.endpos = %d\r\n", lfc.endpos);
#endif
Fclose(lf_fd);
return(lfc.startpos);
} /* end function */
/***************************************************************************************
* 模块名称:ReadLoopFile()
* 输入参数: char *ptr
int len
int offset //记录号,从1~
char *path
* 输出参数:读到的字节数
<=0 读失败
*
***************************************************************************************
*/
int ReadLoopFile(char *ptr, int len, int offset,
char *path)
{
FILE *fd;
struct LF_ctrl lfc;
int ilfc, curpos;
int ans;
if(mainfilename == NULL)
{
return(-1);
}
if(offset < 1)
{
return(-1);
}
if((fd = Fopen(path, "r")) == NULL)
{
return(-1);
}
ilfc = sizeof(struct LF_ctrl);
if(Fread((unsigned char *)&lfc, ilfc, 1, fd) != ilfc)
{
Fclose(fd);
return(-1);
}
#if DEBUG_ALL || DEBUG_MIX
DEBUG_printf("startpos = [%d] \r\n", lfc.startpos);
DEBUG_printf("endpos = [%d] \r\n", lfc.endpos);
DEBUG_printf("total = [%d] \r\n", lfc.total);
DEBUG_printf("sum = [%d] \r\n", lfc.sum);
DEBUG_printf("synflag = [%d] \r\n", lfc.synflag);
DEBUG_printf("nouse1 = [%d] \r\n", lfc.nouse1);
DEBUG_printf("nouse2 = [%d] \r\n", lfc.nouse2);
#endif
if(offset > lfc.total)
{
return(-1);
}
//计算记录位置
/*
if ( lfc.endpos > offset )
curpos = lfc.endpos - offset;
else
curpos = lfc.sum - (offset - lfc.endpos + 1);
*/
curpos = lfc.startpos - (offset - 1);
if(curpos < 1)
{
curpos = lfc.sum + curpos;
}
if(Fseek(fd, (curpos - 1)*len, SEEK_CUR) < 0)
{
return(-1);
}
ans = Fread(ptr, len, 1, fd);
Fclose(fd);
return(ans);
}
/***********************************************************************************************
* 模块名称:UpdateLoopFile()
* 输入参数:char *ptr
int len
int offset //记录号
char *path
* 输出描述: 修改的字节数
<= 0 失败
*
************************************************************************************************
*/
int UpdateLoopFile(char *ptr, int len, int offset,
char *path)
{
FILE *fd;
struct LF_ctrl lfc;
int ilfc, curpos;
int ans;
if(mainfilename == NULL)
{
return(-1);
}
if((fd = Fopen(path, "r")) == NULL)
{
return(-1);
}
ilfc = sizeof(struct LF_ctrl);
if(Fread((unsigned char *)&lfc, ilfc, 1, fd) != ilfc)
{
Fclose(fd);
return(-1);
}
Fclose(fd);
offset = offset % lfc.total;
//计算记录位置
if(lfc.endpos > offset)
{
curpos = lfc.endpos - offset;
}
else
{
curpos = lfc.sum - (offset - lfc.endpos + 1);
}
if((fd = Fwrite(path, "ab+")) == NULL)
{
Fclose(fd);
return(-1);
}
if(Fseek(fd, ((curpos - 1)*len + sizeof(struct LF_ctrl)), SEEK_SET) < 0)
{
Fclose(fd);
return(-1);
}
ans = Fwrite(ptr, len, 1, fd);
Fclose(fd);
return(ans);
}