home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / clipper / cuaclip.zip / NTXPOS.C < prev    next >
Text File  |  1993-06-01  |  5KB  |  229 lines

  1. /*
  2. Copyright(C) Delcom-Deltranik International Software Engineering 1990-1993.
  3.  
  4. $owner: BILLW$
  5.  
  6. $version: 1.0$ $date: March 16, 1993$ $time: 07:33:42 AM$
  7.  
  8. $nokeywords$ */
  9. //.............................................................................
  10. //
  11. //   Program Name: NTXPOS.C          Copyright: RCM Software Pty. Ltd.
  12. //   Date Created: 27/04/91           Language: Microsoft C 5.1
  13. //   Time Created: 17:49:22             Author: Graham D. McKechnie
  14. //
  15. //   Modified 9/25/92 7:00 pm
  16. //   by Brian Marasca
  17. //   Use index handle rather than file name.
  18. //   IndexOrd() is now passed as parameter 1.
  19. //
  20. //   Returns index record number
  21. //   eg. nNtxPos := NtxPos( IndexOrd(), RECNO() )
  22. //.............................................................................
  23.  
  24. #include "extend.h"
  25. #include "stdio.h"
  26.  
  27. #define open  _topen                // Use Clipper's internals
  28. #define close _tclose               // Don't have to link LLIBCA
  29. #define lseek _tlseek
  30. #define read  _tread
  31.  
  32. extern int  _topen( char*, int);
  33. extern int  _tclose( int );
  34. extern long _tlseek( int, long, int);
  35. extern int  _tread( int, char*, int);
  36.  
  37. #define BUFF_SIZE   1024
  38. #define ERROR       -1
  39. #define MAX_KEY     256
  40. #define O_RDONLY    0x0000  // open for reading only
  41. #define O_BINARY    0x8000  // file mode is binary (untranslated)
  42.  
  43.  
  44. // Index structures
  45. typedef struct
  46. {
  47.    unsigned  uSign;
  48.    unsigned  uVersion;
  49.    long      lRoot;
  50.    long      lNextPage;
  51.    unsigned  uItemSize;
  52.    unsigned  uKeySize;
  53.    unsigned  uKeyDec;
  54.    unsigned  uMaxItem;
  55.    unsigned  uHalfPage;
  56.    char      cKeyExpr[MAX_KEY];
  57.    Boolean   bUnique;
  58. } NTXHEADER;
  59.  
  60.  
  61. typedef struct
  62. {
  63.    long lPage;
  64.    long lRecNo;
  65.    char cKey;
  66. } ITEM;
  67.  
  68.  
  69. typedef struct
  70. {
  71.    unsigned uCount;
  72.    unsigned uRef;
  73. } BUFFER;
  74.  
  75.  
  76.  
  77. static Boolean bError = FALSE;
  78. static Boolean bFound = FALSE;
  79.  
  80. static long lRetVal;            // Return value
  81. static long lNtxPos;            // Index position
  82. static int  nNtxHandle;         // Index file handle
  83.  
  84.  
  85. CLIPPER ntxpos()
  86. {
  87.    long lRecNo;
  88.    int nNtxOrd;
  89.    long nNtxPtr;
  90.  
  91.    NTXHEADER NtxHeader;
  92.  
  93.    // Check the parameters passed from Clipper
  94.    if ( PCOUNT != 2 )
  95.    {
  96.       _retni(-1);
  97.       return;
  98.    }
  99.  
  100.    if ( ! (ISNUM(1) && ISNUM(2) ) )
  101.    {
  102.       _retni(-1);
  103.       return;
  104.    }
  105.  
  106.    nNtxOrd  = _parni(1);    // indexord() to go by
  107.    lRecNo   = _parnl(2);    // RECNO() we are looking for
  108.  
  109.    lRetVal  = 0L;           // Set these each time
  110.    lNtxPos  = 0L;
  111.    bFound   = FALSE;
  112.  
  113.    if ( ( nNtxHandle = getNtxHandle ( nNtxOrd ) ) != -1 )
  114.    {
  115.         /* save the ntx file position and reset it to the top */
  116.         nNtxPtr = _tlseek( nNtxHandle, 0L, SEEK_CUR );
  117.         _tlseek( nNtxHandle, 0L, SEEK_SET );
  118.  
  119.         /* read the header */
  120.         if ( read ( nNtxHandle, (char *) &NtxHeader, sizeof(NtxHeader) )
  121.                        != sizeof(NtxHeader) )
  122.         goto ERROR_EXIT;
  123.  
  124.         /* start the traversal from root */
  125.         DumpPage( NtxHeader.lRoot, lRecNo );
  126.  
  127.         if (bError)
  128.         {
  129.              ERROR_EXIT:
  130.              bError = TRUE;
  131.         }
  132.  
  133.         /* restore the ntx file position */
  134.         _tlseek( nNtxHandle, nNtxPtr, SEEK_SET );
  135.    }
  136.  
  137.    _retnl( lRetVal );
  138.  
  139. }
  140.  
  141.  
  142.  
  143. DumpPage(long lPageOffSet, long lRecNo)
  144.  
  145. {
  146.    char     *cPage;
  147.    ITEM     *item;
  148.    BUFFER   *buffer;
  149.    unsigned i;
  150.    unsigned *uItemRef;
  151.  
  152.    /* allocate page */
  153.    cPage = _xalloc(BUFF_SIZE);
  154.  
  155.    /* move to this position in the file */
  156.    if( lseek( nNtxHandle, lPageOffSet, 0 ) != (long)lPageOffSet )
  157.       goto DUMP_EXIT;
  158.  
  159.    /* read the page */
  160.    if( read( nNtxHandle, cPage, BUFF_SIZE) != BUFF_SIZE )
  161.       goto DUMP_EXIT;
  162.  
  163.  
  164.    buffer   = (BUFFER *) cPage;
  165.    uItemRef = &buffer -> uRef;
  166.  
  167.  
  168.    for (i = 0; i < buffer -> uCount; i++)
  169.    {
  170.       item = (ITEM *) &cPage[ uItemRef[ i ] ];
  171.  
  172.       if ( ! bFound )
  173.       {
  174.          if ( item -> lPage )
  175.             DumpPage( item -> lPage, lRecNo );
  176.  
  177.          if (bFound)
  178.          {
  179.             _xfree( cPage );
  180.             return;
  181.          }
  182.          lNtxPos++;
  183.       }
  184.  
  185.       if ( item->lRecNo == lRecNo )
  186.       {
  187.          lRetVal = lNtxPos;
  188.          bFound = TRUE;
  189.          break;
  190.       }
  191.    }
  192.  
  193.    /* handle extra right pointer */
  194.    item = (ITEM *) &cPage[ uItemRef[ buffer -> uCount ] ];
  195.    if (item -> lPage)
  196.       DumpPage(item -> lPage, lRecNo );
  197.  
  198.    _xfree( cPage );
  199.  
  200.    if ( bError )
  201.    {
  202.       DUMP_EXIT:
  203.       _xfree( cPage );
  204.       bError = TRUE;
  205.    }
  206. }
  207.  
  208. /**************************************************************************
  209.    FUNCTION: getNtxHandle ( n )
  210.    Returns the index handle for the current workarea, or -1 if error.
  211.    Brian Marasca 9/25/92
  212. **************************************************************************/
  213.  
  214. static int getNtxHandle ( n )
  215. int n ;
  216. {
  217.      extern char **_workareas ;
  218.      int **hp, han = -1 ;
  219.  
  220.      if(n > 0 && n <= 15)
  221.      {
  222.           hp = (int **) ((*_workareas) + 0x98 + ((n-1) * sizeof (char *))) ;
  223.           han = *hp ? **hp : -1 ;
  224.      }
  225.  
  226.      return han ;
  227. }
  228.  
  229.