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 <iostream.h>
00069 #include <stdio.h>
00070
00074 #if defined(XB_INDEX_ANY)
00075 class XBDLLEXPORT xbIndex;
00076 class XBDLLEXPORT xbNdx;
00077 class XBDLLEXPORT xbNtx;
00078 #endif
00079
00080
00081
00082
00083 #define XB_CHAR_FLD 'C'
00084 #define XB_LOGICAL_FLD 'L'
00085 #define XB_NUMERIC_FLD 'N'
00086 #define XB_DATE_FLD 'D'
00087 #define XB_MEMO_FLD 'M'
00088 #define XB_FLOAT_FLD 'F'
00089
00090
00091
00092
00093 #define XB_CLOSED 0
00094 #define XB_OPEN 1
00095 #define XB_UPDATED 2
00096
00097
00098
00099
00100 #define XB_OVERLAY 1
00101 #define XB_DONTOVERLAY 0
00102
00103 #define XB_CHAREOF '\x1A'
00104 #define XB_CHARHDR '\x0D'
00105
00107
00140 struct XBDLLEXPORT xbSchema {
00141 char FieldName[11];
00142 char Type;
00143
00144
00145 unsigned char FieldLen;
00146 unsigned char NoOfDecs;
00147 };
00148
00150
00153 struct XBDLLEXPORT xbSchemaRec {
00154 char FieldName[11];
00155 char Type;
00156 char *Address;
00157
00158
00159 unsigned char FieldLen;
00160 unsigned char NoOfDecs;
00161 char *Address2;
00162 char *fp;
00163
00164 xbShort LongFieldLen;
00165 };
00166
00168
00171 struct XBDLLEXPORT xbIxList {
00172 xbIxList * NextIx;
00173 xbString IxName;
00174 #if defined(XB_INDEX_ANY)
00175 xbIndex * index;
00176 xbShort Unique;
00177 xbShort KeyUpdated;
00178 #endif
00179 };
00180
00182
00186 #ifdef XB_MEMO_FIELDS
00187 struct XBDLLEXPORT xbMH{
00188 xbLong NextBlock;
00189 char FileName[8];
00190 char Version;
00191 xbShort BlockSize;
00192 };
00193 #endif
00194
00196
00200 class XBDLLEXPORT xbDbf {
00201
00202 public:
00203 xbDbf( xbXBase * );
00204 xbXBase *xbase;
00205
00206
00207
00208 #if defined(XB_INDEX_ANY)
00209 xbShort AddIndexToIxList(xbIndex *, const char *IndexName);
00210 xbShort RemoveIndexFromIxList( xbIndex * );
00211 #endif
00212 xbShort AppendRecord( void );
00213 xbShort BlankRecord( void );
00214 xbLong CalcCheckSum( void );
00215 xbShort CloseDatabase(bool deleteIndexes = 0);
00216 xbShort CopyDbfStructure( const char *, xbShort );
00217 xbShort CreateDatabase( const char * Name, xbSchema *, const xbShort Overlay );
00219
00221 xbLong DbfTell( void ) { return ftell( fp ); }
00223
00225 xbShort DeleteAllRecords( void ) { return DeleteAll(0); }
00226 xbShort DeleteRecord( void );
00227 #ifdef XBASE_DEBUG
00228 xbShort DumpHeader( xbShort );
00229 #endif
00230 xbShort DumpRecord( xbULong );
00232
00234 xbLong FieldCount( void ) { return NoOfFields; }
00236
00238 xbString& GetDbfName( void ) { return DatabaseName; }
00240
00242 xbShort GetDbfStatus( void ) { return DbfStatus; }
00243 xbShort GetFirstRecord( void );
00244 xbShort GetLastRecord( void );
00245 xbShort GetNextRecord( void );
00246 xbShort GetPrevRecord( void );
00248
00250 xbLong GetCurRecNo( void ) { return CurRec; }
00251 xbShort GetRecord( xbULong );
00253
00255 char * GetRecordBuf( void ) { return RecBuf; }
00257
00259 xbShort GetRecordLen( void ) { return RecordLen; }
00260 xbShort NameSuffixMissing( xbShort, const char * );
00261 xbLong NoOfRecords( void );
00262 xbLong PhysicalNoOfRecords(void);
00263 xbShort OpenDatabase( const char * );
00264 xbShort PackDatabase(xbShort LockWaitOption,
00265 void (*packStatusFunc)(xbLong itemNum, xbLong numItems) = 0,
00266 void (*indexStatusFunc)(xbLong itemNum, xbLong numItems) = 0);
00267 xbShort PutRecord(void);
00268 xbShort PutRecord(xbULong);
00269 xbShort RebuildAllIndices(void (*statusFunc)(xbLong itemNum, xbLong numItems) = 0);
00270 xbShort RecordDeleted( void );
00272
00274 void ResetNoOfRecs( void ) { NoOfRecs = 0L; }
00275 xbShort SetVersion( xbShort );
00277
00279 xbShort UndeleteAllRecords( void ) { return DeleteAll(1); }
00280 xbShort UndeleteRecord( void );
00281 xbShort Zap( xbShort );
00282
00283
00284 const char *GetField(xbShort FieldNo) const;
00285 const char *GetField(const char *Name) const;
00286 xbShort GetField( xbShort FieldNo, char *Buf) const;
00287 xbShort GetRawField(const xbShort FieldNo, char *Buf) const;
00288 xbShort GetField( xbShort FieldNo, char *Buf, xbShort RecBufSw) const;
00289 xbShort GetField( const char *Name, char *Buf) const;
00290 xbShort GetRawField(const char *Name, char *Buf) const;
00291 xbShort GetField( const char *Name, char *Buf, xbShort RecBufSw) const;
00292 xbShort GetField(xbShort FieldNo, xbString&, xbShort RecBufSw ) const;
00293 xbShort GetFieldDecimal( const xbShort );
00294 xbShort GetFieldLen( const xbShort );
00295 char * GetFieldName( const xbShort );
00296 xbShort GetFieldNo( const char * FieldName ) const;
00297 char GetFieldType( const xbShort FieldNo ) const;
00298 xbShort GetLogicalField( const xbShort FieldNo );
00299 xbShort GetLogicalField( const char * FieldName );
00300
00301 char * GetStringField( const xbShort FieldNo );
00302 char * GetStringField( const char * FieldName );
00303
00304 xbShort PutField( const xbShort, const char * );
00305 xbShort PutRawField( const xbShort FieldNo, const char *buf );
00306 xbShort PutField( const char *Name, const char *buf);
00307 xbShort PutRawField( const char *Name, const char *buf );
00308 xbShort ValidLogicalData( const char * );
00309 xbShort ValidNumericData( const char * );
00310
00311 xbLong GetLongField( const char *FieldName) const;
00312 xbLong GetLongField( const xbShort FieldNo) const;
00313 xbShort PutLongField( const xbShort, const xbLong );
00314 xbShort PutLongField( const char *, const xbLong);
00315
00316 xbFloat GetFloatField( const char * FieldName );
00317 xbFloat GetFloatField( const xbShort FieldNo );
00318 xbShort PutFloatField( const char *, const xbFloat);
00319 xbShort PutFloatField( const xbShort, const xbFloat);
00320
00321 xbDouble GetDoubleField(const char *);
00322 xbDouble GetDoubleField(const xbShort, xbShort RecBufSw = 0);
00323 xbShort PutDoubleField(const char *, const xbDouble);
00324 xbShort PutDoubleField(const xbShort, const xbDouble);
00325
00326 #ifdef XB_LOCKING_ON
00327 xbShort LockDatabase( const xbShort, const xbShort, const xbULong );
00328 xbShort ExclusiveLock( const xbShort );
00329 xbShort ExclusiveUnlock( void );
00330
00331 #ifndef HAVE_FCNTL
00332 xbShort UnixToDosLockCommand( const xbShort WaitOption,
00333 const xbShort LockType ) const;
00334 #endif
00335
00336 #else
00337 xbShort LockDatabase( const xbShort, const xbShort, const xbLong )
00338 { return XB_NO_ERROR; }
00339 xbShort ExclusiveLock( const xbShort ) { return XB_NO_ERROR; };
00340 xbShort ExclusiveUnlock( void ) { return XB_NO_ERROR; };
00341 #endif
00342
00344
00346 void AutoLockOn( void ) { AutoLock = 1; }
00348
00350 void AutoLockOff( void ) { AutoLock = 0; }
00352
00354 xbShort GetAutoLock(void) { return AutoLock; }
00355
00356 #ifdef XB_MEMO_FIELDS
00357 xbShort GetMemoField( const xbShort FieldNo,const xbLong len,
00358 char * Buf, const xbShort LockOption );
00359 xbLong GetMemoFieldLen( const xbShort FieldNo );
00360 xbShort UpdateMemoData( const xbShort FieldNo, const xbLong len,
00361 const char * Buf, const xbShort LockOption );
00362 xbShort MemoFieldExists( const xbShort FieldNo ) const;
00363 xbShort LockMemoFile( const xbShort WaitOption, const xbShort LockType );
00364 xbShort MemoFieldsPresent( void ) const;
00365 xbLong CalcLastDataBlock();
00366 xbShort FindBlockSetInChain( const xbLong BlocksNeeded, const xbLong
00367 LastDataBlock, xbLong & Location, xbLong &PreviousNode );
00368 xbShort GetBlockSetFromChain( const xbLong BlocksNeeded, const xbLong
00369 Location, const xbLong PreviousNode );
00370
00371 #ifdef XBASE_DEBUG
00372 xbShort DumpMemoFreeChain( void );
00373 void DumpMemoHeader( void ) const;
00374 void DumpMemoBlock( void ) const;
00375 #endif
00376 #endif
00377
00379
00387 void RealDeleteOn(void) { RealDelete = 1; if(fp) ReadHeader(1); }
00390 void RealDeleteOff(void) { RealDelete = 0; if(fp) ReadHeader(1); }
00392
00396 xbShort GetRealDelete(void) { return RealDelete; }
00397
00398 #if defined(XB_INDEX_ANY)
00399 xbShort IndexCount(void);
00400 xbIndex *GetIndex(xbShort indexNum);
00401 #endif
00402
00403 protected:
00404 xbString DatabaseName;
00405 xbShort XFV;
00406 xbShort NoOfFields;
00407 char DbfStatus;
00408
00409
00410 FILE *fp;
00411 xbSchemaRec *SchemaPtr;
00412 char *RecBuf;
00413 char *RecBuf2;
00414
00415 #ifdef XB_MEMO_FIELDS
00416 FILE *mfp;
00417 void *mbb;
00418 xbMH MemoHeader;
00419
00420 xbShort mfield1;
00421 xbShort MStartPos;
00422 xbLong MFieldLen;
00423 xbLong NextFreeBlock;
00424 xbLong FreeBlockCnt;
00425
00426 xbLong MNextBlockNo;
00427 xbLong MNoOfFreeBlocks;
00428
00429 xbLong CurMemoBlockNo;
00430 #endif
00431
00432
00433
00434 char Version;
00435 char UpdateYY;
00436 char UpdateMM;
00437 char UpdateDD;
00438
00439
00440
00441
00442 xbULong NoOfRecs;
00443 xbUShort HeaderLen;
00444 xbUShort RecordLen;
00445
00446
00447 xbULong FirstFreeRec;
00448 xbULong RealNumRecs;
00449
00450
00451 xbIxList * MdxList;
00452 xbIxList * NdxList;
00453 xbIxList * FreeIxList;
00454 xbULong CurRec;
00455 xbShort AutoLock;
00456
00457
00458 xbShort RealDelete;
00459
00460
00461 #ifdef XB_LOCKING_ON
00462 xbShort CurLockType;
00463 xbShort CurLockCount;
00464 xbULong CurLockedRecNo;
00465 xbShort CurRecLockType;
00466 xbShort CurRecLockCount;
00467 xbShort CurMemoLockType;
00468 xbShort CurMemoLockCount;
00469 #endif
00470
00471 xbShort DeleteAll( xbShort );
00472 void InitVars( void );
00473 xbShort PackDatafiles(void (*statusFunc)(xbLong itemNum, xbLong numItems) = 0);
00474 xbShort ReadHeader( xbShort );
00475 xbShort WriteHeader( const xbShort );
00476
00477 #ifdef XB_MEMO_FIELDS
00478 xbShort AddMemoData( const xbShort FieldNo, const xbLong Len, const char * Buf );
00479 xbShort CreateMemoFile( void );
00480 xbShort DeleteMemoField( const xbShort FieldNo );
00481 xbShort GetDbtHeader( const xbShort Option );
00482 xbShort GetMemoBlockSize( void ) { return MemoHeader.BlockSize; }
00483 xbShort OpenMemoFile( void );
00484 xbShort PutMemoData( const xbLong StartBlock, const xbLong BlocksNeeded,
00485 const xbLong Len, const char * Buf );
00486 xbShort ReadMemoBlock( const xbLong BlockNo, const xbShort Option);
00487 xbShort SetMemoBlockSize( const xbShort );
00488 xbShort UpdateHeadNextNode( void ) const;
00489 xbShort WriteMemoBlock( const xbLong BlockNo, const xbShort Option );
00490 xbShort IsType3Dbt( void ) const { return( Version==(char)0x83 ? 1:0 ); }
00491 xbShort IsType4Dbt( void ) const
00492 {return (( Version==(char)0x8B || Version==(char)0x8E ) ? 1:0 );}
00493 #endif
00494 };
00495 #endif // __XB_DBF_H__
00496