📄 dbfop.hpp
字号:
/************************************************************************
DBFOP for C++ V1.00 ,1995.1.10
Develop by John,Liao
This Module include the C++ headfile dbfop.hpp,
and C++ resource file dbfop.cpp.
This module is develop for use DBF(DBASEIII,DBASEII,MFOXBASE)
and IDX(MFOXBASE).but use IDX only allow read or change the field that
isn't key word.
Support netware share .rlock,rlock,unlock_.
This module is reference to the module "wr_dbf.c,dbase.h" which develop
by Owen,chen in NEWELL.
1996.12 attemp add function : del(),undel(),zap(),pack()
1996.12.24 add function zap()
1999.03.19 big change has been made as follow:
A. Suport open dbf file in read only model
B. Use type macro INT2,UINT2,INT4,UINT4,UCHAR,CHAR
C. Megirate class CRT_DBF In this model
D. Add those function, make program easier for Mr.Xiecg
FieldValue(INT2,INT2&); Replace(INT2,INT2&);
FieldValue(INT2,UINT2&); Replace(UINT2,UINT2&);
FieldValue(INT2,INT4&); Replace(INT,INT4&);
FieldValue(INT2,UINT4&); Replace(INT2,UINT4&);
FieldValue(INT2,DOUBLE&); Replace(INT2,DOUTLE4&);
FieldValue(INT2,CHAR&); Replace(INT2,CHAR&);
FieldValue(CHAR*,INT2&); Replace(CHAR*,INT2&);
FieldValue(CHAR*,UINT2&); Replace(CHAR*,UINT2&);
FieldValue(CHAR*,INT4&); Replace(CHAR*,INT4&);
FieldValue(CHAR*,UINT4&); Replace(CHAR*,UINT4&);
FieldValue(CHAR*,DOUBLE&); Replace(CHAR*,DOUBLE&);
FieldValue(CHAR*,CHAR&); Replace(CHAR*,CHAR&);
E: add the length control in function FieldFind;
F: change open idx file from mode read and write to readonly
*************************************************************************/
#ifndef ___DBFOP_HPP___
#define ___DBFOP_HPP___
#include <stdio.h>
// define type value for the class add by liaoj 1999.03.19
#define INT2 short
#define UINT2 unsigned short
#define INT4 int
#define UINT4 unsigned int
#define UCHAR unsigned char
#define CHAR char
#define DOUBLE double
// DOUBLE indecate 64 bit float
// --------------
/*************************************************************************
This struct is used by class IDX.
Normally,a IDX block's length is 200H(512D) bytes.
**************************************************************************/
struct IDX_BLOCK {
INT2 keylen; // KEY WORD length,(BYTES)
CHAR flag; // This block's type.
// 0 - frame ,1 - root, 2 - leaf , 3 - both
UCHAR count; // avalid node in this IDX block
// ... count for the node
UCHAR items;
UINT4 prev; // position of the IDX block which prev this block.
// if 0xFFFFFFFF means this block is the first block
// of this idx file.
UINT4 next; // position of the IDX block which next this block.
// if 0xFFFFFFFF means this block is the last block
CHAR buff[512]; // block size is 512 bytes
INT2 curp; // current position of last move
INT2 skip(); // like next();
public:
INT2 GetFlag() { return (INT2) flag; };
INT2 SetKeyLen(INT2 keylen_par){keylen=keylen_par; return 0;};
UINT4 GetPrev() { return prev;};
UINT4 GetNext() { return next;};
INT2 Prev(){
if(curp==0) return -2;
else curp--;
return 0;
}
INT2 Next(){
if(curp>=items-1) return -3;
else curp++;
return 0;
}
INT2 find(CHAR * key);// 0:Found
// -1 :Not Found
// -2 :Maybe in PrevBlock
// -3 :Maybe in NextBlock
// -10:This is ROOT ,Maybe in GetResult()
UINT4 GetResult();
INT2 ReadBlock(INT2,UINT4 pos);
INT2 Home() { curp=0; return 0; };
INT2 End() {
if(items>0) curp=(char)(items-1);
else curp=0;
return 0;
}
};
/*************************************************************************
This class is call by class DBF inter.
**************************************************************************/
class IDX {
INT2 Installed;
INT2 handle;
// FILE * fp;
UINT4 rootpos;
UINT4 blocks;
INT2 key_len;
IDX_BLOCK block;
public:
IDX();
~IDX();
INT2 open(CHAR *); // 0: OK -1:Not Found
operator void *() { return (void *)Installed; };
INT2 operator !() { return !Installed; };
INT2 Find(CHAR *); // 0: OK -1:Not Found
UINT4 GetRecordNo() { return block.GetResult(); };
INT2 Prev(); // 0: OK -1:HomePosCame
INT2 Next(); // 0: OK -2:EndPosCame
INT2 GoHome(); // 0: OK -1:The IDX is empty
INT2 GoEnd(); // 0: OK -1:The IDX is empty
INT2 Skip(); // 0: OK -1:HomePosCame -2:EndPosCame
};
struct FIELD {
CHAR name[11];
CHAR f_type;
INT4 address;
UCHAR len;
UCHAR bits;
public:
// ~FIELD();
};
/****************************************************************
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:
INT2 Installed;
INT2 OpenMode;
// save current dbf open mode
// valid value:
// DBF::ReadOnly,ReadWrite
CHAR * 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
INT2 changeflag;
FIELD * pFIELD;
INT4 current_recno; // -1 bof -2 eof -3 bof.and.eof
struct FIELD * First;
CHAR Name[100];
// FILE * fp;
INT2 handle;
class IDX * pIDX;
INT4 record_no;
UINT2 head_len;
UINT2 record_len;
INT2 m_errno;
INT2 dbf_buff();
INT2 dbf_wbuff();
INT2 fieldnum;
// ----------------------------------
CHAR * SwapBuffer;
INT2 MaxRecNum;
INT2 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
// ------------------------------------------------------------
INT2 lock_flag; // 0. No lock 1.lockfile 2.lockrecorder
INT4 lock_start; // when lock_flag=0,
INT4 lock_len;
// ------------------------------------------------------------
// these var define next is used for
// loca(),cont() like the locate,continue.command in MFOXPLUS
// ------------------------------------------------------------
INT2 loca_flag; // 0. not call loca 1.already call loca
INT2 fieldno; // last loca fieldno
CHAR * fieldvalue;
INT2 dowith(INT2 fn,CHAR *tempb);
INT2 unlock_temp();
INT2 DBFType; //0:fox serial, 1:clipper
public:
INT2 GetFieldNum();
CHAR * GetFieldName(INT2 fn);
INT2 GetFieldLen(INT2 fn);
INT2 SetSwp(INT2 buffer_number);
INT2 AppendToSwp();
INT2 FlushSwp();
// ------------------------------------------------------------
INT2 GetErrno() { return m_errno;};
INT2 FindField(CHAR * filedname); // -1: not find >=0:return fieldno
INT2 ff(CHAR * fieldname) {return FindField(fieldname);};
INT2 Clear();
INT2 Append();
INT2 AppendBlank(); // -1
DBF();
~DBF();
operator void *() { return (void *)Installed;};
INT2 operator !() { return Installed;};
INT2 Open(CHAR * filename,INT2 ReadWriteMode=ReadWrite,INT2 DBFType_Par=Foxbase);
INT2 GetErrNo() { return m_errno ; };
INT4 GetRecNum() { return record_no; };
INT4 GetRecNo() { return current_recno ;};
INT2 Close();
// in function fv,and FieldValue
// ValueBuffer=0 indecate infinite len of ValueBuffer),
INT2 fv(CHAR * ,CHAR *,INT2 ValueBufferLen=0);
INT2 FieldValue(CHAR * FieldName,CHAR * ValueBuffer,INT2 ValueBufferLen=0);
INT2 fv(INT2,CHAR *,INT2 ValueBufferLen=0); //
INT2 FieldValue(INT2 FieldIndex,CHAR * ValueBuffer,INT2 ValueBufferLen=0);
//--------------------------------------------
INT2 Next();
INT2 Prev();
INT2 Skip();
INT2 GoTop();
INT2 GoBottom();
INT2 Zap();
INT2 IsDeleted();
INT2 Delete();
INT2 UnDelete();
//--------------------------------------------
INT2 GoTo(INT4 recordno); // don't use when idx is avalid
INT2 IsBOF();
INT2 IsEOF();
INT2 Seek(CHAR *);
// loca and cont : return DBFOK found other not found
INT2 Loca(CHAR * fieldname,CHAR * fieldvalue);
INT2 Cont();
INT2 OpenIDX(CHAR *);
INT2 CloseIDX();
INT2 Replace(CHAR * fieldname,CHAR * fieldvalue);
INT2 Replace(INT2 fieldno,CHAR * fieldvalue);
INT2 ReOpen(); // only get the record_no read from file
INT2 Flush(); // write current buff to file
INT2 ReRead(); // read the newest of current record to buff
// ------------------------------------------------------------------
INT2 Flock(); // 0: OK. 1:not locked.
INT2 Rlock(); // 0: OK. 1:not locked. -1:bof.eof.
INT2 Unlock();
// add fuctions as follow make program easier
INT2 FieldValue(INT2,INT2&);
INT2 Replace(INT2,INT2);
INT2 FieldValue(INT2,UINT2&);
INT2 Replace(INT2,UINT2);
INT2 FieldValue(INT2,INT4&);
INT2 Replace(INT2,INT4);
INT2 FieldValue(INT2,UINT4&);
INT2 Replace(INT2,UINT4);
INT2 FieldValue(INT2,DOUBLE&);
INT2 Replace(INT2,DOUBLE);
INT2 FieldValue(INT2,CHAR&);
INT2 Replace(INT2,CHAR);
INT2 FieldValue(CHAR*,INT2&);
INT2 Replace(CHAR*,INT2);
INT2 FieldValue(CHAR*,UINT2&);
INT2 Replace(CHAR*,UINT2);
INT2 FieldValue(CHAR*,INT4&);
INT2 Replace(CHAR*,INT4);
INT2 FieldValue(CHAR*,UINT4&);
INT2 Replace(CHAR*,UINT4);
INT2 FieldValue(CHAR*,DOUBLE&);
INT2 Replace(CHAR*,DOUBLE);
INT2 FieldValue(CHAR*,CHAR&);
INT2 Replace(CHAR*,CHAR);
};
// ---
#define MAXFIELDNUM 256
struct M_DBF_HEAD {
unsigned char id;
unsigned char year;
unsigned char month;
unsigned char day;
UINT4 recorder_num;
UINT2 head_len;
UINT2 record_len;
unsigned char reserved[20];
M_DBF_HEAD();
};
struct M_DBF_FIELD {
char name[11];
char f_type;
long address;
unsigned char len;
unsigned char bits;
unsigned char reserved_data[14];
M_DBF_FIELD();
};
class CDBF{
M_DBF_HEAD head;
M_DBF_FIELD * pFIELD[MAXFIELDNUM];
int FieldNum;
long c_pos;
public:
CDBF();
~CDBF();
add(char* FieldName,char FieldType,unsigned char len,unsigned char bits);
create(char * FileName);
reset();
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -