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

  1.  
  2. /*  i4open.c   (c)Copyright Sequiter Software Inc., 1987-1990.  All rights reserved.
  3.  
  4.     Opens an index file.
  5.  
  6.     Returns
  7.        >=0 The reference (int) to the index file.
  8.        -1  Error
  9.  
  10. */
  11.  
  12. #include "d4all.h"
  13. #include "u4error.h"
  14. #include "p4misc.h"
  15.  
  16. #include <string.h>
  17. #ifndef UNIX
  18. #include <io.h>
  19. #endif
  20.  
  21. extern BASE   *v4base ;
  22. extern INDEX  *v4index ;
  23.  
  24. extern int     v4cur_base, v4block_max ;
  25. extern unsigned char _osmajor;
  26.  
  27.  
  28. int   i4open( char *index_name )
  29. {
  30.    int        index_ref, rc, h_size ;
  31.    char    *result ;
  32.    char     name[90] ;
  33.  
  34.    INDEX   *index_ptr     ;
  35.    BASE    *base_ptr     ;
  36.  
  37.    #ifdef CLIPPER
  38.       u4name_full( name, index_name, ".NTX" ) ;
  39.    #else
  40.       u4name_full( name, index_name, ".NDX" ) ;
  41.    #endif
  42.    if ( (rc=i4ref(name)) >= 0 )  return( rc ) ;  /* File already Open */
  43.  
  44.    index_ref =  h4get((char **) &v4index, -1 ) ;
  45.  
  46.    if ( index_ref < 0 )  return -1 ;
  47.  
  48.    index_ptr=  v4index + index_ref ;
  49.  
  50.    strncpy( index_ptr->name, name, 64) ;
  51.    index_ptr->name[63] = '\000' ;
  52.    u4upper( index_ptr->name ) ;
  53.  
  54.    if ( (index_ptr->file_hand =    u4open( name, 0 ))  < 0 )
  55.    {
  56.       h4free ( (char **) &v4index, index_ref ) ;
  57.       return   ( -1 ) ;
  58.    }
  59.  
  60.    lseek( index_ptr->file_hand, (long) 0, 0) ;
  61.    #ifdef CLIPPER
  62.       h_size =  (int) ((char *)(v4index+1) - (char *)&v4index->sign) ;
  63.       rc =  read( index_ptr->file_hand, (char *)&index_ptr->sign, h_size ) ;
  64.    #else
  65.       h_size =  (int) ((char *)(v4index+1) - (char *)&v4index->root) ;
  66.       rc =  read( index_ptr->file_hand, (char *)&index_ptr->root, h_size ) ;
  67.    #endif
  68.    while (rc < 0)
  69.    {
  70.       /*  Wait until the database is unlocked  */
  71.       lseek( index_ptr->file_hand, (long) 0, 0) ;
  72.       #ifdef CLIPPER
  73.      rc =  read( index_ptr->file_hand, (char *)&index_ptr->sign, h_size ) ;
  74.       #else
  75.      rc =  read( index_ptr->file_hand, (char *)&index_ptr->root, h_size ) ;
  76.       #endif
  77.    }
  78.  
  79.    if ( rc != h_size )
  80.    {
  81.       close( index_ptr->file_hand );
  82.       h4free ( (char **) &v4index, index_ref ) ;
  83.       u4error( E_READ, name, (char *) 0 ) ;
  84.       return( -1 ) ;
  85.    }
  86.  
  87.    /* Do some checks on the index file header */
  88.    #ifdef CLIPPER
  89.    if ( index_ptr->key_len   >    MAX_KEY_SIZE   ||
  90.     index_ptr->key_len   <= 0           ||
  91.     index_ptr->keys_max  != 2* index_ptr->keys_half  ||
  92.     index_ptr->keys_half <= 0           ||
  93.     index_ptr->group_len != index_ptr->key_len+ 8  ||
  94.        (index_ptr->sign != 0x6    &&  index_ptr->sign != 0x106) )
  95.    #else
  96.    if ( index_ptr->key_len   > MAX_KEY_SIZE    ||
  97.     index_ptr->key_len  <= 0  ||
  98.     index_ptr->key_len+8 > index_ptr->group_len  ||
  99.     index_ptr->keys_max  < 4  ||
  100.     index_ptr->keys_max  > 50 )
  101.    #endif
  102.    {
  103.       close( index_ptr->file_hand );
  104.       h4free ( (char **) &v4index, index_ref ) ;
  105.       u4error( E_BAD_NDX, index_ptr->name, (char *) 0 ) ;
  106.       return( -1 ) ;
  107.    }
  108.  
  109.    index_ptr->base_ref = v4cur_base ;
  110.  
  111.    if (e4parse( index_ptr->expression, &index_ptr->compile ) < 0)
  112.    {
  113.       close( index_ptr->file_hand ) ;
  114.       h4free( (char **) &v4index, index_ref ) ;
  115.       return( -1) ;
  116.    } 
  117.  
  118.    if ( v4cur_base >= 0 )
  119.    {
  120.       base_ptr =  v4base +  v4cur_base ;
  121.       memcpy( base_ptr->old_buf, base_ptr->buffer, (size_t) base_ptr->buffer_len ) ;
  122.       d4go(0L) ;
  123.       result = (char *) e4exec( index_ptr->compile)  ;
  124.       memcpy( base_ptr->buffer, base_ptr->old_buf, (size_t) base_ptr->buffer_len ) ;
  125.    }
  126.    else
  127.       result = (char *) e4exec( index_ptr->compile)  ;
  128.  
  129.    rc = 0 ;
  130.    if ( result == (char *) 0)  rc =  -1 ;
  131.  
  132.    /* Check Key Lengths and Types */
  133.    if ( e4type() == 'L'  &&  rc == 0 )
  134.    {
  135.       u4error( E_I_TYPE, index_ptr->name, (char *) 0 ) ;
  136.       rc =  -1 ;
  137.    }
  138.  
  139.    #ifdef CLIPPER
  140.       if ( e4type() == 'C'  &&  (int) strlen(result) !=  index_ptr->key_len )
  141.       {
  142.      u4error( E_I_CHANGED, index_ptr->name, (char *) 0 ) ;
  143.      rc =  -1 ;
  144.       }
  145.    #else
  146.       if ( index_ptr->int_or_date != 0    && e4type() == 'C' && rc == 0)
  147.       {
  148.      u4error( E_I_CHANGED, index_ptr->name, (char *) 0 ) ;
  149.      rc =  -1 ;
  150.       }
  151.  
  152.       if ( e4type() == 'C'  &&  (int) strlen(result) !=  index_ptr->key_len )
  153.       {
  154.      u4error( E_I_CHANGED, index_ptr->name, (char *) 0 ) ;
  155.      rc =  -1 ;
  156.       }
  157.    #endif
  158.  
  159.    if ( rc != 0 )
  160.    {
  161.       h4free_memory( index_ptr->compile ) ;
  162.       close( index_ptr->file_hand ) ;
  163.       h4free( (char **) &v4index, index_ref ) ;
  164.       return( -1) ;
  165.    }
  166.  
  167.    #ifdef CLIPPER
  168.       if ( index_ptr->sign == 0x106 )
  169.      index_ptr->unique =  1 ;
  170.    #endif
  171.  
  172.    index_ptr->block_last =  index_ptr->block_first = index_ptr->block_ref =  -1;
  173.    index_ptr->block_max     =  v4block_max ;
  174.    index_ptr->i_type     =  e4type() ;
  175.    index_ptr->lock     =   0 ;
  176.  
  177.    if ( v4cur_base >= 0 )
  178.    {
  179.       h4add( (char **) &v4index, base_ptr->index_ref, index_ref, 0 ) ;
  180.       base_ptr->index_ref =  base_ptr->current_index =  index_ref ;
  181.    }
  182.  
  183.    return( index_ref );
  184. }
  185.  
  186.  
  187.