home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / SOURCE / I4INDEX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-03  |  2.6 KB  |  111 lines

  1.  
  2. /*  i4index.c    (c)Copyright Sequiter Software Inc., 1987-1990.  All rights reserved.
  3.  
  4.     Creates an Index File
  5. */
  6.  
  7. #include "d4all.h"
  8. #include "u4error.h"
  9.  
  10. #include <string.h>
  11. #ifndef UNIX
  12. #include <io.h>
  13. #endif
  14.  
  15. extern   INDEX  *v4index ;
  16. extern   int     v4cur_base ;
  17. extern   int     v4block_max ;
  18. extern   int     v4lock_wait ;
  19.  
  20. extern unsigned char _osmajor;
  21.  
  22. int i4index( char *name, char *expression, int unique, int safety)
  23. {
  24.    return( i4index_filter(name,expression,unique,safety, (I4FILTER *) 0) ) ;
  25. }
  26.  
  27. #ifdef KR
  28.    int i4index_filter( name, expression, unique, safety, filter_routine ) 
  29.    char *name ;
  30.    char *expression ;
  31.    int   unique ;
  32.    int   safety ;
  33.    int  (*filter_routine)() ;
  34. #else
  35.    int i4index_filter( char *name, char *expression, int unique, int safety, 
  36.                       int (*filter_routine)(void) ) 
  37. #endif
  38. {
  39.    int    index_ref, rc ;
  40.    INDEX *index_ptr ;
  41.    BASE  *base_ptr  ;
  42.    char   full_name[90] ;
  43.  
  44.    #ifdef CLIPPER
  45.       u4name_full( full_name, name, ".NTX" ) ;  
  46.    #else
  47.       u4name_full( full_name, name, ".NDX" ) ;  
  48.    #endif
  49.    rc =  i4ref( full_name ) ;
  50.    if ( rc >= 0 )
  51.    {
  52.       if ( safety ) 
  53.       {
  54.      u4error( E_CREATE, full_name, (char *) 0 ) ;
  55.      return( -1 ) ;
  56.       }
  57.       else
  58.       {
  59.      if ( i4close(rc) < 0 )  return( -1 ) ;
  60.       }
  61.    }
  62.  
  63.    base_ptr =  d4ptr() ;
  64.    if ( base_ptr == (BASE *) 0)
  65.    {
  66.       u4error( E_CREATE, full_name, (char *) 0 ) ;
  67.       return( -1) ;
  68.    }
  69.  
  70.    if ( (rc = d4lock_all( v4lock_wait, 1)) < 0)  
  71.       return( rc ) ;
  72.  
  73.    base_ptr->index_ref= index_ref= h4get((char **)&v4index, base_ptr->index_ref) ;
  74.    if ( index_ref < 0 )  return -1 ;
  75.  
  76.    index_ptr              =  v4index + index_ref ;
  77.    index_ptr->base_ref    =  v4cur_base ;
  78.    index_ptr->block_first =  index_ptr->block_last =  index_ptr->block_ref =  -1 ;
  79.    index_ptr->block_max   =  v4block_max ;
  80.    strncpy( index_ptr->name, full_name, 64) ;
  81.  
  82.    #ifdef CLIPPER
  83.       index_ptr->unique =  unique ? 1 : 0 ;
  84.    #else
  85.       index_ptr->unique =  unique ? 0x100 : 0 ;
  86.    #endif
  87.    strncpy( index_ptr->expression, expression, sizeof(index_ptr->expression) ) ;
  88.  
  89.    if ( safety ) 
  90.       index_ptr->file_hand =  u4open( full_name, 1 ) ;
  91.    else
  92.       index_ptr->file_hand =  u4open( full_name, 2 ) ;
  93.  
  94.    if (index_ptr->file_hand < 0)
  95.    {
  96.       i4close( index_ref ) ;
  97.       return( -1) ;
  98.    }
  99.  
  100.    index_ptr->filter =  filter_routine ;
  101.  
  102.    if ( (rc = i4reindex( index_ref)) < 0)
  103.    {
  104.       i4close( index_ref ) ;
  105.       return  rc ;
  106.    }
  107.    i4select(  index_ref) ;
  108.  
  109.    return( index_ref ) ;
  110. }
  111.