home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 311_01 / db_seq.c < prev    next >
C/C++ Source or Header  |  1990-04-22  |  8KB  |  286 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      db_seq.c  v1.3  (c) 1987-1990  Ken Harris                           */
  5. /*                                                                          */
  6. /*                                                                          */
  7. /****************************************************************************/
  8. /*                                                                          */
  9. /*      This software is made available on an AS-IS basis. Unrestricted     */
  10. /*      use is granted provided that the copyright notice remains intact.   */
  11. /*      The author makes no warranties expressed or implied.                */
  12. /*                                                                          */
  13. /****************************************************************************/
  14.  
  15. #include "dblib.h"
  16.  
  17. /*
  18.  *      db_add_seq  -  Add a record to a sequential file
  19.  */
  20.  
  21. void db_add_seq(df, user_data)
  22.  DATA_FILE df;
  23.  char *user_data;
  24. {
  25.         FILE_HDR  fh;
  26.         BUFFER   buf;
  27.         char   *rbuf;
  28.  
  29.         db_error = 0;
  30.  
  31.         fh  = (FILE_HDR) df->df_fhdr->buf_data;
  32.         buf = df->df_buf;
  33.  
  34.         db_get_next_avail(df, buf);
  35.         if (db_error) return;
  36.  
  37.         db_add_blk = buf->buf_cur_blk;
  38.         db_add_rec = buf->buf_rec_inx;
  39.  
  40.         rbuf = buf->buf_data + (buf->buf_rec_inx - 1) * fh->fh_rec_size;
  41.  
  42.         memcpy(rbuf+fh->fh_ctl_size, user_data, fh->fh_data_size);
  43.  
  44.         fh->fh_rec_cnt++;
  45.  
  46.         db_put_blk(df,df->df_fhdr);
  47.         db_put_blk(df,buf);
  48. }
  49.  
  50. /*
  51.  *      db_read_first_seq  -  Read First Record in a Sequential File
  52.  */
  53.  
  54. void db_read_first_seq(df, user_data)
  55.  DATA_FILE df;
  56.  char *user_data;
  57. {
  58.         FILE_HDR fh;
  59.         BUFFER  buf;
  60.         SEQ_REC seq;
  61.         ulong   blk;
  62.         ushort  rec;
  63.         char  *rbuf;
  64.  
  65.         db_error = 0;
  66.  
  67.         fh  = (FILE_HDR) df->df_fhdr->buf_data;
  68.         buf = df->df_buf;
  69.  
  70.         for (blk=1; blk <= fh->fh_last_block; blk++)
  71.         {       db_get_blk(df, blk, buf);
  72.                 if (db_error) return;
  73.  
  74.                 rbuf = buf->buf_data;
  75.  
  76.                 for (rec=1; rec <= fh->fh_recs_per_blk; rec++)
  77.                 {       seq = (SEQ_REC) rbuf;
  78.  
  79.                         if (seq->seq_stat == DB_INUSE)
  80.                         {       buf->buf_rec_inx = rec;
  81.                                 memcpy(user_data, rbuf+fh->fh_ctl_size,
  82.                                                               fh->fh_data_size);
  83.                                 return;
  84.                         }
  85.                         rbuf += fh->fh_rec_size;
  86.                 }
  87.         }
  88.         db_error= DB_END_OF_FILE;
  89. }
  90.  
  91. /*
  92.  *      db_read_next_seq  -  Read Next Record in a Sequential File
  93.  */
  94.  
  95. void db_read_next_seq(df, user_data)
  96.  DATA_FILE df;
  97.  char *user_data;
  98. {
  99.         FILE_HDR fh;
  100.         BUFFER  buf;
  101.         SEQ_REC seq;
  102.         ulong   blk;
  103.         ushort  rec;
  104.         char  *rbuf;
  105.  
  106.         db_error = 0;
  107.  
  108.         fh  = (FILE_HDR) df->df_fhdr->buf_data;
  109.         buf = df->df_buf;
  110.  
  111.         db_get_blk(df, df->df_prev_blk, buf);
  112.         if (db_error) return;
  113.  
  114.         buf->buf_rec_inx = df->df_prev_rec + 1;
  115.  
  116.         rbuf = buf->buf_data + (buf->buf_rec_inx - 1) * fh->fh_rec_size;
  117.  
  118.         for (rec=buf->buf_rec_inx; rec <= fh->fh_recs_per_blk; rec++)
  119.         {       seq = (SEQ_REC) rbuf;
  120.  
  121.                 if (seq->seq_stat == DB_INUSE)
  122.                 {       buf->buf_rec_inx = rec;
  123.                         memcpy(user_data, rbuf+fh->fh_ctl_size, fh->fh_data_size);
  124.                         return;
  125.                 }
  126.                 rbuf += fh->fh_rec_size;
  127.         }
  128.  
  129.         for (blk=buf->buf_cur_blk+1; blk <= fh->fh_last_block; blk++)
  130.         {       db_get_blk(df, blk, buf);
  131.                 if (db_error) return;
  132.  
  133.                 rbuf = buf->buf_data;
  134.  
  135.                 for (rec=1; rec <= fh->fh_recs_per_blk; rec++)
  136.                 {       seq = (SEQ_REC) rbuf;
  137.  
  138.                         if (seq->seq_stat == DB_INUSE)
  139.                         {       buf->buf_rec_inx = rec;
  140.                                 memcpy(user_data, rbuf+fh->fh_ctl_size,
  141.                                                               fh->fh_data_size);
  142.                                 return;
  143.                         }
  144.                         rbuf += fh->fh_rec_size;
  145.                 }
  146.         }
  147.         db_error= DB_END_OF_FILE;
  148. }
  149.  
  150. /*
  151.  *    db_read_last_seq  -  Read Last Record in a Sequential File
  152.  */
  153.  
  154. void db_read_last_seq(df, user_data)
  155.  DATA_FILE df;
  156.  char *user_data;
  157. {
  158.         FILE_HDR fh;
  159.         BUFFER  buf;
  160.         SEQ_REC seq;
  161.         ulong   blk;
  162.         ushort  rec;
  163.         char  *rbuf;
  164.  
  165.         db_error = 0;
  166.  
  167.         fh  = (FILE_HDR) df->df_fhdr->buf_data;
  168.         buf = df->df_buf;
  169.  
  170.     for (blk=fh->fh_last_block; blk >= 1; blk--)
  171.         {       db_get_blk(df, blk, buf);
  172.                 if (db_error) return;
  173.  
  174.         rbuf = buf->buf_data + (fh->fh_recs_per_blk - 1) * fh->fh_rec_size;
  175.  
  176.         for (rec=fh->fh_recs_per_blk; rec >= 1; rec--)
  177.                 {       seq = (SEQ_REC) rbuf;
  178.  
  179.                         if (seq->seq_stat == DB_INUSE)
  180.                         {       buf->buf_rec_inx = rec;
  181.                                 memcpy(user_data, rbuf+fh->fh_ctl_size,
  182.                                                               fh->fh_data_size);
  183.                                 return;
  184.                         }
  185.             rbuf -= fh->fh_rec_size;
  186.                 }
  187.         }
  188.         db_error= DB_END_OF_FILE;
  189. }
  190.  
  191. /*
  192.  *    db_read_prev_seq  -  Read Prev Record in a Sequential File
  193.  */
  194.  
  195. void db_read_prev_seq(df, user_data)
  196.  DATA_FILE df;
  197.  char *user_data;
  198. {
  199.         FILE_HDR fh;
  200.         BUFFER  buf;
  201.         SEQ_REC seq;
  202.         ulong   blk;
  203.         ushort  rec;
  204.         char  *rbuf;
  205.  
  206.         db_error = 0;
  207.  
  208.         fh  = (FILE_HDR) df->df_fhdr->buf_data;
  209.         buf = df->df_buf;
  210.  
  211.         db_get_blk(df, df->df_prev_blk, buf);
  212.         if (db_error) return;
  213.  
  214.     buf->buf_rec_inx = df->df_prev_rec - 1;
  215.  
  216.         rbuf = buf->buf_data + (buf->buf_rec_inx - 1) * fh->fh_rec_size;
  217.  
  218.     for (rec=buf->buf_rec_inx; rec >= 1; rec--)
  219.         {       seq = (SEQ_REC) rbuf;
  220.  
  221.                 if (seq->seq_stat == DB_INUSE)
  222.                 {       buf->buf_rec_inx = rec;
  223.                         memcpy(user_data, rbuf+fh->fh_ctl_size, fh->fh_data_size);
  224.                         return;
  225.                 }
  226.         rbuf -= fh->fh_rec_size;
  227.         }
  228.  
  229.     for (blk=buf->buf_cur_blk-1; blk >= 1; blk--)
  230.         {       db_get_blk(df, blk, buf);
  231.                 if (db_error) return;
  232.  
  233.         rbuf = buf->buf_data + (fh->fh_recs_per_blk - 1) * fh->fh_rec_size;
  234.  
  235.         for (rec=fh->fh_recs_per_blk; rec >= 1; rec--)
  236.                 {       seq = (SEQ_REC) rbuf;
  237.  
  238.                         if (seq->seq_stat == DB_INUSE)
  239.                         {       buf->buf_rec_inx = rec;
  240.                                 memcpy(user_data, rbuf+fh->fh_ctl_size,
  241.                                                               fh->fh_data_size);
  242.                                 return;
  243.                         }
  244.             rbuf -= fh->fh_rec_size;
  245.                 }
  246.         }
  247.         db_error= DB_END_OF_FILE;
  248. }
  249.  
  250. /*
  251.  *      db_update_seq  -  Update Record in a Sequential File
  252.  */
  253.  
  254. void db_update_seq(df, user_data)
  255.  DATA_FILE df;
  256.  char *user_data;
  257. {
  258.         FILE_HDR  fh;
  259.         BUFFER   buf;
  260.         SEQ_REC  seq;
  261.         char   *rbuf;
  262.  
  263.         db_error = 0;
  264.  
  265.         fh  = (FILE_HDR) df->df_fhdr->buf_data;
  266.         buf = df->df_buf;
  267.  
  268.         if (buf->buf_cur_blk == 0 || buf->buf_rec_inx == 0)
  269.         {       db_error = DB_NO_CURRENT_REC;
  270.                 return;
  271.         }
  272.  
  273.         rbuf = buf->buf_data + (buf->buf_rec_inx - 1) * fh->fh_rec_size;
  274.  
  275.         seq = (SEQ_REC) rbuf;
  276.  
  277.         if (seq->seq_stat != DB_INUSE)
  278.         {       db_error = DB_DELETED_REC;
  279.                 return;
  280.         }
  281.  
  282.         memcpy(rbuf+fh->fh_ctl_size, user_data, fh->fh_data_size);
  283.  
  284.         db_put_blk(df, buf);
  285. }                           
  286.