home *** CD-ROM | disk | FTP | other *** search
-
- /* d4pack.c (c) Copyright Sequiter Software Inc., 1987-1990. All rights reserved.
- */
-
- #include "p4misc.h"
- #include "u4error.h"
- #include "d4all.h"
-
- #ifndef UNIX
- #include <io.h>
- #endif
-
-
- #ifdef NO_CHSIZE
-
- int d4pack()
- {
- u4error( 0, "Routine d4pack is not present when compiled with NO_CHSIZE", (char *) 0 ) ;
- return 0 ;
- }
-
- #else
-
- extern int d4adjust( BASE *, long ) ;
- extern int v4lock_wait ;
-
- static int cleanup(void) ;
-
- static int cleanup()
- {
- d4buf_calc() ;
- d4buf_return() ;
- return -1 ;
- }
-
- int d4pack()
- {
- BASE *base_ptr ;
- char H_PTR ptr ;
- long read_rec, write_rec, rec_count ;
- long tot_bytes, ptr_recs, i_write_rec, i_read_rec, rc ;
- int rec_width, i_rc ;
-
- base_ptr = d4ptr() ;
-
- /* Lock the Entire Database. */
- if ( (i_rc = d4lock_all( v4lock_wait, 1)) < 0 ) return i_rc ;
- if ( d4buf_clear_base(d4select(-1)) < 0 ) return -1 ;
- if ( d4buf_clear_lend(1) == -1 ) return -1 ;
- d4buf_avail_set() ;
-
- rec_width = f4record_width() ;
- rec_count = d4reccount() ;
-
- ptr = (char H_PTR) d4buf_alloc( tot_bytes= d4buf_avail(), 1 ) ;
- if ( tot_bytes < rec_width )
- {
- tot_bytes = rec_width*2 ;
- ptr = (char H_PTR) base_ptr->buffer ;
- }
-
- ptr_recs = tot_bytes/ rec_width ;
- tot_bytes= ptr_recs * rec_width ;
-
- /* Remove the Marked Records */
- for ( write_rec = 1L, read_rec=1L; read_rec <= rec_count;
- read_rec += ptr_recs )
- {
- if ( d4lseek(read_rec) < 0 ) return( cleanup() ) ;
-
- rc = u4huge_read( base_ptr->file_hand, ptr, tot_bytes ) ;
- if ( rc < 0L ) return( cleanup() ) ;
-
- if ( rc < tot_bytes )
- {
- ptr_recs = rc/rec_width ;
- if ( ptr_recs == 0L )
- {
- u4error( E_READ, d4name(), (char *) 0 ) ;
- return( cleanup() ) ;
- }
- }
-
- for (i_write_rec=0L, i_read_rec = 0L; i_read_rec < ptr_recs; i_read_rec++)
- {
- if ( ptr[rec_width*i_read_rec] == ' ' )
- u4huge_cpy( ptr+i_write_rec++*rec_width, ptr+i_read_rec*rec_width,
- (long) rec_width) ;
- }
-
- if ( d4lseek( write_rec) < 0 ) return( cleanup() ) ;
- if ( u4huge_write( base_ptr->file_hand, ptr, i_write_rec*rec_width) < 0 )
- return( cleanup() ) ;
-
- write_rec += i_write_rec ;
- }
-
- cleanup() ;
-
- /* 'd4adjust' is also used by 'd4zap' */
- return( d4adjust(base_ptr, write_rec-1L) ) ;
- }
-
-
- d4adjust( BASE *base_ptr, long num_recs )
- {
- lseek( base_ptr->file_hand,
- base_ptr->header_len + num_recs*base_ptr->buffer_len, 0) ;
-
- /* change the database size */
- if( chsize( base_ptr->file_hand,
- (long)base_ptr->header_len + base_ptr->buffer_len*num_recs )
- != 0 )
- {
- u4error( E_WRITE, base_ptr->name, (char *) 0 ) ;
- return( -1 ) ;
- }
-
- if ( d4lseek(num_recs+1L) < 0 ) return -1 ;
- if ( write( base_ptr->file_hand, "\032", 1 ) != 1 )
- {
- u4error( E_WRITE, base_ptr->name, (char *) 0 ) ;
- return( -1) ;
- }
-
- base_ptr->num_recs = num_recs ;
-
- #ifndef SMALL
- /* Rebuild the Index Files */
- if ( i4reindex(-1) < 0 ) return -1 ;
- #endif
-
- base_ptr->rec_num = 1L ;
- base_ptr->eof = 1 ;
- d4blank() ;
-
- d4unlock( -1L ) ;
-
- return 0 ;
- }
- #endif
-