dbf

说明:应该像下面的模式来创建数据库

CString strFileName;

 oDBF.Add("test1",'C',10,0);

 oDBF.Create((char*)strFileName.GetBuffer(strFileName.GetLength()));

 

用下面的代码来向数据库中加入数据

 DBF dbf;

 dbf.Open(pFileName);

 int i = 0;
 int j = 0;

 char cTem[20];

 for (i=0;i<3;i++)
 {  
  dbf.GoTo(i);
  for (j=0; j<22; j++)
  {
   sprintf(cTem,"%d,%d",i,j);
   dbf.Replace(j,cTem);
  }
  dbf.Append();
 }

 dbf.Close();

 

DBFOP.H  文件

 

 

#ifndef ___DBFOP_HPP___
#define ___DBFOP_HPP___
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/locking.h>
#include <share.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>
// define type value for the class  add by liaoj 1999.03.19

#ifndef BOOL
#define BOOL int
#define TRUE 1
#define FALSE 0
#endif

typedef char   INT8;        
typedef unsigned char UINT8;
typedef short   INT16;
typedef unsigned short UINT16;
typedef int    INT32;
typedef unsigned int UINT32;
typedef double   DOUBLE;
typedef float   FLOAT;


//字段信息
struct FIELD
{
 INT8 name[11];
 INT8 f_type;
 INT32 address;
 UINT8 len;
 UINT8 bits;
};

//---------------------------------------------------------------------
//  This is interface class. User use this module by use class DBF.
//  this class is like <<area>> in MFOXPLUS,FOX,DBASE III.
//  use can use this class do follow work.
// . open a database file and it's index file.(only allow .IDX);
// . close a database file and it's index file if nessary.
// . read a field value in this database.
// . change a field value in this database.(if have .IDX,not change
//  the key field).
// . seek a record by use IDX file.
// . append blank record (Must use IDX);
// . lock a record or a file,unlock it.(not allow multlock,
//   if try it,class will unlock the area which locked before.)
//---------------------------------------------------------------------
class DBF
{
public:
 enum DBF_CONST
 {
  FieldNotFound=-6,
            RecordNotFound=-5,
   
            Empty  =-3,
            DBFEOF =-2,
            DBFBOF =-1,
   
            DBFOK    =0x0000,
   
            AutoDetect =0x4001,
            Clipper   ,
            Foxbase   ,
            //
            ReadOnly  =0x4100,
            ReadWrite ,
   
            FileLock  =0x4200,
            RecordLock,
   
            FlockFailure=0x5001,
            RlockFailure,
            ClassNotInit,
            FileOpenFailure,
            ReadFileError,
            FileNotOpen,
            SeekFileError,
            WriteFileError,
            ChsizeFileError,
            OtherError,
            RecordOutOfRange,
            NotFound,
            IndexNotInit,
            FieldTypeError,
            // index error
            IndexIsOutOfDate=0x6000,
            // fatal error
            NotEnoughMemory=0x7000,
            NotDBFFile,
            OtherFatalError
 };
protected:
 INT16  fInstalled;
 INT16  OpenMode;
 // save current dbf open mode
 // valid value:
 // DBF::ReadOnly,ReadWrite
 INT8 * buff;
 // changeflag is used for buff change;
 // it set by replace() function;
 // it clear by dbf_buff;
 // it used by dbf_wbuff,if no change don't write to file
 INT16  changeflag;
 FIELD *pFIELD;
 INT32 current_recno;    // -1 bof -2 eof -3 bof.and.eof
 struct FIELD * First;
 INT8 Name[100];
 // FILE * fp;
 INT32 handle;
 INT32  record_no;
 UINT16 head_len;
 UINT16 record_len;
 INT16  dbf_buff();
 INT16  dbf_wbuff();
 INT16  fieldnum;
 // ----------------------------------
 INT8 * SwapBuffer;
 INT16  MaxRecNum;
 INT16  CurRecNum;
 // ------------------------------------------------------------
 // next three var is use for flock(),rlock(),unlock() function
 // these used for next condition:
 // when a lock function is going to invoke,
 // the  part locked in before must unlock first.
 // this work will be done by class automatic
 // ------------------------------------------------------------
 INT16 lock_flag;   // 0. No lock 1.lockfile 2.lockrecorder
 INT32 lock_start;  // when lock_flag=0,
 INT32 lock_len;
 // ------------------------------------------------------------
 // these var define next is used for
 // loca(),cont() like the locate,continue.command in MFOXPLUS
 // ------------------------------------------------------------
 INT16  loca_flag;    // 0. not call loca 1.already call loca
 INT16  fieldno;      // last loca fieldno
 INT8 * fieldvalue;
 INT16 dowith(INT16 fn,INT8 *tempb);
 INT16 unlock_temp();
 INT16 DBFType; //0:fox serial, 1:clipper
 public:
  INT16  dbferrno;
  INT8  dbferrmsg[100];
  // ----------------------------------------
  INT16  GetFieldNum();
  INT8  * GetFieldName(INT16 fn);
  INT16  GetFieldLen(INT16 fn);
  INT16  SetSwp(INT16 buffer_number);
  INT16  AppendToSwp();
  INT16  FlushSwp();
  // ----------------------------------------
  INT16  GetErrno()
  {
   return dbferrno;
  }
  INT16  FindField(INT8 * filedname); // -1: not find >=0:return fieldno
  INT16  ff(INT8 * fieldname)
  {
   return FindField(fieldname);
  }
  INT16  Clear();
  INT16  Append();
  INT16  AppendBlank(); // -1
  DBF();
  ~DBF();
  // operator void *()
  // {
  //  return (void *)fInstalled;
  // }
  // INT16  operator !()     
  // {
  //  return fInstalled;
  // }
  INT16  Open(const INT8 * filename, INT16 ReadWriteMode=ReadWrite,INT16 DBFType_Par=Foxbase);
  
  INT32  GetRecNum()      
  {
   return record_no;
  }
  INT32  GetRecNo()
  {
   return current_recno;
  }
  INT16  Close(int Delete=0);
        // in function fv,and FieldValue
        // ValueBuffer=0 indecate infinite len of ValueBuffer),
  INT16  fv(INT8 * ,INT8 *,INT16 ValueBufferLen=0);
  INT16  FieldValue(INT8 * FieldName,INT8 * ValueBuffer,INT16 ValueBufferLen=0);
  INT16  fv(INT16,INT8 *,INT16 ValueBufferLen=0);     //
  INT16  FieldValue(INT16 FieldIndex,INT8 * ValueBuffer,INT16 ValueBufferLen=0);
  //--------------------------------------------
  INT16 Next();
  INT16 Prev();
  INT16 Skip();
  INT16 GoTop();
  INT16 GoBottom();
  
  INT16 Zap();
  INT16 IsDeleted();
  INT16 Delete();
  INT16 UnDelete();
  
