home *** CD-ROM | disk | FTP | other *** search
- /* d4pack.c (c) Copyright Sequiter Software Inc., 1987, 1988, 1989. All rights reserved.
- */
-
- #include "u4error.h"
- #include "d4base.h"
-
- #ifndef UNIX
- #include <io.h>
- #endif
-
- extern int d4adjust( BASE *, long ) ;
-
-
- int d4pack()
- {
- BASE *base_ptr ;
- long read_rec, write_rec, base_count ;
-
- base_ptr = d4ptr() ;
-
- /* Lock the Entire Database */
- if ( d4lock( -1L, 1) < 0 ) return( -1 ) ;
-
- base_count = d4reccount() ;
- write_rec = 0 ;
-
- /* Remove the Marked Records */
- for (read_rec=1; read_rec <= base_count ; read_rec++)
- {
- if ( d4go( read_rec) < 0)
- return( -1 ) ;
-
- if ( *base_ptr->buffer == ' ')
- {
- /* Write the Record */
- lseek( base_ptr->dos_file,
- base_ptr->header_len+ write_rec* base_ptr->buffer_len, 0) ;
- if ( write( base_ptr->dos_file, base_ptr->buffer, base_ptr->buffer_len)
- != base_ptr->buffer_len )
- {
- u4error( E_WRITE, base_ptr->name, (char *) 0 ) ;
- return( -1) ;
- }
- write_rec++ ; /* Now write_rec is the record just written */
- }
- }
-
- /* 'd4adjust' is also used by 'd4zap' */
- return( d4adjust(base_ptr, write_rec) ) ;
- }
-
-
- d4adjust( base_ptr, num_recs )
- BASE *base_ptr ;
- long num_recs ;
- {
- lseek( base_ptr->dos_file,
- base_ptr->header_len + num_recs*base_ptr->buffer_len, 0) ;
-
- if ( write( base_ptr->dos_file, "\x1A", 1) != 1)
- {
- u4error( E_WRITE, base_ptr->name, (char *) 0 ) ;
- return( -1) ;
- }
-
- /* write the record count */
- lseek( base_ptr->dos_file, (long)4, 0) ;
- if ( write( base_ptr->dos_file, (char *) &num_recs, 4) != 4)
- {
- u4error( E_WRITE, base_ptr->name, (char *) 0 ) ;
- return( -1 ) ;
- }
-
- /* change the database size */
- if( chsize( base_ptr->dos_file,
- (long)base_ptr->header_len + base_ptr->buffer_len*num_recs + 1 )
- != 0 ) return( -1 ) ;
-
- #ifndef SMALL
- /* Rebuild the Index Files */
- if ( i4reindex(-1) < 0 ) return -1 ;
- #endif
-
- base_ptr->rec_num = 0 ;
- d4unlock( -1L ) ;
- }