Xbase64 Class Library  3.1.2
xbdbf.h
Go to the documentation of this file.
1 /* xbdbf.h
2 
3  Xbase64 project source code
4 
5  This file contains the Class definition for a xbDBF object.
6 
7  Copyright (C) 1997,2003 Gary A Kunkel
8 
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 
23 
24  Contact:
25 
26  Email:
27 
28  xdb-devel@lists.sourceforge.net
29  xdb-users@lists.sourceforge.net
30 
31 
32  Regular Mail:
33 
34  XBase Support
35  149C South Main St
36  Keller Texas, 76248
37  USA
38 
39 */
40 
41 #ifndef __XB_DBF_H__
42 #define __XB_DBF_H__
43 
44 #ifdef __GNU_LesserG__
45 #pragma interface
46 #endif
47 
48 #ifdef __WIN32__
49 #include <xbase64/xbwincfg.h>
50 #else
51 #include <xbase64/xbconfig.h>
52 #endif
53 
54 #include <xbase64/xbtypes.h>
55 #include <xbase64/xbdate.h>
56 #include <xbase64/xbfile.h>
57 
58 #include <iostream>
59 #include <stdio.h>
60 
64 #if defined(XB_INDEX_ANY)
65  class XBDLLEXPORT xbIndex;
66  class XBDLLEXPORT xbNdx;
67  class XBDLLEXPORT xbNtx;
68 #endif
69 
70 /*****************************/
71 /* Field Types */
72 
73 #define XB_CHAR_FLD 'C'
74 #define XB_LOGICAL_FLD 'L'
75 #define XB_NUMERIC_FLD 'N'
76 #define XB_DATE_FLD 'D'
77 #define XB_MEMO_FLD 'M'
78 #define XB_FLOAT_FLD 'F'
79 
80 /*****************************/
81 /* File Status Codes */
82 
83 #define XB_CLOSED 0
84 #define XB_OPEN 1
85 #define XB_UPDATED 2
86 
87 /*****************************/
88 /* Other defines */
89 
90 #define XB_OVERLAY 1
91 #define XB_DONTOVERLAY 0
92 
93 #define XB_CHAREOF '\x1A' /* end of DBF */
94 #define XB_CHARHDR '\x0D' /* header terminator */
95 
97 
131  char FieldName[11];
132  char Type;
133 // xbUShort FieldLen; /* does not work */
134 // xbUShort NoOfDecs; /* does not work */
135  unsigned char FieldLen; /* fields are stored as one byte on record*/
136  unsigned char NoOfDecs;
137 };
138 
140 
144  char FieldName[11];
145  char Type; /* field type */
146  char *Address; /* pointer to field in record buffer 1 */
147 // xbUShort FieldLen; /* does not work */
148 // xbUShort NoOfDecs; /* does not work */
149  unsigned char FieldLen; /* fields are stored as one byte on record */
150  unsigned char NoOfDecs;
151  char *Address2; /* pointer to field in record buffer 2 */
152  char *fp; /* pointer to null terminated buffer for field */
153  /* see method GetString */
154  xbShort LongFieldLen; /* to handle long field lengths */
155 };
156 
158 
164 #if defined(XB_INDEX_ANY)
165  xbIndex * index;
166  xbShort Unique;
167  xbShort KeyUpdated;
168 #endif
169 };
170 
172 
176 #ifdef XB_MEMO_FIELDS
177 struct XBDLLEXPORT xbMH{ /* memo header */
178  xbLong NextBlock; /* pointer to next block to write */
179  char FileName[8]; /* name of dbt file */
180  char Version; /* not sure */
181  xbShort BlockSize; /* memo file block size */
182 };
183 #endif
184 
186 
190 class XBDLLEXPORT xbDbf : protected xbFile{
191 
192 public:
193  xbDbf( xbXBase * );
194  virtual ~xbDbf();
195 
196  xbXBase *xbase; /* linkage to main base class */
197 
198 /* datafile methods */
199 #if defined(XB_INDEX_ANY)
200  xbShort AddIndexToIxList(xbIndex *, const char *IndexName);
201  xbShort RemoveIndexFromIxList( xbIndex * );
202 #endif
203  xbShort AppendRecord();
204  xbShort BlankRecord();
205  xbShort CloseDatabase( xbBool deleteIndexes = 0 );
206  xbShort CopyDbfStructure( const char *, xbShort );
207  xbShort CreateDatabase( const char * Name, xbSchema *, xbShort Overlay );
209 
211  xbShort DeleteAllRecords() { return DeleteAll(0); }
212  xbShort DeleteRecord();
213 #ifdef XBASE_DEBUG
214  xbShort DumpHeader( xbShort );
215 #endif
216  xbShort DumpRecord( xbULong );
218 
220  xbLong FieldCount() { return NoOfFields; }
222 
224  const xbString& GetDbfName() { return GetFileName(); }
226 
228  xbShort GetDbfStatus() { return DbfStatus; }
229  xbShort GetFirstRecord();
230  xbShort GetLastRecord();
231  xbShort GetNextRecord();
232  xbShort GetPrevRecord();
234 
236  xbLong GetCurRecNo() { return CurRec; }
237  xbShort GetRecord( xbULong );
239 
241  char * GetRecordBuf() { return RecBuf; }
243 
245  xbShort GetRecordLen() { return RecordLen; }
246  xbShort NameSuffixMissing( xbShort, const char * );
247  xbLong GetRecCnt() { return NoOfRecords(); }
248  xbLong NoOfRecords();
249  xbLong PhysicalNoOfRecords();
250  xbShort OpenDatabase( const char * );
251  xbShort PackDatabase(xbShort LockWaitOption,
252  void (*packStatusFunc)(xbLong itemNum, xbLong numItems) = 0,
253  void (*indexStatusFunc)(xbLong itemNum, xbLong numItems) = 0);
254  xbShort PutRecord(); // Put record to current position
255  xbShort PutRecord(xbULong);
256  xbShort RebuildAllIndices(
257  void (*statusFunc)(xbLong itemNum, xbLong numItems) = 0);
258  xbShort RecordDeleted();
260 
262  void ResetNoOfRecs() { NoOfRecs = 0L; }
263  xbShort SetVersion( xbShort );
265 
267  xbShort UndeleteAllRecords() { return DeleteAll(1); }
268  xbShort UndeleteRecord();
269  xbShort Zap( xbShort );
270 
271 /* field methods */
272  const char *GetField(xbShort FieldNo) const; // Using internal static buffer
273  const char *GetField(const char *Name) const;
274  xbShort GetField( xbShort FieldNo, char *Buf) const;
275  xbShort GetRawField( xbShort FieldNo, char *Buf) const;
276  xbShort GetField( xbShort FieldNo, char *Buf, xbShort RecBufSw) const;
277  xbShort GetField( const char *Name, char *Buf) const;
278  xbShort GetRawField(const char *Name, char *Buf) const;
279  xbShort GetField( const char *Name, char *Buf, xbShort RecBufSw) const;
280  xbShort GetField(xbShort FieldNo, xbString&, xbShort RecBufSw ) const;
281  xbShort GetFieldDecimal( xbShort );
282  xbShort GetFieldLen( xbShort );
283  char * GetFieldName( xbShort );
284  xbShort GetFieldNo( const char * FieldName ) const;
285  char GetFieldType( xbShort FieldNo ) const;
286  xbShort GetLogicalField( xbShort FieldNo );
287  xbShort GetLogicalField( const char * FieldName );
288  char * GetStringField( xbShort FieldNo );
289  char * GetStringField( const char * FieldName );
290  xbShort PutField( xbShort, const char * );
291  xbShort PutRawField( xbShort FieldNo, const char *buf );
292  xbShort PutField( const char *Name, const char *buf);
293  xbShort PutRawField( const char *Name, const char *buf );
294  xbShort ValidLogicalData( const char * );
295  xbShort ValidNumericData( const char * );
296 
297  xbLong GetLongField( const char *FieldName) const;
298  xbLong GetLongField( const xbShort FieldNo) const;
299  xbShort PutLongField( const xbShort, const xbLong );
300  xbShort PutLongField( const char *, const xbLong);
301 
302  xbFloat GetFloatField( const char * FieldName );
303  xbFloat GetFloatField( xbShort FieldNo );
304  xbShort PutFloatField( const char *, const xbFloat);
305  xbShort PutFloatField( const xbShort, const xbFloat);
306 
307  xbDouble GetDoubleField( const char *);
308  xbDouble GetDoubleField( xbShort, xbShort RecBufSw = 0);
309  xbShort PutDoubleField( const char *, xbDouble);
310  xbShort PutDoubleField( const xbShort, xbDouble);
311 
312 #ifdef XB_LOCKING_ON
313  xbShort GetLockMode() { return LockMode; }
314  xbShort SetLockMode( xbShort );
315 // xbShort OpenXbLockFile();
316 // xbShort GetTableLockCnt() { return TableLockCnt; }
317 // xbShort LockIndex( xbShort LockType ); /* for XB_XBASE_LOCK_MODE */
318  int GetDbfFileNo() { return fileno( fp ); }
319  int GetMemoFileNo() { return fileno( mfp ); }
320 
321 #ifdef XB_MEMO_FIELDS
322 // xbShort GetMemoLockCnt() { return MemoLockCnt; }
323 #endif
324 
325 /*
326  xbShort LockTable( xbShort LockType );
327  xbShort LockXbaseTable( xbShort LockType );
328  xbShort LockClipperTable( xbShort LockType );
329  xbShort LockFoxproTable( xbShort LockType );
330  xbShort LockDbaseTable( xbShort LockType );
331 
332  xbShort LockRecord( xbShort LockType, xbULong RecNo, xbULong RecCnt );
333  xbShort LockXbaseRecord( xbShort LockType, xbULong RecNo, xbULong RecCnt );
334  xbShort LockClipperRecord(
335  xbShort LockType, xbULong RecNo, xbULong RecCnt );
336  xbShort LockFoxproRecord( xbShort LockType, xbULong RecNo, xbULong RecCnt );
337  xbShort LockDbaseRecord( xbShort LockType, xbULong RecNo, xbULong RecCnt );
338 
339  xbShort LockDatabase( xbShort, xbShort, xbULong );
340  xbShort ExclusiveLock( xbShort );
341  xbShort ExclusiveUnlock();
342  xbShort LockDatabase( xbShort cmd, xbULong recNo ) { return 0; }
343 */
344 
345 #ifndef HAVE_FCNTL
346  xbShort UnixToDosLockCommand( xbShort WaitOption,
347  xbShort LockType ) const;
348 #endif
349 
350 #else
352  { return XB_NO_ERROR; }
355 #endif
356 
358 
360  void AutoLockOn() { AutoLock = 1; }
362 
364  void AutoLockOff() { AutoLock = 0; }
366 
368  xbShort GetAutoLock() { return AutoLock; }
369 
370 #ifdef XB_MEMO_FIELDS
371  xbShort GetMemoField( xbShort FieldNo, xbLong len,
372  char * Buf, xbShort LockOption );
373  xbLong GetMemoFieldLen( xbShort FieldNo );
374  xbShort GetFPTField( xbShort FieldNo, xbLong len,
375  char * Buf, xbShort LockOption );
376  xbLong GetFPTFieldLen( xbShort FieldNo );
377  xbShort UpdateMemoData( xbShort FieldNo, xbLong len,
378  const char * Buf, xbShort LockOption );
379  xbShort MemoFieldExists( xbShort FieldNo ) const;
380  xbShort LockMemoFile( xbShort WaitOption, xbShort LockType );
381 
382  xbShort MemoFieldsPresent() const;
383  xbLong CalcLastDataBlock();
384  xbShort FindBlockSetInChain( xbLong BlocksNeeded, xbLong
385  LastDataBlock, xbLong & Location, xbLong &PreviousNode );
386  xbShort GetBlockSetFromChain( xbLong BlocksNeeded, xbLong
387  Location, xbLong PreviousNode );
388  xbString & GetDbtName() { return MemofileName; }
389 
390 #ifdef XBASE_DEBUG
391  xbShort DumpMemoFreeChain();
392  void DumpMemoHeader() const;
393  void DumpMemoBlock() const;
394 #endif
395 #endif
396 
398 
406  void RealDeleteOn() { RealDelete = 1; if(fp) ReadHeader(1); }
409  void RealDeleteOff() { RealDelete = 0; if(fp) ReadHeader(1); }
411 
415  xbShort GetRealDelete() { return RealDelete; }
416 
417 #if defined(XB_INDEX_ANY)
418  xbShort IndexCount();
419  xbIndex *GetIndex(xbShort indexNum);
420 #endif
421 
422  void Flush();
423  virtual const char* GetExtWithDot( bool lower );
424 
425  private:
426 
427  xbShort DeleteAll( xbShort );
428  void InitVars();
429  xbShort PackDatafiles(void (*statusFunc)(xbLong itemNum, xbLong numItems) = 0);
430  xbShort ReadHeader( xbShort );
431  xbShort WriteHeader( xbShort );
432 
433 #ifdef XB_MEMO_FIELDS
434  xbShort AddMemoData( xbShort FieldNo, xbLong Len, const char * Buf );
435  xbShort CreateMemoFile();
436  xbShort DeleteMemoField( xbShort FieldNo );
437  xbShort GetDbtHeader( xbShort Option );
438  xbShort GetMemoBlockSize() { return MemoHeader.BlockSize; }
439  xbShort OpenMemoFile();
440  xbShort OpenFPTFile();
441  xbShort PutMemoData( xbLong StartBlock, xbLong BlocksNeeded,
442  xbLong Len, const char * Buf );
443  xbShort ReadMemoBlock( xbLong BlockNo, xbShort Option);
444  xbShort SetMemoBlockSize( xbShort );
445  xbShort UpdateHeadNextNode() const;
446  xbShort WriteMemoBlock( xbLong BlockNo, xbShort Option );
447  xbShort IsType3Dbt() const { return( Version==(char)0x83 ? 1:0 ); }
448  xbShort IsType4Dbt() const
449  {return (( Version==(char)0x8B || Version==(char)0x8E ) ? 1:0 );}
450  xbShort CreateUniqueDbfName( xbString &, xbString & );
451 #endif
452 
453 
454 // xbString DatabaseName;
455  xbShort XFV; /* xBASE file version */
456  xbShort NoOfFields;
457  char DbfStatus; /* 0 = closed
458  1 = open
459  2 = updates pending */
460  FILE *fp; /* file pointer */
461  xbSchemaRec *SchemaPtr; /* Pointer to field data */
462  char *RecBuf; /* Pointer to record buffer */
463  char *RecBuf2; /* Pointer to original rec buf */
464 
465 #ifdef XB_MEMO_FIELDS
466  xbString MemofileName; /* memo file name */
467  FILE *mfp; /* memo file pointer */
468  void *mbb; /* memo block buffer */
469  xbMH MemoHeader; /* memo header structure */
470  xbShort mfield1; /* memo block field one FF */
471  xbShort MStartPos; /* memo start pos of data */
472  xbLong MFieldLen; /* memo length of data */
473  xbLong NextFreeBlock; /* next free block in free chain */
474  xbLong FreeBlockCnt; /* count of free blocks this set */
475  xbLong MNextBlockNo; /* free block chain */
476  xbLong MNoOfFreeBlocks; /* free block chain */
477  xbLong CurMemoBlockNo; /* Current block no loaded */
478 #endif
479 
480 /* Next seven variables are read directly off the database header */
481 /* Don't change the order of the following seven items */
482  char Version;
483  char UpdateYY;
484  char UpdateMM;
485  char UpdateDD;
486 // xbLong NoOfRecs;
487 // xbShort HeaderLen;
488 // xbShort RecordLen;
489 
490  xbULong NoOfRecs;
491  xbUShort HeaderLen;
492  xbUShort RecordLen;
493 
494 //#ifdef XB_REAL_DELETE
495  xbULong FirstFreeRec;
496  xbULong RealNumRecs;
497 //#endif
498 
499 // xbIxList * MdxList;
500  xbIxList * NdxList;
501  xbIxList * FreeIxList;
502  xbULong CurRec; /* Current record or zero */
503  xbShort AutoLock; /* Auto update option 0 = off */
504 
505 //#ifdef XB_REAL_DELETE
506  xbShort RealDelete; /* real delete option 0 = off */
507 //#endif
508 
509 #ifdef XB_LOCKING_ON
510  FILE *xblfh; /* xbase lock file pointer for xbase locking */
511  xbShort LockMode; /* lock mode for this table */
512  xbString lfn; /* xbase lock file name for xbase locking */
513  xbShort TableLockCnt; /* number of table locks */
514  xbShort IndexLockCnt; /* no of index locks XB_XBASE_LOCK_MODE only */
515 
516 #ifdef XB_MEMO_FIELDS
517  xbShort MemoLockCnt; /* number of memo file locks */
518 #endif
519 
520  /* old locking stuff */
521  xbShort CurLockType; /* current type of file lock */
522  xbShort CurLockCount; /* number of current file locks */
523  xbULong CurLockedRecNo; /* currently locked record no */
524  xbShort CurRecLockType; /* current type of rec lock held (F_RDLOCK or F_WRLCK) */
525  xbShort CurRecLockCount; /* number of current record locks */
526  xbShort CurMemoLockType; /* current type of memo lock */
527  xbShort CurMemoLockCount; /* number of current memo locks */
528 #endif
529 
530 };
531 #endif // __XB_DBF_H__
532 
533 
xbMH struct
Definition: xbdbf.h:190
void RealDeleteOff()
Definition: xbdbf.h:409
void RealDeleteOn()
Turn on "real" deletes.
Definition: xbdbf.h:406
void AutoLockOff()
Turn autolock off.
Definition: xbdbf.h:364
const xbString & GetDbfName()
Return Dbf name.
Definition: xbdbf.h:224
xbShort ExclusiveUnlock()
Definition: xbdbf.h:354
void ResetNoOfRecs()
Set number of records to zero????
Definition: xbdbf.h:262
xbShort GetDbfStatus()
Return status.
Definition: xbdbf.h:228
void AutoLockOn()
Turn autolock on.
Definition: xbdbf.h:360
xbShort GetRealDelete()
Return whether "real" deletes are on or off.
Definition: xbdbf.h:415
xbShort ExclusiveLock(xbShort)
Definition: xbdbf.h:353
xbShort DeleteAllRecords()
Delete all records.
Definition: xbdbf.h:211
xbShort LockDatabase(xbShort, xbShort, xbLong)
Definition: xbdbf.h:351
xbShort UndeleteAllRecords()
Undelete all records.
Definition: xbdbf.h:267
char * GetRecordBuf()
Return a pointer to the record buffer.
Definition: xbdbf.h:241
xbShort GetRecordLen()
Return record length.
Definition: xbdbf.h:245
xbXBase * xbase
Definition: xbdbf.h:196
xbShort GetAutoLock()
Return whether or not autolocking is on or off.
Definition: xbdbf.h:368
xbLong GetCurRecNo()
Return current record number.
Definition: xbdbf.h:236
xbLong GetRecCnt()
Definition: xbdbf.h:247
xbLong FieldCount()
Return number of fields.
Definition: xbdbf.h:220
Definition: xbfile.h:53
xbIndex class
Definition: xbindex.h:63
xbNdx class
Definition: xbndx.h:126
xbNtx class
Definition: xbntx.h:116
xbString class
Definition: xbstring.h:63
xbXBase class
Definition: xbase64.h:152
xbIxList struct
Definition: xbdbf.h:161
xbString IxName
Definition: xbdbf.h:163
xbIxList * NextIx
Definition: xbdbf.h:162
Defines a field in an XBase file header (DBF file header)
Definition: xbdbf.h:143
xbShort LongFieldLen
Definition: xbdbf.h:154
char * Address2
Definition: xbdbf.h:151
unsigned char NoOfDecs
Definition: xbdbf.h:150
char * fp
Definition: xbdbf.h:152
char Type
Definition: xbdbf.h:145
unsigned char FieldLen
Definition: xbdbf.h:149
char * Address
Definition: xbdbf.h:146
Used to define the fields in a database (DBF file).
Definition: xbdbf.h:130
unsigned char NoOfDecs
Definition: xbdbf.h:136
char Type
Definition: xbdbf.h:132
unsigned char FieldLen
Definition: xbdbf.h:135
class XBDLLEXPORT xbDbf
Definition: xbase64.h:138
#define XBDLLEXPORT
Definition: xbase64.h:86
#define XB_NO_ERROR
Definition: xbretcod.h:50
long xbLong
Definition: xbtypes.h:61
short int xbBool
xbBool type
Definition: xbtypes.h:78
short int xbShort
xbShort type
Definition: xbtypes.h:60
unsigned short int xbUShort
xbUShort type
Definition: xbtypes.h:55
double xbDouble
xbDouble type
Definition: xbtypes.h:73
unsigned long int xbULong
xbULong type
Definition: xbtypes.h:50
float xbFloat
xbFloat type
Definition: xbtypes.h:67