📄 dbfop.cpp
字号:
if(buff!=NULL){
delete buff;
buff=NULL;
}
buff=(CHAR *) new CHAR[record_len+20];
if(buff==NULL){
this->Close();
m_errno=NotEnoughMemory;
return m_errno;
} // memset strset
Clear(); // add by liaoj 1996.12.24
if(record_no==0l){
current_recno=Empty;
}else {
current_recno=1;
dbf_buff();
}
pFIELD=new FIELD[fieldnum];
if(pFIELD==NULL){
this->Close();
m_errno=NotEnoughMemory;
return m_errno;
}
lseek(handle,32l,SEEK_SET);
tpf=&(pFIELD[0]);
FieldOffset=1;
for(i=0;i<fieldnum;i++,tpf++){
// fread(tempbuf,32l,1l,fp); // memset
read(handle,tempbuf,32);
memcpy(tpf,tempbuf,18);
j=(INT2)strlen(tpf->name);
// ----------- Cliepper or Foxbase -----
if (DBFType==Foxbase)
j=(INT2)(j>11?11:j);
else
j=(INT2)7?7:j;
tpf->name[j]=0;
for(j--;j>=0;j--){
if(tpf->name[j]==' ')
tpf->name[j]=0;
else
break;
}
if(DBFType==Clipper){
tpf->address=FieldOffset;
FieldOffset+=tpf->len;
}
else{
tpf->address=FieldOffset;
FieldOffset+=tpf->len;
}
}
int posi = 1;
for(int i=0;i<fieldnum;i++){
pFIELD[i].address = posi;
posi += pFIELD[i].len;
}
Installed=1;
changeflag=0;
errno=DBFOK;
return 0;
}
INT2 DBF::SetSwp(INT2 buffer_number)
{
if (SwapBuffer!=NULL) delete SwapBuffer;
CurRecNum=0;
MaxRecNum=0;
SwapBuffer=(CHAR *) new CHAR[(record_len+1)*buffer_number];
if (SwapBuffer==NULL){
m_errno=NotEnoughMemory;
return m_errno;
}
CurRecNum=0;
MaxRecNum=buffer_number;
return DBFOK;
}
INT2 DBF::AppendToSwp()
{
if (MaxRecNum==0) return this->Append();
if (CurRecNum>=MaxRecNum) FlushSwp();
if (CurRecNum>=MaxRecNum) {
m_errno=OtherError;
return OtherError;
}
memcpy(SwapBuffer+CurRecNum*record_len,buff,(size_t)record_len);
CurRecNum++;
return 0;
}
INT2 DBF::FlushSwp()
{
CHAR tempbuf[100];
UINT4 temp_recno;
// INT2 i;
INT4 offset;
if(CurRecNum==0) return 0;
if(!Installed){ // Not open this file
errno=ClassNotInit;
return m_errno;
}
if(lseek(handle,0l,SEEK_SET)!=0){
errno=SeekFileError;
return m_errno;
}
if(read(handle,tempbuf,8)!=8){
// this->close();
errno=ReadFileError;
return m_errno;
}
(*(UINT4*)(tempbuf+4))+=(UINT4)CurRecNum;//recordnum+1;
temp_recno=(*(UINT4*)(tempbuf+4));
if(lseek(handle,0l,SEEK_SET)!=0){
errno=SeekFileError;
return m_errno;
}
if(write(handle,tempbuf,8)!=8){
errno=SeekFileError;
return m_errno;
}
offset=(INT4)head_len+(temp_recno-(UINT4)CurRecNum)*record_len;
if(lseek(handle,offset,SEEK_SET)!=offset){
errno=SeekFileError;
return m_errno;
}
write(handle,SwapBuffer,(UINT2)(record_len*CurRecNum));
write(handle,"\x1a",1);
this->ReOpen();
this->GoTo(temp_recno);
CurRecNum=0;
return DBFOK;
}
INT2 DBF::Close()
{
if(handle!=-1){
::close(handle);
handle=-1;
}
if(pIDX!=NULL){
delete pIDX;
pIDX = NULL;
}
if(buff!=NULL){
delete buff;
buff=NULL;
}
if(SwapBuffer!=NULL) delete SwapBuffer;
SwapBuffer=NULL;
MaxRecNum=0;
CurRecNum=0;
if(pFIELD!=NULL){
delete pFIELD;
pFIELD=NULL;
}
if(fieldvalue!=NULL){
delete fieldvalue;
fieldvalue=NULL;
}
lock_flag=0;
Installed=0;
errno=DBFOK;
return DBFOK;
}
INT2 DBF::Prev()
{
INT2 RetCode;
if(pIDX==NULL){
// skip Not use IDX
if(current_recno==-2){ // in the DBFEOF
current_recno=record_no;
errno=dbf_buff();
return m_errno;
}
if(current_recno>1){ // in the MIDDLE
current_recno--;
errno=dbf_buff();
return m_errno;
}
if(current_recno==1){ // in the FirstRecord
current_recno=DBFBOF;
errno=DBFOK;
return m_errno;
}
if((current_recno==DBFEOF)||(current_recno==Empty)){ // in the DBFEOF;
errno=RecordOutOfRange;
return m_errno;
}
errno=OtherError;
return m_errno;
}
if(current_recno==-2){ // in the DBFBOF
pIDX->GoEnd();
current_recno=pIDX->GetRecordNo();
if(current_recno>record_no||current_recno<1){
// Fatal Error :: the IDX file is old
m_errno=IndexIsOutOfDate;
return m_errno;
}
if(current_recno==0){
current_recno=Empty;
errno=RecordNotFound;
return m_errno;
}else{
dbf_buff();
}
errno=DBFOK;
return m_errno;
}
if((current_recno==DBFBOF)||(current_recno==Empty)){ // in the DBFEOF;
errno=RecordNotFound;
return m_errno;
}
RetCode=pIDX->Prev();
if(RetCode<0){
current_recno=-1;
errno=RecordNotFound;
return RecordNotFound;
}
current_recno=pIDX->GetRecordNo();
if(current_recno>record_no||current_recno<1){
// Fatal Error :: the IDX file is old +++++++++++++++++
m_errno=IndexIsOutOfDate;
return m_errno;
}
if(current_recno==0){
current_recno=-3;
}else{
dbf_buff();
}
errno=0;
return 0;
}
INT2 DBF::Next()
{
return Skip();
}
INT2 DBF::Skip()
{
INT2 RetCode;
if(pIDX==NULL){
// skip Not use IDX
if(current_recno==DBFBOF){ // in the BOF
current_recno=1;
errno=dbf_buff();
return m_errno;
}
if(current_recno<record_no){ // in the MIDDLE
current_recno++;
errno=dbf_buff();
return m_errno;
}
if(current_recno==record_no){ // in the LastRecord
current_recno=DBFEOF;
errno=DBFOK;
return m_errno;
}
if((current_recno==DBFEOF)||(current_recno==Empty)){ // in the DBFEOF;
m_errno=RecordOutOfRange;
return m_errno;
}
errno=OtherError;
return m_errno;
}else {
if(current_recno==DBFBOF){ // in the BOF
pIDX->GoHome();
current_recno=pIDX->GetRecordNo();
if(current_recno>record_no||current_recno<1){
// Fatal Error :: the IDX file is old
}
errno=dbf_buff();
return m_errno;
}
if((current_recno==DBFEOF)||(current_recno==Empty)){ // in the DBFEOF;
m_errno=RecordOutOfRange;
return RecordOutOfRange;
}
RetCode=pIDX->Next();
if(RetCode<0){
current_recno=DBFEOF;
m_errno=RecordOutOfRange;
return RecordOutOfRange;
}
current_recno=pIDX->GetRecordNo();
if(current_recno==0L){
current_recno=DBFEOF; // set bof()
}
if(current_recno>record_no||current_recno<1){
// Fatal Error :: the IDX file is old
}
if(current_recno==0L){
current_recno=DBFEOF; // set EOF()
errno=DBFEOF;
}else {
errno=dbf_buff();
}
return m_errno;
}
}
INT2 DBF::GoTo(INT4 recordno)
{
if((recordno<=record_no)&&(recordno>0)){
current_recno=(UINT4)recordno;
dbf_buff();
errno=DBFOK;
}else {
errno=RecordOutOfRange;
}
return m_errno;
}
INT2 DBF::IsBOF()
{
if(current_recno==Empty||current_recno==DBFBOF) return 1;
else return 0;
}
INT2 DBF::IsEOF()
{
if(current_recno==Empty||current_recno==DBFEOF) return 1;
else return 0;
}
INT2 DBF::GoTop()
{
if(pIDX==NULL){
if(record_no!=0){
current_recno=1;
errno=dbf_buff();
return m_errno;
}else{
m_errno=DBFOK;
return m_errno;
}
}
pIDX->GoHome();
current_recno=pIDX->GetRecordNo();
if(current_recno>0){
m_errno=dbf_buff();
}else{
current_recno=Empty; // bof() .and. eof()
m_errno=DBFOK;
}
return m_errno;
}
INT2 DBF::GoBottom()
{
return 0;
}
// 0 : ok
// -1: not found()
INT2 DBF::Seek(CHAR * keyword)
{
INT2 RetCode;
if(pIDX==NULL){
errno=IndexNotInit;
return m_errno;
}
RetCode=pIDX->Find(keyword);
if(RetCode!=0){
current_recno=DBFEOF; // set to eof
errno=RecordOutOfRange;
return m_errno;
}
current_recno=pIDX->GetRecordNo();
if(current_recno>record_no||current_recno<1){
// Fatal Error IDX is not match to DBF file
}
errno=dbf_buff();
return m_errno;
}
INT2 DBF::fv(CHAR * fieldname_par,CHAR * fieldvalue_par,INT2 BufferLen)
{
INT2 RetCode;
if((RetCode=FindField(fieldname_par))<0){
fieldvalue_par[0]='\x0';
errno=FieldNotFound;
return m_errno;
}
if(RetCode>=fieldnum){
fieldvalue_par[0]='\x0';
errno=FieldNotFound;
return m_errno;
}
return fv(RetCode,fieldvalue_par,BufferLen);
}
// -1: is eof() bof() in dbase.
INT2 DBF::fv(INT2 fieldno_par,CHAR * fieldvalue_par,INT2 BufferLen)
{
struct FIELD * tpf;
tpf=&(pFIELD[fieldno_par]);
if(fieldno_par>=fieldnum){
fieldvalue_par[0]='\x0';
errno=FieldNotFound;
return FieldNotFound;
}
if((current_recno>record_no)||(current_recno<0)){
fieldvalue_par[0]='\x0';
errno=RecordOutOfRange;
return m_errno;
}
if(tpf->len<BufferLen||BufferLen==0){
strncpy(fieldvalue_par,(CHAR *)(buff+tpf->address), tpf->len);
fieldvalue_par[tpf->len]='\x0';
}else{
strncpy(fieldvalue_par,(CHAR *)(buff+tpf->address),
BufferLen-1);
fieldvalue_par[BufferLen-1]='\x0';
}
/*
switch(tpf->f_type){
case 'C': m_errno=1; return 1;
case 'N': m_errno=2; return 2;
case 'L': m_errno=3; return 3;
case 'M': m_errno=4; return 4;
case 'D': m_errno=5; return 5;
}
*/
errno=DBFOK;
return DBFOK;
}
INT2 DBF::dowith(INT2 fn,CHAR * tempb)
{
CHAR buffer[80];
struct FIELD * tpf;
tpf=&(pFIELD[fn]);
INT2 i;
i=(INT2)strlen(tempb);
//switch(pFIELD[fn].f_type){
switch(tpf->f_type){
case 'M':
case 'C':
case 'D':
// for(;i<pFIELD[fn].len;i++)
for(;i<tpf->len;i++)
tempb[i]=' ';
//tempb[pFIELD[fn].len]='\x0';
tempb[tpf->len]='\x0';
break;
case 'N':
::ccc(tempb,(INT2)tpf->len,(INT2)tpf->bits);
break;
case 'L':
if(tempb[0]=='1'||tempb[0]=='t'||fieldvalue[0]=='T')
buffer[0]='T';
else if(tempb[0]=='0'||tempb[0]=='f'||tempb[0]=='F')
buffer[0]='F';
else
buffer[0]=tempb[0];
tempb[0]=buffer[0];
tempb[1]='\x0';
break;
default:
errno=FieldTypeError;
return FieldTypeError;
}
errno=DBFOK;
return DBFOK;
}
INT2 DBF::Loca(CHAR * fieldname,CHAR * fieldvalue_par)
{
CHAR tempbuff[100];
loca_flag=0;
if(fieldvalue!=NULL)
delete fieldvalue;
fieldvalue=new CHAR[strlen(fieldvalue_par)+10];
if(fieldvalue==NULL) {
// printf("Not enough memory!");
m_errno=NotEnoughMemory;
return m_errno;
}
strcpy(fieldvalue,fieldvalue_par);
if((fieldno=FindField(fieldname))<0){
errno=FieldNotFound;
return m_errno;
}
loca_flag=1;
if(pIDX==NULL){
current_recno=1;
errno=dbf_buff();
}else{
pIDX->GoHome();
current_recno=pIDX->GetRecordNo();
errno=dbf_buff();
}
if(errno!=DBFOK) return m_errno;
do {
// check current record weather need.
fv(fieldno,tempbuff);
dowith(fieldno,tempbuff);
if(strcmp(tempbuff,fieldvalue)==0){
errno=DBFOK;
return DBFOK;// found the just record
}
}while((Skip()==0)&&(!this->IsEOF()));
if(this->IsEOF()){
errno=NotFound;
return m_errno;
}else{
errno=DBFOK;
return m_errno;
}
}
INT2 DBF::Cont()
{
CHAR tempbuff[100];
if(!loca_flag){
errno=OtherError;
return m_errno;
}
if(this->IsEOF()){
errno=NotFound;
return m_errno;
}
while(Skip()==0){
if(this->IsEOF()){
errno=NotFound;
return m_errno;
}
fv(fieldno,tempbuff);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -