home *** CD-ROM | disk | FTP | other *** search
-
- /* x4copy.c (c)Copyright Sequiter Software Inc., 1987-1990. All rights reserved.
-
- Copies to another database.
- */
-
- #include "d4all.h"
- #include "u4error.h"
-
- #include <string.h>
-
- extern int v4cur_base, v4lock_wait ;
-
- static int cleanup(void) ;
- static int new_ref, start_ref ;
-
- static int cleanup()
- {
- d4select( new_ref ) ;
- d4buf_return() ;
- if ( new_ref >= 0 )
- if ( d4buf_total( 0L, 0, 0) < 0) return -2 ;
-
- d4select( start_ref ) ;
-
- return -1 ;
- }
-
- int x4copy( char *new_base, long start_rec, int safety )
- {
- int buf_len, rc ;
- long i_rec, count, bytes_left ;
- char H_PTR rec_ptr ;
- BASE *new_ptr, *start_ptr ;
-
- start_ptr = d4ptr() ;
- start_ref= d4select(-1) ;
- buf_len = f4record_width() ;
- new_ref = -1 ;
-
- if ( (rc = d4lock_all(v4lock_wait, 1)) < 0) return rc ;
- count = d4reccount() ;
-
- /* Memory Buffer Operations. */
- if ( d4buf_clear_lend(1) == -1 ) return( cleanup() ) ;
- if ( d4flush( v4cur_base) < 0 ) return( cleanup() ) ;
- d4buf_avail_set() ;
-
- bytes_left = d4buf_avail()/2 ;
- if ( bytes_left > 0xFFE0 ) bytes_left = 0xFFE0 ;
- if ( bytes_left < f4record_width() ) bytes_left = 0 ;
-
- if ( u4read_init( start_ptr->file_hand, (char H_PTR) d4buf_alloc( bytes_left,1),
- (unsigned) bytes_left,
- (long) start_ptr->header_len+(start_rec-1L)*start_ptr->buffer_len) < 0)
- return( cleanup() ) ;
-
- rec_ptr = (char H_PTR) f4record() ;
-
- new_ref = d4create( new_base, start_ptr->num_fields, start_ptr->fields, safety) ;
- if ( new_ref < 0) return( cleanup()) ;
-
- if ( d4lock( -1L, 1) < 0 ) return( cleanup() ) ;
-
- new_ptr = d4ptr() ;
-
- bytes_left = d4buf_avail() ;
- if ( bytes_left > 0xFFE0 ) bytes_left = 0xFFE0 ;
- if ( bytes_left < f4record_width() ) bytes_left = 0 ;
-
- if ( u4write_init( new_ptr->file_hand, (char H_PTR) d4buf_alloc(bytes_left,1),
- (unsigned) bytes_left, (long) new_ptr->header_len ) < 0 )
- return( cleanup() ) ;
-
- d4select( start_ref ) ;
-
- for ( i_rec = start_rec; i_rec <= count; i_rec++ )
- {
- if ( u4read( rec_ptr, buf_len ) != 0 )
- return( cleanup() ) ;
-
- start_ptr->rec_num = i_rec ;
-
- rc = x4filter_do() ;
- if ( rc == 1 ) continue ;
- if ( rc == 2 ) break ;
- if ( rc < 0 ) return( cleanup() ) ;
-
- if ( u4write( rec_ptr, buf_len) < 0 )
- {
- d4close() ;
- return( cleanup() ) ;
- }
- }
-
- if ( u4write_flush() < 0 ) return( cleanup() ) ;
-
- if ( cleanup() < -1 ) return -1 ;
-
- return( new_ref ) ;
- }
-