📄 dbf.c
字号:
/**************************************************/
/* DBMS Engine source File */
/*Copyright:WangHaiyun,HiSoft */
/*Date:1996/08/01 */
/*Last Modified: */
/**************************************************/
#include<stdio.h>
#include<stdarg.h>
#include<fcntl.h>
#include<io.h>
#include<ctype.h>
#include<stdlib.h>
#include<c_base.h>
/*Open database*/
DBF *_Cdecl use(char* dbfname)
{
DBF *dbf;
unsigned char byte_buf;
int i;
dbf=(DBF *)malloc(sizeof(DBF));
if((dbf->handle=open(dbf_name,O_RDWR|O_DENYNONE|O_BINARY))
==-1)
{
free(dbf);
return NULL;
}
read(dbf->handle,&byte_buf,1);
if(byte_buf!=0x03&&byte_buf!=0x83)
{
close(dbf->handle);
free(dbf);
return NULL;
}
read(dbf->handle,&byte_buf,1);
dbf->modify_date.da_year=byte_buf+1900;
read(dbf->handle,&byte_buf,1);
dbf->modify_date.da_mon=byte_buf;
read(dbf->handle,&byte_buf,1);
dbf->modify_date.da_day=byte_buf;
read(dbf->handle,&(dbf->rec_count),4);
read(dbf->handle,&(dbf->stru_len),2);
dbf->fld_count=(dbf->stru_len-33)/32;
read(dbf->handle,&(dbf->rec_len),2);
dbf->dbf_stru=(DBF_STRU*)malloc(dbf->fld_count*sizeof(DBF_STRU));
lseek(dbf->handle,32L,SEEK_SET);
for(i=0;i<dbf->fld_count;i++)
{
read(dbf->handle,dbf->stru[i].field_name,11);
read(dbf->handle,&(dbf->dbf_stru[i].field_type),1);
read(dbf->handle,&(dbf->dbf_stru[i].field_pos),2);
lseek(dbf->handle,2L,SEEK_CUR);
read(dbf->handle,&(dbf->dbf_stru[i].field_len),1);
read(dbf->handle,&(dbf->dbf_stru[i].field_dec),1);
lseek(dbf->handle,14L,SEEK_CUR);
}
lseek(dbf->handle,1L,SEEK_CUR);
dbf->curr_rec=(char*)malloc(dbf->rec_len+1);
if(dbf->rec_count>0)
{
dbf->rec_no=1;
read(dbf->handle,dbf->curr_rec,dbf->rec_len);
dbf->curr_rec[dbf->rec_len]=0;
dbf->eof=NO;
}
else
{
for(i=0;i<dbf->rec_len;i++)
dbf->curr_rec[i]=' ';
dbf->curr_rec[dbf->rec_len]=0;
dbf->rec_no=0;
dbf->eof=YES;
}
dbf->update_tag=NO;
return(dbf);
}
/* Close database */
int _Cdecl close_DBF(DBF* dbf)
{
char temp[3];
if(dbf==NULL)
return 0;
if(dbf->update_tag==YES)
{
getdate(&(dbf->modify_date));
lseek(dbf->handle,1L,SEEK_SET);
tmp[0]=dbf->modify_date.da_year-1900;
tmp[1]=dbf->modify_date.da_mon;
tmp[2]=dbf->modify_date.da_day;
write(dbf->handle,tmp,3);
write(dbf->handle,&(dbf->rec_count),4);
}
free(dbf->curr_rec);
free(dbf->dbf_stru);
close(dbf->handle);
free(dbf);
return 1;
}
/*Database file pointer relative moving function */
long _Cdecl skip(DBF *dbf,int step)
{
if(dbf==NULL||dbf->rec_no==0)
return 0;
if(dbf->rec_no+step<1)
step=1-dbf->rec_no;
else if(dbf->rec_no+step>dbf->rec_count)
{
step=dbf->rec_count-dbf->rec_no;
dbf->eof=YES;
}
dbf->rec_no+=step;
if(step!=1)
lseek(dbf->handle,dbf->rec_len*(step-1),SEEK_CUR);
read(dbf->handle,dbf->curr_rec,dbf->rec_len);
return(dbf->rec_no);
}
/*get current record*/
long _Cdecl get_record(DBF *dbf,char *fmt,...)
{
va_list arglist;
int *ival;
long *lval;
char *sval;
double *fval;
GRAPH_TEXT *gval;
struct date *dval;
int no=1;
int tmp;
char *p;
char word[MAX_FIELD_LEN];
if(dbf==NULL||dbf->eof==YES)
return 0;
va_start(arglist,fmt);
for(;*fmt;fmt++)
{
tmp=0;
while(*fmt&&isdigit(*fmt))
{
tmp=tmp*10+*fmt-'0';
fmt++;
}
no=tmp==0?no:tmp;
if(*fmt=='i'||*fmt=='l'||*fmt=='f'||*fmt=='d'
||*fmt=='s'||*fmt=='c'||*fmt=='g'||*fmt=='m')
{
p=dbf->curr_rec+dbf->dbf_stru[no-1].field_pos;
strncpy(word,p,dbf->dbf_stru[no-1].field_len);
word[dbf->dbf_stru[no-1].field_len]=0;
switch(*fmt)
{
case 'i':
ival=va_arg(arglist,int*);
*ival=atoi(word);
break;
case 'l':
lval=va_arg(arglist,long*);
*lval=atol(word);
break;
case 'f':
fval=va_arg(arglist,double *);
*fval=atof(word);
break;
case 'd':/*note the 2000 year problem*/
dval=va_arg(arglist,struct date *);
dval->da_day=atoi(word+6);
word[6]=0;
dval->da_mon=atoi(word+4);
word[4]=0;
dval->da_year=atoi(word);
break;
case 's':
sval=va_arg(arglist,char *);
strcpy(sval,word);
break;
case 'c':
ival=va_arg(arglist,int *);
*ival=word[0];
break;
case 'g':
gval=va_arg(arglist,GRAPH_TEXT *);
memcpy(gval,word,10);
break;
case 'm':
lval=va_arg(arglist,long *);
*lval=atol(word);
break;
}
no++;
}
}
va_end(arglist);
return(dbf->rec_no);
}
/*write database file current record function*/
long _Cdecl put_record(DBF *dbf,char *fmt,...)
{
va_list arglist;
int ival;
long lval;
char *sval;
double fval;
struct date dval;
GRAPH_TEXT gval;
int no=1;
int i;
int tmp;
char *p;
char word[MAX_FIELD_LEN];
char format[20];
if(dbf==NULL||dbf->rec_no==0||dbf->eof==YES)
return 0;
va_start(arglist,fmt);
for(;*fmt;fmt++)
{
tmp=0;
while(*fmt&&isdigit(*fmt))
{
tmp=tmp*10+*fmt-'0';
fmt++;
}
no=tmp==0?no:tmp;
if(*fmt=='i'||*fmt='l'||*fmt=='f'||*fmt=='d'\
||*fmt=='s'||*fmt=='c'||*fmt=='m'||*fmt=='g')
{
strcpy(format,"%");
switch(dbf->dbf_stru[no-1].field_type)
{
case 'N':
itoa(dbf->dbf_stru[no-1].field_len,word,10);
strcat(format,word);
if(dbf->dbf_stru[no-1].field_dec==0)
strcat(format,"d");
else
{
itoa(dbf->dbf_stru[no-1].field_dec,word,10);
strcat(format,".");
strcat(format,word);
strcat(format,"f");
}
break;
case 'C':
strcat(format,"-");
itoa(dbf->dbf_stru[no-1].field_len,word,10);
strcat(format,word);
strcat(format,"s");
break;
case 'D':
strcat(format,"4d%02d%02d");
break;
case 'L':
format[0]='l';
break;
case 'M':
strcat(format,"010d");
}
switch(*fmt)
{
case 'i':
ival=va_arg(arglist,int);
if(dbf->dbf_stru[no-1].field_dec==0)
sprintf(word,format,ival);
else
{
fval=ival;
sprintf(word,format,fval);
}
break;
case 'l':
lval=va_arg(arglist,long);
if(dbf->dbf_stru[no-1].field_dec==0)
sprintf(word,format,lval);
else
{
fval=lval;
sprintf(word,format,fval);
}
break;
case 'f':
fval=va_arg(arglist,double);
if(dbf->dbf_stru[no-1].field_dec!=0)
sprintf(word,format,fval);
else
{
lval=fval;
sprintf(word,format,lval);
}
break;
case 'd':
dval=va_arg(arglist,struct date);
sprintf(word,format,dval.da_year,dval.da_mon,dval.da_day);
break;
case 's':
sval=va_arg(arglist,char *);
sprintf(word,format,sval);
break;
case 'c':
ival=va_arg(arglist,int);
word[0]=format[0]=='l'?((ival=='y'||ival=='Y'||\
ival=='t'||ival=='T')?'T':'F'):ival;
word[1]=0;
break;
case 'm':
lval=va_arg(arglist,long);
sprintf(word,format,lval);
break;
case 'g':
gval=va_arg(arglist,GRAPH_TEXT);
memcpy(word,&gval,10);
}
p=dbf->curr_rec+dbf->dbf_stru[no-1].field_pos;
for(i=0;i<dbf->dbf_stru[no-1].field_len;i++)
*p++=word[i];
no++;
}
}
va_end(arglist);
lseek(dbf->handle,-dbf->rec_len,SEEK_CUR);
write(dbf->handle,dbf->curr_rec,dbf->rec_len);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -