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
#ifndef __XB_DBF_H__
00047
#define __XB_DBF_H__
00048
00049
#ifdef __GNUG__
00050
#pragma interface
00051
#endif
00052
00053
#ifdef __WIN32__
00054
#include <xbase/xbconfigw32.h>
00055
#else
00056
#include <xbase/xbconfig.h>
00057
#endif
00058
00059
#include <xbase/xtypes.h>
00060
#include <xbase/xdate.h>
00061
00062
#include <iostream>
00063
#include <stdio.h>
00064
00068
#if defined(XB_INDEX_ANY)
00069
class XBDLLEXPORT xbIndex;
00070
class XBDLLEXPORT xbNdx;
00071
class XBDLLEXPORT xbNtx;
00072
#endif
00073
00074
00075
00076
00077 #define XB_CHAR_FLD 'C'
00078 #define XB_LOGICAL_FLD 'L'
00079 #define XB_NUMERIC_FLD 'N'
00080 #define XB_DATE_FLD 'D'
00081 #define XB_MEMO_FLD 'M'
00082 #define XB_FLOAT_FLD 'F'
00083
00084
00085
00086
00087 #define XB_CLOSED 0
00088 #define XB_OPEN 1
00089 #define XB_UPDATED 2
00090
00091
00092
00093
00094 #define XB_OVERLAY 1
00095 #define XB_DONTOVERLAY 0
00096
00097 #define XB_CHAREOF '\x1A'
00098 #define XB_CHARHDR '\x0D'
00099
00101
00134 struct XBDLLEXPORT xbSchema {
00135 char FieldName[11];
00136 char Type;
00137
00138
00139 unsigned char FieldLen;
00140 unsigned char NoOfDecs;
00141 };
00142
00144
00147 struct XBDLLEXPORT xbSchemaRec {
00148 char FieldName[11];
00149 char Type;
00150 char *Address;
00151
00152
00153 unsigned char FieldLen;
00154 unsigned char NoOfDecs;
00155 char *Address2;
00156 char *fp;
00157
00158 xbShort LongFieldLen;
00159 };
00160
00162
00165 struct XBDLLEXPORT xbIxList {
00166 xbIxList * NextIx;
00167 xbString IxName;
00168
#if defined(XB_INDEX_ANY)
00169
xbIndex * index;
00170
xbShort Unique;
00171
xbShort KeyUpdated;
00172
#endif
00173
};
00174
00176
00180
#ifdef XB_MEMO_FIELDS
00181
struct XBDLLEXPORT xbMH{
00182
xbLong NextBlock;
00183
char FileName[8];
00184
char Version;
00185
xbShort BlockSize;
00186 };
00187
#endif
00188
00190
00194 class XBDLLEXPORT xbDbf {
00195
00196
public:
00197
xbDbf(
xbXBase * );
00198
virtual ~
xbDbf();
00199
00200 xbXBase *xbase;
00201
00202
00203
00204
#if defined(XB_INDEX_ANY)
00205
xbShort AddIndexToIxList(xbIndex *,
const char *IndexName);
00206
xbShort RemoveIndexFromIxList( xbIndex * );
00207
#endif
00208
xbShort AppendRecord(
void );
00209
xbShort BlankRecord(
void );
00210
xbLong CalcCheckSum(
void );
00211
xbShort CloseDatabase(
bool deleteIndexes = 0);
00212
xbShort CopyDbfStructure(
const char *, xbShort );
00213
xbShort CreateDatabase(
const char * Name, xbSchema *,
const xbShort Overlay );
00215
00217 xbLong DbfTell(
void ) {
return ftell( fp ); }
00219
00221 xbShort DeleteAllRecords(
void ) {
return DeleteAll(0); }
00222
xbShort DeleteRecord(
void );
00223
#ifdef XBASE_DEBUG
00224
xbShort DumpHeader( xbShort );
00225
#endif
00226
xbShort DumpRecord(
xbULong );
00228
00230 xbLong FieldCount(
void ) {
return NoOfFields; }
00232
00234 xbString& GetDbfName(
void ) {
return DatabaseName; }
00236
00238 xbShort GetDbfStatus(
void ) {
return DbfStatus; }
00239
xbShort GetFirstRecord(
void );
00240
xbShort GetLastRecord(
void );
00241
xbShort GetNextRecord(
void );
00242
xbShort GetPrevRecord(
void );
00244
00246 xbLong GetCurRecNo(
void ) {
return CurRec; }
00247
xbShort GetRecord( xbULong );
00249
00251 char * GetRecordBuf(
void ) {
return RecBuf; }
00253
00255 xbShort GetRecordLen(
void ) {
return RecordLen; }
00256
xbShort NameSuffixMissing( xbShort,
const char * );
00257
xbLong NoOfRecords(
void );
00258
xbLong PhysicalNoOfRecords(
void);
00259
xbShort OpenDatabase(
const char * );
00260
xbShort PackDatabase(xbShort LockWaitOption,
00261
void (*packStatusFunc)(xbLong itemNum, xbLong numItems) = 0,
00262
void (*indexStatusFunc)(xbLong itemNum, xbLong numItems) = 0);
00263
xbShort PutRecord(
void);
00264
xbShort PutRecord(xbULong);
00265
xbShort RebuildAllIndices(
void (*statusFunc)(xbLong itemNum, xbLong numItems) = 0);
00266
xbShort RecordDeleted(
void );
00268
00270 void ResetNoOfRecs(
void ) { NoOfRecs = 0L; }
00271
xbShort SetVersion( xbShort );
00273
00275 xbShort UndeleteAllRecords(
void ) {
return DeleteAll(1); }
00276
xbShort UndeleteRecord(
void );
00277
xbShort Zap( xbShort );
00278
00279
00280
const char *GetField(xbShort FieldNo)
const;
00281
const char *GetField(
const char *Name)
const;
00282
xbShort GetField( xbShort FieldNo,
char *Buf)
const;
00283
xbShort GetRawField(
const xbShort FieldNo,
char *Buf)
const;
00284
xbShort GetField( xbShort FieldNo,
char *Buf, xbShort RecBufSw)
const;
00285
xbShort GetField(
const char *Name,
char *Buf)
const;
00286
xbShort GetRawField(
const char *Name,
char *Buf)
const;
00287
xbShort GetField(
const char *Name,
char *Buf, xbShort RecBufSw)
const;
00288
xbShort GetField(xbShort FieldNo,
xbString&, xbShort RecBufSw )
const;
00289
xbShort GetFieldDecimal(
const xbShort );
00290
xbShort GetFieldLen(
const xbShort );
00291
char * GetFieldName(
const xbShort );
00292
xbShort GetFieldNo(
const char * FieldName )
const;
00293
char GetFieldType(
const xbShort FieldNo )
const;
00294
xbShort GetLogicalField(
const xbShort FieldNo );
00295
xbShort GetLogicalField(
const char * FieldName );
00296
00297
char * GetStringField(
const xbShort FieldNo );
00298
char * GetStringField(
const char * FieldName );
00299
00300
xbShort PutField(
const xbShort,
const char * );
00301
xbShort PutRawField(
const xbShort FieldNo,
const char *buf );
00302
xbShort PutField(
const char *Name,
const char *buf);
00303
xbShort PutRawField(
const char *Name,
const char *buf );
00304
xbShort ValidLogicalData(
const char * );
00305
xbShort ValidNumericData(
const char * );
00306
00307
xbLong GetLongField(
const char *FieldName)
const;
00308
xbLong GetLongField(
const xbShort FieldNo)
const;
00309
xbShort PutLongField(
const xbShort,
const xbLong );
00310
xbShort PutLongField(
const char *,
const xbLong);
00311
00312
xbFloat GetFloatField(
const char * FieldName );
00313
xbFloat GetFloatField(
const xbShort FieldNo );
00314
xbShort PutFloatField(
const char *,
const xbFloat);
00315
xbShort PutFloatField(
const xbShort,
const xbFloat);
00316
00317
xbDouble GetDoubleField(
const char *);
00318
xbDouble GetDoubleField(
const xbShort, xbShort RecBufSw = 0);
00319
xbShort PutDoubleField(
const char *,
const xbDouble);
00320
xbShort PutDoubleField(
const xbShort,
const xbDouble);
00321
00322
#ifdef XB_LOCKING_ON
00323
xbShort LockDatabase(
const xbShort,
const xbShort,
const xbULong );
00324
xbShort ExclusiveLock(
const xbShort );
00325
xbShort ExclusiveUnlock(
void );
00326
00327
#ifndef HAVE_FCNTL
00328
xbShort UnixToDosLockCommand(
const xbShort WaitOption,
00329
const xbShort LockType )
const;
00330
#endif
00331
00332
#else
00333 xbShort LockDatabase(
const xbShort,
const xbShort,
const xbLong )
00334 {
return XB_NO_ERROR; }
00335 xbShort ExclusiveLock(
const xbShort ) {
return XB_NO_ERROR; };
00336 xbShort ExclusiveUnlock(
void ) {
return XB_NO_ERROR; };
00337
#endif
00338
00340
00342 void AutoLockOn(
void ) { AutoLock = 1; }
00344
00346 void AutoLockOff(
void ) { AutoLock = 0; }
00348
00350 xbShort GetAutoLock(
void) {
return AutoLock; }
00351
00352
#ifdef XB_MEMO_FIELDS
00353
xbShort GetMemoField(
const xbShort FieldNo,
const xbLong len,
00354
char * Buf,
const xbShort LockOption );
00355
xbLong GetMemoFieldLen(
const xbShort FieldNo );
00356
xbShort GetFPTField(
const xbShort FieldNo,
const xbLong len,
00357
char * Buf,
const xbShort LockOption );
00358
xbLong GetFPTFieldLen(
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
void Flush();
00403
00404
protected:
00405 xbString DatabaseName;
00406 xbShort XFV;
00407 xbShort NoOfFields;
00408 char DbfStatus;
00409
00410
00411 FILE *fp;
00412 xbSchemaRec *SchemaPtr;
00413 char *RecBuf;
00414 char *RecBuf2;
00415
00416
#ifdef XB_MEMO_FIELDS
00417
FILE *mfp;
00418
void *mbb;
00419 xbMH MemoHeader;
00420
00421
xbShort mfield1;
00422
xbShort MStartPos;
00423
xbLong MFieldLen;
00424
xbLong NextFreeBlock;
00425
xbLong FreeBlockCnt;
00426
00427
xbLong MNextBlockNo;
00428
xbLong MNoOfFreeBlocks;
00429
00430
xbLong CurMemoBlockNo;
00431
#endif
00432
00433
00434
00435 char Version;
00436 char UpdateYY;
00437 char UpdateMM;
00438 char UpdateDD;
00439
00440
00441
00442
00443 xbULong NoOfRecs;
00444 xbUShort HeaderLen;
00445 xbUShort RecordLen;
00446
00447
00448 xbULong FirstFreeRec;
00449 xbULong RealNumRecs;
00450
00451
00452 xbIxList * MdxList;
00453 xbIxList * NdxList;
00454 xbIxList * FreeIxList;
00455 xbULong CurRec;
00456 xbShort AutoLock;
00457
00458
00459 xbShort RealDelete;
00460
00461
00462
#ifdef XB_LOCKING_ON
00463
xbShort CurLockType;
00464
xbShort CurLockCount;
00465
xbULong CurLockedRecNo;
00466
xbShort CurRecLockType;
00467
xbShort CurRecLockCount;
00468
xbShort CurMemoLockType;
00469
xbShort CurMemoLockCount;
00470
#endif
00471
00472
xbShort DeleteAll( xbShort );
00473
void InitVars(
void );
00474
xbShort PackDatafiles(
void (*statusFunc)(xbLong itemNum, xbLong numItems) = 0);
00475
xbShort ReadHeader( xbShort );
00476
xbShort WriteHeader(
const xbShort );
00477
00478
#ifdef XB_MEMO_FIELDS
00479
xbShort AddMemoData(
const xbShort FieldNo,
const xbLong Len,
const char * Buf );
00480
xbShort CreateMemoFile(
void );
00481
xbShort DeleteMemoField(
const xbShort FieldNo );
00482
xbShort GetDbtHeader(
const xbShort Option );
00483
xbShort GetMemoBlockSize(
void ) {
return MemoHeader.BlockSize; }
00484
xbShort OpenMemoFile(
void );
00485
xbShort OpenFPTFile(
void);
00486
xbShort PutMemoData(
const xbLong StartBlock,
const xbLong BlocksNeeded,
00487
const xbLong Len,
const char * Buf );
00488
xbShort ReadMemoBlock(
const xbLong BlockNo,
const xbShort Option);
00489
xbShort SetMemoBlockSize(
const xbShort );
00490
xbShort UpdateHeadNextNode(
void ) const;
00491 xbShort WriteMemoBlock( const xbLong BlockNo, const xbShort Option );
00492 xbShort IsType3Dbt(
void )
const {
return( Version==(
char)0x83 ? 1:0 ); }
00493
xbShort IsType4Dbt(
void )
const
00494
{
return (( Version==(
char)0x8B || Version==(
char)0x8E ) ? 1:0 );}
00495
#endif
00496
};
00497
#endif // __XB_DBF_H__
00498
00499