巨经典的DBF文件C++操作程序Word文档下载推荐.docx
《巨经典的DBF文件C++操作程序Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《巨经典的DBF文件C++操作程序Word文档下载推荐.docx(70页珍藏版)》请在冰点文库上搜索。
#include"
stdafx.h"
#ifdefDEBUG
#definedebugmAfxMessageBox
#definenewDEBUG_NEW
#endif
staticINT2ccc(CHAR*,INT2,INT2);
INT2ccc(CHAR*ptsr,INT2len,INT2bit)
{
CHARtemp1[30],temp11[30],i1;
CHARtemp2[30],temp22[30],i2;
INT2
tempi,i;
CHAR*ps,*ps1;
for(ps1=ptsr;
(*ps1=='
'
)&
&
(*ps1!
='
\x0'
);
ps1++);
ps=strchr(ps1,'
.'
if(ps==NULL){
//NOTHAVE'
strcpy(temp1,ps1);
temp2[0]='
;
}else{
*ps='
ps++;
strcpy(temp2,ps);
}
i1=len-(bit?
(bit+1):
0);
if((int)strlen(temp1)>
(int)i1){
strncpy(temp11,temp1,i1);
temp11[i1]='
tempi=i1-strlen(temp1);
for(i=0;
i<
tempi;
i++)temp11='
strcpy(temp11+tempi,temp1);
//------------------------------------------
if(bit>
0){
if((int)strlen(temp2)>
(int)bit){
strncpy(temp22,temp2,bit);
temp22[bit]='
i2=strlen(temp2);
tempi=bit-strlen(temp2);
strcpy(temp22,temp2);
i++)(temp22+i2)='
0'
strcpy(ptsr,temp11);
if(bit!
=0){
strcat(ptsr,"
."
strcat(ptsr,temp22);
return0;
}
INT2IDX_BLOCK:
skip()
if(curp>
=(INT2)items-1){
return-2;
curp++;
//double
find(CHAR*key)
INT2compi=keylen;
CHAR*p=buff+12;
INT2RetCode,i;
if((int)strlen(key)<
(int)keylen)
compi=strlen(key);
for(i=0,p=buff+12;
items;
i++,p+=keylen+4){
RetCode=strncmp(key,p,compi);
if(RetCode==0)
//Foundedthiskey
if(flag>
=2){
//.and.thisisaleaforleaf&
rootnode
curp=i;
//Founded,OKreturntomaster
}else{
//.and.thisisaframeorrootnode
return-10;
//ThisisaROOT,MaybeingavenBLOCK
if(RetCode<
0){
//ifthekey<
curentkeyinidxfile
if(flag<
2){
//andthisisFRAMEorROOTnode
//inFRAMEorROOTnodekeyisMAXKEYingivennode
//ThisisaROOT,MaybeingavenBLOCK
return-3;
//MaybeinNextBlock
IDX:
IDX()
Installed=0;
//fp=NULL;
handle=-1;
rootpos=blocks=0l;
key_len=0;
return;
~IDX()
if(this->
handle!
=-1){
close(handle);
//openfunctionopentheidxfile
INT2IDX:
pen(CHAR*filename)
CHARbuff[30];
//fp=_fsopen(filename,"
rb"
SH_DENYNONE);
//changebyliaojint1999.3.22
handle=sopen(filename,_O_RDONLY|_O_BINARY,_SH_DENYNO,_S_IREAD);
/*
handle=sopen(filename,O_RDWR|O_BINARY|O_DENYNONE,
SH_DENYNONE,S_IREAD|S_I_write);
*/
//if(fp==NULL)return-1;
if(handle==-1)return-1;
if(_lseek(handle,0,SEEK_SET)!
=0)return-1;
if(read(handle,buff,30)!
=30)return-1;
rootpos=*(UINT4*)buff;
blocks=*(UINT4*)(buff+
key_len=*(INT2*)(buff+12);
block.SetKeyLen(key_len);
block.ReadBlock(this->
handle,rootpos);
while(block.GetFlag()<
2){
handle,block.GetResult());
GoHome();
rev()
UINT4PrevBlock;
if(block.Prev()!
PrevBlock=block.GetPrev();
if(PrevBlock==0xffffffffl){
return-1;
handle,(INT4)PrevBlock);
block.End();
Next()
UINT4NextBlock;
if(block.Next()!
=0){
//Isalreadyinthelastnodeinthisblock
NextBlock=block.GetNext();
if(NextBlock==0xffffffffl){
handle,(INT4)NextBlock);
block.Home();
Find(CHAR*key)
INT2RetCode;
for(;
RetCode=block.find(key);
switch(RetCode){
case0:
return0;
case-1:
return-1;
case-2:
if(block.GetPrev()==(UINT4)0xffffffffl){
}else{
block.ReadBlock(this->
handle,block.GetPrev());
break;
case-3:
if(block.GetNext()==(UINT4)0xffffffffl){
}else{
handle,block.GetNext());
break;
case-10:
default:
//-------------------------------------------------
//FatalError:
returncodeisnotallow
//
inclassIDX_BLOCK:
find(CHAR*);
Skip()
returnNext();
INT2
GoHome()
while(block.GetPrev()!
=(UINT4)0xffffffffl){
};
GoEnd()
while(block.GetNext()!
UINT4IDX_BLOCK:
GetResult()
CHARtf[5];
CHAR*p=buff+12+curp*(keylen+4);
p+=keylen;
tf[3]=p[0];
tf[2]=p[1];
tf[1]=p[2];
tf[0]=p[3];
return*(UINT4*)tf;
//INT2IDX_BLOCK:
ReadBlock(FILE*fp,UINT4pos)
ReadBlock(INT2handle,UINT4pos)
//fseek(fp,pos,SEEK_SET);
_lseek(handle,pos,SEEK_SET);
//fread(buff,512l,1,fp);
read(handle,buff,512);
flag=buff[0];
count=buff[1];
items=buff[2];
prev=*(UINT4*)(buff+4);
next=*(UINT4*)(buff+
curp=0;
//------------------NextisDBF----------------------
DBF:
BF()
First=NULL;
pIDX=NULL;
buff=NULL;
//---------------------------------
SwapBuffer=NULL;
MaxRecNum=0;
CurRecNum=0;
//--------------------------------
current_recno=record_no=0l;
record_len=0;
Name[0]='
changeflag=0;
dbferrno=0;
fieldvalue=NULL;
~DBF()
if(First!
=NULL){
deleteFirst;
if(SwapBuffer!
=NULL)deleteSwapBuffer;
if(pIDX!
if(handle!
=-1){//this
:
close(handle);
if(buff!
deletebuff;
if(fieldvalue!
deletefieldvalue;
return;
INT2DBF:
Clear()
memset(buff,'
record_len);
return