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

  1. # include <stdio.h>
  2. # include <bench.h>
  3. # include <proc.io>
  4. # include "field.h"
  5. # include "sup.h"
  6. # include "screen.h"
  7. # include "iodef.h"
  8. # include "passwd.h"
  9.  
  10. extern int tot_size;
  11.  
  12. /*
  13.  * Driving AUTO_ADD Mode Logic
  14.  *
  15.  * Pre-Add the record, and update it later
  16.  * on, with the TRUE Add.
  17. */
  18. int auto_mode(dummy, tab, head, lptr, fld)
  19. FIELD *dummy;
  20. struct _table *tab;
  21. struct a_line **head, **lptr;
  22. struct fldinfx *fld;
  23. {
  24.     short tmpi = 0;
  25.     unsigned long tmpl = 0;
  26.     float tmpf = 0.0;
  27.     double tmpd = 0.0;
  28.     char tmps[20];
  29.     int istat, stat, try = 0;
  30.  
  31.     /*
  32.      * We should always have a current lptr for a screen program
  33.      * but not for a batch program. Therefore create one.
  34.     */
  35.     if (*lptr == ANULL)
  36.     {
  37.         (*lptr) = (struct a_line *) alloc(tot_size);
  38.         (*lptr)->curr = ANULL;
  39.     }
  40.  
  41.     /*
  42.      * Only allow 1 auto add per record
  43.     */
  44.     if ((*lptr)->curr != ANULL)
  45.         return (IOGOOD);
  46.  
  47.     /*
  48.      * Allocate current record, so that pointer is set.
  49.      * Indicates that we are in Auto-Add.
  50.     */
  51.     (*lptr)->curr = (struct a_line *) alloc(tot_size);
  52.  
  53.     /*
  54.      * Copy current record contents to a safe place
  55.     */
  56.     bytecpy((*lptr)->curr->rec, tab->rec, tab->size);
  57.  
  58.     /* 
  59.      * Get the Last Record
  60.     */
  61.     selectinx(tab->fd, tab->uniquekey, *(tab->keymatch), tab->retry);
  62.  
  63.     /*
  64.       * We could issue a message here depending on the user's
  65.      * setting, but to avoid unwanted messages we can swicth
  66.      * no_msg to TRUE.
  67.      *
  68.     no_msg = !(tab->messages);
  69.     */
  70.     no_msg = TRUE;
  71.  
  72.     istat = lastkey(tab->fd, tab->rec);
  73.  
  74.     /*
  75.      * Try & Lock retry times before failing
  76.     */
  77.     if (istat != IOEOF)
  78.         while (istat != IOGOOD && ++try <= tab->retry)
  79.         {
  80.             if (istat != IOGOOD && istat != IOLOCKED)
  81.                 return (do_error(istat));
  82.  
  83.             istat = lock_rec(tab->fd, tab->rec); 
  84.         }
  85.  
  86.     if (istat != IOEOF && istat != IOGOOD)
  87.         return (do_error(istat));
  88.  
  89.     /*
  90.      * Now, copy the contents of the current record
  91.      * back into the buffer, not forgetting to update
  92.      * the auto field, and write it.
  93.      * This involves knowing the fields offset, type
  94.      * and length etc, to be able to copy it.
  95.     */
  96.  
  97.     /*
  98.      * If we successfully find the last record, then create the 
  99.      * new value from this, otherwise just do a copy of the 
  100.      * default value which is passed in.
  101.     */
  102.     if (istat != IOEOF)
  103.         switch (fld->fldtype)
  104.         {
  105.        case CHRTYP : /* Numeric Literal */
  106.             bytecpy(tmps, tab->rec + fld->fldstart, fld->fldlen);
  107.             tmps[fld->fldlen] = '\0';  /* Make Sure */
  108.             tmpd = atof(tmps);
  109.             f_to_a(tmps, ++tmpd, fld->fldlen);
  110.             strcpy(dummy->fbuff, tmps);
  111.             break;
  112.        case INTTYP :
  113.             bytecpy((char *)&tmpi, tab->rec + fld->fldstart, sizeof(short));
  114.               sprintf(dummy->fbuff, "%d", ++tmpi);
  115.             break;
  116.        case LNGTYP :
  117.             bytecpy((char *)&tmpl, tab->rec + fld->fldstart, sizeof(long));
  118.               sprintf(dummy->fbuff, "%lu", ++tmpl);
  119.             break;
  120.        case FLTTYP :
  121.             bytecpy((char *)&tmpf, tab->rec + fld->fldstart, sizeof(float));
  122.             f_to_a(dummy->fbuff, ++tmpf, 20);  /* 20 is arbitrary */
  123.             break;
  124.        case DBLTYP :
  125.             bytecpy((char *)&tmpd, tab->rec + fld->fldstart, sizeof(double));
  126.             f_to_a(dummy->fbuff, ++tmpd, 20);  /* 20 is arbitrary */
  127.             break;
  128.         }
  129.     else
  130.         switch (fld->fldtype)
  131.         {
  132.        case CHRTYP : /* Numeric Literal */
  133.             strcpy(tmps, dummy->fbuff);
  134.             break;
  135.        case INTTYP :
  136.             tmpi = atoi(dummy->fbuff);
  137.             break;
  138.        case LNGTYP :
  139.             tmpl = atol(dummy->fbuff);
  140.             break;
  141.        case FLTTYP :
  142.             tmpf = (float) atof(dummy->fbuff);
  143.             break;
  144.        case DBLTYP :
  145.             tmpd = atof(dummy->fbuff);
  146.             break;
  147.         }
  148.  
  149.     /*
  150.      * Unlock Last Record if previously locked
  151.     */
  152.     if (istat != IOEOF && tab->retry != 0)
  153.         ulock_rec(tab->fd, tab->rec);
  154.  
  155.     /*
  156.      * Restore our present record's values
  157.     */
  158.     bytecpy(tab->rec, (*lptr)->curr->rec, tab->size);
  159.  
  160.     /*
  161.      * Update the New auto-increment field value
  162.     */
  163.     switch (fld->fldtype)
  164.     {
  165.    case CHRTYP : /* Numeric Literal */
  166.         bytecpy(tab->rec + fld->fldstart, tmps, fld->fldlen);
  167.         break;
  168.    case INTTYP :
  169.         bytecpy(tab->rec + fld->fldstart, (char *)&tmpi, sizeof(short));
  170.         break;
  171.    case LNGTYP :
  172.         bytecpy(tab->rec + fld->fldstart, (char *)&tmpl, sizeof(long));
  173.         break;
  174.    case FLTTYP :
  175.         bytecpy(tab->rec + fld->fldstart, (char *)&tmpf, sizeof(float));
  176.         break;
  177.    case DBLTYP :
  178.         bytecpy(tab->rec + fld->fldstart, (char *)&tmpd, sizeof(double));
  179.         break;
  180.     }
  181.  
  182.     /* 
  183.      * Now Add the New Last Record
  184.     */
  185.     stat = appendrec(tab->fd, tab->rec);
  186.  
  187.     if (do_error(stat) != IOGOOD)
  188.         return (do_error(stat));
  189.  
  190.     /*
  191.      * Try & Lock retry times before failing
  192.     */
  193.     do
  194.         stat = lock_rec(tab->fd, tab->rec); 
  195.     while (stat != IOGOOD && ++try <= tab->retry);
  196.  
  197.     return (istat);
  198. }
  199.  
  200.