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

  1. /*
  2.  *    low level fractal search routine
  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. #include "ctstdr.h"        /* standard i/o header         */
  24. #include "ctoptn.h"        /* c-tree configuration options */
  25. #include "cterrc.h"        /* c-tree error codes        */
  26. #include "ctstrc.h"        /* c-tree data structures    */
  27. #include "ctgvar.h"        /* c-tree global variables    */
  28.  
  29. #define SCALE        10000
  30. #define FAC_SCALE    100
  31.  
  32. /*
  33. FRCKEY returns the 4-byte record position associated with the key value
  34. located approximately fractal/100 of the way through the file. idxval
  35. points to the output key buffer.
  36. */
  37.  
  38. POINTER FRCKEY(keyno,idxval,fractal)
  39. COUNT           keyno;
  40. TEXT            *idxval;
  41. COUNT                fractal;
  42. {
  43.     FAST CTFILE    *knum;
  44.     FAST TREEBUFF    *buffer;
  45.     COUNT         bias,nsize,sfractal,npoint;
  46.     LONG          node;
  47.  
  48.     CTFILE        *tstfnm();
  49.     LONG         gtroot(),nodpnt();
  50.     POINTER         drnpnt();
  51.     TREEBUFF    *getnod();
  52.     COUNT         uerr();
  53.     TEXT        *valpnt();
  54.  
  55.     if ((knum = tstfnm(keyno)) == NULL)
  56.         return(uerr_cod);
  57.     else if (fractal < 0 || fractal > (SCALE / FAC_SCALE))
  58.         return(uerr(KFRC_ERR));
  59.  
  60.     if ((node = gtroot(knum)) == DRNZERO)
  61.         return(DRNZERO);
  62.  
  63.     sfractal = fractal;
  64.     bias     = 0;
  65.     fractal *= FAC_SCALE;
  66.     while (node) {
  67.         if ((buffer = getnod(node,knum)) == NULL)
  68.             return(DRNZERO);
  69.         nsize = SCALE;
  70.         if (buffer->nkv)
  71.             nsize = SCALE / buffer->nkv;
  72.         npoint = (fractal + nsize - 1) / nsize;
  73.         if (npoint == buffer->nkv + 1)
  74.             npoint--;
  75.         if (npoint == 0) {
  76.             npoint  = 1;
  77.             fractal = 0;
  78.         } else {
  79.             if (npoint > buffer->nkv) terr(251);
  80.             bias    = fractal - ((npoint - 1) * nsize);
  81.             fractal = bias * buffer->nkv;
  82.             if (fractal > SCALE) fractal = SCALE;
  83.         }
  84.         if (buffer->leaf == LEAF)
  85.             break;
  86.         node = nodpnt(buffer,npoint);
  87.     }
  88.  
  89.     if (!node)
  90.         terr(250);
  91.  
  92.     if (buffer->nkv) {
  93.         nsize  = SCALE / buffer->nkv;
  94.         npoint = (fractal + nsize - 1) / nsize;
  95.         if (npoint == 0)
  96.             npoint = 1;
  97.         else if (npoint == buffer->nkv + 1)
  98.             npoint--;
  99.         if (npoint > buffer->nkv) terr(252);
  100.         cpybuf(idxval,valpnt(buffer,npoint),knum->length);
  101.         return(drnpnt(buffer,npoint));
  102.     } else {
  103.         while (node = buffer->sucesr) {
  104.             if ((buffer = getnod(node,knum)) == NULL)
  105.                 return(DRNZERO);
  106.             if (buffer->nkv) {
  107.                 cpybuf(idxval,valpnt(buffer,1),knum->length);
  108.                 return(drnpnt(buffer,1));
  109.             }
  110.         }
  111.         setnul(idxval);
  112.         return(DRNZERO);
  113.     }
  114. }
  115.  
  116. /* end of ctfrac.c */
  117.