  //--------------------------------------------
  INT16 GoTo(INT32 recordno);  // don't use when idx is avalid
  INT16 IsBOF();
  INT16 IsEOF();
        // loca and cont : return DBFOK found other not found
  INT16 Loca(INT8 * fieldname,INT8 * fieldvalue);
  INT16 Cont();
  INT16 Replace(INT8 * fieldname,INT8 * fieldvalue);
  INT16 Replace(INT16 fieldno,INT8 * fieldvalue);
  INT16 ReOpen();     // only get the record_no read from file
  INT16 Flush();      // write current buff to file
  INT16 ReRead();     // read the newest of current record to buff
  // add fuctions as follow make program easier
  INT16 FieldValue(INT16,INT16&);
  INT16 Replace(INT16,INT16);
  INT16 FieldValue(INT16,UINT16&);
  INT16 Replace(INT16,UINT16);
  INT16 FieldValue(INT16,INT32&);
  INT16 Replace(INT16,INT32);
  INT16 FieldValue(INT16,UINT32&);
  INT16 Replace(INT16,UINT32);
  INT16 FieldValue(INT16,DOUBLE&);
  INT16 Replace(INT16,DOUBLE);
  INT16 FieldValue(INT16,INT8&);
  INT16 Replace(INT16,INT8);
  
  INT16 FieldValue(INT8*,INT16&);
  INT16 Replace(INT8*,INT16);
  INT16 FieldValue(INT8*,UINT16&);
  INT16 Replace(INT8*,UINT16);
  INT16 FieldValue(INT8*,INT32&);
  INT16 Replace(INT8*,INT32);
  INT16 FieldValue(INT8*,UINT32&);
  INT16 Replace(INT8*,UINT32);
  INT16 FieldValue(INT8*,DOUBLE&);
  INT16 Replace(INT8*,DOUBLE);
  INT16 FieldValue(INT8*,INT8&);
  INT16 Replace(INT8*,INT8);

  //
  INT16 SortByField(UINT8 Fld);
};


#define MAXFIELDNUM 64
//DBF_HEAD 结构为 32 字节
//struct DBF_HEAD
class DBF_HEAD
{
public:
 UINT8 id;
 UINT8 year;
 UINT8 month;
 UINT8 day;
 UINT32 recorder_num;
 //unsigned int  head_len;
 //unsigned int  record_len;
 UINT16  head_len;
 UINT16  record_len;
 UINT8 reserved[20];
 DBF_HEAD();
};

//32个字节(字段信息)
//struct DBF_FIELD
class DBF_FIELD
{
public:
 char name[11];
 INT8 f_type;
 INT32 address;
 UINT8 len;
 UINT8 bits;
 UINT8 reserved_data[14];
 DBF_FIELD();
 ~DBF_FIELD();
};

//创建DBF文件类
class CDBF
{
private:
 DBF_HEAD head;
 DBF_FIELD *pFIELD[MAXFIELDNUM];
 UINT8 FieldNum;
 INT32 c_pos;
 
public:
 CDBF();
 ~CDBF();
 INT16 Add(const char* FieldName,char FieldType,UINT8 len,UINT8 bits);
 INT16 Create(const char * FileName);
 void ReSet();
};

#endif

 

 

DBFOP.CPP 文件

 

 

#include "stdafx.h"
#ifdef DEBUG
#define debugm AfxMessageBox
#define new DEBUG_NEW
#endif
#include"DBFOP.h"

//---------------------------------------------
//INT8:格式化的字符串
//len: 总长度
//bit: 小数长度
//-------------------------------------------------
static INT16 ccc(char *, INT16, INT16);
INT16 ccc(char * ptsr,INT16 len, INT16 bit)
{
 char temp1[30],temp11[30];
 char temp2[30],temp22[30];
 INT8 i1, i2;
 INT16 tempi,i;
 INT8 *ps,*ps1;
 
 for(ps1=ptsr;(*ps1==' ')&&(*ps1!='/x0');ps1++);  //移出前面的空格符号
 ps=strchr(ps1,'.');
 if(ps==NULL)  // NOT HAVE '.'
 { 
  strcpy(temp1,ps1);
  temp2[0]='/x0';
 }
 else
 {
  *ps='/x0';
  strcpy(temp1,ps1);
  ps++;
  strcpy(temp2,ps);
 }
 
 if( bit!=0 )
 {
  i1 = (INT8)(len-bit-1);
 }
 else
 {
  i1 = (INT8)len;  //整数长度
 }
 if((int)strlen(temp1)>(int)i1)  //调整整数长度
 {
  strncpy(temp11,temp1,i1);
  temp11[i1]='/x0';
 }
 else
 {
  tempi =(INT16)(i1-strlen(temp1));
  for(i=0;i<tempi;i++)
  {
   temp11[i]=' ';  //前面添空格
  }
  strcpy(temp11+tempi,temp1);
 }

 // ------------------------------------------
 if(bit>0)  //调整小数长度
 {
  if((int)strlen(temp2)>(int)bit)
  {
   strncpy(temp22,temp2,bit);
   temp22[bit]='/x0';
  }
  else
  {
   i2 = (INT8)strlen(temp2);
   tempi=(INT16)(bit-strlen(temp2));
   strcpy(temp22,temp2);
   for(i=0;i<tempi;i++) (temp22+i2)[i]='0';
   temp22[bit]='/x0';
  }
 }
 strcpy(ptsr,temp11);
 if(bit!=0)
 {
  strcat(ptsr,".");
  strcat(ptsr,temp22);
 }
 return 0;
}


//------------------Next is DBF ----------------------
DBF::DBF()
{
 assert( sizeof(INT8)==1 );
 assert( sizeof(UINT8)==1 );
 assert( sizeof(INT16)==2 );
 assert( sizeof(UINT16)==2 );
 assert( sizeof(INT32)==4 );
 assert( sizeof(UINT32)==4 );
 fInstalled=0;
 First=NULL;
 handle=-1;
 buff = NULL;
 // ---------------------------------
 SwapBuffer=NULL;
 MaxRecNum=0;
 CurRecNum=0;
 // --------------------------------
 current_recno=record_no=0l;
 record_len=0;
 Name[0]='/x0';
 changeflag=0;
 dbferrno=0;
 fieldvalue=NULL;
}

DBF::~DBF()
{
 if(First!=NULL)
 {
  delete First;
  First=NULL;
 }
 if(SwapBuffer!=NULL)
 {
  delete SwapBuffer;
 }
 SwapBuffer=NULL;
 MaxRecNum=0;
 CurRecNum=0;
 
 if(handle!=-1)
 {
  ::close(handle); //文件关闭函数
  handle=-1;
 }
 if(buff!=NULL)
 {
  delete buff;
  buff=NULL;
 }
 if( fieldvalue!=NULL )
 {
  delete fieldvalue;
  fieldvalue=NULL;
 }
}

