Xbase Class Library  2.0.0
dbf.h
Go to the documentation of this file.
1 /* $Id: dbf.h,v 1.8 2001/01/13 20:20:53 dbryson Exp $
2 
3  Xbase project source code
4 
5  This file contains the Class definition for a xbDBF object.
6 
7  Copyright (C) 1997 StarTech, Gary A. Kunkel
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library 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 GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 
23  Contact:
24 
25  Mail:
26 
27  Technology Associates, Inc.
28  XBase Project
29  1455 Deming Way #11
30  Sparks, NV 89434
31  USA
32 
33  Email:
34 
35  xbase@techass.com
36 
37  See our website at:
38 
39  xdb.sourceforge.net
40 
41 
42  V 1.0 10/10/97 - Initial release of software
43  V 1.3 11/30/97 - Added memo field processing
44  V 1.6a 4/1/98 - Added expression support
45  V 1.6b 4/8/98 - Numeric index keys
46  V 1.7.4d 10/28/98 - Added support for OS2/DOS/Win/NT locking
47  V 1.8 11/29/98 - New class names and types
48  V 1.9.2 9/14/99 - Updated EOR and EOF processing
49 */
50 
51 
52 #ifndef __XB_DBF_H__
53 #define __XB_DBF_H__
54 
55 #ifdef __GNUG__
56 #pragma interface
57 #endif
58 
59 #ifdef __WIN32__
60 #include <xbase/xbconfigw32.h>
61 #else
62 #include <xbase/xbconfig.h>
63 #endif
64 
65 #include <xbase/xtypes.h>
66 #include <xbase/xdate.h>
67 
68 #include <iostream>
69 using namespace std;
70 #include <stdio.h>
71 
75 #if defined(XB_INDEX_ANY)
76  class XBDLLEXPORT xbIndex;
77  class XBDLLEXPORT xbNdx;
78  class XBDLLEXPORT xbNtx;
79 #endif
80 
81 /*****************************/
82 /* Field Types */
83 
84 #define XB_CHAR_FLD 'C'
85 #define XB_LOGICAL_FLD 'L'
86 #define XB_NUMERIC_FLD 'N'
87 #define XB_DATE_FLD 'D'
88 #define XB_MEMO_FLD 'M'
89 #define XB_FLOAT_FLD 'F'
90 
91 /*****************************/
92 /* File Status Codes */
93 
94 #define XB_CLOSED 0
95 #define XB_OPEN 1
96 #define XB_UPDATED 2
97 
98 /*****************************/
99 /* Other defines */
100 
101 #define XB_OVERLAY 1
102 #define XB_DONTOVERLAY 0
103 
104 #define XB_CHAREOF '\x1A' /* end of DBF */
105 #define XB_CHARHDR '\x0D' /* header terminator */
106 
108 
142  char FieldName[11];
143  char Type;
144 // xbUShort FieldLen; /* does not work */
145 // xbUShort NoOfDecs; /* does not work */
146  unsigned char FieldLen; /* fields are stored as one byte on record*/
147  unsigned char NoOfDecs;
148 };
149 
151 
155  char FieldName[11];
156  char Type; /* field type */
157  char *Address; /* pointer to field in record buffer 1 */
158 // xbUShort FieldLen; /* does not work */
159 // xbUShort NoOfDecs; /* does not work */
160  unsigned char FieldLen; /* fields are stored as one byte on record */
161  unsigned char NoOfDecs;
162  char *Address2; /* pointer to field in record buffer 2 */
163  char *fp; /* pointer to null terminated buffer for field */
164  /* see method GetString */
165  xbShort LongFieldLen; /* to handle long field lengths */
166 };
167 
169 
175 #if defined(XB_INDEX_ANY)
176  xbIndex * index;
177  xbShort Unique;
178  xbShort KeyUpdated;
179 #endif
180 };
181 
183 
187 #ifdef XB_MEMO_FIELDS
188 struct XBDLLEXPORT xbMH{ /* memo header */
189  xbLong NextBlock; /* pointer to next block to write */
190  char FileName[8]; /* name of dbt file */
191  char Version; /* not sure */
192  xbShort BlockSize; /* memo file block size */
193 };
194 #endif
195 
197 
202 
203 public:
204  xbDbf( xbXBase * );
205  xbXBase *xbase; /* linkage to main base class */
206 // char EofChar[10];
207 
208 /* datafile methods */
209 #if defined(XB_INDEX_ANY)
210  xbShort AddIndexToIxList(xbIndex *, const char *IndexName);
211  xbShort RemoveIndexFromIxList( xbIndex * );
212 #endif
213  xbShort AppendRecord( void );
214  xbShort BlankRecord( void );
215  xbLong CalcCheckSum( void );
216  xbShort CloseDatabase(bool deleteIndexes = 0);
217  xbShort CopyDbfStructure( const char *, xbShort );
218  xbShort CreateDatabase( const char * Name, xbSchema *, const xbShort Overlay );
220 
222  xbLong DbfTell( void ) { return ftell( fp ); }
224 
226  xbShort DeleteAllRecords( void ) { return DeleteAll(0); }
227  xbShort DeleteRecord( void );
228 #ifdef XBASE_DEBUG
229  xbShort DumpHeader( xbShort );
230 #endif
231  xbShort DumpRecord( xbULong );
233 
235  xbLong FieldCount( void ) { return NoOfFields; }
237 
239  xbString& GetDbfName( void ) { return DatabaseName; }
241 
243  xbShort GetDbfStatus( void ) { return DbfStatus; }
244  xbShort GetFirstRecord( void );
245  xbShort GetLastRecord( void );
246  xbShort GetNextRecord( void );
247  xbShort GetPrevRecord( void );
249 
251  xbLong GetCurRecNo( void ) { return CurRec; }
252  xbShort GetRecord( xbULong );
254 
256  char * GetRecordBuf( void ) { return RecBuf; }
258 
260  xbShort GetRecordLen( void ) { return RecordLen; }
261  xbShort NameSuffixMissing( xbShort, const char * );
262  xbLong NoOfRecords( void );
263  xbLong PhysicalNoOfRecords(void);
264  xbShort OpenDatabase( const char * );
265  xbShort PackDatabase(xbShort LockWaitOption,
266  void (*packStatusFunc)(xbLong itemNum, xbLong numItems) = 0,
267  void (*indexStatusFunc)(xbLong itemNum, xbLong numItems) = 0);
268  xbShort PutRecord(void); // Put record to current position
269  xbShort PutRecord(xbULong);
270  xbShort RebuildAllIndices(void (*statusFunc)(xbLong itemNum, xbLong numItems) = 0);
271  xbShort RecordDeleted( void );
273 
275  void ResetNoOfRecs( void ) { NoOfRecs = 0L; }
276  xbShort SetVersion( xbShort );
278 
280  xbShort UndeleteAllRecords( void ) { return DeleteAll(1); }
281  xbShort UndeleteRecord( void );
282  xbShort Zap( xbShort );
283 
284 /* field methods */
285  const char *GetField(xbShort FieldNo) const; // Using internal static buffer
286  const char *GetField(const char *Name) const;
287  xbShort GetField( xbShort FieldNo, char *Buf) const;
288  xbShort GetRawField(const xbShort FieldNo, char *Buf) const;
289  xbShort GetField( xbShort FieldNo, char *Buf, xbShort RecBufSw) const;
290  xbShort GetField( const char *Name, char *Buf) const;
291  xbShort GetRawField(const char *Name, char *Buf) const;
292  xbShort GetField( const char *Name, char *Buf, xbShort RecBufSw) const;
293  xbShort GetField(xbShort FieldNo, xbString&, xbShort RecBufSw ) const;
294  xbShort GetFieldDecimal( const xbShort );
295  xbShort GetFieldLen( const xbShort );
296  char * GetFieldName( const xbShort );
297  xbShort GetFieldNo( const char * FieldName ) const;
298  char GetFieldType( const xbShort FieldNo ) const;
299  xbShort GetLogicalField( const xbShort FieldNo );
300  xbShort GetLogicalField( const char * FieldName );
301 
302  char * GetStringField( const xbShort FieldNo );
303  char * GetStringField( const char * FieldName );
304 
305  xbShort PutField( const xbShort, const char * );
306  xbShort PutRawField( const xbShort FieldNo, const char *buf );
307  xbShort PutField( const char *Name, const char *buf);
308  xbShort PutRawField( const char *Name, const char *buf );
309  xbShort ValidLogicalData( const char * );
310  xbShort ValidNumericData( const char * );
311 
312  xbLong GetLongField( const char *FieldName) const;
313  xbLong GetLongField( const xbShort FieldNo) const;
314  xbShort PutLongField( const xbShort, const xbLong );
315  xbShort PutLongField( const char *, const xbLong);
316 
317  xbFloat GetFloatField( const char * FieldName );
318  xbFloat GetFloatField( const xbShort FieldNo );
319  xbShort PutFloatField( const char *, const xbFloat);
320  xbShort PutFloatField( const xbShort, const xbFloat);
321 
322  xbDouble GetDoubleField(const char *);
323  xbDouble GetDoubleField(const xbShort, xbShort RecBufSw = 0);
324  xbShort PutDoubleField(const char *, const xbDouble);
325  xbShort PutDoubleField(const xbShort, const xbDouble);
326 
327 #ifdef XB_LOCKING_ON
328  xbShort LockDatabase( const xbShort, const xbShort, const xbULong );
329  xbShort ExclusiveLock( const xbShort );
330  xbShort ExclusiveUnlock( void );
331 
332 #ifndef HAVE_FCNTL
333  xbShort UnixToDosLockCommand( const xbShort WaitOption,
334  const xbShort LockType ) const;
335 #endif
336 
337 #else
338  xbShort LockDatabase( const xbShort, const xbShort, const xbLong )
339  { return XB_NO_ERROR; }
340  xbShort ExclusiveLock( const xbShort ) { return XB_NO_ERROR; };
341  xbShort ExclusiveUnlock( void ) { return XB_NO_ERROR; };
342 #endif
343 
345 
347  void AutoLockOn( void ) { AutoLock = 1; }
349 
351  void AutoLockOff( void ) { AutoLock = 0; }
353 
355  xbShort GetAutoLock(void) { return AutoLock; }
356 
357 #ifdef XB_MEMO_FIELDS
358  xbShort GetMemoField( const xbShort FieldNo,const xbLong len,
359  char * Buf, const xbShort LockOption );
360  xbLong GetMemoFieldLen( const xbShort FieldNo );
361  xbShort UpdateMemoData( const xbShort FieldNo, const xbLong len,
362  const char * Buf, const xbShort LockOption );
363  xbShort MemoFieldExists( const xbShort FieldNo ) const;
364  xbShort LockMemoFile( const xbShort WaitOption, const xbShort LockType );
365  xbShort MemoFieldsPresent( void ) const;
366  xbLong CalcLastDataBlock();
367  xbShort FindBlockSetInChain( const xbLong BlocksNeeded, const xbLong
368  LastDataBlock, xbLong & Location, xbLong &PreviousNode );
369  xbShort GetBlockSetFromChain( const xbLong BlocksNeeded, const xbLong
370  Location, const xbLong PreviousNode );
371 
372 #ifdef XBASE_DEBUG
373  xbShort DumpMemoFreeChain( void );
374  void DumpMemoHeader( void ) const;
375  void DumpMemoBlock( void ) const;
376 #endif
377 #endif
378 
380 
388  void RealDeleteOn(void) { RealDelete = 1; if(fp) ReadHeader(1); }
391  void RealDeleteOff(void) { RealDelete = 0; if(fp) ReadHeader(1); }
393 
397  xbShort GetRealDelete(void) { return RealDelete; }
398 
399 #if defined(XB_INDEX_ANY)
400  xbShort IndexCount(void);
401  xbIndex *GetIndex(xbShort indexNum);
402 #endif
403 
404 protected:
406  xbShort XFV; /* xBASE file version */
408  char DbfStatus; /* 0 = closed
409  1 = open
410  2 = updates pending */
411  FILE *fp; /* file pointer */
412  xbSchemaRec *SchemaPtr; /* Pointer to field data */
413  char *RecBuf; /* Pointer to record buffer */
414  char *RecBuf2; /* Pointer to original rec buf */
415 
416 #ifdef XB_MEMO_FIELDS
417  FILE *mfp; /* memo file pointer */
418  void *mbb; /* memo block buffer */
419  xbMH MemoHeader; /* memo header structure */
420 
421  xbShort mfield1; /* memo block field one FF */
422  xbShort MStartPos; /* memo start pos of data */
423  xbLong MFieldLen; /* memo length of data */
424  xbLong NextFreeBlock; /* next free block in free chain */
425  xbLong FreeBlockCnt; /* count of free blocks this set */
426 
427  xbLong MNextBlockNo; /* free block chain */
428  xbLong MNoOfFreeBlocks; /* free block chain */
429 
430  xbLong CurMemoBlockNo; /* Current block no loaded */
431 #endif
432 
433 /* Next seven variables are read directly off the database header */
434 /* Don't change the order of the following seven items */
435  char Version;
436  char UpdateYY;
437  char UpdateMM;
438  char UpdateDD;
439 // xbLong NoOfRecs;
440 // xbShort HeaderLen;
441 // xbShort RecordLen;
442 
446 
447 //#ifdef XB_REAL_DELETE
450 //#endif
451 
455  xbULong CurRec; /* Current record or zero */
456  xbShort AutoLock; /* Auto update option 0 = off */
457 
458 //#ifdef XB_REAL_DELETE
459  xbShort RealDelete; /* real delete option 0 = off */
460 //#endif
461 
462 #ifdef XB_LOCKING_ON
463  xbShort CurLockType; /* current type of file lock */
464  xbShort CurLockCount; /* number of current file locks */
465  xbULong CurLockedRecNo; /* currently locked record no */
466  xbShort CurRecLockType; /* current type of rec lock held (F_RDLOCK or F_WRLCK) */
467  xbShort CurRecLockCount; /* number of current record locks */
468  xbShort CurMemoLockType; /* current type of memo lock */
469  xbShort CurMemoLockCount; /* number of current memo locks */
470 #endif
471 
472  xbShort DeleteAll( xbShort );
473  void InitVars( void );
474  xbShort PackDatafiles(void (*statusFunc)(xbLong itemNum, xbLong numItems) = 0);
475  xbShort ReadHeader( xbShort );
476  xbShort WriteHeader( const xbShort );
477 
478 #ifdef XB_MEMO_FIELDS
479  xbShort AddMemoData( const xbShort FieldNo, const xbLong Len, const char * Buf );
480  xbShort CreateMemoFile( void );
481  xbShort DeleteMemoField( const xbShort FieldNo );
482  xbShort GetDbtHeader( const xbShort Option );
483  xbShort GetMemoBlockSize( void ) { return MemoHeader.BlockSize; }
484  xbShort OpenMemoFile( void );
485  xbShort PutMemoData( const xbLong StartBlock, const xbLong BlocksNeeded,
486  const xbLong Len, const char * Buf );
487  xbShort ReadMemoBlock( const xbLong BlockNo, const xbShort Option);
488  xbShort SetMemoBlockSize( const xbShort );
489  xbShort UpdateHeadNextNode( void ) const;
490  xbShort WriteMemoBlock( const xbLong BlockNo, const xbShort Option );
491  xbShort IsType3Dbt( void ) const { return( Version==(char)0x83 ? 1:0 ); }
492  xbShort IsType4Dbt( void ) const
493  {return (( Version==(char)0x8B || Version==(char)0x8E ) ? 1:0 );}
494 #endif
495 };
496 #endif // __XB_DBF_H__
497 
xbShort GetRecordLen(void)
Return record length.
Definition: dbf.h:260
xbIxList * NextIx
Definition: dbf.h:173
char Version
Definition: dbf.h:435
unsigned char FieldLen
Definition: dbf.h:146
void RealDeleteOff(void)
Definition: dbf.h:391
void AutoLockOff(void)
Turn autolock off.
Definition: dbf.h:351
xbXBase class
Definition: xbase.h:138
double xbDouble
xbDouble type
Definition: xtypes.h:76
xbString & GetDbfName(void)
Return Dbf name.
Definition: dbf.h:239
xbULong NoOfRecs
Definition: dbf.h:443
xbShort NoOfFields
Definition: dbf.h:407
xbLong DbfTell(void)
Return the current position in the dbf file.
Definition: dbf.h:222
xbString DatabaseName
Definition: dbf.h:405
xbNtx class
Definition: ntx.h:119
xbNdx class
Definition: ndx.h:136
#define XBDLLEXPORT
Definition: xbase.h:101
unsigned char NoOfDecs
Definition: dbf.h:147
char UpdateDD
Definition: dbf.h:438
xbULong CurRec
Definition: dbf.h:455
class XBDLLEXPORT xbDbf
Definition: xbase.h:119
xbShort AutoLock
Definition: dbf.h:456
unsigned char NoOfDecs
Definition: dbf.h:161
xbString IxName
Definition: dbf.h:174
xbShort GetDbfStatus(void)
Return status.
Definition: dbf.h:243
char * Address
Definition: dbf.h:157
xbString class
Definition: xbstring.h:69
char * Address2
Definition: dbf.h:162
#define XB_NO_ERROR
Definition: retcodes.h:58
xbLong FieldCount(void)
Return number of fields.
Definition: dbf.h:235
xbShort DeleteAllRecords(void)
Delete all records.
Definition: dbf.h:226
xbShort XFV
Definition: dbf.h:406
char * fp
Definition: dbf.h:163
xbUShort HeaderLen
Definition: dbf.h:444
unsigned long int xbULong
xbULong type
Definition: xtypes.h:57
xbIxList struct
Definition: dbf.h:172
xbIndex class
Definition: index.h:67
float xbFloat
xbFloat type
Definition: xtypes.h:72
FILE * fp
Definition: dbf.h:411
xbShort ExclusiveLock(const xbShort)
Definition: dbf.h:340
xbShort RealDelete
Definition: dbf.h:459
xbShort LongFieldLen
Definition: dbf.h:165
Defines a field in an XBase file header (DBF file header)
Definition: dbf.h:154
xbShort GetRealDelete(void)
Return whether &quot;real&quot; deletes are on or off.
Definition: dbf.h:397
char DbfStatus
Definition: dbf.h:408
#define xbLong
Definition: xtypes.h:67
xbIxList * MdxList
Definition: dbf.h:452
short int xbShort
xbShort type
Definition: xtypes.h:65
xbShort GetAutoLock(void)
Return whether or not autolocking is on or off.
Definition: dbf.h:355
char * RecBuf
Definition: dbf.h:413
unsigned char FieldLen
Definition: dbf.h:160
xbXBase * xbase
Definition: dbf.h:205
char * RecBuf2
Definition: dbf.h:414
xbLong GetCurRecNo(void)
Return current record number.
Definition: dbf.h:251
xbShort LockDatabase(const xbShort, const xbShort, const xbLong)
Definition: dbf.h:338
char UpdateMM
Definition: dbf.h:437
char * GetRecordBuf(void)
Return a pointer to the record buffer.
Definition: dbf.h:256
xbIxList * NdxList
Definition: dbf.h:453
char Type
Definition: dbf.h:143
xbMH struct
Definition: dbf.h:201
xbUShort RecordLen
Definition: dbf.h:445
unsigned short int xbUShort
xbUShort type
Definition: xtypes.h:61
char UpdateYY
Definition: dbf.h:436
xbULong RealNumRecs
Definition: dbf.h:449
void AutoLockOn(void)
Turn autolock on.
Definition: dbf.h:347
void RealDeleteOn(void)
Turn on &quot;real&quot; deletes.
Definition: dbf.h:388
Used to define the fields in a database (DBF file).
Definition: dbf.h:141
void ResetNoOfRecs(void)
Set number of records to zero????
Definition: dbf.h:275
xbULong FirstFreeRec
Definition: dbf.h:448
char Type
Definition: dbf.h:156
xbShort ExclusiveUnlock(void)
Definition: dbf.h:341
xbShort UndeleteAllRecords(void)
Undelete all records.
Definition: dbf.h:280
xbSchemaRec * SchemaPtr
Definition: dbf.h:412
xbIxList * FreeIxList
Definition: dbf.h:454