home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / SOURCE / D4SKIP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-22  |  3.5 KB  |  169 lines

  1.  
  2. /*  d4skip.c   (c)Copyright Sequiter Software Inc., 1987-1990.  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.       -2  No Record to Skip From
  11. */
  12.  
  13. #include "d4all.h"
  14. #include "u4error.h"
  15.  
  16. extern  BASE  *v4base  ;
  17. extern  INDEX *v4index ;
  18. extern  int    v4cur_base, v4lock_wait ;
  19.  
  20. static int  position_index( int, long ) ;
  21. static int  d4skip_seq( BASE *, long ) ;
  22. static int  d4skip_index( BASE *, long ) ;
  23.  
  24.  
  25. /* Skip Sequentially. */
  26.  
  27. static int  d4skip_seq( BASE *base_ptr, long num_skip )
  28. {
  29.    int   rc, rc2 ;
  30.    long  new_rec, count ;
  31.  
  32.    count =  d4reccount() ;
  33.    if ( count <= 0L )
  34.    {
  35.       base_ptr->rec_num =  1L ;
  36.       base_ptr->eof =  base_ptr->bof =  1 ;
  37.       d4blank() ;
  38.       if ( num_skip >= 0L )
  39.          return 3 ;
  40.       else
  41.          return 1 ;
  42.    }
  43.  
  44.    rc =  base_ptr->bof =  base_ptr->eof =  0 ;
  45.    new_rec =  base_ptr->rec_num + num_skip ;
  46.  
  47.    if ( new_rec < 1L )
  48.    {
  49.       base_ptr->bof =  1 ;
  50.       new_rec =  1L ;
  51.       rc =  1 ;
  52.    }
  53.  
  54.    if ( new_rec > count )
  55.    {
  56.       base_ptr->rec_num =  count +1L ;
  57.       base_ptr->eof =  1 ;
  58.       d4blank() ;
  59.       return 3 ;
  60.    }
  61.  
  62.    if ( (rc2 = d4read(new_rec)) < 0 )  return rc2 ;
  63.  
  64.    return  rc ;
  65. }
  66.  
  67.  
  68. /* Skip with an Index. */
  69.  
  70. static int  d4skip_index( BASE *base_ptr, long num_skip )
  71. {
  72.    int    index_ref, rc ;
  73.    long   skipped ;
  74.  
  75.    index_ref =  base_ptr->current_index ;
  76.  
  77.    if ( base_ptr->eof )
  78.    {
  79.       if ( num_skip >= 0 )
  80.       {
  81.          d4blank() ;
  82.          return 3 ;
  83.       }
  84.  
  85.       base_ptr->eof =  0 ;
  86.       if ( (rc = d4bottom()) < 0 )  return rc ;
  87.       if ( rc == 3 )  return 1 ;
  88.       num_skip++ ;
  89.    }
  90.  
  91.    if ( num_skip == 0 ) return( 0 ) ;
  92.    base_ptr->bof =  0 ;
  93.  
  94.    if ( (rc = position_index( index_ref, base_ptr->rec_num)) < 0)  return rc ;
  95.  
  96.    skipped =  i4skip( index_ref, num_skip) ;
  97.  
  98.    if ( skipped == num_skip )
  99.       return( d4read( i4key(base_ptr->current_index)->rec_num ) )  ;
  100.    if ( skipped == -num_skip  && skipped != 0 )
  101.         return( -1 ) ; /* Error */
  102.  
  103.    if ( num_skip < 0)
  104.    {
  105.       /* BOF */
  106.       rc =  d4top() ;
  107.       base_ptr->bof = 1 ;
  108.  
  109.       if ( rc >= 0 )
  110.          return( 1) ;
  111.       else
  112.          return( rc ) ;
  113.    }
  114.    else
  115.    {
  116.       /* EOF */
  117.       base_ptr->rec_num =  d4reccount()+1 ;
  118.       base_ptr->eof =  1 ;
  119.       d4blank() ;
  120.       return( 3 ) ;
  121.    }
  122. }
  123.  
  124.  
  125. static int  position_index( int index_ref, long rec_num )
  126. {
  127.    KEY   *key_ptr ;
  128.    char  *eval_ptr ;
  129.    int    rc ;
  130.  
  131.    if ( (key_ptr = i4key(index_ref)) != (KEY *) 0)
  132.       if ( key_ptr->rec_num == rec_num)
  133.       {
  134.      if ( (rc = i4lock( index_ref, v4lock_wait)) < 0)  return rc ;
  135.  
  136.          if ( (key_ptr = i4key(index_ref)) != (KEY *) 0)
  137.             if ( key_ptr->rec_num == rec_num)   return 0 ;
  138.       }
  139.  
  140.    /* The Index File Needs to be Repositioned. */
  141.    if ( (rc = d4read( rec_num)) < 0)  return rc ;
  142.  
  143.    if ( (eval_ptr = i4eval( index_ref)) == (char *) 0)   return -1 ;
  144.    if ( (rc =  i4go( index_ref, eval_ptr, rec_num)) < 0) return -1;
  145.    if ( rc != 0 )  return -3 ;
  146.  
  147.    return 0 ;
  148. }
  149.  
  150.  
  151. int  d4skip( long num_skip )
  152. {
  153.    BASE *base_ptr ;
  154.  
  155.    if ( v4cur_base < 0 )
  156.    {
  157.       u4error( E_D_MISSING, (char *) 0 ) ;
  158.       return( -1 ) ;
  159.    }
  160.  
  161.    base_ptr =  v4base + v4cur_base ;
  162.    if ( d4changed( base_ptr )  < 0 )  return -1 ;
  163.  
  164.    if ( base_ptr->current_index >= 0 )
  165.       return( d4skip_index( base_ptr, num_skip ) ) ;
  166.    else
  167.       return( d4skip_seq( base_ptr, num_skip ) ) ;
  168. }
  169.