//记录清空函数
INT16 DBF::Clear()
{
 memset(buff,' ',record_len);
 return 0;
}

//增加空白记录
INT16 DBF::AppendBlank()
{
 INT8 tempbuf[100];
 UINT32  temp_recno;
 INT16 i;
 INT32 offset;
 //-
 if(!fInstalled)
 {  // Not open this file
  dbferrno=ClassNotInit;
  strcpy(dbferrmsg,"ClassNotInit!");
  return ClassNotInit;
 }
 
 if(_lseek(handle,0l,SEEK_SET)!=0)
 {
  dbferrno=SeekFileError;
  strcpy(dbferrmsg, "SeekFileError!");
  return dbferrno;
 }
 if(read(handle,tempbuf,8)!=8)
 {
  dbferrno=ReadFileError;
  strcpy(dbferrmsg,"ReadFileError!");
  return dbferrno;
 }
 temp_recno=++(*(UINT32*)(tempbuf+4));   //记录个数(并增加1)
 if(_lseek(handle,0l,SEEK_SET)!=0)
 {
  dbferrno=SeekFileError;
  strcpy(dbferrmsg,"SeekFileError!");
  return dbferrno;
 }
 if(_write(handle,tempbuf,8)!=8)
 {
  dbferrno=WriteFileError;
  strcpy(dbferrmsg,"WriteFileError!");
  return dbferrno;
 }
 offset=(INT32)head_len+(temp_recno-1l)*record_len;
 _lseek(handle,offset,SEEK_SET);
 for(i=0;i<record_len;i++)  // fputc(0x20,fp); // FILL BLANK
 {
  _write(handle," ",1);
 }
 _write(handle,"/x1a",1);   //文件结束标记
 ReOpen();
 GoTo(temp_recno);
 return 0;
}

INT16 DBF::Append()
{
 INT8 tempbuf[100];
 UINT32  temp_recno;
 INT32 offset;
 if(!fInstalled)
 {  // Not open this file
  dbferrno=ClassNotInit;
  strcpy(dbferrmsg,"ClassNotInit!");
  return ClassNotInit;
 }
 if(_lseek(handle,0l,SEEK_SET)!=0)
 {
  dbferrno=SeekFileError;
  strcpy(dbferrmsg,"SeekFileError!");
  return SeekFileError;
 }
 if(read(handle,tempbuf,8)!=8)
 {
  dbferrno=ReadFileError;
  strcpy(dbferrmsg,"ReadFileError!");
  return dbferrno;
 }
 temp_recno=++(*(UINT32*)(tempbuf+4));// recordnum+1;
 if(_lseek(handle, 0, SEEK_SET)!=0)
 {
  dbferrno=SeekFileError;
  strcpy(dbferrmsg,"SeekFileError!");
  return dbferrno;
 }
 if(_write(handle,tempbuf,8)!=8)
 {
  dbferrno=WriteFileError;
  strcpy(dbferrmsg,"WriteFileError!");
  return dbferrno;
 }
 offset=(INT32)head_len+(temp_recno-1)*record_len;
 if(_lseek(handle,offset,SEEK_SET)!=offset)
 {
  dbferrno=SeekFileError;
  strcpy(dbferrmsg,"SeekFileError!");
  return dbferrno;
 }
 _write(handle,buff,(UINT16)record_len);
 _write(handle,"/x1a",1);
 ReOpen();
 GoTo(temp_recno);
 return 0;
}

//把缓冲区输入写入数据库
INT16 DBF::dbf_wbuff()
{
 INT32 offset;
 
 offset = (INT32)head_len+(current_recno-1)*record_len;
 if(current_recno<=0)
 {
  dbferrno=RecordOutOfRange;
  strcpy(dbferrmsg,"RecordOutOfRange!");
  return dbferrno;
 }
 if( changeflag==0 )
 {
  dbferrno=DBFOK;
  strcpy(dbferrmsg,"DBFOK!");
  return DBFOK;
 }
 if( _lseek(handle, offset, SEEK_SET)!=offset )
 {
  dbferrno=SeekFileError;
  strcpy(dbferrmsg,"SeekFileError!");
  return dbferrno;
 }
 if( _write(handle, buff, (UINT32)record_len)!=(INT32)record_len )
 {
  dbferrno=WriteFileError;
  strcpy(dbferrmsg,"WriteFileError!");
  return dbferrno;
 }
 dbferrno=DBFOK;
 strcpy(dbferrmsg,"DBFOK!");
 return DBFOK;
}

//把当前记录读入缓冲区
INT16 DBF::dbf_buff()
{
 INT32 offset=(INT32)head_len+(current_recno-1)*record_len;
 
 if(current_recno<=0)
 {
  dbferrno=RecordOutOfRange;
  strcpy(dbferrmsg,"RecordOutOfRange!");
  return dbferrno;
 }
 if(_lseek(handle,offset,SEEK_SET)!=offset)
 {
  dbferrno=SeekFileError;
  strcpy(dbferrmsg,"SeekFileError!");
  return dbferrno;
 }
 if(::eof(handle))
 {
  dbferrno=SeekFileError;
  strcpy(dbferrmsg,"SeekFileError!");
  return dbferrno;
 }
 if(read(handle,buff,(UINT32)record_len)!=(INT32)record_len)
 {
  dbferrno=ReadFileError;
  strcpy(dbferrmsg,"ReadFileError!");
  return dbferrno;
 }
 changeflag=0;
 dbferrno=0;
 strcpy(dbferrmsg,"DBFOK!");
 return 0;
}

//清空数据表
INT16 DBF::Zap()
{
 INT8 tempbuf[100];
 if(!fInstalled)
 {  // Not open this file
  dbferrno=ClassNotInit;
  strcpy(dbferrmsg,"ClassNotInit!");
  return dbferrno;
 }
 _lseek(handle, 0, SEEK_SET);
 //if(fread(tempbuf,32l,1l,fp)!=1){
 if( read(handle, tempbuf, 32)!=32 )
 {
  Close();
  dbferrno=NotDBFFile;
  strcpy(dbferrmsg,"NotDBFFile!");
  return dbferrno;
 }
 *(UINT32 *)(tempbuf+4)=0; // record_no //记录个数清0
 if( chsize(handle, head_len)!=0 )      //重值文件大小
 {
  // ------------------------
  dbferrno=ChsizeFileError;
  strcpy(dbferrmsg,"ChsizeFileError!");
  return dbferrno;
 }
 _lseek(handle, 0, SEEK_SET);
 if(_write(handle, tempbuf, 32)!=32 )
 {
  //close();
  dbferrno=WriteFileError;
  strcpy(dbferrmsg,"WriteFileError!");
  return WriteFileError;
 }
 _lseek(handle,0l,SEEK_END);
 _write(handle,"/x1a/x0",2);
 MaxRecNum=0;
 CurRecNum=0;
 lock_flag=0;
 ReOpen();
    dbferrno=DBFOK;
 strcpy(dbferrmsg,"DBFOK!");
 return dbferrno;
}

