home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CODE4-1.ZIP / SOURCE.ZIP / D4SKIP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-14  |  2.9 KB  |  119 lines

  1.  
  2. /*  d4skip.c   (c)Copyright Sequiter Software Inc., 1987, 1988, 1989.  All rights reserved.
  3.  
  4.     Locates and reads the record.
  5.     Returns
  6.        0  Success
  7.        1  BOF
  8.        3  EOF
  9.       -1  Error
  10. */
  11.  
  12. #include "d4base.h"
  13. #include "u4error.h"
  14. #include <string.h>
  15.  
  16. extern  BASE  *v4base  ;
  17. extern  INDEX *v4index ;
  18. extern  int    v4cur_base ;
  19.  
  20.  
  21. d4skip( num_skip )
  22. long num_skip ;
  23. {
  24.    BASE *base_ptr ;
  25.    char *eval_ptr ;
  26.    int   index_ref, rc, do_repos ;
  27.    long  skipped ;
  28.  
  29.    if ( v4cur_base < 0 )
  30.    {
  31.       u4error( E_D_MISSING, (char *) 0 ) ;
  32.       return( -1 ) ;
  33.    }
  34.  
  35.    base_ptr  =  v4base + v4cur_base ;
  36.    index_ref =  base_ptr->current_index ;
  37.  
  38.    if ( base_ptr->rec_num <= 0 )
  39.    {
  40.       if ( base_ptr->rec_num == 0 )
  41.       {
  42.      if( (rc =  d4top()) < 0)  return( -1 ) ;
  43.      if ( rc == 3)  return( 3 ) ;
  44.       }
  45.       else
  46.       {
  47.          /* EOF -  Position to the Bottom */
  48.      if( (rc =  d4bottom()) < 0)  return( -1 ) ;
  49.      if ( rc == 3 )  return 3 ;
  50.      num_skip++ ;
  51.       }
  52.    }
  53.  
  54.    if ( num_skip == 0 ) return( 0 ) ;
  55.  
  56.    if (index_ref < 0)
  57.    {
  58.       base_ptr->rec_num +=  num_skip ;
  59.       if ( base_ptr->rec_num >= 1  &&  base_ptr->rec_num <= d4reccount() )
  60.          return( d4go( base_ptr->rec_num )  ) ;
  61.    }
  62.    else
  63.    {
  64.       /* Make sure its initial position is correct. */
  65.       do_repos =  0 ;
  66.       if ( b4key(index_ref) == (KEY *) 0  )
  67.          do_repos =  1 ;
  68.       else
  69.          if ( b4key(index_ref)->rec_num != base_ptr->rec_num )
  70.             do_repos =  1 ;
  71.       if ( do_repos )
  72.       {
  73.          /* Lock Index File Before Locking Database */
  74.          if ( i4lock(index_ref,1) < 0) return -1 ;
  75.  
  76.          if ( d4go( base_ptr->rec_num ) < 0 )  return( -1 ) ;
  77.          eval_ptr =  i4eval( index_ref ) ;
  78.          rc =  i4go( index_ref, eval_ptr, base_ptr->rec_num ) ;
  79.          if ( rc < 0 ) return( -1 ) ;
  80.          if ( rc != 0 )
  81.          {
  82.             char buffer[64], rec_buffer[10] ;
  83.             c4key( eval_ptr, buffer, i4type(index_ref) ) ;
  84.             c4ltoa( base_ptr->rec_num, rec_buffer, 7 ) ;
  85.             rec_buffer[7] = '\0' ;
  86.             u4error( E_I_MISSING, v4index[index_ref].name,
  87.                     "Key:", buffer,
  88.                     "Record Number:", rec_buffer, (char *) 0 ) ;
  89.             return( -1 ) ;
  90.          }
  91.       }
  92.  
  93.       skipped =  i4skip( index_ref, num_skip) ;
  94.       if ( skipped == num_skip )
  95.          return( d4go( b4key(base_ptr->current_index)->rec_num ) )  ;
  96.       if ( skipped == -num_skip  && skipped != 0 )
  97.            return( -1 ) ; /* Error */
  98.    }
  99.  
  100.    if ( num_skip < 0)
  101.    {
  102.       /* BOF */
  103.       rc =  d4top() ;
  104.       if ( rc == 0 )
  105.          return( 1) ;
  106.       else
  107.          return( rc ) ;
  108.    }
  109.    else
  110.    {
  111.       /* EOF */
  112.       base_ptr->rec_num =  -1 ;
  113.       memset( base_ptr->buffer, (int) ' ', (size_t) base_ptr->buffer_len ) ;
  114.       return( 3 ) ;
  115.    }
  116. }
  117.  
  118.  
  119.