home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STRUCFIL.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  5KB  |  159 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*******************************************************************
  4.  * Generic structure <> disk file manipulations. These functions
  5.  * form a basic template for reading and writing structures to a 
  6.  * sequential file. This template is probably most useful for files
  7.  * with 500 or less records and eliminates the need for a more 
  8.  * elaborate file handler such as C-Tree, DB-Vista, Mix etc.
  9.  * Routines to put data in the struct is out of scope here.
  10.  * Written by Lynn Nash 8/28/91 and donated to the public domain.
  11.  */
  12. #include <io.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "sniptype.h"
  16. #include "strucfil.h"
  17.  
  18. /*-------------------- general globals ---------------------*/
  19.  
  20. static long cur_rec = 0;      /* the current record number */
  21. static FILE *fsptr = NULL;    /* fixed record data file pointer */
  22.  
  23. /* if file exists open in read/write mode else create file */
  24.  
  25. FILE * open_file(char *filename)
  26. {
  27.       if (access(filename, 0) == 0)
  28.             fsptr = fopen(filename, "rb+"); 
  29.       else  fsptr = fopen(filename, "wb+"); 
  30.       return fsptr;                       /* return the file pointer */
  31.  
  32. /* add new records to the data file */
  33.  
  34. int datadd(void)
  35. {
  36.       if (fsptr)
  37.       {
  38.             if (fseek(fsptr, 0L, SEEK_END) != 0)
  39.                   return Error_;  /* seek failure */
  40.             rec.delete_flag = 0; /* active record tag */
  41.             rec.recordnum = (int) (ftell(fsptr) / 
  42.                   (long) sizeof(struct blackbook));
  43.             if (fwrite(&rec, sizeof(struct blackbook), 1, fsptr) != 1)
  44.             {
  45.                   return Error_; /* write error */
  46.             }
  47.             else
  48.             {
  49.                   /* put your clean up code here */ 
  50.                   return Success_;
  51.             }
  52.       }
  53.       return Error_;
  54. }
  55.  
  56. /* tag the last record read in the file as deleted */
  57.  
  58. int data_delete(void)
  59. {
  60.       if (fsptr)
  61.       {
  62.             if (fseek(fsptr, (long) sizeof(struct blackbook) * -1L,
  63.                   SEEK_CUR) != 0)
  64.             {
  65.                   return Error_;
  66.             }
  67.             rec.delete_flag = -1;   /* tag the record as deleted */
  68.             if (fwrite(&rec, sizeof(struct blackbook), 1, fsptr) != 1)
  69.                   return Error_;
  70.             else  return Success_;
  71.       }
  72.       return Error_;
  73. }
  74.  
  75. /* read a random structure. If successful the global cur_rec will 
  76.  * contain the number of the last record read & it can be compared
  77.  * to the number in the struct as a double check (belt & suspenders)
  78.  */
  79.  
  80. int data_read(long recnum)
  81. {
  82.       if (fseek(fsptr, (long) sizeof(struct blackbook) * recnum,
  83.             SEEK_SET) != 0)
  84.       {
  85.             return Error_;
  86.       }
  87.       cur_rec = recnum; /* keep tabs on record pointer in global */  
  88.  
  89.       /* now read the record into save struct*/
  90.  
  91.       if (fread(&oldrec, sizeof(struct blackbook), 1, fsptr) != 1)
  92.       {
  93.             return Error_;
  94.       }
  95.       else                          /* copy save struct to edit struct */
  96.       {
  97.             memcpy(&rec, &oldrec, sizeof(struct blackbook));
  98.             return Success_;
  99.       }
  100. }
  101.  
  102. /* rewrite the last read record back to disk */
  103.  
  104. int data_update(void)
  105. {
  106.       if (memcmp(&rec, &oldrec, sizeof(struct blackbook)) == 0)
  107.             return True_;  /* no update needed */
  108.  
  109.       /* back up one record before writing */
  110.  
  111.       if (fseek(fsptr, (long) sizeof(struct blackbook) * -1L, 
  112.             SEEK_CUR) != 0)
  113.       {
  114.             return Error_; /* seek error */ 
  115.       }
  116.  
  117.       /* now write the record */
  118.  
  119.       if (fwrite(&rec, sizeof(struct blackbook), 1, fsptr) != 1)
  120.             return Error_; /* write error */
  121.       return Success_;
  122. }
  123.  
  124. /* get the next valid record in the file */
  125.  
  126. int read_forward(void)
  127. {
  128.       do
  129.       {
  130.             cur_rec++; /* upcount the record number */
  131.             if (data_read(cur_rec) != 0)
  132.             {
  133.                   cur_rec--; /* decrement the record number */
  134.                   return Error_;
  135.             }
  136.       } while (oldrec.delete_flag != 0); /* record read was deleted */
  137.       return Success_;
  138. }
  139.  
  140. /* get the previous valid record in the file */
  141.  
  142. int read_backward(void)
  143. {
  144.       do
  145.       {
  146.             cur_rec--;  /* decrement the record number */
  147.             if (cur_rec >= 0)
  148.             {
  149.                   if ( data_read(cur_rec) != 0 )
  150.                   {
  151.                         cur_rec++; /* increment the record number */
  152.                         return Error_;
  153.                   }
  154.             }  
  155.       } while (oldrec.delete_flag != 0); /* record read was deleted */
  156.       return Success_;
  157. }
  158.