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 / AUTO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-16  |  5.1 KB  |  212 lines

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