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.15 2003/08/16 19:59:39 gkunkel Exp $
00002 
00003     Xbase project source code
00004 
00005     This file contains the Class definition for a xbDBF object.
00006 
00007     Copyright (C) 1997  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         xdb-devel@lists.sourceforge.net
00037         xdb-users@lists.sourceforge.net
00038 
00039       See our website at:
00040 
00041         xdb.sourceforge.net
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 /* Field Types               */
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 /* File Status Codes         */
00086 
00087 #define XB_CLOSED  0
00088 #define XB_OPEN    1
00089 #define XB_UPDATED 2
00090 
00091 /*****************************/
00092 /* Other defines             */
00093 
00094 #define XB_OVERLAY     1
00095 #define XB_DONTOVERLAY 0
00096 
00097 #define XB_CHAREOF  '\x1A'         /* end of DBF        */
00098 #define XB_CHARHDR  '\x0D'         /* header terminator */
00099 
00101 
00134 struct XBDLLEXPORT xbSchema {
00135    char      FieldName[11];
00136    char      Type;
00137 // xbUShort  FieldLen;       /* does not work */
00138 // xbUShort  NoOfDecs;       /* does not work */
00139    unsigned  char FieldLen;  /* fields are stored as one byte on record*/
00140    unsigned  char NoOfDecs;
00141 };
00142 
00144 
00147 struct XBDLLEXPORT xbSchemaRec {
00148    char     FieldName[11];
00149    char     Type;            /* field type */
00150    char     *Address;        /* pointer to field in record buffer 1 */
00151 // xbUShort FieldLen;        /* does not work */
00152 // xbUShort NoOfDecs;        /* does not work */
00153    unsigned char FieldLen;   /* fields are stored as one byte on record */
00154    unsigned char NoOfDecs;
00155    char     *Address2;       /* pointer to field in record buffer 2 */
00156    char     *fp;             /* pointer to null terminated buffer for field */
00157                              /* see method GetString */
00158    xbShort  LongFieldLen;    /* to handle long field lengths */
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{                      /* memo header                    */
00182    xbLong  NextBlock;             /* pointer to next block to write */
00183    char    FileName[8];           /* name of dbt file               */
00184    char    Version;               /* not sure                       */
00185    xbShort BlockSize;             /* memo file block size           */
00186 };
00187 #endif
00188 
00190 
00194 class XBDLLEXPORT xbDbf {
00195 
00196 public:
00197    xbDbf( xbXBase * );
00198    virtual ~xbDbf();
00199 
00200    xbXBase  *xbase;               /* linkage to main base class */
00201 //   char EofChar[10];
00202 
00203 /* datafile methods */
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); // Put record to current position
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 /* field methods */
00280    const char *GetField(xbShort FieldNo) const; // Using internal static buffer
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;                  /* xBASE file version            */
00407    xbShort  NoOfFields;
00408    char   DbfStatus;              /* 0 = closed
00409                                      1 = open
00410                                      2 = updates pending           */
00411    FILE   *fp;                    /* file pointer                  */
00412    xbSchemaRec *SchemaPtr;        /* Pointer to field data         */
00413    char   *RecBuf;                /* Pointer to record buffer      */
00414    char   *RecBuf2;               /* Pointer to original rec buf   */
00415 
00416 #ifdef XB_MEMO_FIELDS
00417    FILE    *mfp;                  /* memo file pointer             */
00418    void    *mbb;                  /* memo block buffer             */
00419    xbMH     MemoHeader;           /* memo header structure         */
00420 
00421    xbShort  mfield1;              /* memo block field one FF       */
00422    xbShort  MStartPos;            /* memo start pos of data        */
00423    xbLong   MFieldLen;            /* memo length of data           */
00424    xbLong   NextFreeBlock;        /* next free block in free chain */
00425    xbLong   FreeBlockCnt;         /* count of free blocks this set */
00426 
00427    xbLong   MNextBlockNo;         /* free block chain              */
00428    xbLong   MNoOfFreeBlocks;      /* free block chain              */
00429 
00430    xbLong   CurMemoBlockNo;       /* Current block no loaded       */
00431 #endif
00432 
00433 /* Next seven variables are read directly off the database header */
00434 /* Don't change the order of the following seven items            */
00435    char   Version;
00436    char   UpdateYY;
00437    char   UpdateMM;
00438    char   UpdateDD;
00439 //   xbLong   NoOfRecs;
00440 //   xbShort  HeaderLen;
00441 //   xbShort  RecordLen;
00442 
00443    xbULong  NoOfRecs;
00444    xbUShort HeaderLen;
00445    xbUShort RecordLen;
00446 
00447 //#ifdef XB_REAL_DELETE
00448    xbULong  FirstFreeRec;
00449    xbULong  RealNumRecs;
00450 //#endif
00451 
00452    xbIxList * MdxList;
00453    xbIxList * NdxList;
00454    xbIxList * FreeIxList;
00455    xbULong  CurRec;               /* Current record or zero   */
00456    xbShort  AutoLock;             /* Auto update option 0 = off  */
00457 
00458 //#ifdef XB_REAL_DELETE
00459    xbShort  RealDelete;           /* real delete option 0 = off */
00460 //#endif
00461 
00462 #ifdef XB_LOCKING_ON
00463    xbShort CurLockType;           /* current type of file lock */
00464    xbShort CurLockCount;          /* number of current file locks */
00465    xbULong CurLockedRecNo;        /* currently locked record no */
00466    xbShort CurRecLockType;        /* current type of rec lock held (F_RDLOCK or F_WRLCK) */
00467    xbShort CurRecLockCount;       /* number of current record locks */
00468    xbShort CurMemoLockType;       /* current type of memo lock */
00469    xbShort CurMemoLockCount;      /* number of current memo locks */
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 

Generated on Wed Jan 26 11:45:09 2005 for Xbase Class Library by  doxygen 1.4.1