#ifdef __cplusplus
extern "C"
{
#endif
#include "config.h"
#ifdef __cplusplus
};
#endif
tag_DBF_WORKAREA_LIST g_Dbf_WorkArea_List;
//DBF文件操作函数实现
uint8 DbfCreateFile(uint8 *pFileName,tag_DBF_FieldInfo *pFieldInfo,uint8 nCount)
{
time_t tm_t;
struct tm *ptm;
uint8 n;
uint8 buf[]={DBF_HeadInfo_EOF,DBF_FILE_EOF};
tag_DBF_WORKAREA dbfWorkArea;
memset(&dbfWorkArea,0,sizeof(tag_DBF_WORKAREA));
strcpy((char *)dbfWorkArea.m_sDbfFileName,(char *)pFileName);
dbfWorkArea.m_DbfHeadInfo.m_FileType=0x03;
tm_t=time(NULL);
ptm=localtime(&tm_t);
dbfWorkArea.m_DbfHeadInfo.m_LastModifyDate.m_year=ptm->tm_year;
dbfWorkArea.m_DbfHeadInfo.m_LastModifyDate.m_month=ptm->tm_mon;
dbfWorkArea.m_DbfHeadInfo.m_LastModifyDate.m_day=ptm->tm_mday;
memcpy(dbfWorkArea.m_DbfFieldInfo,pFieldInfo,nCount*sizeof(tag_DBF_FieldInfo));
dbfWorkArea.m_DbfHeadInfo.m_FieldCount=nCount;
dbfWorkArea.m_DbfHeadInfo.m_FirstRecordPos=sizeof(tag_DBF_HeadInfo)+nCount*sizeof(tag_DBF_FieldInfo)+1;
dbfWorkArea.m_DbfHeadInfo.m_ByteSizePerRecord++;
for(n=0;n<nCount;n++)
{
dbfWorkArea.m_DbfHeadInfo.m_ByteSizePerRecord+=pFieldInfo[n].m_FieldByteSize;
}
dbfWorkArea.m_pFileHandle=fopen((char *)pFileName,"wb+");
if(dbfWorkArea.m_pFileHandle==NULL)
{
return FALSE;
}
fseek(dbfWorkArea.m_pFileHandle,0,SEEK_SET);
fwrite(&dbfWorkArea.m_DbfHeadInfo,sizeof(tag_DBF_HeadInfo),1,dbfWorkArea.m_pFileHandle);
fwrite(pFieldInfo,nCount*sizeof(tag_DBF_FieldInfo),1,dbfWorkArea.m_pFileHandle);
fwrite(buf,2,1,dbfWorkArea.m_pFileHandle);
fclose(dbfWorkArea.m_pFileHandle);
return TRUE;
}
uint8 DbfUse(uint8 *pFileName)
{
uint8 n,m;
for(n=0;n<DBF_MAX_WORKAREA_COUNT;n++)
{
if(g_Dbf_WorkArea_List.m_WorkAreas[n].m_pFileHandle==NULL)
{
g_Dbf_WorkArea_List.m_WorkAreas[n].m_pFileHandle=fopen((char *)pFileName,"r");
if(g_Dbf_WorkArea_List.m_WorkAreas[n].m_pFileHandle==NULL)
{
memset(&g_Dbf_WorkArea_List.m_WorkAreas[n],0,sizeof(tag_DBF_WORKAREA));
return 0xff;
}
fclose(g_Dbf_WorkArea_List.m_WorkAreas[n].m_pFileHandle);
g_Dbf_WorkArea_List.m_WorkAreas[n].m_pFileHandle=fopen((char *)pFileName,"rb+");
if(g_Dbf_WorkArea_List.m_WorkAreas[n].m_pFileHandle==NULL)
{
memset(&g_Dbf_WorkArea_List.m_WorkAreas[n],0,sizeof(tag_DBF_WORKAREA));
return 0xff;
}
//
strcpy((char *)g_Dbf_WorkArea_List.m_WorkAreas[n].m_sDbfFileName,(char *)pFileName);
//
fseek(g_Dbf_WorkArea_List.m_WorkAreas[n].m_pFileHandle,0,SEEK_SET);
//
fread(&g_Dbf_WorkArea_List.m_WorkAreas[n].m_DbfHeadInfo,sizeof(tag_DBF_HeadInfo),1,g_Dbf_WorkArea_List.m_WorkAreas[n].m_pFileHandle);
if(g_Dbf_WorkArea_List.m_WorkAreas[n].m_DbfHeadInfo.m_FileType!=0x03)
{
fclose(g_Dbf_WorkArea_List.m_WorkAreas[n].m_pFileHandle);
memset(&g_Dbf_WorkArea_List.m_WorkAreas[n],0,sizeof(tag_DBF_WORKAREA));
return 0xff;
}
g_Dbf_WorkArea_List.m_WorkAreas[n].m_DbfHeadInfo.m_FieldCount=(g_Dbf_WorkArea_List.m_WorkAreas[n].m_DbfHeadInfo.m_FirstRecordPos-sizeof(tag_DBF_HeadInfo)-1)/sizeof(tag_DBF_FieldInfo);
//
m=g_Dbf_WorkArea_List.m_WorkAreas[n].m_DbfHeadInfo.m_FieldCount;
fread(g_Dbf_WorkArea_List.m_WorkAreas[n].m_DbfFieldInfo,sizeof(tag_DBF_FieldInfo),m,g_Dbf_WorkArea_List.m_WorkAreas[n].m_pFileHandle);
//
fseek(g_Dbf_WorkArea_List.m_WorkAreas[n].m_pFileHandle,1,SEEK_CUR);
//
return n;
}
}
return 0xff;
}
uint8 DbfUnUse(uint8 nWorkArea)
{
if(nWorkArea>=DBF_MAX_WORKAREA_COUNT)
{
return FALSE;
}
if(g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_pFileHandle!=NULL)
{
fclose(g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_pFileHandle);
memset(&g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea],0,sizeof(tag_DBF_WORKAREA));
}
return TRUE;
}
tag_DBF_WORKAREA *DbfGetWorkAreaInfo(uint8 nWorkArea)
{
if(nWorkArea>=DBF_MAX_WORKAREA_COUNT)
{
return FALSE;
}
return &g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea];
}
tag_DBF_HeadInfo *DbfGetHeadInfo(uint8 nWorkArea)
{
if(nWorkArea>=DBF_MAX_WORKAREA_COUNT)
{
return FALSE;
}
return &g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_DbfHeadInfo;
}
tag_DBF_FieldInfo *DbfGetFieldInfo(uint8 nWorkArea)
{
if(nWorkArea>=DBF_MAX_WORKAREA_COUNT)
{
return FALSE;
}
if(g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_DbfHeadInfo.m_FieldCount==0)
{
return FALSE;
}
return &g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_DbfFieldInfo[g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_nCurFieldIndex];
}
uint8 DbfBof(uint8 nWorkArea)
{
if(nWorkArea>=DBF_MAX_WORKAREA_COUNT)
{
return FALSE;
}
if(g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_nCurRecordIndex>0)
{
return FALSE;
}
return TRUE;
}
uint8 DbfEof(uint8 nWorkArea)
{
if(nWorkArea>=DBF_MAX_WORKAREA_COUNT)
{
return FALSE;
}
if(g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_nCurRecordIndex<(g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_DbfHeadInfo.m_RecordCount-1))
{
return FALSE;
}
return TRUE;
}
uint8 DbfReadField(uint8 nWorkArea,uint8 *pBuf,uint8 nlen)
{
if(nWorkArea>=DBF_MAX_WORKAREA_COUNT)
{
return FALSE;
}
if(pBuf==NULL)
{
return FALSE;
}
if(g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_DbfFieldInfo[g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_nCurFieldIndex].m_FieldByteSize>nlen)
{
return FALSE;
}
DbfSeek(nWorkArea, g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_nCurRecordIndex, g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_nCurFieldIndex);
fread(pBuf,g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_DbfFieldInfo[g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_nCurFieldIndex].m_FieldByteSize,1,g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_pFileHandle);
return TRUE;
}
uint8 DbfWriteField(uint8 nWorkArea,uint8 *pBuf,uint8 nlen)
{
if(nWorkArea>=DBF_MAX_WORKAREA_COUNT)
{
return FALSE;
}
if(pBuf==NULL)
{
return FALSE;
}
if(g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_DbfFieldInfo[g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_nCurFieldIndex].m_FieldByteSize<nlen)
{
return FALSE;
}
DbfSeek(nWorkArea, g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_nCurRecordIndex, g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_nCurFieldIndex);
fwrite(pBuf,nlen,1,g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_pFileHandle);
return TRUE;
}
uint8 DbfSeek(uint8 nWorkArea,uint32 nRecord,uint8 nField)
{
uint32 offset;
uint8 n;
if(nWorkArea>=DBF_MAX_WORKAREA_COUNT)
{
return FALSE;
}
if(nRecord>(g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_DbfHeadInfo.m_RecordCount-1))
{
return FALSE;
}
if(nField>(g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_DbfHeadInfo.m_FieldCount-1))
{
return FALSE;
}
g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_nCurRecordIndex=nRecord;
g_Dbf_WorkArea_List.m_WorkAreas[nWorkArea].m_nCurFieldIndex=nField;
offset=g_Dbf_WorkArea_List.m_WorkAreas