home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 125.img / PRO-C4.ZIP / BENCH1.ZIP / GENSUP / DEL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-28  |  3.8 KB  |  208 lines

  1. # include <stdio.h>
  2. # include <bench.h>
  3. # include <proc.io>
  4. # include "field.h"
  5. # include "screen.h"
  6. # include "iodef.h"
  7. # include "sup.h"
  8. # include "passwd.h"
  9.  
  10. /*
  11.  * Delete the current record from the file
  12. */
  13. int delete_mode(tab, head, lptr, table, mode)
  14. struct _table *tab;
  15. struct a_line **head, **lptr;
  16. int table, mode;
  17. {
  18.     int stat, try = 0;
  19.    struct a_line *tmp;
  20.  
  21.     if (!(tab->perms & PERM_DELETE))
  22.     {
  23.         errmsg(ERR_NODEL);
  24.         return (IOPERM);
  25.     }
  26.  
  27.     if (*lptr == ANULL)
  28.         return (IOGOOD);
  29.  
  30.     if ((*lptr)->deleted == TRUE)
  31.     {
  32.         errmsg(ERR_DELETE);
  33.         return (IOGOOD);
  34.     }
  35.  
  36.     if (tab->query_box && !warning(99, DEL_CONFIRM))
  37.         return (IOGOOD);
  38.  
  39.     /*
  40.      * Lock the required record
  41.     */
  42.     no_msg = !(tab->messages);
  43.  
  44.     stat = lock_rec(tab->fd, tab->rec);
  45.  
  46.     while (stat != IOGOOD && ++try <= tab->retry)
  47.     {
  48.         if (stat != IOGOOD && stat != IOLOCKED)
  49.             return (do_error(stat));
  50.  
  51.         stat = lock_rec(tab->fd, tab->rec); 
  52.     }
  53.  
  54.     if (stat != IOGOOD)
  55.         return (do_error(stat));
  56.  
  57.     stat = removerec(tab->fd, tab->rec);
  58.  
  59.     if (do_error(stat) == IOGOOD)
  60.     {
  61.         /*
  62.          * Delete from the list if deleted from
  63.          * the file successfully
  64.         */
  65.         delete_list(tab, head, lptr, tab->index);
  66.  
  67.         /*
  68.          * do_page_mode adjusts lptr, so save it and restore
  69.         */
  70.        tmp = *lptr;
  71.  
  72.         do_page_mode(tab, head, lptr, mode);
  73.  
  74.           *lptr = tmp;
  75.  
  76. # ifdef NOTHERE
  77.         if (*lptr != ANULL)
  78.         {
  79.            bytecpy(tab->rec, (*lptr)->rec, tab->size);
  80.  
  81.             /*
  82.              * Re-find the current record
  83.             */
  84.             stat = findkey(tab->fd, tab->rec);
  85.  
  86.             /*
  87.              * Try & Lock retry times before failing
  88.             */
  89.             while (stat != IOGOOD && ++try <= tab->retry)
  90.             {
  91.                 if (stat != IOGOOD && stat != IOLOCKED)
  92.                     return (do_error(stat));
  93.  
  94.                 stat = lock_rec(tab->fd, tab->rec); 
  95.             }
  96.  
  97.             if (stat != IOGOOD)
  98.                 return (do_error(stat));
  99.  
  100.             if (tab->retry != 0)
  101.                 ulock_rec(tab->fd, tab->rec);
  102.         }
  103. # endif
  104.     }
  105.  
  106.     return (stat);
  107. }
  108.  
  109.  
  110. /*
  111.  * Remove record from the list
  112.  *
  113.  * We need to set the current record pointer to 
  114.  * something sensible after a deletion.  
  115.  * The current record is retained by all file managers
  116.  * even after a delete, and so the next/prev records
  117.  * can be obtained.
  118. */
  119. void delete_list(tab, head, lptr, idx)
  120. struct _table *tab;
  121. struct a_line **head, **lptr;
  122. int *idx;
  123. {
  124.    struct a_line *tmp, *prv, *nxt;
  125.     int stat = IOERROR;
  126.     int try = 0;
  127.  
  128.    nxt = (*lptr)->next;
  129.    prv = (*lptr)->prev;
  130.    tmp = ANULL;
  131.  
  132.     /*
  133.      * Check forward pointers
  134.     */
  135.    if (nxt != ANULL)
  136.    {
  137.         stat = nextrec(tab->fd, (*lptr)->rec);
  138.  
  139.         while ((stat = lock_rec(tab->fd, tab->rec)) != IOGOOD && ++try <= tab->retry)
  140.             ;
  141.  
  142.       tmp = nxt;
  143.       nxt->prev = prv;
  144.    }
  145.    else
  146.    {
  147.       (*idx)--;
  148.  
  149.         /*
  150.          * Deleting the only record on the list !!
  151.         */
  152.       if (*idx < 0)
  153.       {
  154.          *idx = 0;
  155.          *head = prv;
  156.       }
  157.    }
  158.  
  159.     /*
  160.      * Check backward pointers
  161.     */
  162.    if (prv != ANULL)
  163.    {
  164.         /*
  165.          * Only try to get the previous record
  166.          * if there is no next record.
  167.          * File Managers which have trouble doing
  168.          * a prevrec (eg Oracle) will not like this.
  169.         */
  170.         if (stat != IOGOOD)
  171.         {
  172.             try = 0;
  173.  
  174.             stat = prevrec(tab->fd, (*lptr)->rec);
  175.  
  176.             while ((stat = lock_rec(tab->fd, tab->rec)) != IOGOOD && ++try <= tab->retry)
  177.                 ;
  178.  
  179.           tmp = prv;
  180.         }
  181.  
  182.       prv->next = nxt;
  183.    }
  184.    else
  185.       *head = nxt;
  186.  
  187.    free((char *)*lptr);
  188.  
  189.     /*
  190.      * If we cannot establish a current record for
  191.      * any reason, then set all pointers to NULL to
  192.      * avoid any horrible situation that may arise
  193.      * This may occur in managers such as Oracle
  194.      * which cannot do a prevrec.
  195.     */
  196.     if (stat != IOGOOD && stat != IOERROR)
  197.     {
  198.         do_error(stat);
  199.         *lptr = *head = tmp = ANULL;
  200.     }
  201.  
  202.     if (stat == IOGOOD && tab->retry != 0)
  203.         ulock_rec(tab->fd, tab->rec);
  204.  
  205.     *lptr = tmp;
  206. }
  207.  
  208.