home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_12 / 9n12104a < prev    next >
Text File  |  1991-10-30  |  3KB  |  69 lines

  1. //  Listing 3 - Header file for binaryfile class 
  2. //  This contains the full class definition with
  3. //     the constructor and destructor functions
  4. //
  5.  
  6. enum FILEMODE  {Readonly, Update, AnyWrite, Append, Recreate};
  7. enum READMODE  {FirstRecord, NextRecord, PreviousRecord, LastRecord};
  8. enum SHAREMODE {NoneShared, ReadShared, WriteShared};
  9.  
  10. class binaryfile {
  11. private:
  12.     char name[81];      // name of file  (size is DOS specific)
  13.     enum FILEMODE mode;   // access mode of the file
  14.     enum SHAREMODE share; // whether shared access is allowed
  15.     unsigned length;      // record length
  16.     int      handle;      // file handle  (-1 if not open)
  17.     long     header;      // size of header at start of file
  18.     long     count;       // number of records in the file
  19.     long     recnbr;      // current record number (starting at 1)
  20.     long     locked;      // if >0, currently locked record
  21.     long     countrecords();  // check size of file
  22. public:
  23.     char     *record;    // pointer to record area
  24.     binaryfile (unsigned);  // constructor 
  25.     ~binaryfile ();           // destructor
  26.     // inline functions to read private elements
  27.     long     getcount  ()      {
  28.         return (share == WriteShared ? countrecords() : count);
  29.     };
  30.     long     getheader ()      {return header;};
  31.     unsigned getlength ()      {return length;};
  32.     long     getlocked ()      {return locked;};
  33.     enum FILEMODE getmode ()   {return mode;};
  34.     char*    getname (char *n) {return (strcpy (n, name));};
  35.     long     getrecnbr ()      {return recnbr;};
  36.     enum SHAREMODE getshare()  {return share;};
  37.     int      isopen ()         {return (handle >= 0);};
  38.     // Functions to open, close, read, write
  39.     int  changelength (unsigned); // change record length
  40.     int  fileopen  (const char *, enum FILEMODE = Readonly,
  41.          long = 0L, enum SHAREMODE = ReadShared);// Open file
  42.     int  fileclose ();       // close the file
  43.     int  fileread  (enum READMODE = NextRecord); // read sequentially
  44.     int  fileread  (long);   // read a specific record
  45.     int  filewrite ();       // rewrite record, or append
  46.     int  filewrite (long);   // write a specific record
  47.     int  lockrecord (long);  // lock a specific record
  48.     void unlockrecord ();    // unlock current record
  49. };
  50.  
  51. //  =============================================
  52. //  The constructor for class binaryfile
  53. //
  54. binaryfile::binaryfile (unsigned len) {
  55.     length = len;  // Assign the record length
  56.     handle = -1;
  57.     record = (len <= 4096 ? (char*) malloc (len + 2) : NULL);
  58.         // assign record space with safety zone
  59. }
  60.  
  61. //  =============================================
  62. //  The destructor for class binaryfile
  63. //
  64. binaryfile::~binaryfile  () {
  65.     if (isopen()) fileclose();  // close the file if open
  66.     if (record != NULL) free (record);  // release area
  67. }
  68.  
  69.