INT16 DBF::Open(const INT8 * strFileName,INT16 OpenMode_Par,INT16 DBFType_Par)
{
 INT8 tempbuf[100];
 INT16  i,j,FieldOffset;
 struct FIELD * tpf;
 
 if(fInstalled)
 {
  Close();
 }
 strcpy(Name,strFileName);
 OpenMode = OpenMode_Par;  //打开模式
 DBFType=DBFType_Par;     //dbf 数据类型
 if(OpenMode==ReadWrite)
 {
  handle=_sopen(Name,_O_RDWR|_O_BINARY,_SH_DENYNO,_S_IREAD|_S_IWRITE);
 }
 else
 {
  handle=_sopen(Name,_O_RDONLY|_O_BINARY,_SH_DENYNO,_S_IREAD);
 }
 
 if(handle==-1)
 {
  dbferrno=FileOpenFailure;
  strcpy(dbferrmsg,"FileOpenFailure!");
  return dbferrno;
 } 
 _lseek(handle, 0, SEEK_SET); 
 if( read(handle,tempbuf,32)!=32 )
 {
  Close();
  dbferrno=ReadFileError;
  strcpy(dbferrmsg,"ReadFileError!");
  return dbferrno;
 }
 record_no=*(UINT32 *)(tempbuf+4);   //记录个数
 head_len=*(UINT16 *)(tempbuf+8);    //文件头长度
 record_len=*(UINT16*)(tempbuf+10);  //记录长度
 fieldnum=(INT16)((head_len)/32-1);     //字段个数
 // need automatic detect the dbf file type is foxbase or clipper
 if(DBFType==AutoDetect)
 {
  if( (head_len-1)%32==0 )
  {
   DBFType=Foxbase;
  }
  else
  {
   DBFType=Clipper;
  }
 }
 if( buff!=NULL )
 {
  delete buff;
  buff=NULL;
 }
 
 buff=(INT8 *) new INT8[record_len+20];
 if(buff==NULL)
 {
#ifdef DEBUG
  debugm("DBF::Open alloc failure!");
#endif
  Close();
  dbferrno=NotEnoughMemory;
  strcpy(dbferrmsg,"NotEnoughMemory!");
  return dbferrno;
 }             // memset strset
 Clear();  // add by liaoj 1996.12.24
 if(record_no==0)
 {
  current_recno=Empty;
 }
 else
 {
  current_recno=1;
  dbf_buff();
 }
 pFIELD=new FIELD[fieldnum];
 if(pFIELD==NULL)
 {
#ifdef DEBUG
  debugm("DBF::open alloc failure!");
#endif
  
  Close();
  dbferrno=NotEnoughMemory;
  strcpy(dbferrmsg,"NotEnoughMemory!");
  return dbferrno;
 }
 _lseek(handle, 32, 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=(INT16)strlen(tpf->name);
        // ----------- Cliepper or Foxbase -----
  if( DBFType==Foxbase )
  {   
            //j=j>11?11:j;
   if( j>11 )
   {
    j = 11;
   }
  }
  else
  {
   //j=7?7:j;
   //XB 07.10.19
   j=7;
  }
  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+=(INT16)tpf->len;
  //}
  tpf->address=FieldOffset;
  FieldOffset = (INT16)(FieldOffset+tpf->len);
 }
 fInstalled=1;
 changeflag=0;
 dbferrno=DBFOK;
 strcpy(dbferrmsg,"DBFOK!");
 return 0;
}

//设置缓冲区记录数
INT16 DBF::SetSwp(INT16 buffer_number)
{
 if (SwapBuffer!=NULL)
 {
  delete SwapBuffer;
 }
 CurRecNum=0;
 MaxRecNum=0;
 SwapBuffer=(INT8 *) new INT8[(record_len+1)*buffer_number];
 if (SwapBuffer==NULL)
 {
#ifdef DEBUG
  debugm("GGROUP::SetSwp alloc failure!");
#endif
  dbferrno=NotEnoughMemory;
  strcpy(dbferrmsg,"NotEnoughMemory!");
  return dbferrno;
 }
 CurRecNum=0;
 MaxRecNum=buffer_number;
 return DBFOK;
}

INT16 DBF::AppendToSwp()
{
 if (MaxRecNum==0)
 {
  return Append();
 }
 if (CurRecNum>=MaxRecNum)
 {
  FlushSwp();
 }
 if (CurRecNum>=MaxRecNum)
 {
  dbferrno = OtherError;
  strcpy(dbferrmsg,"OtherError!");
  return OtherError;
    }
 memcpy(SwapBuffer+CurRecNum*record_len, buff, (size_t)record_len);
 CurRecNum++;
 return 0;
}

INT16 DBF::FlushSwp()
{
 INT8 tempbuf[100];
 UINT32  temp_recno;
 INT32 offset;
 
 if(CurRecNum==0)
 {
  return 0;
 }
 if(!fInstalled)
 {  // Not open this file
  dbferrno=ClassNotInit;
  strcpy(dbferrmsg,"ClassNotInit!");
  return dbferrno;
 }
 if(_lseek(handle,0, SEEK_SET)!=0)
 {
  dbferrno=SeekFileError;
  strcpy(dbferrmsg,"SeekFileError!");
  return dbferrno;
 }
 if(read(handle,tempbuf,8)!=8)
 {
  // close();
  dbferrno=ReadFileError;
  strcpy(dbferrmsg,"ReadFileError!");
  return dbferrno;
 }
 (*(UINT32*)(tempbuf+4))+=(UINT32)CurRecNum;//recordnum+1;
 temp_recno=(*(UINT32*)(tempbuf+4));
 if(_lseek(handle, 0, SEEK_SET)!=0)
 {
  dbferrno=SeekFileError;
  strcpy(dbferrmsg,"SeekFileError!");
  return dbferrno;
 }
 if(_write(handle,tempbuf,8)!=8)
 {
  dbferrno=SeekFileError;
  strcpy(dbferrmsg,"SeekFileError!");
  return dbferrno;
 }
 offset=(INT32)head_len+(temp_recno-(UINT32)CurRecNum)*record_len;
 if(_lseek(handle,offset,SEEK_SET)!=offset)
 {
  dbferrno=SeekFileError;
  strcpy(dbferrmsg,"SeekFileError!");
  return dbferrno;
 }
 _write(handle,SwapBuffer,(UINT16)(record_len*CurRecNum));
 _write(handle,"/x1a",1);
 ReOpen();
 GoTo(temp_recno);
 CurRecNum=0;
 strcpy(dbferrmsg,"DBFOK!");
 return DBFOK;
}

// add support delete function in this modul
// addby liaoj 19990426
INT16 DBF::Close(int Delete)
{
 if(handle!=-1)
 {
  ::close(handle); // unlink
  if(Delete)  //with delete
  {
   ::unlink(Name);
  }
  handle=-1;
 }
 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;
 fInstalled=0;
 dbferrno=DBFOK;
 strcpy(dbferrmsg,"DBFOK!");
 return DBFOK;
}

