home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c004 / 1.ddi / CTISMU.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-18  |  11.7 KB  |  531 lines

  1. /*
  2.  *    isam utilities
  3.  *
  4.  *    This program is the CONFIDENTIAL and PROPRIETARY property 
  5.  *    of FairCom(R) Corporation. Any unauthorized use, reproduction or
  6.  *    transfer of this program is strictly prohibited.
  7.  *
  8.  *      Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989 FairCom Corporation
  9.  *    (Subject to limited distribution and
  10.  *     restricted disclosure only.)
  11.  *    *** ALL RIGHTS RESERVED ***
  12.  *
  13.  *    4006 West Broadway
  14.  *    Columbia, MO 65203
  15.  *
  16.  *
  17.  *    c-tree(R)    Version 4.3
  18.  *            Release C 
  19.  *            February 7, 1989 17:30
  20.  *
  21.  */
  22.  
  23.  
  24. #include "ctstdr.h"        /* standard i/o header         */
  25. #include "ctoptn.h"        /* c-tree configuration options */
  26. #include "cterrc.h"        /* c-tree error codes        */
  27. #include "ctstrc.h"        /* c-tree data structures    */
  28. #include "ctgvar.h"        /* c-tree global variables    */
  29. #undef  EXTERN
  30. #define EXTERN /* */
  31. #include "ctisam.h"        /* c-tree isam header        */
  32.  
  33. #ifdef REVBIN
  34. #undef REVBIN
  35. #endif
  36. #ifdef LOW_HIGH
  37. #define REVBIN
  38. #endif
  39. #ifdef HIGH_LOW
  40. #ifdef UNIFRMAT
  41. #define REVBIN
  42. #endif
  43. #endif
  44.  
  45. #ifdef CTSERVER
  46. extern COUNT cts_csuc;
  47. #endif
  48.  
  49. #ifdef PARMFILE
  50. #ifdef PERC_H
  51. #define IC1    "%h %h %h %h"
  52. #define IC2    "%h %s %h %h %h %h"
  53. #define IC3    "%h %h %h"
  54. #define IC4    "%h %s %h %h %h %h %h %h %h %h %h"
  55. #define IC5    "%h %h %h %h %h %h %h"
  56. #endif
  57. #ifdef PERC_D
  58. #define IC1    "%d %d %d %d"
  59. #define IC2    "%d %s %d %d %d %d"
  60. #define IC3    "%d %d %d"
  61. #define IC4    "%d %s %d %d %d %d %d %d %d %d %d"
  62. #define IC5    "%d %d %d %d %d %d %d"
  63. #endif
  64. #ifdef PERC_HD
  65. #define IC1    "%hd %hd %hd %hd"
  66. #define IC2    "%hd %s %hd %hd %hd %hd"
  67. #define IC3    "%hd %hd %hd"
  68. #define IC4    "%hd %s %hd %hd %hd %hd %hd %hd %hd %hd %hd"
  69. #define IC5    "%hd %hd %hd %hd %hd %hd %hd"
  70. #endif
  71. #endif
  72.  
  73. #ifdef RTREE
  74. #include "rtpars.h"
  75. #include "rtstrc.h"
  76.  
  77. RKEY  usrkey[MAXFIL];
  78. RDAT  usrdat[MAXFIL];
  79. COUNT srtknm,srtdat,tmpdat;
  80.  
  81. #ifdef PARMFILE
  82. TEXT         *mballc();
  83.  
  84. COUNT setudat(ifd,datno)
  85. FILE         *ifd;
  86. COUNT          datno;
  87. {
  88.     COUNT ierr();
  89.  
  90.     COUNT i;
  91.  
  92.     for (i = 1; i < 3; i++) {
  93.         if (fscanf(ifd,"%s",ct_buf) != 1)
  94.             return(ierr(IDRU_ERR,datno));
  95.         if ((usrdat[datno].fldnam[i] = mballc(1,strlen(ct_buf)+1)) ==
  96.             NULL)
  97.             return(ierr(IDMU_ERR,datno));
  98.         strcpy(usrdat[datno].fldnam[i],ct_buf);
  99.     }
  100.     return(NO_ERROR);
  101. }
  102.  
  103. COUNT setukey(ifd,keyno,keytyp)
  104. FILE         *ifd;
  105. COUNT          keyno,keytyp;
  106. {
  107.     COUNT ierr();
  108.  
  109.     if (fscanf(ifd,"%s",ct_buf) != 1)
  110.         return(ierr(IKRU_ERR,keyno));
  111.     else if ((usrkey[keyno].rsymb = mballc(1,strlen(ct_buf)+1)) == NULL)
  112.         return(ierr(IKMU_ERR,keyno));
  113.     else {
  114.         strcpy(usrkey[keyno].rsymb,ct_buf);
  115.         usrkey[keyno].rtype = keytyp;
  116.         return(NO_ERROR);
  117.     }
  118. }
  119. #endif
  120. #endif
  121.  
  122. COUNT RLOCK(),DLOCK();
  123.  
  124. COUNT ierr(err_cod,file_no)
  125. COUNT err_cod;
  126. COUNT file_no;
  127.  
  128. {
  129.     isam_fil = file_no;
  130.     return(isam_err = err_cod);
  131. }
  132.  
  133.  
  134. COUNT addlok(pntr,datno)
  135. POINTER pntr;
  136. COUNT   datno;
  137. {
  138.     FAST COUNT   i,d;
  139.     COUNT        is,tmpflg;
  140.     FAST LOKS   *lp;
  141.  
  142.     CTFILE      *tstfnm();
  143.  
  144.     tmpflg = ct_nwrcfg;
  145.     ct_nwrcfg = NO;
  146.     if (ct_ismlk == FREE || ct_ismlk == SUSPEND)
  147.         return(NO_ERROR);
  148.  
  149.     for (i = 0, is = -1, lp = ct_locks; i < MAX_LOCKS; i++,lp++)
  150.         if ((d = lp->datfnm) < 0 && is < 0)
  151.             is = i;
  152.         else if (d == datno && lp->recnum == pntr) {
  153.             if (lp->ltltyp == ct_ismlk)
  154.                 return(NO_ERROR);
  155.             else {
  156.                 is = i;
  157.                 break;
  158.             }
  159.         }
  160.  
  161.     if (is < 0)
  162.         return(ierr(INOL_ERR,datno));
  163.     if (!tmpflg && (tstfnm(datno) == NULL ||
  164. #ifdef CTSERVER
  165.        (ct_ismlk == ENABLE  && cts_csuc == NO && DLOCK(pntr,ct_key + datno)) ||
  166.        (ct_ismlk == READREC && cts_csuc == NO && RLOCK(pntr,ct_key + datno))))
  167. #else
  168.        (ct_ismlk == ENABLE  && DLOCK(pntr,ct_key + datno)) ||
  169.        (ct_ismlk == READREC && RLOCK(pntr,ct_key + datno))))
  170. #endif
  171.         return(ierr(uerr_cod,datno));
  172.  
  173.     (lp = ct_locks + is)->datfnm = datno;
  174.     lp->recnum          = pntr;
  175.     lp->ltltyp          = ct_ismlk;
  176.     return(NO_ERROR);
  177. }
  178.  
  179. TEXT ucase(t)
  180. TEXT       t;
  181. {
  182.     if (t >= 'a' && t <= 'z')
  183.         return((TEXT) (t + 'A' - 'a'));
  184.     else
  185.         return(t);
  186. }
  187.  
  188.  
  189. COUNT frmkey(keyno,recptr,txt,pntr)
  190. COUNT       keyno;
  191. TEXT       *recptr;
  192. PFAST TEXT *txt;
  193. POINTER        pntr;
  194. {
  195.     FAST COUNT j;
  196.     COUNT        i,k,len,slen,mod,altflg,dscflg;
  197.     FAST TEXT *recpos;
  198.     TEXT       *savtxt;
  199.     CTFILE    *ctnum;
  200.  
  201.     ctnum  = ct_key + ct_rvmap[keyno];
  202.     savtxt = txt;
  203.     for (i = len = 0; i < MAX_KEY_SEG; i++) {
  204.         if ((k = ct_sgpos[keyno][i]) < 0)
  205.             break;
  206.         recpos    = recptr + k;
  207.         len    += (slen = ct_sglen[keyno][i]);
  208.         mod    = ct_sgmod[keyno][i];
  209.         dscflg    = mod & DSCSEG;
  210.         altflg    = mod & ALTSEG;
  211.         mod    &= SEGMSK;
  212.         switch (mod) {
  213. case REGSEG:
  214. #ifndef NATURAL_SERIAL
  215. case SRLSEG:
  216. #endif
  217. #ifndef REVBIN
  218. case INTSEG:
  219. #ifdef NATURAL_SERIAL
  220. case SRLSEG:
  221. #endif
  222. #endif
  223.         for (j = 0; j++ < slen; )
  224.             *txt++ = *recpos++;
  225.         break;
  226. #ifdef REVBIN
  227. case INTSEG:
  228. #ifdef NATURAL_SERIAL
  229. case SRLSEG:
  230. #endif
  231.         recpos += slen;
  232.         for (j = 0; j++ < slen; )
  233.             *txt++ = *(--recpos);
  234.         break;
  235. #endif
  236. case UREGSEG:
  237.         for (j = 0; j++ < slen; )
  238.             *txt++ = ucase(*recpos++);
  239.         break;
  240.  
  241. #ifdef VARLDATA
  242. case VARSEG:
  243. case UVARSEG:
  244.         recpos = recptr + ctnum->reclen; /* ptr to end of minlen */
  245.         /* skip k var segments each terminated by FIELD_DELIM */
  246.         while (k-- > 0)
  247.             while (*recpos++ != FIELD_DELIM)
  248.                 ;
  249.         k = 0;
  250.         while (k < slen && *recpos != FIELD_DELIM) {
  251.             k++;
  252.             if (mod == UVARSEG)
  253.                 *txt++ = ucase(*recpos++);
  254.             else
  255.                 *txt++ = *recpos++;
  256.         }
  257.         while (k++ < slen)
  258.             *txt++ = PADDING;
  259.         break;
  260. #endif
  261.  
  262. case SGNSEG:
  263. case FLTSEG:
  264. #ifdef REVBIN
  265.         txt += slen;
  266.         for (j = 0; j++ < slen; )
  267.             *(--txt) = *recpos++;
  268. #else
  269.         cpybuf(txt,recpos,slen);
  270. #endif
  271.         if (*txt & 0x0080) {
  272.             if (mod == SGNSEG)
  273.             *txt &= 0x007f;
  274.             else
  275.             dscflg = !dscflg;
  276.         } else
  277.             *txt |= 0x0080;
  278.  
  279.         txt += slen;
  280.         break;
  281.  
  282. default:
  283.         terr(226);
  284.         }
  285.  
  286. /*           ALTERNATIVE COLLATING SEQUENCE            */
  287. /*                                    */
  288. /*    altseq should point to an array which contains a remapping of    */
  289. /* ASCII bytes into an acceptable ordering for those cases (such as    */
  290. /* foreign language character sets) in which the standard ASCII    sequence*/
  291. /* is not acceptable. altseq will only be used if the key segment mode    */
  292. /* has been or'ed with 16.  For example, a segment mode of 4 (variable    */
  293. /* length field segment) can be converted to an alternative sequence by */
  294. /* adding 16 yielding a mode value of 20.                */
  295. /*                                    */
  296. /*    The constraints on altseq[] are that altseq[0] == 0,         */
  297. /* altseq[255] == 255, and that no value of altseq[] exceed 255.    */
  298. /*                                    */
  299.  
  300.  
  301.         if (altflg) {
  302.             txt -= slen;
  303.             for (j = 0; j < slen; j++, txt++)
  304.                 *txt = altseq[0x00ff & *txt];
  305.         }
  306.         if (dscflg) {
  307.             txt -= slen;
  308.             for (j = 0; j < slen; j++, txt++)
  309.                 *txt = 0x00ff & ~(*txt);
  310.         }
  311.     }
  312.  
  313.     slen = len;
  314.     if ((ct_key + keyno)->autodup) {
  315.         recpos = (TEXT *) &pntr;
  316.         len   += sizeof(POINTER);
  317.  
  318. #ifdef LOW_HIGH
  319.         txt += sizeof(POINTER);
  320.         for (j = 0; j < sizeof(POINTER); j++)
  321.             *--txt = *recpos++;
  322. #else
  323.         for (j = 0; j < sizeof(POINTER); j++)
  324.             *txt++ = *recpos++;
  325. #endif
  326.     }
  327.  
  328.     if (ct_nlkey[keyno] == CHKNUL) {
  329.         recpos = &ct_nlchr[keyno];
  330.         for (j = 0, txt = savtxt; j < slen; j++)
  331.             if (*txt++ != *recpos)
  332.                 return(len);
  333.         return(0);
  334.     }
  335.         
  336.     return(len);
  337. }
  338.  
  339. TEXT *TFRMKEY(keyno,tarptr)
  340. COUNT          keyno;
  341. TEXT           *tarptr;
  342. {
  343.     COUNT i,len,tarpos[MAX_KEY_SEG],tarmod[MAX_KEY_SEG],savmsk;
  344.     TEXT  tmpkey[MAXLEN];
  345.  
  346.     /* temporarily store actual parm file info & replace with */
  347.     /* target oriented info                                   */
  348.     for (i = len = 0; i < MAX_KEY_SEG; i++) {
  349.         if ((tarpos[i] = ct_sgpos[keyno][i]) < 0)
  350.             break;
  351.         tarmod[i] = ct_sgmod[keyno][i];
  352.         savmsk      = tarmod[i] & ~SEGMSK;
  353.         ct_sgpos[keyno][i] = len;
  354.         len += ct_sglen[keyno][i];
  355.  
  356.     /* disable varying field segments */
  357.         if ((tarmod[i] & SEGMSK) == VARSEG)
  358.             ct_sgmod[keyno][i] = REGSEG | savmsk;
  359.         else if ((tarmod[i] & SEGMSK) == UVARSEG)
  360.             ct_sgmod[keyno][i] = UREGSEG | savmsk;
  361.     }
  362.  
  363.     /* invoke frmkey with modified parm file info */
  364.     len = frmkey(keyno,tarptr,tmpkey,DRNZERO);
  365.  
  366.     /* copy target key back to tarptr */
  367.     cpybuf(tarptr,tmpkey,len);
  368.  
  369.     /* restore parm file info */
  370.     for (i = 0; i < MAX_KEY_SEG; i++) {
  371.         if ((ct_sgpos[keyno][i] = tarpos[i]) < 0)
  372.             break;
  373.         ct_sgmod[keyno][i] = tarmod[i];
  374.     }
  375.     /* return pointer to target key */
  376.     return(tarptr);
  377. }
  378.  
  379. COUNT tstinm(filno)
  380. COUNT filno;
  381. {
  382.     if (filno < 0 || filno >= ct_mxfil)
  383.         return(ierr(FNUM_ERR,filno));
  384.     else if (ct_dtmap[filno])
  385.         return(ierr(FUSE_ERR,filno));
  386.     else {
  387.         ct_dtmap[filno] = YES;
  388.         return(NO_ERROR);
  389.     }
  390. }
  391.  
  392. #ifdef PARMFILE
  393. COUNT getintr(ifd,pbufs,pkeys,psecs,pdats)
  394. FILE  *ifd;
  395. COUNT *pbufs,*pkeys,*psecs,*pdats;
  396. {
  397.  
  398. #ifdef RTREE
  399.     COUNT i;
  400.  
  401.     for (i = 0; i < MAXFIL; i++) {
  402.         usrkey[i].rtype = -1;
  403.         usrkey[i].rsymb = NULL;
  404.     }
  405. #endif
  406.  
  407.     if (fscanf(ifd,IC1,pbufs,pkeys,psecs,pdats) != 4)
  408.         return(ierr(IGIN_ERR,0));
  409.     else if ((*pdats + *pkeys) > MAXFIL)
  410.         return(ierr(IFIL_ERR,0));
  411. #ifdef RTREE
  412.     else if ((*pdats + *pkeys) > (MAXFIL - 3))
  413.         return(ierr(IKSR_ERR,0));
  414.     *pkeys += 3;
  415.     srtknm  = *pdats + *pkeys - 1;
  416.     srtdat  = srtknm - 1;
  417.     tmpdat  = srtdat - 1;
  418. #endif
  419.     return(NO_ERROR);
  420. }
  421.  
  422.  
  423. COUNT getdatr(ifd,pdatno,pname,pdatln,pdkeys,pxtsiz,pfilmd)
  424. FILE   *ifd;
  425. COUNT  *pdatno;
  426. TEXT   *pname;
  427. UCOUNT *pdatln;
  428. COUNT  *pdkeys;
  429. UCOUNT *pxtsiz;
  430. COUNT  *pfilmd;
  431. {
  432.     if (fscanf(ifd,IC2,pdatno,pname,pdatln,pxtsiz,pfilmd,pdkeys) != 6)
  433.         return(ierr(IDRI_ERR,*pdatno));
  434.     else if (*pdkeys > MAX_DAT_KEY)
  435.         return(ierr(IDRK_ERR,*pdatno));
  436.     else if (tstinm(*pdatno))
  437.         return(isam_err);
  438. #ifdef RTREE
  439.     else if (setudat(ifd,*pdatno))
  440.         return(isam_err);
  441. #endif
  442.     else
  443.         return(NO_ERROR);
  444. }
  445.  
  446. COUNT setmap(datno,j,keyno,klen,ksegs,dflg,nchar,nulflg,ifd)
  447. COUNT        datno,j,keyno,klen,ksegs,dflg,nchar,nulflg;
  448. FILE                               *ifd;
  449. {
  450.     FAST COUNT k,totseg;
  451.     COUNT       tstmod;
  452.  
  453.     ct_kymap[datno][j] = keyno;
  454.     ct_rvmap[keyno]    = datno;
  455.     ct_nlchr[keyno]    = nchar;
  456.     ct_nlkey[keyno]    = nulflg;
  457.     ct_vfin[keyno]     = NO;
  458.  
  459.     for (k = 0, totseg = klen; k < ksegs; k++) {
  460.         if (fscanf(ifd,IC3,&ct_sgpos[keyno][k],
  461.             &ct_sglen[keyno][k],&ct_sgmod[keyno][k]) != 3)
  462.             return(ierr(ISRC_ERR,keyno));
  463.         tstmod  = ct_sgmod[keyno][k] & SEGMSK;
  464.         totseg -= ct_sglen[keyno][k];
  465.         if (tstmod == SRLSEG)
  466.             (ct_key + datno)->srlpos = ct_sgpos[keyno][k] + 1;
  467.         else if (tstmod == VARSEG || tstmod == UVARSEG)
  468.             ct_vfin[keyno] = YES;
  469.     }
  470.     if ((dflg == DUPKEY && totseg != sizeof(POINTER)) ||
  471.         (dflg != DUPKEY && totseg))
  472.         return(ierr(ISLN_ERR,keyno));
  473.  
  474.     if (k < MAX_KEY_SEG)
  475.         ct_sgpos[keyno][k] = -1;
  476.     return(NO_ERROR);
  477. }
  478.  
  479.  
  480. COUNT getidxr(ifd,datno,j,pkeyno,pname,pklen,pktyp,pdflg,pnmem,pxtsiz,pfilmd)
  481. FILE   *ifd;
  482. COUNT   datno,j,*pkeyno;
  483. TEXT   *pname;
  484. COUNT  *pklen,*pktyp,*pdflg,*pnmem;
  485. UCOUNT *pxtsiz;
  486. COUNT  *pfilmd;
  487. {
  488.     COUNT nulflg,nchar,ksegs;
  489.  
  490.     if (fscanf(ifd,IC4,pkeyno,pname,pklen,pktyp,pdflg,pnmem,pxtsiz,pfilmd,
  491.         &nulflg,&nchar,&ksegs)
  492.         != 11)
  493.         return(ierr(IKRI_ERR,j));
  494.     else if (ksegs > MAX_KEY_SEG)
  495.         return(ierr(IKRS_ERR,*pkeyno));
  496.     else if (tstinm(*pkeyno))
  497.         return(isam_err);
  498. #ifdef RTREE
  499.     else if (setukey(ifd,*pkeyno,*pktyp))
  500.         return(isam_err);
  501. #endif
  502.     else
  503.         return(setmap(datno,j,*pkeyno,*pklen,ksegs,*pdflg,
  504.             nchar,nulflg,ifd));
  505. }
  506.  
  507. COUNT getambr(ifd,datno,j,keyno,pklen,pktyp,pdflg)
  508. FILE   *ifd;
  509. COUNT   datno,j,keyno,*pklen,*pktyp,*pdflg;
  510. {
  511.     COUNT impkey,nulflg,nchar,ksegs;
  512.  
  513.     if (fscanf(ifd,IC5,&impkey,pklen,pktyp,pdflg,&nulflg,
  514.         &nchar,&ksegs) != 7)
  515.         return(ierr(IMRI_ERR,keyno));
  516.     else if (impkey != keyno)
  517.         return(ierr(IMKY_ERR,keyno));
  518.     else if (ksegs > MAX_KEY_SEG)
  519.         return(ierr(IKRS_ERR,keyno));
  520. #ifdef RTREE
  521.     else if (setukey(ifd,keyno,*pktyp))
  522.         return(isam_err);
  523. #endif
  524.     else
  525.         return(setmap(datno,j,keyno,*pklen,ksegs,*pdflg,
  526.             nchar,nulflg,ifd));
  527. }
  528. #endif
  529.  
  530. /* end of ctismu.c */
  531.