home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CODE4-1.ZIP / SOURCE.ZIP / I4OPEN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-14  |  4.8 KB  |  177 lines

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