home *** CD-ROM | disk | FTP | other *** search
- /*
- * isam routines
- *
- * This program is the CONFIDENTIAL and PROPRIETARY property
- * of FairCom(R) Corporation. Any unauthorized use, reproduction or
- * transfer of this program is strictly prohibited.
- *
- * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989 FairCom Corporation
- * (Subject to limited distribution and
- * restricted disclosure only.)
- * *** ALL RIGHTS RESERVED ***
- *
- * 4006 West Broadway
- * Columbia, MO 65203
- *
- *
- * c-tree(R) Version 4.3
- * Release C
- * February 7, 1989 17:30
- *
- */
-
- #include "ctstdr.h" /* standard i/o header */
- #include "ctoptn.h" /* c-tree configuration options */
- #include "cterrc.h" /* c-tree error codes */
- #include "ctstrc.h" /* c-tree data structures */
- #include "ctgvar.h" /* c-tree global variables */
- #include "ctisam.h" /* c-tree isam header */
- #ifdef VARLDATA
- #include "ctvrec.h"
- #endif
-
- #define SEQNXT 1
- #define SEQPRV 2
-
- POINTER NEWREC(),EQLKEY(),GTEKEY(),NXTKEY(),PRVKEY(),FRSKEY();
- POINTER LSTKEY(),GTKEY(),LTKEY(),LTEKEY();
- COUNT compar();
- COUNT REDREC(),WRTREC(),RETREC(),frmkey();
- COUNT ierr(),addlok(),addikey(),rwtikey(),delikey();
- CTFILE *tstfnm();
- COUNT ctio();
- VOID iundo();
- #ifdef VARLDATA
- COUNT getvhdr();
- LOCAL VHDR vrhdr;
- #endif
-
- #ifdef CTSERVER
- COUNT MLTWRT();
- EXTERN UCOUNT cts_mxaln,cts_clen;
- EXTERN COUNT cts_ctry,cts_csuc,cts_cdat,cts_cseq;
- EXTERN TEXT *cts_cptr;
-
- cmbchk(keyno,recptr)
- COUNT keyno;
- TEXT *recptr;
- {
- FAST CTFILE *knum,*dnum;
-
- cts_ctry = cts_csuc = cts_cseq = NO;
- if ((knum = tstfnm(keyno)) == NULL || (dnum = tstfnm(ct_rvmap[keyno])) ==
- NULL)
- return;
- if ((knum->length + dnum->reclen) <= cts_mxaln) {
- cts_cptr = recptr;
- cts_clen = dnum->reclen;
- cts_ctry = CMBREDWO;
- cts_cdat = ct_rvmap[keyno];
- if (ct_ismlk == ENABLE)
- cts_ctry = CMBREDLK;
- else if (ct_ismlk == READREC)
- cts_ctry = CMBREDLK + 1;
- }
- }
- #endif
-
- LOCAL COUNT ismred(datno,keyno,pntr,recptr)
- COUNT datno,keyno;
- POINTER pntr;
- TEXT *recptr;
- {
- #ifdef CTSERVER
- COUNT tmpsuc;
- #endif
-
- #ifdef MUSTFRCE
- TEXT chkkey[MAXLEN];
- COUNT i;
-
- ct_vfsg = NO;
- #endif
-
- #ifdef CTSERVER
- tmpsuc = cts_csuc;
- cts_csuc = NO;
- if (tmpsuc == NO) {
- if (REDREC(datno,pntr,recptr))
- return(ierr(uerr_cod,datno));
- }
- #else
- if (REDREC(datno,pntr,recptr))
- return(ierr(uerr_cod,datno));
- #endif
-
- if (*recptr == DELFLG && (ct_key + datno)->clstyp == DAT_CLOSE)
- #ifndef MUSTFRCE
- return(ierr(IRED_ERR,datno));
- #else
- return(ierr(ITIM_ERR,datno));
-
- if (datno != keyno) {
- if (ct_vfin[keyno] == YES)
- ct_vfsg = keyno + 1;
- else {
- frmkey(keyno,recptr,chkkey,pntr);
- if (compar(chkkey,ct_fndval,ct_key+keyno))
- return(ierr(ITIM_ERR,datno));
- }
- }
-
- #endif
-
- cur_recno[datno] = pntr;
- cur_image[datno] = recptr;
- return(ierr(NO_ERROR,0));
- }
-
-
- COUNT reset_cur(keyno,pntr,recptr)
- PFAST COUNT keyno;
- POINTER pntr;
- TEXT *recptr;
- {
- FAST COUNT datno;
-
- if (!pntr)
- if (uerr_cod)
- return(ierr(uerr_cod,keyno));
- else
- return(ierr(INOT_ERR,keyno)); /* target not found */
-
- datno = ct_rvmap[keyno];
- if (addlok(pntr,datno))
- return(isam_err);
- else
- return(ismred(datno,keyno,pntr,recptr));
- }
-
- LOCAL COUNT reset_phy(ctnum,pntr,recptr,mode)
- CTFILE *ctnum;
- POINTER pntr;
- TEXT *recptr;
- COUNT mode;
- {
- POINTER bfpos;
- COUNT tstrec();
-
- isam_err = uerr_cod = NO_ERROR;
- #ifdef VARLDATA
- if (ctnum->clstyp == VAT_CLOSE) {
- while (getvhdr(ctnum,pntr,&vrhdr) == NO_ERROR &&
- (vrhdr.recmrk == VDEL_FLAG || vrhdr.recmrk == VNOD_FLAG))
- pntr += (vrhdr.trclen + SIZVHDR);
- if (uerr_cod == NO_ERROR && vrhdr.recmrk != VACT_FLAG &&
- tstrec(ctnum,pntr) == NO_ERROR)
- uerr_cod = VFLG_ERR;
- } else {
- #endif
- if (mode == SEQPRV) {
- bfpos = (ctnum->reclen + 127) / ctnum->reclen *
- ctnum->reclen;
- if (pntr < bfpos)
- return(ierr(uerr_cod = LBOF_ERR,
- ctnum->filnum));
- }
- while (ctio(CTREAD,ctnum,pntr,ct_buf,1) == NO_ERROR &&
- ct_buf[0] == DELFLG)
- if (mode == SEQNXT) {
- if (tstrec(ctnum,(pntr += ctnum->reclen)))
- break;
- } else {
- if ((pntr -= ctnum->reclen) < bfpos) {
- uerr_cod = LBOF_ERR;
- break;
- }
- }
- #ifdef VARLDATA
- }
- #endif
-
- if (uerr_cod)
- return(ierr(uerr_cod,ctnum->filnum));
- else if (addlok(pntr,ctnum->filnum ))
- return(isam_err);
- else
- return(ismred(ctnum->filnum,ctnum->filnum,pntr,recptr));
- }
-
- COUNT LKISAM(lokmod)
- COUNT lokmod;
- {
- FAST COUNT i;
- FAST LOKS *lp;
-
- COUNT UDLOCK();
- #ifdef CTSERVER
- VOID intlmsg(),addlmsg(),sndlmsg();
- #endif
-
- switch (lokmod) {
- case SUSPEND:
- if (ct_ismlk == ENABLE || ct_ismlk == READREC)
- ct_ismlk = SUSPEND;
- break;
- case RESTORE:
- if (ct_ismlk == SUSPEND)
- ct_ismlk = ENABLE;
- break;
- case RESTRED:
- if (ct_ismlk == SUSPEND)
- ct_ismlk = READREC;
- break;
- case READREC:
- if (ct_ismlk == ENABLE)
- ct_ismlk = READREC;
- break;
- }
- if (lokmod > ENABLE)
- return(isam_err = NO_ERROR);
-
- #ifdef CTSERVER
- if (lokmod == FREE || lokmod == RESET)
- intlmsg();
- #endif
- for (i = 0, lp = ct_locks; i < MAX_LOCKS; i++,lp++)
- if (lp->datfnm >= 0) {
- if (lokmod == FREE || lokmod == RESET) {
- #ifdef CTSERVER
- addlmsg(lp->recnum,lp->datfnm);
- #else
- UDLOCK(lp->recnum,ct_key + lp->datfnm);
- #endif
- lp->datfnm = -1;
- } else
- return(ierr(IPND_ERR,0));
- }
- #ifdef CTSERVER
- if (lokmod == FREE || lokmod == RESET)
- sndlmsg();
- #endif
- if (lokmod != RESET)
- ct_ismlk = lokmod;
- else
- ct_ismlk = ENABLE;
-
- return(isam_err = NO_ERROR);
- }
-
- COUNT ADDREC(datno,recptr)
- PFAST COUNT datno;
- TEXT *recptr;
- {
- POINTER pntr;
- DATFILE *dnum;
- #ifdef CTSERVER
- COUNT j;
- #endif
-
- isam_err = NO_ERROR;
- if ((dnum = tstfnm(datno)) == NULL)
- return(ierr(uerr_cod,datno));
- else if (dnum->clstyp != DAT_CLOSE)
- return(ierr(FMOD_ERR,datno));
- #ifdef CTSERVER
- pntr = DRNZERO;
- ct_nwrcfg = YES;
- j = MLTWRT(datno,&pntr,recptr,dnum->reclen,dnum->srlpos);
- if (j == -2)
- return(ierr(uerr_cod,0));
- else if (j || addlok(pntr,datno)) {
- ierr(uerr_cod,datno);
- ct_nwrcfg = NO;
- if (j != 1)
- iundo(ISADD,datno,0,recptr,pntr,DRNZERO);
- return(isam_err);
- }
-
- #else
- else if ((pntr = NEWREC(datno)) == DRNZERO)
- return(ierr(uerr_cod,datno));
- setsrlpos(datno,recptr);
-
- ct_nwrcfg = YES;
- if (addlok(pntr,datno))
- return(isam_err);
-
- if (WRTREC(datno,pntr,recptr)) {
- ierr(uerr_cod,datno);
- iundo(ISADD,datno,0,recptr,pntr,DRNZERO);
- return(isam_err);
- }
- #endif
-
- if (addikey(datno,recptr,pntr))
- return(isam_err);
-
- cur_recno[datno] = pntr;
- cur_image[datno] = recptr;
- #ifdef DOSFLUSH
- flushdos(datno);
- #endif
- return(NO_ERROR);
- }
-
- POINTER chkism(datno)
- COUNT datno;
-
- {
- DATFILE *dnum;
- POINTER pntr;
-
- isam_err = NO_ERROR;
-
- if ((dnum = tstfnm(datno)) == NULL)
- ierr(uerr_cod,datno);
- else if (dnum->clstyp != DAT_CLOSE)
- ierr(FMOD_ERR,datno);
- else if (!(pntr = cur_recno[datno]))
- ierr(ICUR_ERR,datno);
- else
- addlok(pntr,datno);
-
- if (!isam_err)
- return(pntr);
- else
- return(0);
- }
-
-
-
- COUNT RWTREC(datno,recptr) /* assumes that current record for datno is old
- version to be rewritten by recptr */
- PFAST COUNT datno;
- TEXT *recptr;
- {
- POINTER pntr;
- COUNT i;
-
- if (!(pntr = chkism(datno)))
- return(isam_err);
-
- if ((i = rwtikey(datno,recptr,pntr,pntr)) > 0)
- return(isam_err);
-
- if (WRTREC(datno,pntr,recptr)) {
- ierr(uerr_cod,datno);
- iundo(ISRWT,datno,-i,recptr,pntr,pntr);
- return(isam_err);
- }
-
- cur_recno[datno] = pntr;
- cur_image[datno] = recptr;
- #ifdef DOSFLUSH
- flushdos(datno);
- #endif
- return(NO_ERROR);
- }
-
- COUNT DELREC(datno)
- PFAST COUNT datno;
- {
- TEXT *recptr;
- POINTER pntr;
- COUNT i;
-
- if (!(pntr = chkism(datno)))
- return(isam_err);
-
- recptr = cur_image[datno];
-
- if ((i = delikey(datno,recptr,pntr)) > 0)
- return(isam_err);
-
- if (RETREC(datno,pntr)) {
- ierr(uerr_cod,datno);
- iundo(ISDEL,datno,-i,recptr,DRNZERO,pntr);
- return(isam_err);
- }
- #ifdef DOSFLUSH
- flushdos(datno);
- #endif
- return(NO_ERROR);
- }
-
- COUNT EQLREC(keyno,target,recptr)
-
- PFAST COUNT keyno;
- PFAST TEXT *target;
- PFAST TEXT *recptr;
-
- {
- #ifdef MUSTFRCE
- cpybuf(ct_fndval,target,(ct_key + keyno)->length);
- #endif
- #ifdef CTSERVER
- cmbchk(keyno,recptr);
- #endif
- return(reset_cur(keyno,EQLKEY(keyno,target),recptr));
- }
-
- COUNT GTEREC(keyno,target,recptr)
-
- PFAST COUNT keyno;
- PFAST TEXT *target;
- PFAST TEXT *recptr;
-
- {
- #ifdef CTSERVER
- cmbchk(keyno,recptr);
- #endif
- return(reset_cur(keyno,GTEKEY(keyno,target,ct_fndval),recptr));
- }
-
- COUNT LTEREC(keyno,target,recptr)
-
- PFAST COUNT keyno;
- PFAST TEXT *target;
- PFAST TEXT *recptr;
-
- {
- #ifdef CTSERVER
- cmbchk(keyno,recptr);
- #endif
- return(reset_cur(keyno,LTEKEY(keyno,target,ct_fndval),recptr));
- }
-
- LOCAL COUNT seqrec(mode,filno,recptr)
- COUNT mode,filno;
- TEXT *recptr;
- {
- FAST COUNT datno;
- POINTER pntr;
- FAST CTFILE *ctnum;
-
- if ((ctnum = tstfnm(filno)) == NULL)
- return(ierr(uerr_cod,filno));
- if (ctnum->clstyp == IDX_CLOSE)
- datno = ct_rvmap[filno];
- else
- datno = filno;
- if (!(pntr = cur_recno[datno]))
- return(ierr(ICUR_ERR,datno));
-
- if (ctnum->clstyp == IDX_CLOSE) {
- if (frmkey(filno,cur_image[datno],ct_kyval,pntr)) {
- #ifdef CTSERVER
- cmbchk(filno,recptr);
- #endif
- if (mode == SEQNXT)
- pntr = GTKEY(filno,ct_kyval,ct_fndval);
- else
- pntr = LTKEY(filno,ct_kyval,ct_fndval);
- return(reset_cur(filno,pntr,recptr));
- } else
- return(ierr(INOT_ERR,filno));
- } else if (ctnum->clstyp == DAT_CLOSE) {
- if (mode == SEQNXT)
- pntr += ctnum->reclen;
- else
- pntr -= ctnum->reclen;
- #ifdef VARLDATA
- } else {
- if (mode != SEQNXT)
- return(ierr(uerr_cod = FMOD_ERR,filno));
- if (getvhdr(ctnum,pntr,&vrhdr))
- return(ierr(uerr_cod,filno));
- pntr += (vrhdr.trclen + SIZVHDR);
- }
- #else
- }
- #endif
- datno = reset_phy(ctnum,pntr,recptr,mode);
- if (datno == LEOF_ERR || datno == LBOF_ERR || datno == READ_ERR)
- isam_err = INOT_ERR;
- return(isam_err);
- }
-
- COUNT NXTREC(filno,recptr)
- COUNT filno;
- TEXT *recptr;
- {
- return(seqrec(SEQNXT,filno,recptr));
- }
-
- COUNT PRVREC(filno,recptr)
- PFAST COUNT filno;
- TEXT *recptr;
- {
- return(seqrec(SEQPRV,filno,recptr));
- }
-
- LOCAL COUNT bndrec(mode,filno,recptr)
- COUNT mode,filno;
- PFAST TEXT *recptr;
- {
- FAST CTFILE *ctnum;
- POINTER recbyt;
- #ifdef MUSTFRCE
- COUNT tstrec();
- #endif
-
- if ((ctnum = tstfnm(filno)) == NULL)
- return(ierr(uerr_cod,filno));
- if (ctnum->clstyp == IDX_CLOSE) {
- #ifdef CTSERVER
- cmbchk(filno,recptr);
- #endif
- if (mode == SEQNXT)
- recbyt = FRSKEY(filno,ct_fndval);
- else
- recbyt = LSTKEY(filno,ct_fndval);
- return(reset_cur(filno,recbyt,recptr));
- } else if (ctnum->clstyp == DAT_CLOSE) {
- if (mode == SEQNXT)
- recbyt = (ctnum->reclen + 127) / ctnum->reclen *
- ctnum->reclen;
- else {
- #ifdef MUSTFRCE
- if (tstrec(ctnum,ctnum->numrec + 1L) &&
- uerr_cod != LEOF_ERR)
- return(ierr(uerr_cod,filno));
- #endif
- recbyt = ctnum->numrec + 1L - ctnum->reclen;
- }
- }
- #ifdef VARLDATA
- else {
- if (mode == SEQNXT)
- recbyt = ctnum->recsiz + SIZVHDR;
- else
- return(ierr(uerr_cod = FMOD_ERR,filno));
- }
- #endif
- filno = reset_phy(ctnum,recbyt,recptr,mode);
- if (filno == LEOF_ERR || filno == LBOF_ERR || filno == READ_ERR)
- isam_err = INOT_ERR;
- return(isam_err);
- }
-
- COUNT FRSREC(filno,recptr)
- PFAST COUNT filno;
- PFAST TEXT *recptr;
- {
- return(bndrec(SEQNXT,filno,recptr));
- }
-
- COUNT LSTREC(filno,recptr)
- PFAST COUNT filno;
- TEXT *recptr;
- {
- return(bndrec(SEQPRV,filno,recptr));
- }
-
- RRDREC(datno,recptr)
- PFAST COUNT datno;
- TEXT *recptr;
- {
- isam_err = 0;
- if (cur_recno[datno] == DRNZERO)
- return(ierr(ICUR_ERR,datno));
- else if (addlok(cur_recno[datno],datno))
- return(isam_err);
- else
- return(ismred(datno,datno,cur_recno[datno],recptr));
- }
-
- /* end of ctisam.c */
-