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

  1.  
  2. /* (c)Copyright Sequiter Software Inc., 1987-1990. All rights reserved.
  3.  
  4.    d4buf_tot.c  
  5.  
  6.    Must change the order of the databases so that they match the
  7.    order in which buffer memory was added.  The order of buffer
  8.    memory allocation is important to 'd4buf_compress' which is
  9.    used to compress buffer memory.
  10. */
  11.  
  12. #include  "d4all.h"
  13. #include  "h4memory.h"
  14.  
  15. extern BASE *v4base ;
  16. extern int   v4last_base, v4cur_base ;
  17.  
  18.  
  19. int  d4buf_total( long rec_tot, int max_bufs, int may_lend )
  20. {
  21.    BASE  *base_ptr ;
  22.    int   i_buf ;
  23.  
  24.    base_ptr =  d4ptr() ;
  25.    if ( rec_tot  < 0L) rec_tot  =  base_ptr->rec_tot ;
  26.    if ( max_bufs < 0)  max_bufs =  base_ptr->max_bufs ;
  27.    if ( may_lend < 0)  may_lend =  base_ptr->buf_may_lend ;
  28.  
  29.    if ( d4buf_clear_base( v4cur_base ) < 0 )  return -1 ;
  30.  
  31.    base_ptr->n_bufs =  0 ;
  32.    base_ptr->buf_status =  0 ;
  33.  
  34.    if ( max_bufs != base_ptr->max_bufs )
  35.    {
  36.       if ( base_ptr->bufs != (BASE_BUF *) 0 )
  37.          h4free_memory( (char *) base_ptr->bufs - sizeof(MEMORY) ) ;
  38.  
  39.       if ( max_bufs > 0 )
  40.       {
  41.      if (h4create( (char **) &base_ptr->bufs, max_bufs, (int) sizeof(BASE_BUF),-1) <0)
  42.          {
  43.             base_ptr->bufs = (BASE_BUF *) 0 ;
  44.         base_ptr->max_bufs = 0 ;
  45.             return 1 ;
  46.          }
  47.          for ( i_buf = 0; i_buf < max_bufs; i_buf++ )
  48.         base_ptr->bufs[i_buf].start_rec= base_ptr->bufs[i_buf].end_rec= -1L;
  49.       }
  50.       else
  51.      base_ptr->bufs =  (BASE_BUF *) 0 ;
  52.    }
  53.  
  54.    base_ptr->max_bufs  =  max_bufs ;
  55.  
  56.    if ( base_ptr->rec_tot == rec_tot && base_ptr->buf_may_lend == may_lend )
  57.       return 0 ;
  58.  
  59.    /* Algorithm.
  60.  
  61.       1.  Determine the new position of the database and clear the databases
  62.          - If lend status has not changed, the position will not change.
  63.        Clear from the current database.
  64.      - If lend status has gone to NOLEND, the position is immediately
  65.        after the last NOLEND database.
  66.        Clear all which may be lent.
  67.      - If lend status has gone to LEND, the database position becomes
  68.        the very end.
  69.        Clear from the current database.
  70.  
  71.       2.  Move the database
  72.  
  73.       3.  Reset the memory.
  74.  
  75.       4.  Recalculate all of the pointers.
  76.    */
  77.  
  78.    if ( base_ptr->buf_may_lend != may_lend )
  79.    {
  80.       if ( may_lend )
  81.       {
  82.          if ( d4buf_clear_after( v4cur_base) < 0 )  return -1 ;
  83.  
  84.      /* Move to the end. */
  85.          h4move( (char **) &v4base, v4last_base, v4cur_base, 0 ) ;
  86.      if ( v4cur_base != v4last_base )  v4last_base =  v4cur_base ;
  87.       }
  88.       else
  89.       {
  90.          int  first_lend ;
  91.  
  92.          /* May no longer be lent. */
  93.      if ( (first_lend =  d4buf_clear_lend(0)) < 0 )  return -1 ;
  94.  
  95.      if ( v4cur_base == v4last_base  &&  first_lend != v4cur_base )
  96.         if ( base_ptr->prev >= 0 )   v4last_base =  base_ptr->prev ;
  97.          h4move( (char **) &v4base, first_lend, v4cur_base, 1 ) ;
  98.       }
  99.    }
  100.    else
  101.    {
  102.       /* Lend has not changed. */
  103.       if ( d4buf_clear_after( v4cur_base) < 0 )  return -1 ;
  104.    }
  105.  
  106.    base_ptr->buf_may_lend =  may_lend ;
  107.    base_ptr->rec_tot =  rec_tot ;
  108.  
  109.    return( d4buf_calc() ) ;
  110. }
  111.  
  112.  
  113.