home *** CD-ROM | disk | FTP | other *** search
- # include <stdio.h>
- # include <bench.h>
- # include <proc.io>
- # include "field.h"
- # include "sup.h"
- # include "screen.h"
- # include "iodef.h"
- # include "passwd.h"
-
- extern int tot_size;
-
- /*
- * Driving AUTO_ADD Mode Logic
- *
- * Pre-Add the record, and update it later
- * on, with the TRUE Add.
- */
- int auto_mode(dummy, tab, head, lptr, fld)
- FIELD *dummy;
- struct _table *tab;
- struct a_line **head, **lptr;
- struct fldinfx *fld;
- {
- short tmpi = 0;
- unsigned long tmpl = 0;
- float tmpf = 0.0;
- double tmpd = 0.0;
- char tmps[20];
- int istat, stat, try = 0;
-
- /*
- * We should always have a current lptr for a screen program
- * but not for a batch program. Therefore create one.
- */
- if (*lptr == ANULL)
- {
- (*lptr) = (struct a_line *) alloc(tot_size);
- (*lptr)->curr = ANULL;
- }
-
- /*
- * Only allow 1 auto add per record
- */
- if ((*lptr)->curr != ANULL)
- return (IOGOOD);
-
- /*
- * Allocate current record, so that pointer is set.
- * Indicates that we are in Auto-Add.
- */
- (*lptr)->curr = (struct a_line *) alloc(tot_size);
-
- /*
- * Copy current record contents to a safe place
- */
- bytecpy((*lptr)->curr->rec, tab->rec, tab->size);
-
- /*
- * Get the Last Record
- */
- selectinx(tab->fd, tab->uniquekey, *(tab->keymatch), tab->retry);
-
- /*
- * We could issue a message here depending on the user's
- * setting, but to avoid unwanted messages we can swicth
- * no_msg to TRUE.
- *
- no_msg = !(tab->messages);
- */
- no_msg = TRUE;
-
- istat = lastkey(tab->fd, tab->rec);
-
- /*
- * Try & Lock retry times before failing
- */
- if (istat != IOEOF)
- while (istat != IOGOOD && ++try <= tab->retry)
- {
- if (istat != IOGOOD && istat != IOLOCKED)
- return (do_error(istat));
-
- istat = lock_rec(tab->fd, tab->rec);
- }
-
- if (istat != IOEOF && istat != IOGOOD)
- return (do_error(istat));
-
- /*
- * Now, copy the contents of the current record
- * back into the buffer, not forgetting to update
- * the auto field, and write it.
- * This involves knowing the fields offset, type
- * and length etc, to be able to copy it.
- */
-
- /*
- * If we successfully find the last record, then create the
- * new value from this, otherwise just do a copy of the
- * default value which is passed in.
- */
- if (istat != IOEOF)
- switch (fld->fldtype)
- {
- case CHRTYP : /* Numeric Literal */
- bytecpy(tmps, tab->rec + fld->fldstart, fld->fldlen);
- tmps[fld->fldlen] = '\0'; /* Make Sure */
- tmpd = atof(tmps);
- f_to_a(tmps, ++tmpd, fld->fldlen);
- strcpy(dummy->fbuff, tmps);
- break;
- case INTTYP :
- bytecpy((char *)&tmpi, tab->rec + fld->fldstart, sizeof(short));
- sprintf(dummy->fbuff, "%d", ++tmpi);
- break;
- case LNGTYP :
- bytecpy((char *)&tmpl, tab->rec + fld->fldstart, sizeof(long));
- sprintf(dummy->fbuff, "%lu", ++tmpl);
- break;
- case FLTTYP :
- bytecpy((char *)&tmpf, tab->rec + fld->fldstart, sizeof(float));
- f_to_a(dummy->fbuff, ++tmpf, 20); /* 20 is arbitrary */
- break;
- case DBLTYP :
- bytecpy((char *)&tmpd, tab->rec + fld->fldstart, sizeof(double));
- f_to_a(dummy->fbuff, ++tmpd, 20); /* 20 is arbitrary */
- break;
- }
- else
- switch (fld->fldtype)
- {
- case CHRTYP : /* Numeric Literal */
- strcpy(tmps, dummy->fbuff);
- break;
- case INTTYP :
- tmpi = atoi(dummy->fbuff);
- break;
- case LNGTYP :
- tmpl = atol(dummy->fbuff);
- break;
- case FLTTYP :
- tmpf = (float) atof(dummy->fbuff);
- break;
- case DBLTYP :
- tmpd = atof(dummy->fbuff);
- break;
- }
-
- /*
- * Unlock Last Record if previously locked
- */
- if (istat != IOEOF && tab->retry != 0)
- ulock_rec(tab->fd, tab->rec);
-
- /*
- * Restore our present record's values
- */
- bytecpy(tab->rec, (*lptr)->curr->rec, tab->size);
-
- /*
- * Update the New auto-increment field value
- */
- switch (fld->fldtype)
- {
- case CHRTYP : /* Numeric Literal */
- bytecpy(tab->rec + fld->fldstart, tmps, fld->fldlen);
- break;
- case INTTYP :
- bytecpy(tab->rec + fld->fldstart, (char *)&tmpi, sizeof(short));
- break;
- case LNGTYP :
- bytecpy(tab->rec + fld->fldstart, (char *)&tmpl, sizeof(long));
- break;
- case FLTTYP :
- bytecpy(tab->rec + fld->fldstart, (char *)&tmpf, sizeof(float));
- break;
- case DBLTYP :
- bytecpy(tab->rec + fld->fldstart, (char *)&tmpd, sizeof(double));
- break;
- }
-
- /*
- * Now Add the New Last Record
- */
- stat = appendrec(tab->fd, tab->rec);
-
- if (do_error(stat) != IOGOOD)
- return (do_error(stat));
-
- /*
- * Try & Lock retry times before failing
- */
- do
- stat = lock_rec(tab->fd, tab->rec);
- while (stat != IOGOOD && ++try <= tab->retry);
-
- return (istat);
- }
-
-