home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / PROCWRKB.ZIP / BENCH1.ZIP / GENSUP / DEL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-21  |  4.0 KB  |  203 lines

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