INT16 DBF::Prev()
{
 // skip Not use IDX
 if(current_recno==-2)
 {           // in the DBFEOF
  current_recno=record_no;
  dbferrno=dbf_buff();
  return dbferrno;
 }
 if(current_recno>1)
 {     // in the MIDDLE
  current_recno--;
  dbferrno = dbf_buff();
  return dbferrno;
 }
 if(current_recno==1)
 {    //in the FirstRecord
  current_recno=DBFBOF;
  dbferrno=DBFOK;
  return dbferrno;
 }
 if((current_recno==DBFEOF)||(current_recno==Empty))
 {  // in the DBFEOF;
  dbferrno=RecordOutOfRange;
  return  dbferrno;
 }
 dbferrno=OtherError;
 return dbferrno;
}

INT16 DBF::Next()
{
 return Skip();
}

INT16 DBF::Skip()
{
 // skip Not use IDX
 if(current_recno==DBFBOF)
 {           // in the BOF
  current_recno=1;
  dbferrno=dbf_buff();
  return dbferrno;
 }
 if(current_recno<record_no)
 {     // in the MIDDLE
  current_recno++;
  dbferrno=dbf_buff();
  return dbferrno;
 }
 if(current_recno==record_no)
 {    // in the LastRecord
  current_recno=DBFEOF;
  dbferrno=DBFOK;
  strcpy(dbferrmsg,"DBFOK!");
  return dbferrno;
 }
 if((current_recno==DBFEOF)||(current_recno==Empty))
 {  // in the DBFEOF;
  dbferrno=RecordOutOfRange;
  strcpy(dbferrmsg,"RecordOutOfRange!");
  return dbferrno;
 }
 dbferrno=OtherError;
 strcpy(dbferrmsg,"OtherError!");
 return dbferrno;
}

INT16 DBF::GoTo(INT32 recordno)
{
 if((recordno<=record_no)&&(recordno>0))
 {
  current_recno=(UINT32)recordno;
  dbf_buff();
  dbferrno=DBFOK;
  strcpy(dbferrmsg,"DBFOK!");
    }
 else
 {
  dbferrno=RecordOutOfRange;
  if(recordno<=0)
  {
   current_recno=DBFBOF;
  }
  else if(recordno>record_no)
  {
   current_recno=DBFEOF;
  }
  strcpy(dbferrmsg,"RecordOutOfRange!");
 }
    return dbferrno;
}
INT16 DBF::IsBOF()
{
 if(current_recno==Empty||current_recno==DBFBOF)
 {
  return 1;
 }
 else
 {
  return 0;
 }
}
INT16 DBF::IsEOF()
{
 if(current_recno==Empty||current_recno==DBFEOF) 
 {
  return 1;
 }
 else
 {
  return 0;
 }
}
INT16 DBF::GoTop()
{
 if(record_no!=0)
 {
  current_recno=1;
  dbferrno=dbf_buff();
  return dbferrno;
 }
 else
 {
  dbferrno=DBFOK;
  return dbferrno;
 }
}

INT16 DBF::GoBottom()
{
 if(record_no!=0)
 {
  current_recno=record_no;
  dbferrno = dbf_buff();
  return dbferrno;
 }
 else
 {
  dbferrno=DBFOK;
  return dbferrno;
 }
}


INT16 DBF::fv(INT8 * strFldName,INT8 * FldValue,INT16 BufferLen)
{
 INT16 RetCode;
 if((RetCode=FindField(strFldName))<0)
 {
  FldValue[0]='/x0';
  dbferrno=FieldNotFound;
  strcpy(dbferrmsg,"FieldNotFound!");
  return dbferrno;
 }
 if(RetCode>=fieldnum)
 {
  FldValue[0]='/x0';
  dbferrno=FieldNotFound;
  strcpy(dbferrmsg,"FileNotFound!");
  return dbferrno;
 }
 return fv(RetCode,FldValue,BufferLen);
}

// -1: is eof() bof() in dbase.
//取得字段的数据
INT16 DBF::fv(INT16 FldNo,INT8 * FldValue,INT16 BufferLen)
{
 struct FIELD * tpf;

 if(handle==-1||fInstalled!=1)
 {
  FldValue[0]='/x0';
  dbferrno=FieldNotFound;
  strcpy(dbferrmsg,"FileNotFound!");
  return FieldNotFound;
 }
 if(FldNo>=fieldnum)
 {
  FldValue[0]='/x0';
  dbferrno=FieldNotFound;
  strcpy(dbferrmsg,"FileNotFound!");
  return FieldNotFound;
 }
 if((current_recno>record_no)||(current_recno<0))
 {
  FldValue[0]='/x0';
  dbferrno=RecordOutOfRange;
  strcpy(dbferrmsg,"RecordOutOfRange!");
  return dbferrno;
 }
 tpf=&(pFIELD[FldNo]);
 if(tpf->len<BufferLen||BufferLen==0)
 {
  strncpy(FldValue,(INT8 *)(buff+tpf->address), tpf->len);
  FldValue[tpf->len]='/x0';
 }
 else
 {
  strncpy(FldValue,(INT8 *)(buff+tpf->address), BufferLen-1);
  FldValue[BufferLen-1]='/x0';
 }
 // switch(tpf->f_type)
 // {
 //  case 'C': dbferrno=1; return 1;
 //  case 'N': dbferrno=2; return 2;
 //  case 'L': dbferrno=3; return 3;
 //  case 'M': dbferrno=4; return 4;
 //  case 'D': dbferrno=5; return 5;
 // }
    dbferrno=DBFOK;
 strcpy(dbferrmsg,"DBFOK!");
 return DBFOK;
}

