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