home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / SOURCE / D4PACK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-19  |  3.2 KB  |  142 lines

  1.  
  2. /* d4pack.c   (c) Copyright Sequiter Software Inc., 1987-1990.  All rights reserved.
  3. */
  4.  
  5. #include "p4misc.h"
  6. #include "u4error.h"
  7. #include "d4all.h"
  8.  
  9. #ifndef UNIX
  10. #include <io.h>
  11. #endif
  12.  
  13.  
  14. #ifdef NO_CHSIZE
  15.  
  16. int d4pack()
  17. {
  18.    u4error( 0, "Routine d4pack is not present when compiled with NO_CHSIZE", (char *) 0 ) ;
  19.    return 0 ;
  20. }
  21.  
  22. #else
  23.  
  24. extern  int  d4adjust( BASE *, long ) ;
  25. extern  int  v4lock_wait ;
  26.  
  27. static int  cleanup(void) ;
  28.  
  29. static int  cleanup()
  30. {
  31.    d4buf_calc() ;
  32.    d4buf_return() ;
  33.    return -1 ;
  34. }
  35.  
  36. int d4pack()
  37. {
  38.    BASE  *base_ptr ;
  39.    char H_PTR  ptr ;
  40.    long   read_rec, write_rec, rec_count ;
  41.    long   tot_bytes, ptr_recs, i_write_rec, i_read_rec, rc ;
  42.    int    rec_width, i_rc  ;
  43.  
  44.    base_ptr   = d4ptr() ;
  45.  
  46.    /* Lock the Entire Database. */
  47.    if ( (i_rc = d4lock_all( v4lock_wait, 1)) < 0 ) return i_rc ;
  48.    if ( d4buf_clear_base(d4select(-1)) < 0 )  return -1 ;
  49.    if ( d4buf_clear_lend(1) == -1 )   return -1 ;
  50.    d4buf_avail_set() ;
  51.  
  52.    rec_width =  f4record_width() ;
  53.    rec_count =  d4reccount() ;
  54.  
  55.    ptr =  (char H_PTR) d4buf_alloc( tot_bytes= d4buf_avail(), 1 ) ;
  56.    if ( tot_bytes < rec_width )
  57.    {
  58.       tot_bytes = rec_width*2 ;
  59.       ptr = (char H_PTR) base_ptr->buffer ;
  60.    }
  61.  
  62.    ptr_recs =  tot_bytes/ rec_width ;
  63.    tot_bytes=  ptr_recs * rec_width ;
  64.  
  65.    /* Remove the Marked Records */
  66.    for ( write_rec = 1L, read_rec=1L; read_rec <= rec_count;  
  67.          read_rec += ptr_recs )
  68.    {
  69.       if ( d4lseek(read_rec) < 0 )  return( cleanup() ) ;
  70.  
  71.       rc =  u4huge_read( base_ptr->file_hand, ptr, tot_bytes ) ;
  72.       if ( rc < 0L )  return( cleanup() ) ;
  73.  
  74.       if ( rc < tot_bytes )
  75.       {
  76.      ptr_recs =  rc/rec_width ;
  77.      if ( ptr_recs == 0L )
  78.      {
  79.         u4error( E_READ, d4name(), (char *) 0 ) ;
  80.         return( cleanup() ) ;
  81.      }
  82.       }
  83.  
  84.       for (i_write_rec=0L, i_read_rec = 0L; i_read_rec < ptr_recs; i_read_rec++)
  85.       {
  86.          if ( ptr[rec_width*i_read_rec] == ' ' )
  87.         u4huge_cpy( ptr+i_write_rec++*rec_width, ptr+i_read_rec*rec_width, 
  88.                         (long) rec_width) ;
  89.       }
  90.  
  91.       if ( d4lseek( write_rec) < 0 )  return( cleanup() ) ;
  92.       if ( u4huge_write( base_ptr->file_hand, ptr, i_write_rec*rec_width) < 0 )
  93.          return( cleanup() ) ;
  94.  
  95.       write_rec += i_write_rec ;
  96.    }
  97.  
  98.    cleanup() ;
  99.  
  100.    /* 'd4adjust' is also used by 'd4zap' */
  101.    return( d4adjust(base_ptr, write_rec-1L) ) ;
  102. }
  103.  
  104.  
  105. d4adjust( BASE *base_ptr, long num_recs )
  106. {
  107.    lseek( base_ptr->file_hand,
  108.       base_ptr->header_len + num_recs*base_ptr->buffer_len, 0) ;
  109.  
  110.    /* change the database size */
  111.    if( chsize( base_ptr->file_hand,
  112.       (long)base_ptr->header_len + base_ptr->buffer_len*num_recs )
  113.        != 0 )
  114.    {
  115.       u4error( E_WRITE, base_ptr->name, (char *) 0 ) ;
  116.       return( -1 ) ;
  117.    }
  118.  
  119.    if ( d4lseek(num_recs+1L) < 0 )  return -1 ;
  120.    if ( write( base_ptr->file_hand, "\032", 1 ) != 1 )
  121.    {
  122.       u4error( E_WRITE, base_ptr->name, (char *) 0 ) ;
  123.       return( -1) ;
  124.    }
  125.  
  126.    base_ptr->num_recs =  num_recs ;
  127.  
  128.    #ifndef SMALL
  129.    /* Rebuild the Index Files */
  130.    if ( i4reindex(-1) < 0 )  return -1 ;
  131.    #endif
  132.  
  133.    base_ptr->rec_num = 1L ;
  134.    base_ptr->eof =  1 ;
  135.    d4blank() ;
  136.  
  137.    d4unlock( -1L ) ;
  138.  
  139.    return 0 ;
  140. }
  141. #endif
  142.