//处理字段内容的格式化
INT16 DBF::dowith(INT16 FldNo,INT8 * tempb)
{
 INT8 buffer[80];
 struct FIELD * tpf; 
 INT16 i;

 tpf=&(pFIELD[FldNo]);
 i = (INT16)strlen(tempb);
 //switch(pFIELD[FldNo].f_type){
 switch(tpf->f_type)
 {
 case 'M':
 case 'C':
 case 'D':
  // for(;i<pFIELD[FldNo].len;i++)
  for(; i<tpf->len; i++)
  {
   tempb[i]=' ';
  }
  tempb[tpf->len]='/x0';
  break;
 case 'N': //格式化数字数据
  ::ccc(tempb,(INT16)tpf->len,(INT16)tpf->bits);
  break;
 case 'L':
  if(tempb[0]=='1'||tempb[0]=='t'||tempb[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:
  dbferrno=FieldTypeError;
  strcpy(dbferrmsg,"FieldTypeError!");
  return FieldTypeError;
 }
 dbferrno=DBFOK;
 strcpy(dbferrmsg,"DBFOK!");
 return DBFOK;
}
//查找第一个出现的记录
INT16 DBF::Loca(INT8 * strFldName,INT8 * FldValue)
{
 INT8 tempbuff[100];
 loca_flag=0;

 if(fieldvalue!=NULL)
 {
  delete fieldvalue;
 }
 fieldvalue=new INT8[strlen(FldValue)+10];
 if(fieldvalue==NULL)
 {
#ifdef DEBUG
  debugm("GGROUP::Loca alloc failure!");
#endif
  // printf("Not enough memory!");
  dbferrno=NotEnoughMemory;
  strcpy(dbferrmsg,"NotEnoughMemory!");
  return dbferrno;
 }
 strcpy(fieldvalue,FldValue);
 if((fieldno=FindField(strFldName))<0)
 {
  dbferrno=FieldNotFound;
  strcpy(dbferrmsg,"FileNotFound!");
  return dbferrno;
 }
 loca_flag=1; 
 current_recno=1;
 dbferrno=dbf_buff();
 
 if(dbferrno!=DBFOK)
 {
  return dbferrno;
 }
 do
 {
  // check current record weather need.
  fv(fieldno, tempbuff);
  dowith(fieldno,tempbuff);
  if(strcmp(tempbuff,fieldvalue)==0)
  {
   dbferrno=DBFOK;
   return DBFOK;// found the just record
  }
 }while((Skip()==0)&&(!IsEOF()));
 if(IsEOF())
 {
  dbferrno=NotFound;
  strcpy(dbferrmsg,"NotFound!");
  return dbferrno;
 }
 else
 {
  dbferrno=DBFOK;
  return dbferrno;
 }
}

//计数
INT16 DBF::Cont()
{
 INT8 tempbuff[100];
 if(!loca_flag)
 {
  dbferrno=OtherError;
  strcpy(dbferrmsg,"OtherError!");
  return dbferrno;
 }
 if(IsEOF())
 {
  dbferrno=NotFound;
  strcpy(dbferrmsg,"NotFound!");
  return dbferrno;
 }
 while(Skip()==0)
 {
  if(IsEOF())
  {
   dbferrno=NotFound;
   strcpy(dbferrmsg,"NotFound!");
   return dbferrno;
  }
  fv(fieldno,tempbuff);
  dowith(fieldno,tempbuff);
  if(strcmp(tempbuff,fieldvalue)==0)
  {
   dbferrno=0;
   return 0;// found the just record
  }
 }
 dbferrno=NotFound;
 strcpy(dbferrmsg,"NotFound!");
 return dbferrno;
}

INT16 DBF::IsDeleted()
{
 if(current_recno<=0)
 {
  dbferrno=RecordOutOfRange;
  strcpy(dbferrmsg,"RecordOutOf!");
  return dbferrno;
 }
    dbferrno=DBFOK;
 strcpy(dbferrmsg,"DBFOK!"); 
 if(*buff==' ')
 {
  return 0;
 }
 else
 {
  return 1;
 }
}

INT16 DBF::Delete()
{
    if(current_recno<=0)
 {
  dbferrno=RecordOutOfRange;
  strcpy(dbferrmsg,"RecordOutOfRange!");
  return dbferrno;
 }
 dbferrno=DBFOK;
 strcpy(dbferrmsg,"DBFOK!");
    *buff='*';
    return dbferrno;
}

INT16 DBF::UnDelete()
{
 if(current_recno<=0)
 {
  dbferrno=RecordOutOfRange;
  strcpy(dbferrmsg,"RecordOutOfRange!");
  return dbferrno;
 }
 *buff=' ';
    dbferrno=DBFOK;
 return dbferrno;
}


INT16 DBF::Replace(INT16 FldNo,INT8 * FldValue)
{
 INT8 tempbuff[200];
 INT8 * tempp;
 struct FIELD * tpf;
 tempp=tempbuff;
 if(FldNo<0||FldNo>=fieldnum)
 {
  dbferrno=FieldNotFound;
  strcpy(dbferrmsg,"FieldNotFound!");
  return dbferrno;
 }
 strncpy(tempbuff,FldValue,199);
 tempbuff[199]='/x0';
 dowith(FldNo,tempp);
 tpf=&(pFIELD[FldNo]);
 //memcpy(buff+pFIELD[FldNo].address,tempp,pFIELD[FldNo].len);
 memcpy(buff+tpf->address,tempp,tpf->len);
 changeflag=1;
 dbferrno=0;
 return 0;
}

INT16 DBF::Replace(INT8 * strFldName,INT8 * FldValue)
{
 INT16 RetCode;
 if((RetCode=FindField(strFldName))<0)
 {
  dbferrno=FieldNotFound;
  strcpy(dbferrmsg,"FieldNotFound!");
  return dbferrno;
 }
 if(RetCode>=fieldnum)
 {
  dbferrno=FieldNotFound;
  strcpy(dbferrmsg,"FieldNotFound!");
  return dbferrno;
 }
 return Replace(RetCode,FldValue);
}
// reread the record no from dbf file
// return :  0: OK.
//          -1: FAILURE.
INT16 DBF::ReOpen()
{
 INT8 tempbuf[100];
 
 if(!fInstalled)
 {
  dbferrno=ClassNotInit;
  strcpy(dbferrmsg,"ClassNotInit!");
  return dbferrno;
 }
 if(handle==-1)
 {
  dbferrno=FileNotOpen;
  strcpy(dbferrmsg,"FileNotOpen!");
  return dbferrno;
 }
 _lseek(handle,0l,SEEK_SET);
 if(read(handle,tempbuf,32)!=32)
 {
  Close();
  dbferrno=NotDBFFile;
  strcpy(dbferrmsg,"NotDBFFile!");
  return dbferrno;
 }
 record_no=*(UINT32 *)(tempbuf+4);
 dbferrno=DBFOK;
 return DBFOK;
}

// flush current record to dbf file
INT16 DBF::Flush()
{
 return dbf_wbuff();
}

// Retry read current record from dbf file
INT16 DBF::ReRead()
{
 if(current_recno<=record_no||current_recno>=1)
 {
  return  dbf_buff();
 }
 dbferrno=RecordOutOfRange;
 strcpy(dbferrmsg,"RecordOutOfRange!");
 return dbferrno;
}

INT16 DBF::FindField(INT8 * strFldName)
{
 INT16 i,j;
 INT8 tempbuffer[20];
 strncpy(tempbuffer,strFldName,10);
 tempbuffer[10]='/x0';
 j = (INT16)strlen(tempbuffer);
 for(i=0;i<j;i++)
 {
  tempbuffer[i] = (char)toupper(tempbuffer[i]);  //转化为大写符号
 }
 for(i=0;i<fieldnum;i++)
 {
  if(strcmp(pFIELD[i].name,tempbuffer)==0)
  {
   dbferrno=0;
   return i;
  }
 }
 dbferrno=FieldNotFound;
 strcpy(dbferrmsg,"FieldNotFOund!");
 return dbferrno;
}

INT8* DBF::GetFieldName(INT16 FldNo)
{
 INT16 fnaddr;   // 本 fn 的字段在数据库中的位置
 static INT8 tmpbf[11];    // read field name from dbf.

 fnaddr=(INT16)((FldNo+1)*32);
 _lseek(handle,fnaddr,SEEK_SET);
 if( read(handle,tmpbf,10) == -1 )
 {
  return "";
 }
 tmpbf[10]=0;
 return tmpbf;
}

INT16  DBF::GetFieldLen(INT16 FldNo)
{
 if (FldNo>fieldnum)
 {
  strcpy(dbferrmsg,"FieldNotFound!");
  return FieldNotFound;
 }
 return pFIELD[FldNo].len;
}

INT16 DBF::GetFieldNum()
{
 return fieldnum;
}

//
INT16 DBF::FieldValue(INT16 Fieldid, INT16& FldValue)
{
 INT8 TempBuf[310];

 FieldValue(Fieldid,TempBuf,300);
 if(dbferrno!=DBFOK)
 {
  return dbferrno;
 }
 FldValue=(INT16)atoi(TempBuf);
 return dbferrno;
}

INT16 DBF::Replace(INT16 Fieldid,INT16 FldValue)
{
 INT8 TempBuf[310];
 sprintf(TempBuf,"%d",FldValue);
 return Replace(Fieldid,TempBuf);
}

INT16 DBF::FieldValue(INT16 Fieldid,UINT16& FldValue)
{
 INT8 TempBuf[310];
 FieldValue(Fieldid,TempBuf,300);
 if(dbferrno!=DBFOK)
 {
  return dbferrno;
 }
 FldValue = (UINT16)atoi(TempBuf);
 return dbferrno;
}

INT16 DBF::Replace(INT16 Fieldid,UINT16 FldValue)
{
 INT8 TempBuf[310];
 sprintf(TempBuf,"%u",FldValue);
 return Replace(Fieldid,TempBuf);
}

INT16 DBF::FieldValue(INT16 Fieldid,INT32& FldValue)
{
 INT8 TempBuf[310];
 
 FieldValue(Fieldid,TempBuf,300);
 if( dbferrno!=DBFOK )
 {
  return dbferrno;
 }
 FldValue=atol(TempBuf);
 return dbferrno;
}
INT16 DBF::Replace(INT16 Fieldid,INT32 FldValue)
{
 INT8 TempBuf[310];
 sprintf(TempBuf,"%ld",FldValue);
 return Replace(Fieldid,TempBuf);
}

INT16 DBF::FieldValue(INT16 Fieldid,UINT32& FldValue)
{
 INT8 TempBuf[310];
 FieldValue(Fieldid,TempBuf,300);
 if(dbferrno!=DBFOK)
 {
  return dbferrno;
 }
 FldValue=(UINT32)atol(TempBuf);
 return dbferrno;
}

INT16 DBF::Replace(INT16 Fieldid,UINT32 FldValue)
{
 INT8 TempBuf[310];
 sprintf(TempBuf,"%lu",FldValue);
 return Replace(Fieldid,TempBuf);
}
INT16 DBF::FieldValue(INT16 Fieldid,DOUBLE& FldValue)
{
 INT8 TempBuf[310];
 FieldValue(Fieldid,TempBuf,300);
 if(dbferrno!=DBFOK)
 {
  return dbferrno;
 }
 FldValue=(DOUBLE)atof(TempBuf);
 return dbferrno;
}

INT16 DBF::Replace(INT16 Fieldid,DOUBLE FldValue)
{
 INT8 TempBuf[310];

 sprintf(TempBuf,"%50.25f",FldValue);
 return Replace(Fieldid,TempBuf);
}

INT16 DBF::FieldValue(INT16 Fieldid,INT8& FldValue)
{
 INT8 TempBuf[310];

 FieldValue(Fieldid,TempBuf,300);
 if(dbferrno!=DBFOK)
 {
  return dbferrno;
 }
 FldValue=TempBuf[0];
 return dbferrno;
}
INT16 DBF::Replace(INT16 Fieldid,INT8 FldValue)
{
 INT8 TempBuf[310];

 TempBuf[0]=FldValue;
 TempBuf[1]='/x0';
 return Replace(Fieldid,TempBuf);
}

INT16 DBF::FieldValue(INT8* strFldName,INT16& FldValue)
{
 return FieldValue(FindField(strFldName),FldValue);
}

INT16 DBF::Replace(INT8* strFldName,INT16 FldValue)
{
 return Replace(FindField(strFldName),FldValue);
}
INT16 DBF::FieldValue(INT8* strFldName,UINT16& FldValue)
{
 return FieldValue(FindField(strFldName),FldValue);
}
INT16 DBF::Replace(INT8* strFldName,UINT16 FldValue)
{
 return Replace(FindField(strFldName),FldValue);
}
INT16 DBF::FieldValue(INT8* strFldName,INT32& FldValue)
{
 return FieldValue(FindField(strFldName),FldValue);
}
INT16 DBF::Replace(INT8* strFldName,INT32 FldValue)
{
 return Replace(FindField(strFldName),FldValue);
}
INT16 DBF::FieldValue(INT8* strFldName,UINT32& FldValue)
{
 return FieldValue(FindField(strFldName),FldValue);
}
INT16 DBF::Replace(INT8* strFldName,UINT32 FldValue)
{
 return Replace(FindField(strFldName),FldValue);
}
INT16 DBF::FieldValue(INT8* strFldName,DOUBLE& FldValue)
{
 return FieldValue(FindField(strFldName),FldValue);
}
INT16 DBF::Replace(INT8* strFldName,DOUBLE FldValue)
{
 return Replace(FindField(strFldName),FldValue);
}
INT16 DBF::FieldValue(INT8* strFldName,INT8& FldValue)
{
 return FieldValue(FindField(strFldName),FldValue);
}
INT16 DBF::Replace(INT8* strFldName,INT8 FldValue)
{
 return Replace(FindField(strFldName),FldValue);
}

//取得数据
INT16  DBF::FieldValue(INT8 * strFldName,INT8 * ValueBuffer,INT16 ValueBufferLen)
{
 return fv(strFldName,ValueBuffer,ValueBufferLen);
}
//取得数据
INT16  DBF::FieldValue(INT16 FldNo,INT8 *ValueBuffer,INT16 ValueBufferLen)
{
 return fv(FldNo,ValueBuffer,ValueBufferLen);
}

/*
//快速排序
void DBF::QuickSort(int low, int high)  
{
 int i=low, j=high;  
 char strTemp[1024] = "";
 struct FIELD * tpf=&(pFIELD[FldNo]); 
 char *pFld = &SwapBuffer[low]+tpf->address;

 memcpy(strTemp, &SwapBuffer[low], record_len);

 while( i<j ) 
 {
  //while (i<j && x[j]>t) 
  while (i<j && strcmp(SwapBuffer[j])>0) 
   j--;   
  //x[i]=x[j];
  memcpy(&SwapBuffer[i], &SwapBuffer[j], record_len);  
  
  while (i<j && x[i]<=t)
   i++;     
  //x[j]=x[i];     
  //x[i] = t;
  memcpy(&SwapBuffer[j], &SwapBuffer[i], record_len);  
  memcpy(&SwapBuffer[i], strTemp, record_len); 
  QuickSort(x,low,i-1); //递归调用此函数 
  QuickSort(x,i+1,high); 
 }
}
*/

INT32 Address = 0;
UINT8 FldLen = 0;

int compare(const void *pE1, const void *pE2)
{
 char *p1  = (char*)pE1+Address;
 char *p2  = (char*)pE2+Address;

 return( memcmp(p1, p2, FldLen) );
}

//根据单个字段内容排序
INT16 DBF::SortByField(UINT8 FldNo)
{
 INT32  nRecNum, i;

 if(FldNo<0||FldNo>=fieldnum)
 {
  dbferrno=FieldNotFound;
  strcpy(dbferrmsg,"FieldNotFound!");
  return dbferrno;
 }
 Address = pFIELD[FldNo-1].address;
 FldLen = pFIELD[FldNo-1].len;
 nRecNum = GetRecNum();
 if( nRecNum>1 )
 {
  SetSwp( nRecNum );
  GoTop();  
  for(i=0; i<nRecNum; i++)
  {
   AppendToSwp();
   //memcpy(SwapBuffer[i*]
   Next();
  }
  //进行排序  
  qsort(SwapBuffer, nRecNum, record_len,  compare); //
  _lseek(handle, head_len, SEEK_SET);
  write(handle,SwapBuffer,record_len*nRecNum);  
 }
 dbferrno=0;
 return 0;
}

//---------------------------------------------
//与创建DBF文件有关的类
//dbf文件头结构
DBF_HEAD::DBF_HEAD()
{
 int i;
 
 id=0x3;
    year=107;
    month=10;
    day=1;
 recorder_num=0;
 head_len=0x21;
 record_len=0x0;
 for(i=0;i<20;i++)
 {
  reserved[i]=0;
 }
}

DBF_FIELD::DBF_FIELD()
{
 assert( sizeof(DBF_FIELD)==32 );
 f_type = 0;
 address = 0;
 len = 0;
 bits = 0;
 memset(reserved_data, 0, sizeof(reserved_data) );
 memset(name, 0, sizeof(name) );
}

DBF_FIELD::~DBF_FIELD()
{
 ;
}

CDBF::CDBF()
{
 INT8 i;
 
 assert( sizeof(INT8)==1 );
 assert( sizeof(UINT8)==1 );
 assert( sizeof(INT16)==2 );
 assert( sizeof(UINT16)==2 );
 assert( sizeof(INT32)==4 );
 assert( sizeof(UINT32)==4 );
 
 FieldNum=0;
 for(i=0;i<MAXFIELDNUM;i++)
 {
  pFIELD[i]=NULL;
 }
 c_pos = 0;
 
}

void CDBF::ReSet()
{
 int i;
 for(i=0;i<FieldNum;i++)
 {
  if(pFIELD[i]!=NULL)
  {
   delete pFIELD[i];
   pFIELD[i]=NULL;
  }
 }
 c_pos = 0;
 FieldNum = 0;
}


CDBF::~CDBF()
{
 try
 {
  ReSet();
 }
 catch(...)
 {
  ;
 }
}


//只能处理 C N D三种字段
INT16 CDBF::Add(const char * strFldName, char FieldType, UINT8 len, UINT8 bits)
{
 INT32 str_len;
 INT32 i;
 
 // Check Parameter FieldType='CNDL'
 if((FieldType!='C')&&(FieldType!='N')
  &&(FieldType!='D') &&(FieldType!='L') )
 {
  return -1;
 }
 pFIELD[FieldNum]=new DBF_FIELD;
 //
 if(pFIELD[FieldNum]==NULL)
 {
#ifdef DEBUG
  debugm("CDBF::add alloc failure!");
#endif
  return -2;
 }
 FieldNum++;
 //
 str_len = (INT32)strlen(strFldName);
 if(str_len>10)
 {
  str_len=10;
 }
 for(i=0;i<str_len;i++)
 {
  pFIELD[FieldNum-1]->name[i]=(char)toupper(strFldName[i]);
 }
 for(i=str_len;i<11;i++)
 {
  pFIELD[FieldNum-1]->name[i]='/x0';
 }
 pFIELD[FieldNum-1]->f_type=FieldType;
 if(FieldType=='D')  //日期
 {
  pFIELD[FieldNum-1]->len=8;
 }
 else                 
 {
  pFIELD[FieldNum-1]->len=len;
 }
 if(FieldType=='N')           //数字类型
 {
  pFIELD[FieldNum-1]->bits=bits;
 }
 else
 {
  pFIELD[FieldNum-1]->bits=0;
 }
 // ----------------------
 pFIELD[FieldNum-1]->address=c_pos+1;
 c_pos+=len;
 // ----------------------
 return 0;
}

INT16 CDBF::Create(const char * strFileName)
{
 INT32  i,rec_len;
 //char tail[3]="/x0d/x1a";
 char tail[3]="/x0d";
 FILE * fp;
 time_t  TimeValue;
 struct tm *pTm = NULL;
 
 TimeValue = time(NULL);
 pTm = localtime((const time_t*)&TimeValue);
 head.year    = (UINT8)(pTm->tm_year);
 head.month   =(UINT8)(pTm->tm_mon+1);
 head.day      =(UINT8)pTm->tm_mday;
 
 head.head_len = (UINT16)(32+32*FieldNum+1); // dbf头长度
 for(i=0,rec_len=0;i<FieldNum;i++)
 {
  rec_len+=pFIELD[i]->len;
 }
 head.record_len=(UINT16)(rec_len+1);
 if((fp=fopen(strFileName,"wb"))==NULL)  //创建数据库文件
 {
  return -1;
 }
 if( fwrite(&(head),sizeof(DBF_HEAD),1,fp)!=1 )
 {
  fclose(fp);
  return -2;
 }
 for(i=0;i<FieldNum;i++)
 {
  if(fwrite(pFIELD[i],sizeof(DBF_FIELD),1,fp)!=1)
  {
   fclose(fp);
   return -3;
  }
 }
 fwrite(tail,1, 1,fp);
 fclose(fp);
 return 0;
}

void NO_USED_FUN()
{
 INT16 Re;
 DBF dbf;

 Re =  dbf.GetErrno();
 Re = dbf.ff("SDFLK");
 Re  = (INT16)dbf.GetRecNum();
 Re = (INT16)dbf.GetRecNo();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值