/******************************************************************************
*
* Copyrigth(C), 2002-2005,Sanxing Tech,Co.,Ltd.
* Filename: dbf.c
* Version: V1.0
* Description: DbaseIII读写函数实现
* Author:Arvin
* Date:2008/03/09
modification history
---------------------
* $Log: dbf.c,v $
*******************************************************************************/
#define _DBASE_C_
#ifdef __cplusplus /* Maintain C++ compatibility */
extern "C" {
#endif /* __cplusplus */
/*---------------------------------------------------------------------------
Section: Includes
----------------------------------------------------------------------------*/
#include "dbase.h"
/*---------------------------------------------------------------------------
---
Section: Macro Definitions
----------------------------------------------------------------------------*/
/* None */
/*---------------------------------------------------------------------------
Section: Type Definitions
----------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------
Section: Globals
----------------------------------------------------------------------------*/
/* None */
/*---------------------------------------------------------------------------
Section: Function Prototypes
----------------------------------------------------------------------------*/
/******************************************************************************
*
*
* db_open - 打开db文件
*
* INPUTS:
* char *filename :文件路径
* RETURNS:
* DBASE_FILE: 成功:文件信息指针
* 失败:NULL
*
*******************************************************************************/
DBASE_FILE *db_open(char *filename)
{
INT16 i, inptr,j;
DBASE_FILE *db_pointer ;
INT16 uHeadLenth;
DB_DESC *db_desc;
DBASE_HEADER *db_header;
INT16 nDescNum;
/*分配文件说明信息内存*/
if( (db_pointer = malloc(sizeof(DBASE_FILE))) == 0 ){
puts("Insufficient HEAP space to open DBASE file description block");
puts("Called from db_open");
exit(0);
}
/*分配头dbf 32字节内存*/
if( (db_header = malloc(sizeof(DBASE_HEADER))) == 0 ){
puts("Insufficient HEAP space to open DBASE file description block");
puts("Called from db_open");
exit(0);
}
/*打开dbf文件*/
if( (inptr=open(filename, O_RDWR)) == ERROR)return(NULL);
/*读前32字节*/
if(read(inptr,db_header,DB_HDR_SZ)== ERROR)return(NULL);
/*取得文件头字节数*/
uHeadLenth = db_header->head_num;
/*计算变量域个数*/
nDescNum = (uHeadLenth - DB_HDR_SZ - 1)>>5;
/*动态分配存储变量域信息内存*/
if( (db_desc = malloc(nDescNum*sizeof(DB_DESC))) == 0 ){
puts("Insufficient HEAP space to open DBASE file description block");
puts("Called from db_open");
exit(0);
}
/*将文件偏移至变量说明域开始*/
if(lseek(inptr,DB_HDR_SZ,0)== ERROR)return(NULL);
/*读变量说明域*/
if(read(inptr,db_desc,(nDescNum*sizeof(DB_DESC)))== ERROR)return(NULL);
/*存储文件信息*/
db_pointer->db_header = db_header;
db_pointer->field_desc = db_desc;
db_pointer->var_num = nDescNum;
db_pointer->file_ptr = inptr;
db_pointer->chng_ind = FALSE ;
db_pointer->curr_rec = 0 ;
/*置文件指针位置至数据开始处*/
if(lseek(inptr,uHeadLenth+1,0)== ERROR)return(NULL);
/*返回文件信息指针*/
return(db_pointer);
}
/******************************************************************************
*
*
* db_read - 读db文件
*
* INPUTS:
* DBASE_FILE *db_ptr :文件信息
* char buffer[]:读缓冲
* int n :字节个数
* RETURNS:
* int: 成功:记录条数失败:EOF
*
*******************************************************************************/
int db_read(DBASE_FILE *db_ptr,char buffer[],int n )
{
INT16 char_transfered, length ;
length = n * db_ptr->db_header->rec_len;
char_transfered = read(db_ptr->file_ptr,buffer,length) ;
if( char_transfered == length){
buffer[char_transfered] = '\0' ;
db_ptr->curr_rec++ ;
return( char_transfered/db_ptr->db_header->rec_len );
}else {
if( char_transfered == EOF) {
buffer[0] = '\0' ;
return(EOF);
}else {
buffer[char_transfered] = '\0' ;
return( char_transfered/db_ptr->db_header->rec_len);
}
}
}
/******************************************************************************
*
*
* main - 测试函数
*
* INPUTS:
* void
* RETURNS:
* int: 0
*
*******************************************************************************/
int main()
{
DBASE_FILE *pDbFile;
INT16 i,j,nLen,index,k,rec_num;
char *nBuffer;
if((pDbFile = db_open("tt.dbf"))== NULL){
printf("Open File Error!\n\r");
getch();
return 0;
}
printf("Version:%d\n\r",pDbFile->db_header->version);
printf("RecNum:%d\n\r",pDbFile->db_header->rec_num);
printf("Field:%d\n\r",pDbFile->var_num);
printf("----------------------------------------------------------------------\n\r");
if((nBuffer = malloc((pDbFile->var_num)*(pDbFile->db_header->rec_len))) == 0){
puts("Insufficient HEAP space to open DBASE file description block");
puts("Called from db_open");
exit(0);
}
rec_num = db_read(pDbFile,nBuffer,((pDbFile->var_num)*(pDbFile->db_header->rec_len)));
for(index = 0;index < rec_num;index++){
for(i = 0;i < pDbFile->var_num;i++){
for(j = 0;j<11;j++)
printf("%c",pDbFile->field_desc[i].name[j]);
printf("(%c)(%02d)",pDbFile->field_desc[i].var_type,pDbFile->field_desc[i].var_len);
switch(pDbFile->field_desc[i].var_type){
case'B':
case'C':
case'G':
case'M':
case'F':
for(k = 0;k < pDbFile->field_desc[i].var_len;k++)
printf("%c",nBuffer[k]);
nBuffer += k;
break;
case'N':
for(k = 0;k < pDbFile->field_desc[i].var_len;k++)
printf("%c",nBuffer[k]);
nBuffer += k;
break;
}
printf("\n\r");
}
printf("----------------------------------------------------------------------\n\r");
nBuffer +=1;
}
getch();
return 0;
}
#ifdef __cplusplus /* Maintain C++ compatibility */
}
#endif /*_DBASE_C_ */
/*-------------------------------End of dbf.c-------------------------------*/
评论0