00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #ifndef __XB_DBF_H__
00053 #define __XB_DBF_H__
00054
00055 #ifdef __GNUG__
00056 #pragma interface
00057 #endif
00058
00059 #ifdef __WIN32__
00060 #include <xbase/xbconfigw32.h>
00061 #else
00062 #include <xbase/xbconfig.h>
00063 #endif
00064
00065 #include <xbase/xtypes.h>
00066 #include <xbase/xdate.h>
00067
00068 #include <stdio.h>
00069
00073 #if defined(XB_INDEX_ANY)
00074 class XBDLLEXPORT xbIndex;
00075 class XBDLLEXPORT xbNdx;
00076 class XBDLLEXPORT xbNtx;
00077 #endif
00078
00079
00080
00081
00082 #define XB_CHAR_FLD 'C'
00083 #define XB_LOGICAL_FLD 'L'
00084 #define XB_NUMERIC_FLD 'N'
00085 #define XB_DATE_FLD 'D'
00086 #define XB_MEMO_FLD 'M'
00087 #define XB_FLOAT_FLD 'F'
00088
00089
00090
00091
00092 #define XB_CLOSED 0
00093 #define XB_OPEN 1
00094 #define XB_UPDATED 2
00095
00096
00097
00098
00099 #define XB_OVERLAY 1
00100 #define XB_DONTOVERLAY 0
00101
00102 #define XB_CHAREOF '\x1A'
00103 #define XB_CHARHDR '\x0D'
00104
00106
00139 struct XBDLLEXPORT xbSchema {
00140 char FieldName[11];
00141 char Type;
00142
00143
00144 unsigned char FieldLen;
00145 unsigned char NoOfDecs;
00146 };
00147
00149
00152 struct XBDLLEXPORT xbSchemaRec {
00153 char FieldName[11];
00154 char Type;
00155 char *Address;
00156
00157
00158 unsigned char FieldLen;
00159 unsigned char NoOfDecs;
00160 char *Address2;
00161 char *fp;
00162
00163 xbShort LongFieldLen;
00164 };
00165
00167
00170 struct XBDLLEXPORT xbIxList {
00171 xbIxList * NextIx;
00172 xbString IxName;
00173 #if defined(XB_INDEX_ANY)
00174 xbIndex * index;
00175 xbShort Unique;
00176 xbShort KeyUpdated;
00177 #endif
00178 };
00179
00181
00185 #ifdef XB_MEMO_FIELDS
00186 struct XBDLLEXPORT xbMH{
00187 xbLong NextBlock;
00188 char FileName[8];
00189 char Version;
00190 xbShort BlockSize;
00191 };
00192 #endif
00193
00195
00199 class XBDLLEXPORT xbDbf {
00200
00201 public:
00202 xbDbf( xbXBase * );
00203 xbXBase *xbase;
00204
00205
00206
00207 #if defined(XB_INDEX_ANY)
00208 xbShort AddIndexToIxList(xbIndex *, const char *IndexName);
00209 xbShort RemoveIndexFromIxList( xbIndex * );
00210 #endif
00211 xbShort AppendRecord( void );
00212 xbShort BlankRecord( void );
00213 xbLong CalcCheckSum( void );
00214 xbShort CloseDatabase(bool deleteIndexes = 0);
00215 xbShort CopyDbfStructure( const char *, xbShort );
00216 xbShort CreateDatabase( const char * Name, xbSchema *, const xbShort Overlay );
00218
00220 xbLong DbfTell( void ) { return ftell( fp ); }
00222
00224 xbShort DeleteAllRecords( void ) { return DeleteAll(0); }
00225 xbShort DeleteRecord( void );
00226 #ifdef XBASE_DEBUG
00227 xbShort DumpHeader( xbShort );
00228 #endif
00229 xbShort DumpRecord( xbULong );
00231
00233 xbLong FieldCount( void ) { return NoOfFields; }
00235
00237 xbString& GetDbfName( void ) { return DatabaseName; }
00239
00241 xbShort GetDbfStatus( void ) { return DbfStatus; }
00242 xbShort GetFirstRecord( void );
00243 xbShort GetLastRecord( void );
00244 xbShort GetNextRecord( void );
00245 xbShort GetPrevRecord( void );
00247
00249 xbLong GetCurRecNo( void ) { return CurRec; }
00250 xbShort GetRecord( xbULong );
00252
00254 char * GetRecordBuf( void ) { return RecBuf; }
00256
00258 xbShort GetRecordLen( void ) { return RecordLen; }
00259 xbShort NameSuffixMissing( xbShort, const char * );
00260 xbLong NoOfRecords( void );
00261 xbLong PhysicalNoOfRecords(void);
00262 xbShort OpenDatabase( const char * );
00263 xbShort PackDatabase(xbShort LockWaitOption,
00264 void (*packStatusFunc)(xbLong itemNum, xbLong numItems) = 0,
00265 void (*indexStatusFunc)(xbLong itemNum, xbLong numItems) = 0);
00266 xbShort PutRecord(void);
00267 xbShort PutRecord(xbULong);
00268 xbShort RebuildAllIndices(void (*statusFunc)(xbLong itemNum, xbLong numItems) = 0);
00269 xbShort RecordDeleted( void );
00271
00273 void ResetNoOfRecs( void ) { NoOfRecs = 0L; }
00274 xbShort SetVersion( xbShort );
00276
00278 xbShort UndeleteAllRecords( void ) { return DeleteAll(1); }
00279 xbShort UndeleteRecord( void );
00280 xbShort Zap( xbShort );
00281
00282
00283 const char *GetField(xbShort FieldNo) const;
00284 const char *GetField(const char *Name) const;
00285 xbShort GetField( xbShort FieldNo, char *Buf) const;
00286 xbShort GetRawField(const xbShort FieldNo, char *Buf) const;
00287 xbShort GetField( xbShort FieldNo, char *Buf, xbShort RecBufSw) const;
00288 xbShort GetField( const char *Name, char *Buf) const;
00289 xbShort GetRawField(const char *Name, char *Buf) const;
00290 xbShort GetField( const char *Name, char *Buf, xbShort RecBufSw) const;
00291 xbShort GetField(xbShort FieldNo, xbString&, xbShort RecBufSw ) const;
00292 xbShort GetFieldDecimal( const xbShort );
00293 xbShort GetFieldLen( const xbShort );
00294 char * GetFieldName( const xbShort );
00295 xbShort GetFieldNo( const char * FieldName ) const;
00296 char GetFieldType( const xbShort FieldNo ) const;
00297 xbShort GetLogicalField( const xbShort FieldNo );
00298 xbShort GetLogicalField( const char * FieldName );
00299
00300 char * GetStringField( const xbShort FieldNo );
00301 char * GetStringField( const char * FieldName );
00302
00303 xbShort PutField( const xbShort, const char * );
00304 xbShort PutRawField( const xbShort FieldNo, const char *buf );
00305 xbShort PutField( const char *Name, const char *buf);
00306 xbShort PutRawField( const char *Name, const char *buf );
00307 xbShort ValidLogicalData( const char * );
00308 xbShort ValidNumericData( const char * );
00309
00310 xbLong GetLongField( const char *FieldName) const;
00311 xbLong GetLongField( const xbShort FieldNo) const;
00312 xbShort PutLongField( const xbShort, const xbLong );
00313 xbShort PutLongField( const char *, const xbLong);
00314
00315 xbFloat GetFloatField( const char * FieldName );
00316 xbFloat GetFloatField( const xbShort FieldNo );
00317 xbShort PutFloatField( const char *, const xbFloat);
00318 xbShort PutFloatField( const xbShort, const xbFloat);
00319
00320 xbDouble GetDoubleField(const char *);
00321 xbDouble GetDoubleField(const xbShort, xbShort RecBufSw = 0);
00322 xbShort PutDoubleField(const char *, const xbDouble);
00323 xbShort PutDoubleField(const xbShort, const xbDouble);
00324
00325 #ifdef XB_LOCKING_ON
00326 xbShort LockDatabase( const xbShort, const xbShort, const xbULong );
00327 xbShort ExclusiveLock( const xbShort );
00328 xbShort ExclusiveUnlock( void );
00329
00330 #ifndef HAVE_FCNTL
00331 xbShort UnixToDosLockCommand( const xbShort WaitOption,
00332 const xbShort LockType ) const;
00333 #endif
00334
00335 #else
00336 xbShort LockDatabase( const xbShort, const xbShort, const xbLong )
00337 { return XB_NO_ERROR; }
00338 xbShort ExclusiveLock( const xbShort ) { return XB_NO_ERROR; };
00339 xbShort ExclusiveUnlock( void ) { return XB_NO_ERROR; };
00340 #endif
00341
00343
00345 void AutoLockOn( void ) { AutoLock = 1; }
00347
00349 void AutoLockOff( void ) { AutoLock = 0; }
00351
00353 xbShort GetAutoLock(void) { return AutoLock; }
00354
00355 #ifdef XB_MEMO_FIELDS
00356 xbShort GetMemoField( const xbShort FieldNo,const xbLong len,
00357 char * Buf, const xbShort LockOption );
00358 xbLong GetMemoFieldLen( const xbShort FieldNo );
00359 xbShort UpdateMemoData( const xbShort FieldNo, const xbLong len,
00360 const char * Buf, const xbShort LockOption );
00361 xbShort MemoFieldExists( const xbShort FieldNo ) const;
00362 xbShort LockMemoFile( const xbShort WaitOption, const xbShort LockType );
00363 xbShort MemoFieldsPresent( void ) const;
00364 xbLong CalcLastDataBlock();
00365 xbShort FindBlockSetInChain( const xbLong BlocksNeeded, const xbLong
00366 LastDataBlock, xbLong & Location, xbLong &PreviousNode );
00367 xbShort GetBlockSetFromChain( const xbLong BlocksNeeded, const xbLong
00368 Location, const xbLong PreviousNode );
00369
00370 #ifdef XBASE_DEBUG
00371 xbShort DumpMemoFreeChain( void );
00372 void DumpMemoHeader( void ) const;
00373 void DumpMemoBlock( void ) const;
00374 #endif
00375 #endif
00376
00378
00386 void RealDeleteOn(void) { RealDelete = 1; if(fp) ReadHeader(1); }
00389 void RealDeleteOff(void) { RealDelete = 0; if(fp) ReadHeader(1); }
00391
00395 xbShort GetRealDelete(void) { return RealDelete; }
00396
00397 #if defined(XB_INDEX_ANY)
00398 xbShort IndexCount(void);
00399 xbIndex *GetIndex(xbShort indexNum);
00400 #endif
00401
00402 protected:
00403 xbString DatabaseName;
00404 xbShort XFV;
00405 xbShort NoOfFields;
00406 char DbfStatus;
00407
00408
00409 FILE *fp;
00410 xbSchemaRec *SchemaPtr;
00411 char *RecBuf;
00412 char *RecBuf2;
00413
00414 #ifdef XB_MEMO_FIELDS
00415 FILE *mfp;
00416 void *mbb;
00417 xbMH MemoHeader;
00418
00419 xbShort mfield1;
00420 xbShort MStartPos;
00421 xbLong MFieldLen;
00422 xbLong NextFreeBlock;
00423 xbLong FreeBlockCnt;
00424
00425 xbLong MNextBlockNo;
00426 xbLong MNoOfFreeBlocks;
00427
00428 xbLong CurMemoBlockNo;
00429 #endif
00430
00431
00432
00433 char Version;
00434 char UpdateYY;
00435 char UpdateMM;
00436 char UpdateDD;
00437
00438
00439
00440
00441 xbULong NoOfRecs;
00442 xbUShort HeaderLen;
00443 xbUShort RecordLen;
00444
00445
00446 xbULong FirstFreeRec;
00447 xbULong RealNumRecs;
00448
00449
00450 xbIxList * MdxList;
00451 xbIxList * NdxList;
00452 xbIxList * FreeIxList;
00453 xbULong CurRec;
00454 xbShort AutoLock;
00455
00456
00457 xbShort RealDelete;
00458
00459
00460 #ifdef XB_LOCKING_ON
00461 xbShort CurLockType;
00462 xbShort CurLockCount;
00463 xbULong CurLockedRecNo;
00464 xbShort CurRecLockType;
00465 xbShort CurRecLockCount;
00466 xbShort CurMemoLockType;
00467 xbShort CurMemoLockCount;
00468 #endif
00469
00470 xbShort DeleteAll( xbShort );
00471 void InitVars( void );
00472 xbShort PackDatafiles(void (*statusFunc)(xbLong itemNum, xbLong numItems) = 0);
00473 xbShort ReadHeader( xbShort );
00474 xbShort WriteHeader( const xbShort );
00475
00476 #ifdef XB_MEMO_FIELDS
00477 xbShort AddMemoData( const xbShort FieldNo, const xbLong Len, const char * Buf );
00478 xbShort CreateMemoFile( void );
00479 xbShort DeleteMemoField( const xbShort FieldNo );
00480 xbShort GetDbtHeader( const xbShort Option );
00481 xbShort GetMemoBlockSize( void ) { return MemoHeader.BlockSize; }
00482 xbShort OpenMemoFile( void );
00483 xbShort PutMemoData( const xbLong StartBlock, const xbLong BlocksNeeded,
00484 const xbLong Len, const char * Buf );
00485 xbShort ReadMemoBlock( const xbLong BlockNo, const xbShort Option);
00486 xbShort SetMemoBlockSize( const xbShort );
00487 xbShort UpdateHeadNextNode( void ) const;
00488 xbShort WriteMemoBlock( const xbLong BlockNo, const xbShort Option );
00489 xbShort IsType3Dbt( void ) const { return( Version==(char)0x83 ? 1:0 ); }
00490 xbShort IsType4Dbt( void ) const
00491 {return (( Version==(char)0x8B || Version==(char)0x8E ) ? 1:0 );}
00492 #endif
00493 };
00494 #endif // __XB_DBF_H__
00495