home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / database / dbase_c.zip / DBSEEK.C < prev    next >
Text File  |  1989-03-20  |  4KB  |  76 lines

  1.  
  2. #include  "libc.h"
  3. #include  "dbase.h"
  4.  
  5.  
  6. /*************************************************************************
  7. * db_seek positions the read/write pointer to the ith record in the data *
  8. * base.  It also works with an offset so that the pointer can be moved   *
  9. * back or forward n records from the current position, the beginning  or *
  10. * end of the file.  If db_seek is sucessful it returns the current posi- *
  11. * tion in the file; otherwise, it returns a -1.  The offset codes are as *
  12. * follows:                                                               *
  13. *                                                                        *
  14. *          OFFSET                             MEANING                    *
  15. *          ------                   ------------------------------       *
  16. *                                                                        *
  17. *            0                       offset from the beginning of the    *
  18. *                                    data base. zero or + offset only    *
  19. *                                                                        *
  20. *            1                       offset from the current position    *
  21. *                                    in the database. zero, + or - offst *
  22. *                                    allowed                             *
  23. *                                                                        *
  24. *            2                       offset from the end of the database *
  25. *                                    zero or - offset only               *
  26. *                                                                        *
  27. *************************************************************************/
  28. db_seek(db_ptr,rcrds,origin)
  29.    DBASE_FILE  *db_ptr ;
  30.    int         rcrds, origin ;
  31.    {long       offset, char_pos, lseek();
  32.  
  33.       /*----- calculate the character position offset for the "records" position -----*/ 
  34.       switch ( origin  )
  35.         {
  36.          case 0 :
  37.            if( rcrds < 1 )
  38.               return( ERROR );
  39.            else {
  40.               offset = (long) (DATA_START + (rcrds-1) * db_ptr->rec_len) ;     
  41.               break;
  42.            }
  43.  
  44.          case 1 :
  45.            offset =  (long) ( rcrds * db_ptr->rec_len ) ;
  46.            if( (offset > 0) && (db_ptr->curr_rec + offset/db_ptr->rec_len) > db_ptr->rec_num )
  47.               return( ERROR );          /* tried to get past end of file      */
  48.            else if( (offset < 0) && (db_ptr->curr_rec + offset/db_ptr->rec_len) < 0 )
  49.                    return( ERROR );     /* tried to get past beginning of file */
  50.            break;
  51.  
  52.          case 2 :
  53.            if( rcrds > 0 )
  54.              return( ERROR );               /* attempt to offset past end-of-file */
  55.            else {
  56.              offset = (long) DATA_START + (db_ptr->rec_num + rcrds ) * db_ptr->rec_len ;  
  57.              origin = 0 ;                   /* reset origin since offset is from beginning of file */
  58.              break;
  59.              }
  60.  
  61.          default :
  62.            return ( ERROR );                /* undefined origin value */
  63.         }
  64.  
  65.       /*----- return the record number -----*/
  66.       if( (char_pos = lseek(db_ptr->file_ptr,offset,origin )) == ERROR ){
  67.          db_seek(db_ptr,db_ptr->curr_rec,0);          /* reset the file pointer */
  68.          return(char_pos);
  69.          }
  70.       else{
  71.          /*----- reset the current record number pointer -----*/
  72.          db_ptr->curr_rec = (char_pos-DATA_START)/(db_ptr->rec_len)  ;
  73.          return( db_ptr->curr_rec );
  74.          }
  75.    }
  76.