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

  1.  
  2. /*  x4copy.c   (c)Copyright Sequiter Software Inc., 1987-1990.  All rights reserved.
  3.  
  4.     Copies to another database.
  5. */
  6.  
  7. #include "d4all.h"
  8. #include "u4error.h"
  9.  
  10. #include <string.h>
  11.  
  12. extern int  v4cur_base, v4lock_wait ;
  13.  
  14. static  int  cleanup(void) ;
  15. static  int  new_ref, start_ref ;
  16.  
  17. static int  cleanup()
  18. {
  19.    d4select( new_ref ) ;
  20.    d4buf_return() ;
  21.    if ( new_ref >= 0 )
  22.       if ( d4buf_total( 0L, 0, 0) < 0) return -2 ;
  23.  
  24.    d4select( start_ref ) ;
  25.  
  26.    return -1 ;
  27. }
  28.  
  29. int  x4copy( char *new_base, long start_rec, int safety )
  30. {
  31.    int   buf_len, rc ;
  32.    long  i_rec, count, bytes_left ;
  33.    char H_PTR rec_ptr ;
  34.    BASE *new_ptr, *start_ptr ;
  35.  
  36.    start_ptr =  d4ptr() ;
  37.    start_ref=  d4select(-1) ;
  38.    buf_len  =  f4record_width() ;
  39.    new_ref  =  -1 ;
  40.  
  41.    if ( (rc = d4lock_all(v4lock_wait, 1)) < 0)  return rc ;
  42.    count =  d4reccount() ;
  43.  
  44.    /* Memory Buffer Operations. */
  45.    if ( d4buf_clear_lend(1) == -1 )  return( cleanup() ) ;
  46.    if ( d4flush( v4cur_base) < 0 )  return( cleanup() ) ;
  47.    d4buf_avail_set() ;
  48.  
  49.    bytes_left =  d4buf_avail()/2 ;
  50.    if ( bytes_left > 0xFFE0 )  bytes_left = 0xFFE0 ;
  51.    if ( bytes_left < f4record_width() )  bytes_left =  0 ;
  52.  
  53.    if ( u4read_init( start_ptr->file_hand, (char H_PTR) d4buf_alloc( bytes_left,1),
  54.            (unsigned) bytes_left,
  55.            (long) start_ptr->header_len+(start_rec-1L)*start_ptr->buffer_len) < 0)
  56.       return( cleanup() ) ;
  57.  
  58.    rec_ptr =  (char H_PTR) f4record() ;
  59.  
  60.    new_ref =  d4create( new_base, start_ptr->num_fields, start_ptr->fields, safety) ;
  61.    if ( new_ref < 0)  return( cleanup()) ;
  62.  
  63.    if ( d4lock( -1L, 1) < 0 )  return( cleanup() ) ;
  64.  
  65.    new_ptr =  d4ptr() ;
  66.  
  67.    bytes_left =  d4buf_avail() ;
  68.    if ( bytes_left > 0xFFE0 )  bytes_left = 0xFFE0 ;
  69.    if ( bytes_left < f4record_width() )  bytes_left =  0 ;
  70.  
  71.    if ( u4write_init( new_ptr->file_hand, (char H_PTR) d4buf_alloc(bytes_left,1),
  72.                  (unsigned) bytes_left, (long) new_ptr->header_len ) < 0 )
  73.       return( cleanup() ) ;
  74.  
  75.    d4select( start_ref ) ;
  76.  
  77.    for ( i_rec = start_rec; i_rec <= count; i_rec++ )
  78.    {
  79.       if ( u4read( rec_ptr, buf_len ) != 0 )
  80.          return( cleanup() ) ;
  81.  
  82.       start_ptr->rec_num =  i_rec ;
  83.  
  84.       rc = x4filter_do() ;
  85.       if ( rc == 1 )  continue ;
  86.       if ( rc == 2 )  break ;
  87.       if ( rc < 0 )  return( cleanup() ) ;
  88.  
  89.       if ( u4write( rec_ptr, buf_len) < 0 ) 
  90.       {
  91.      d4close() ;
  92.          return( cleanup() ) ;
  93.       }
  94.    }
  95.  
  96.    if ( u4write_flush() < 0 )  return( cleanup() ) ;
  97.  
  98.    if ( cleanup() < -1 )  return -1 ;
  99.  
  100.    return( new_ref ) ;
  101. }
  102.