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

  1.  
  2. /* i4close.c   (c)Copyright Sequiter Software Inc., 1987-1990.  All rights reserved.
  3.  
  4.    Closes an index file.
  5. */
  6.  
  7. #include "d4all.h"
  8. #include "u4error.h"
  9.  
  10. #ifndef UNIX
  11. #include <io.h>
  12. #endif
  13.  
  14. extern  INDEX  *v4index ;
  15. extern  BASE   *v4base  ;
  16. extern  BLOCK  *v4block ;
  17. extern  int     v4index_free ;
  18.  
  19. i4close( int index_ref )
  20. {
  21.    INDEX *index_ptr ;
  22.    BASE  *base_ptr  ;
  23.    int    index_on ;
  24.  
  25.    if ( index_ref < 0)
  26.    {
  27.       u4error( E_I_CLOSE, (char *) 0) ;
  28.       return( -1) ;
  29.    }
  30.  
  31.    if ( v4index_free == index_ref )  v4index_free =  -1 ;
  32.  
  33.    index_ptr =  v4index + index_ref ;
  34.    if (index_ptr->base_ref < 0)
  35.    {
  36.       u4error( E_I_CLOSE, (char *) 0) ;
  37.       return( -1) ;
  38.    }
  39.  
  40.    if ( index_ptr->base_ref >= 0 )
  41.    {
  42.       base_ptr  =  v4base  + index_ptr->base_ref ;
  43.       for( index_on=base_ptr->index_ref; index_on >= 0; index_on = v4index[index_on].prev)
  44.          if ( index_on == index_ref )  break ;
  45.  
  46.       if ( index_on != index_ref )
  47.       {
  48.          u4error( E_I_CLOSE, (char *) 0) ;
  49.          return( -1) ;
  50.       }
  51.    }
  52.  
  53.    if ( i4free( index_ref ) < 0)  return -1 ;
  54.    if ( i4unlock( index_ref ) < 0)   /* This function also frees block memory */
  55.       return( -1) ;
  56.  
  57.    if ( index_ptr->file_hand >= 0)
  58.       if ( close( index_ptr->file_hand ) < 0)
  59.       {
  60.      u4error( E_I_CLOSE, (char *) 0) ;
  61.      return( -1) ;
  62.       }
  63.  
  64.    if ( index_ptr->compile != (char *) 0)
  65.       h4free_memory( index_ptr->compile ) ;
  66.  
  67.    if ( index_ptr->base_ref >= 0 )
  68.    {
  69.       if ( base_ptr->index_ref == index_ref )
  70.          base_ptr->index_ref =  h4free( (char **) &v4index, index_ref ) ;
  71.       else
  72.          h4free( (char **) &v4index, index_ref) ;
  73.  
  74.       if ( base_ptr->current_index == index_ref )
  75.          base_ptr->current_index =  -1 ;
  76.    }
  77.  
  78.    return( 0) ;
  79. }
  80.  
  81.