Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

dbf.h

Go to the documentation of this file.
00001 /*  $Id: dbf.h,v 1.8 2001/01/13 20:20:53 dbryson Exp $
00002 
00003     Xbase project source code
00004 
00005     This file contains the Class definition for a xbDBF object.
00006 
00007     Copyright (C) 1997  StarTech, Gary A. Kunkel   
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public
00020     License along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     Contact:
00024 
00025       Mail:
00026 
00027         Technology Associates, Inc.
00028         XBase Project
00029         1455 Deming Way #11
00030         Sparks, NV 89434
00031         USA
00032 
00033       Email:
00034 
00035         xbase@techass.com
00036 
00037       See our website at:
00038 
00039         xdb.sourceforge.net
00040 
00041 
00042     V 1.0    10/10/97   - Initial release of software
00043     V 1.3    11/30/97   - Added memo field processing
00044     V 1.6a   4/1/98     - Added expression support
00045     V 1.6b   4/8/98     - Numeric index keys
00046     V 1.7.4d 10/28/98   - Added support for OS2/DOS/Win/NT locking
00047     V 1.8    11/29/98   - New class names and types 
00048     V 1.9.2  9/14/99    - Updated EOR and EOF processing
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 /* Field Types               */
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 /* File Status Codes         */
00092 
00093 #define XB_CLOSED  0
00094 #define XB_OPEN    1
00095 #define XB_UPDATED 2
00096 
00097 /*****************************/
00098 /* Other defines             */
00099 
00100 #define XB_OVERLAY     1
00101 #define XB_DONTOVERLAY 0
00102 
00103 #define XB_CHAREOF  '\x1A'         /* end of DBF        */
00104 #define XB_CHARHDR  '\x0D'         /* header terminator */
00105 
00107 
00140 struct XBDLLEXPORT xbSchema {
00141    char      FieldName[11];
00142    char      Type;
00143 // xbUShort  FieldLen;       /* does not work */
00144 // xbUShort  NoOfDecs;       /* does not work */
00145    unsigned  char FieldLen;  /* fields are stored as one byte on record*/
00146    unsigned  char NoOfDecs;
00147 };
00148 
00150 
00153 struct XBDLLEXPORT xbSchemaRec {
00154    char     FieldName[11];
00155    char     Type;            /* field type */
00156    char     *Address;        /* pointer to field in record buffer 1 */
00157 // xbUShort FieldLen;        /* does not work */
00158 // xbUShort NoOfDecs;        /* does not work */
00159    unsigned char FieldLen;   /* fields are stored as one byte on record */
00160    unsigned char NoOfDecs;
00161    char     *Address2;       /* pointer to field in record buffer 2 */
00162    char     *fp;             /* pointer to null terminated buffer for field */
00163                              /* see method GetString */
00164    xbShort  LongFieldLen;    /* to handle long field lengths */
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{                      /* memo header                    */
00188    xbLong  NextBlock;             /* pointer to next block to write */
00189    char    FileName[8];           /* name of dbt file               */
00190    char    Version;               /* not sure                       */
00191    xbShort BlockSize;             /* memo file block size           */
00192 };
00193 #endif
00194 
00196 
00200 class XBDLLEXPORT xbDbf {
00201 
00202 public:
00203    xbDbf( xbXBase * );
00204    xbXBase  *xbase;               /* linkage to main base class */
00205 //   char EofChar[10];
00206 
00207 /* datafile methods */
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); // Put record to current position
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 /* field methods */
00284    const char *GetField(xbShort FieldNo) const; // Using internal static buffer
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;                  /* xBASE file version            */
00406    xbShort  NoOfFields;
00407    char   DbfStatus;              /* 0 = closed
00408                                      1 = open
00409                                      2 = updates pending           */
00410    FILE   *fp;                    /* file pointer                  */
00411    xbSchemaRec *SchemaPtr;        /* Pointer to field data         */
00412    char   *RecBuf;                /* Pointer to record buffer      */
00413    char   *RecBuf2;               /* Pointer to original rec buf   */
00414 
00415 #ifdef XB_MEMO_FIELDS
00416    FILE    *mfp;                  /* memo file pointer             */
00417    void    *mbb;                  /* memo block buffer             */
00418    xbMH     MemoHeader;           /* memo header structure         */
00419 
00420    xbShort  mfield1;              /* memo block field one FF       */
00421    xbShort  MStartPos;            /* memo start pos of data        */
00422    xbLong   MFieldLen;            /* memo length of data           */
00423    xbLong   NextFreeBlock;        /* next free block in free chain */
00424    xbLong   FreeBlockCnt;         /* count of free blocks this set */
00425 
00426    xbLong   MNextBlockNo;         /* free block chain              */
00427    xbLong   MNoOfFreeBlocks;      /* free block chain              */
00428 
00429    xbLong   CurMemoBlockNo;       /* Current block no loaded       */
00430 #endif
00431 
00432 /* Next seven variables are read directly off the database header */
00433 /* Don't change the order of the following seven items            */
00434    char   Version;
00435    char   UpdateYY;
00436    char   UpdateMM;
00437    char   UpdateDD;
00438 //   xbLong   NoOfRecs;
00439 //   xbShort  HeaderLen;
00440 //   xbShort  RecordLen;
00441 
00442    xbULong  NoOfRecs;
00443    xbUShort HeaderLen;
00444    xbUShort RecordLen;
00445 
00446 //#ifdef XB_REAL_DELETE
00447    xbULong  FirstFreeRec;
00448    xbULong  RealNumRecs;
00449 //#endif
00450 
00451    xbIxList * MdxList;
00452    xbIxList * NdxList;
00453    xbIxList * FreeIxList;
00454    xbULong  CurRec;               /* Current record or zero   */
00455    xbShort  AutoLock;             /* Auto update option 0 = off  */
00456 
00457 //#ifdef XB_REAL_DELETE
00458    xbShort  RealDelete;           /* real delete option 0 = off */
00459 //#endif
00460 
00461 #ifdef XB_LOCKING_ON
00462    xbShort CurLockType;           /* current type of file lock */
00463    xbShort CurLockCount;          /* number of current file locks */
00464    xbULong CurLockedRecNo;        /* currently locked record no */
00465    xbShort CurRecLockType;        /* current type of rec lock held (F_RDLOCK or F_WRLCK) */
00466    xbShort CurRecLockCount;       /* number of current record locks */
00467    xbShort CurMemoLockType;       /* current type of memo lock */
00468    xbShort CurMemoLockCount;      /* number of current memo locks */
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 

Generated on Tue Feb 28 22:53:38 2006 for Xbase Class Library by  doxygen 1.4.4