home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / database / cdbms / datafile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-01  |  3.1 KB  |  160 lines

  1. /* ---------------- datafile.c -------------------------- */
  2.  
  3. #include <stdio.h>
  4. #include "cdata.h"
  5.  
  6. #define flocate(r,l) ((long)(sizeof(FHEADER)+(((r)-1)*(l))))
  7.  
  8. static int handle [MXFILS];
  9. FHEADER fh [MXFILS];
  10.  
  11. /* --------- create a file ----------- */
  12. void file_create(name, len)
  13. char *name;
  14. int len;
  15. {
  16.     int fp;
  17.     FHEADER hd;
  18. #if COMPILER == MICROSOFT
  19.     extern int _iomode;
  20.     _iomode = 0x8000;
  21. #endif
  22. #if COMPILER == LATTICE
  23.     extern int _iomode;
  24.     _iomode = 0x8000;
  25. #endif
  26.  
  27.     unlink(name);
  28.     fp = creat(name, CMODE);
  29.     close(fp);
  30.     fp = open(name, OPENMODE);
  31.     hd.next_record = 1;
  32.     hd.first_record = 0;
  33.     hd.record_length = len;
  34.     write(fp, (char *) &hd, sizeof hd);
  35.     close(fp);
  36. }
  37.  
  38.  
  39.  
  40.  
  41. /* --------------  open a file ---------------- */
  42. int file_open(name)
  43. char *name;
  44. {
  45.     int fp;
  46. #if COMPILER == MICROSOFT
  47.     extern int _iomode;
  48.     _iomode = 0x8000;
  49. #endif
  50. #if COMPILER == LATTICE
  51.     extern int _iomode;
  52.     _iomode = 0x8000;
  53. #endif
  54.  
  55.     for (fp = 0; fp < MXFILS; fp++)
  56.         if (handle [fp] == 0)
  57.             break;
  58.     if (fp == MXFILS)
  59.         return ERROR;
  60.     if ((handle [fp] = open(name, OPENMODE)) == ERROR)
  61.         return ERROR;
  62.     lseek(handle [fp], 0L, 0);
  63.     read(handle [fp], (char *) &fh [fp], sizeof(FHEADER));
  64.     return fp;
  65. }
  66.  
  67. /* --------------- close a file ----------------- */
  68. void file_close(fp)
  69. int fp;
  70. {
  71.     lseek(handle [fp], 0L, 0);
  72.     write(handle [fp], (char *) &fh [fp], sizeof(FHEADER));
  73.     close(handle [fp]);
  74.     handle [fp] = 0;
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. /* -------------- create a new record ------------- */
  82. RPTR new_record(fp, bf)
  83. int fp;
  84. char *bf;
  85. {
  86.     RPTR rcdno;
  87.     extern char *malloc();
  88.     FHEADER *c;
  89.     extern int free();
  90.  
  91.     if (fh [fp].first_record)    {
  92.         rcdno = fh [fp].first_record;
  93.         if ((c = (FHEADER *)
  94.                 malloc(fh [fp].record_length)) == NULL)    {
  95.             errno = D_OM;
  96.             dberror();
  97.         }
  98.         get_record(fp, rcdno, c);
  99.         fh [fp].first_record = c->next_record;
  100.         free(c);
  101.     }
  102.     else
  103.         rcdno = fh [fp].next_record++;
  104.     put_record(fp, rcdno, bf);
  105.     return rcdno;
  106. }
  107.  
  108. /* ---------------- retrieve a record -------------- */
  109. int get_record(fp, rcdno, bf)
  110. int fp;
  111. RPTR rcdno;
  112. char *bf;
  113. {
  114.     if (rcdno >= fh [fp].next_record)
  115.         return ERROR;
  116.     lseek(handle [fp],
  117.             flocate(rcdno, fh [fp].record_length), 0);
  118.     read(handle [fp], bf, fh [fp].record_length);
  119.     return OK;
  120. }
  121.  
  122. /* ---------------- rewrite a record -------------- */
  123. int put_record(fp, rcdno, bf)
  124. int fp;
  125. RPTR rcdno;
  126. char *bf;
  127. {
  128.     if (rcdno > fh [fp].next_record)
  129.         return ERROR;
  130.     lseek(handle [fp],
  131.                 flocate(rcdno, fh [fp].record_length), 0);
  132.     write(handle [fp], bf, fh [fp].record_length);
  133.     return OK;
  134. }
  135.  
  136. /* -------------- delete a record ---------------- */
  137. int delete_record(fp, rcdno)
  138. int fp;
  139. RPTR rcdno;
  140. {
  141.     FHEADER *bf;
  142.     extern char *malloc();
  143.  
  144.     if (rcdno > fh [fp].next_record)
  145.         return ERROR;
  146.     if ((bf = (FHEADER *)
  147.             malloc(fh [fp].record_length)) == NULL)    {
  148.         errno = D_OM;
  149.         dberror();
  150.     }
  151.     set_mem(bf, fh [fp].record_length, '\0');
  152.     bf->next_record = fh [fp].first_record;
  153.     bf->first_record = -1;
  154.     fh [fp].first_record = rcdno;
  155.     put_record(fp, rcdno, bf);
  156.     free(bf);
  157.     return OK;
  158. }
  159.  
  160.