home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / vbdfile.h < prev    next >
Encoding:
Text File  |  1999-03-18  |  18.1 KB  |  471 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- //
  5. // C++ Header File Name: vbdfile.h 
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer    
  8. // File Creation Date: 02/04/1997 
  9. // Date Last Modified: 03/18/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // --------------------------------------------------------- // 
  12. // ---------- Include File Description and Details  ---------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. The VBD file manager class is responsible for handling all
  32. low-level file operations through the use of VBDFile objects
  33. or by inheriting the VBDFile class. Low-level file operations
  34. refer to functions such as: Create(), Open(), Read(), Write(),
  35. Alloc(), Delete(), and Close(). These functions contain all the
  36. routines needed to create and maintain VBD files in accordance
  37. with the VBD File Format. VBDFile objects are reference counted
  38. and must be created dynamically due to the way reference
  39. counting is implemented. 
  40.  
  41. Changes:
  42. ================================================================
  43. 01/15/1998 - The template implementation in refcount.h caused a
  44. linker error when compling under HPUX 10.20 (using HPUX CPP 1024)
  45. in some wxWindows applications.
  46. Changed by: Doug Gaer
  47.  
  48. 01/15/1998 - Added the __USE_VBDREFCOUNT__ macro to enable use of
  49. the non-template version of the Refcount class.
  50. Changed by: Doug Gaer
  51.  
  52. 01/20/1998 - Combined the debug version with this version.
  53. Changed by: Doug Gaer
  54.  
  55. 01/20/1998 - Added BlindOpen() debug function.
  56. Changed by: Doug Gaer
  57.  
  58. 01/20/1998 - Added VBSearch() debug function.
  59. Changed by: Doug Gaer
  60.  
  61. 01/20/1998 - VBDSignature data member changed from private to
  62. public access for compatibility with VB_DEBUG program.
  63. Changed by: Doug Gaer
  64.  
  65. 01/20/1998 - VBDVersion data member changed from private to
  66. public assess for compatibility with VB_DEBUG program.
  67. Changed by: Doug Gaer
  68.  
  69. 02/06/1998 - The ReadVBHdr() function was changed to throw a
  70. CSyncError execption instead of a CFileCorrupt exception for
  71. file synchronization errors.
  72. Changed by: Doug Gaer
  73.  
  74. 03/11/1998 - Modified the Open() function to check the true
  75. end of file and compare it to the VBD file header's end of
  76. file. If the true end of file is greater then VBD file header's
  77. EndOfFile value, the EndOfFile value will be changed to match the
  78. true end of file.
  79. Changed by: Doug Gaer
  80.  
  81. 03/12/1998 -  Changed ReadHdr() function to pubilc access
  82. so that applications can force the VBD file header to reread.
  83. Changed by: Doug Gaer
  84.  
  85. 03/13/1998 - Modified InitHdr() function to write the VBD file
  86. header and flush the disk buffers to maintain file integrity
  87. during multiple file access when a new file has been created.
  88. Changed by: Doug Gaer
  89.  
  90. 03/13/1998 - Modified the Write() function to allow applications 
  91. to flush disk buffers after each write operation to ensure the
  92. file data stays in sync during multiple file access. This approach
  93. was used as opposed to un-buffered read and writes to maintain code
  94. portability between all platforms.  
  95. Changed by: Doug Gaer
  96.  
  97. 03/13/1998 - Modified the Alloc() function to write the VBD file
  98. header to disk after each allocation to ensure that the VBD file
  99. header stays in sync during multiple file access.
  100. Changed by: Doug Gaer
  101.  
  102. 03/13/1998 - Modified the Delete() function to write the VBD file
  103. header to disk after each deletion to ensure that the VBD file
  104. header stays in sync during multiple file access.
  105. Changed by: Doug Gaer
  106.  
  107. 03/13/1998 - Added the TestVBDHeader() function to ensure that
  108. the in memory copy of the VBD file header and the disk copy
  109. stay in sync during multiple file access.
  110. Added by: Doug Gaer
  111.  
  112. 03/13/1998 - Modified the FindFirstVB() function to read the
  113. VBD file from the disk before each search to ensure that the
  114. VBD file header stays in sync during multiple file access.
  115. Changed by: Doug Gaer
  116.  
  117. 03/13/1998 - Modified the VBDeleted() function to read the
  118. VBD file from the disk before each search to ensure that the
  119. VBD file header stays in sync during multiple file access.
  120. Changed by: Doug Gaer
  121.  
  122. 03/13/1998 - Added non-const versions of GetSignature(),
  123. GetVersion(), FileHeaderSize(), VBHeaderSize(), VBDFileName
  124. and GetFileStatus().
  125. Added by: Doug Gaer
  126.  
  127. 03/13/1998 - Removed the const versions of the StaticArea(),
  128. GetFreeSpace(), GetVBDFreeSpace(), GetEOF(), GetHeapStart()
  129. and GetHighestVB() functions.
  130. Changed by: Doug Gaer
  131.  
  132. 03/13/1998 - Modified the StaticArea(), GetFreeSpace(),
  133. GetVBDFreeSpace(), GetEOF(), GetHeapStart() and GetHighestVB()
  134. functions to compare the in memory copy and disk copy of the
  135. VBD file header. If they differ the VBD file header will be
  136. reread from the disk. 
  137. Changed by: Doug Gaer
  138.  
  139. 03/16/1998 - Modified the VBTotal() function to perform its own
  140. independent read operation. Will throw CAccessViolation exception
  141. if an end of file error occurs during multiple file access over
  142. an NFS mount.
  143. Changed by: Doug Gaer
  144.  
  145. 03/16/1998 - Modified the FindFirstVB() function to perform its 
  146. own independent read operation. Will throw CAccessViolation
  147. exception if an end of file error occurs during multiple file
  148. access over an NFS mount.
  149. Changed by: Doug Gaer
  150.  
  151. 03/17/1998: Modified the Read() function to throw CAccessViolation
  152. exception if an end of file error occurs during multiple file
  153. access over an NFS mount.
  154. Changed by: Doug Gaer
  155.  
  156. 07/22/1998: In version 1025 the Open function no longer checks 
  157. the revision letter when opening a file. Also, the last character 
  158. of the VBDSignature member has been changed from a space to a
  159. null byte.
  160. Changed by: Doug Gaer
  161.  
  162. 07/22/1998: Version number changed to 1025, Revsion 0, to reflect
  163. all modifcations made from 01/15/1998 to 07/22/1998. 
  164. Changed by: Doug Gaer
  165.  
  166. 09/03/1998: Added the FSListCorrupt constant to tell the Reclaim(),
  167. UnDelete(), and VBDeleted() functions that the free space list is
  168. corrupt.
  169. Added by: Doug Gaer
  170.  
  171. 09/04/1998: Added two versions of the Reclaim() function, one using
  172. the best-fit method and the other using the first-fit method. Define
  173. the __RECLAIM_BEST_FIT__ macro to use the best-fit routine or the
  174. __RECLAIM_FIRST_FIT__ macro to use the first-fit routine.
  175. Added by: Doug Gaer
  176.  
  177. 09/04/1998: Modifed the Reclaim() and VBDeleted() functions, allowing
  178. them to write the FSListCorrupt constant to the file header and abort
  179. the operation.
  180. Changed by: Doug Gaer
  181.  
  182. 09/08/1998: Changed the UnDelete() function to mange changes to the
  183. free space list independently of the Reclaim() function.
  184. Changed by: Doug Gaer
  185.  
  186. 09/08/1998: By default the file manager will use the first-fit method
  187. to reclaim deleted or removed blocks. The __RECLAIM_BEST_FIT__ macro
  188. must be defined to use the best-fit method.
  189. Changed by: Doug Gaer
  190.  
  191. 09/08/1998: The Remove() function no longer allocates memory for the
  192. length of the entire block. A single byte value, equal to zero, is
  193. written to the file byte by byte for the length of the block.
  194. Changed by: Doug Gaer
  195.  
  196. 09/09/1998: Modified the VBDeleted() function to pass back the
  197. total number of deleted and removed blocks in the __UWORD__ *d,
  198. __UWORD__ *r variables.
  199. Changed by: Doug Gaer
  200.  
  201. 09/11/1998: Added the __DOS_INCLUDES__ macro for compilers that
  202. require the use of DOS path separators in include files.
  203. Added by: Doug Gaer
  204.  
  205. 09/21/1998: Added the CalcChecksum() function. This function will
  206. return a 32-bit CRC checksum for specified number of bytes at a
  207. given file address.
  208. Added by: Doug Gaer
  209.  
  210. 09/24/1998: Added the GetRevLetter() function. This function will
  211. return a single character representing the VBD file's revision
  212. letter.
  213. Added by: Doug Gaer
  214.  
  215. 09/24/1998: Modified the InitHdr() and Open() functions to record
  216. the VBD file's revision letter in the rev_letter variable.
  217. Changed by: Doug Gaer
  218.  
  219. 09/24/1998: Modified the Alloc() function to allocate an additional
  220. 4 bytes with each block to allow an application to store a 32-bit
  221. checksum with each block. This changes the VBD revision letter from
  222. zero to rev 'A'.
  223. Changed by: Doug Gaer
  224.  
  225. 09/24/1998: Modified the Write() function to perform a CRC check
  226. with each write operation by default. The CRC check will compare
  227. of the CRC of the buffer to the CRC of the actual bytes written to
  228. disk. If the CRC test fails, the Write() function will throw a
  229. CChecksumError exception.
  230. Changed by: Doug Gaer
  231.  
  232. 09/25/1998: Added the WriteObjectChecksum() function to write
  233. a 32-bit checksum for an object in the pre-allocated space at
  234. the end of the block. This function will return zero for any
  235. file below revision letter 'A'.
  236. Added by: Doug Gaer
  237.  
  238. 09/25/1998: Added the ReadObjectChecksum() function to read
  239. the 32-bit checksum of an object from the pre-allocated space
  240. at the end of the block. This function will always return true
  241. for any file below revision letter 'A'.
  242. Added by: Doug Gaer
  243.  
  244. 09/25/1998: Added the ReOpen() function to reassign the file
  245. pointer to another file.
  246. Added by: Doug Gaer
  247.  
  248. 09/28/1998: Modified the Remove() function to allocate
  249. memory for the length of the object by default. If memory
  250. allocation fails or is disabled a single byte value, equal
  251. to zero, is written to the file byte by byte for the length
  252. of the object. 
  253. Changed by: Doug Gaer
  254.  
  255. 09/30/1998: Added the Rewind() function to reposition the file
  256. pointer to the beginning of a file.
  257. Added by: Doug Gaer
  258.  
  259. 09/30/1998: Added the GetPosition() function to get the current
  260. file position using fpos_t type.
  261. Added by: Doug Gaer
  262.  
  263. 09/30/1998: Added the SetPosition() function to reset the current
  264. file position using fpos_t type.
  265. Added by: Doug Gaer
  266.  
  267. 09/30/1998: Added the SeekTo() function to perform an optimized
  268. seek operation by moving the file position indicator based on the
  269. current stream position.
  270. Added by: Doug Gaer
  271.  
  272. 10/01/1998: Modifed the Alloc() function to reposition the file
  273. pointer to the file address where the next write operation
  274. should take place after allocating space for an object.
  275. Changed by: Doug Gaer
  276.  
  277. 02/10/1999: An AccessViolation exception in the Read() function
  278. is now considered fatal and will cause the program to terminate.
  279. Changed by: Doug Gaer
  280. */
  281. // ----------------------------------------------------------- //  
  282. #ifndef __VBDFILE_HPP
  283. #define __VBDFILE_HPP
  284.  
  285. #include <stdio.h>
  286.  
  287. // Enable to use the non-template version of the Refcount class
  288. // #ifndef __USE_VBDREFCOUNT__
  289. // #define __USE_VBDREFCOUNT__
  290. // #endif
  291.  
  292. #ifdef __USE_VBDREFCOUNT__
  293. #include "vbdref.h"
  294. #else
  295. #include "refcount.h"
  296. #endif
  297.  
  298. #include "ehandler.h"
  299. #include "dtypes.h"
  300. #include "int32.h"
  301. #include "uint32.h"
  302. #include "crc32.h"
  303.  
  304. // typedefs for file operations and stream position
  305. typedef INT32 FAU; // (F)ile (A)ddress (U)nit, physical file address type
  306. typedef INT32 StreamPos;   // Stream position
  307.  
  308. // CRC-32 checksum used to detect bit errors (4 bytes)
  309. typedef UINT32 vbChecksum;
  310.  
  311. // Variable Block Database (F)ile (H)eader
  312. struct FileHeader // VBD file header information (28 bytes total)
  313.   FAU FreeSpace;      // Address to first block of free heap space (4 bytes)
  314.   FAU EndOfFile;      // Address of byte after end of file (4 bytes)
  315.   FAU HeapStart;      // Address of the start of the heap space (4 bytes)
  316.   FAU HighestVB;      // Highest allocated (V)ariable (B)lock
  317.   __SBYTE__ Signature[8]; // Signature used for every VBD file (8 bytes)
  318.   INT32 Version;      // VBD Version number (4 bytes)
  319. };
  320.  
  321. // (V)ariable (B)lock (H)eader 
  322. struct VBHeader // Marks each variable data block (16 bytes total)
  323. {
  324.   UINT32 CkWord;      // Check-word used to mark each block (4 bytes)
  325.   UINT32 Length;      // Block Length (Header + Object + CRC) (4 bytes) 
  326.   UINT32 Status;      // Status of the block's dynamic data (4 byte)
  327.   FAU NextDeletedVB;  // Pointer to next deleted block (4 bytes) 
  328. };
  329.  
  330. // Constants for dynamic data attributes used by the VB Header Status member
  331. const __SBYTE__ NormalVB  = 'N'; // Normal Read/Write attribute 
  332. const __SBYTE__ DeletedVB = 'D'; // Deleted Variable Block 
  333. const __SBYTE__ RemovedVB = 'R'; // Removed Variable Block
  334. const __SBYTE__ BadVB     = 'B'; // Bad Variable Block
  335.  
  336. // Constants for file operations and stream position 
  337. const int MaxNameLength   = 255;        // Max len of file names
  338. const FAU StartOfFile     = 0;          // First byte in the file
  339. const FAU CurrAddress     = -1;         // Indicates current location
  340. const UINT32 CheckWord    = 0x0000fefe; // 32-bit Check Word
  341. const INT32 FSListCorrupt = -1;         // Free space list is corrupt
  342.  
  343. class VBDFile : public CountedObject
  344. {
  345. public:
  346.   enum IO_Operation { READ, WRITE, SEEK, REWIND };
  347.   enum AccessMode { READWRITE, READONLY };
  348.  
  349. public:
  350.   VBDFile();
  351.   virtual ~VBDFile();
  352.  
  353. public: // Debug VBD File manager: revisions 01/20/1997
  354.   virtual int BlindOpen(const char *FName, AccessMode Mode = READONLY);
  355.   FAU VBSearch(FAU Offset = 0);     // Finds first valid Variable Block
  356.   static __SBYTE__ VBDSignature[8]; // Signature for VBD files
  357.   static __LWORD__ VBDVersion;      // Version number VBD files
  358.  
  359. public:
  360.   virtual int Create(const char *FName, FAU StaticSize = 0);
  361.   virtual int Open(const char *FName, AccessMode Mode = READWRITE);
  362.   virtual int ReOpen(const char *FName, AccessMode Mode = READWRITE);
  363.   virtual void Close(int flush = 1);
  364.   virtual void Flush();
  365.   FAU Alloc(__UWORD__ Bytes);
  366.   FAU ReAlloc(FAU Address, __UWORD__ Bytes); 
  367.   int Delete(FAU Address); 
  368.   int Remove(FAU Address, int mem_alloc = 1);
  369.   int UnDelete(FAU Address);
  370.   void Read(void *buf, __UWORD__ Bytes, FAU Address = CurrAddress);
  371.   void Write(const void *buf, __UWORD__ Bytes, FAU Address = CurrAddress,
  372.              int flush = 1, int bit_test = 1);
  373.   void Seek(FAU Offset, int SeekMode = SEEK_SET);
  374.   FAU SeekTo(FAU Address);
  375.   void Rewind();
  376.   void SetPosition(const fpos_t fpos);
  377.   fpos_t GetPosition();
  378.   int IsOpen() const { return (Status & 0x02); }
  379.   int ReadOnly() const { return (Status & 0x04) == 0; }
  380.   int ReadyForWriting() const { return (Status & 0x07) == 0x07; }
  381.   void ClearErr() { Error->ClearException(); Status |= 0x01; }
  382.   int IsOK() const { return (Status & 0x03) == 0x03; }
  383.   __UWORD__ ObjectLength(FAU Address = CurrAddress);
  384.   __UWORD__ VBLength(FAU Address = CurrAddress);
  385.   StreamPos FilePosition();
  386.  
  387.   // 32-bit CRC checksum routines
  388.   __ULWORD__ CalcChecksum(__UWORD__ Bytes, FAU Address, int mem_alloc = 1);
  389.   UINT32 WriteObjectChecksum(FAU Address);
  390.   int ReadObjectChecksum(FAU Address, __ULWORD__ *object_crc = 0,
  391.              __ULWORD__ *calc_crc = 0);
  392.  
  393.   // Static file statistics
  394.   const char *GetSignature() const;
  395.   char *GetSignature();
  396.   char GetRevLetter() { return rev_letter; }
  397.   const char GetRevLetter() const { return rev_letter; }
  398.   INT32 GetVersion() const { return Header.Version; }
  399.   INT32 GetVersion() { return Header.Version; }
  400.   size_t FileHeaderSize() const { return sizeof(FileHeader); }
  401.   size_t FileHeaderSize() { return sizeof(FileHeader); }
  402.   size_t VBHeaderSize() const { return sizeof(VBHeader); }
  403.   size_t VBHeaderSize() { return sizeof(VBHeader); }
  404.   const char *VBDFileName() const { return (const char *)FileName; }
  405.   char *VBDFileName() { return FileName; }
  406.   __SBYTE__ GetFileStatus() const { return Status; }
  407.   __SBYTE__ GetFileStatus() { return Status; }
  408.   __LWORD__ StaticArea();
  409.  
  410.   // Dynamic file statistics
  411.   FAU GetFreeSpace();
  412.   FAU GetVBDFreeSpace(); // Use this version for wxWindows programs
  413.   FAU GetEOF();
  414.   FAU GetHeapStart();
  415.   FAU GetHighestVB();
  416.  
  417.   // Return total number of removed and deleted VBs
  418.   INT32 VBDeleted(__UWORD__ *d = 0, __UWORD__ *r = 0);
  419.   
  420.   int TestVBDHeader();  // Keeps VBD file header's dynamic data in sync
  421.   __UWORD__ VBTotal();  // Return total number of Variable Blocks
  422.   FAU FindFirstVB(FAU Offset = 0);     // Finds first valid Variable Block
  423.   FAU FindNextVB(FAU Offset = 0);      // Finds next after first 
  424.   FAU FindFirstObject(FAU Offset = 0); // Finds first object
  425.   FAU FindNextObject(FAU Offset = 0);  // Finds next after first 
  426.  
  427. public: // General purpose file utilities
  428.   static int Exists(const char *FName);
  429.   static __LWORD__ FileSize(const char *FName);
  430.  
  431. protected:
  432.   FAU Reclaim(__UWORD__ Bytes); 
  433.   void InitHdr();
  434.   void WriteHdr();
  435.   void ReadVBHdr(VBHeader &hdr, FAU Address = CurrAddress);
  436.   void WriteVBHdr(const VBHeader &hdr, FAU Address = CurrAddress);
  437.  
  438. public: // 03/12/1998: Changed to pubilc access
  439.   void ReadHdr();
  440.   
  441. protected:
  442.   char FileName[MaxNameLength]; // Name of the file
  443.   char rev_letter;              // Revision number
  444.   FileHeader Header;            // File header as stored in memory
  445.   FILE *fp;                     // Stream file handle
  446.   IO_Operation LastOperation;   // Last I/O operation
  447.  
  448.   __SBYTE__ Status; // File Status bits used in VBD Files:
  449.   // 210
  450.   // |||---- good = 1, bad = 0
  451.   // |+----- open = 1, closed = 0
  452.   // +------ read/write = 1, read/only = 0
  453.  
  454. public: // Overloaded operators
  455.   int operator!() const { return (Status & 0x03) != 0x03; }
  456.   operator const int () const { return (Status & 0x03) == 0x03; }
  457. };
  458.  
  459. #ifdef __USE_VBDREFCOUNT__
  460. typedef vbdRefCount VBDFilePtr;
  461. #else
  462. typedef RefCount<VBDFile> VBDFilePtr;
  463. #endif
  464.  
  465. #endif // __VBDFILE_HPP
  466. // ----------------------------------------------------------- // 
  467. // ------------------------------- //
  468. // --------- End of File --------- //
  469. // ------------------------------- //
  470.