home *** CD-ROM | disk | FTP | other *** search
-
- /* i4open.c (c)Copyright Sequiter Software Inc., 1987-1990. All rights reserved.
-
- Opens an index file.
-
- Returns
- >=0 The reference (int) to the index file.
- -1 Error
-
- */
-
- #include "d4all.h"
- #include "u4error.h"
- #include "p4misc.h"
-
- #include <string.h>
- #ifndef UNIX
- #include <io.h>
- #endif
-
- extern BASE *v4base ;
- extern INDEX *v4index ;
-
- extern int v4cur_base, v4block_max ;
- extern unsigned char _osmajor;
-
-
- int i4open( char *index_name )
- {
- int index_ref, rc, h_size ;
- char *result ;
- char name[90] ;
-
- INDEX *index_ptr ;
- BASE *base_ptr ;
-
- #ifdef CLIPPER
- u4name_full( name, index_name, ".NTX" ) ;
- #else
- u4name_full( name, index_name, ".NDX" ) ;
- #endif
- if ( (rc=i4ref(name)) >= 0 ) return( rc ) ; /* File already Open */
-
- index_ref = h4get((char **) &v4index, -1 ) ;
-
- if ( index_ref < 0 ) return -1 ;
-
- index_ptr= v4index + index_ref ;
-
- strncpy( index_ptr->name, name, 64) ;
- index_ptr->name[63] = '\000' ;
- u4upper( index_ptr->name ) ;
-
- if ( (index_ptr->file_hand = u4open( name, 0 )) < 0 )
- {
- h4free ( (char **) &v4index, index_ref ) ;
- return ( -1 ) ;
- }
-
- lseek( index_ptr->file_hand, (long) 0, 0) ;
- #ifdef CLIPPER
- h_size = (int) ((char *)(v4index+1) - (char *)&v4index->sign) ;
- rc = read( index_ptr->file_hand, (char *)&index_ptr->sign, h_size ) ;
- #else
- h_size = (int) ((char *)(v4index+1) - (char *)&v4index->root) ;
- rc = read( index_ptr->file_hand, (char *)&index_ptr->root, h_size ) ;
- #endif
- while (rc < 0)
- {
- /* Wait until the database is unlocked */
- lseek( index_ptr->file_hand, (long) 0, 0) ;
- #ifdef CLIPPER
- rc = read( index_ptr->file_hand, (char *)&index_ptr->sign, h_size ) ;
- #else
- rc = read( index_ptr->file_hand, (char *)&index_ptr->root, h_size ) ;
- #endif
- }
-
- if ( rc != h_size )
- {
- close( index_ptr->file_hand );
- h4free ( (char **) &v4index, index_ref ) ;
- u4error( E_READ, name, (char *) 0 ) ;
- return( -1 ) ;
- }
-
- /* Do some checks on the index file header */
- #ifdef CLIPPER
- if ( index_ptr->key_len > MAX_KEY_SIZE ||
- index_ptr->key_len <= 0 ||
- index_ptr->keys_max != 2* index_ptr->keys_half ||
- index_ptr->keys_half <= 0 ||
- index_ptr->group_len != index_ptr->key_len+ 8 ||
- (index_ptr->sign != 0x6 && index_ptr->sign != 0x106) )
- #else
- if ( index_ptr->key_len > MAX_KEY_SIZE ||
- index_ptr->key_len <= 0 ||
- index_ptr->key_len+8 > index_ptr->group_len ||
- index_ptr->keys_max < 4 ||
- index_ptr->keys_max > 50 )
- #endif
- {
- close( index_ptr->file_hand );
- h4free ( (char **) &v4index, index_ref ) ;
- u4error( E_BAD_NDX, index_ptr->name, (char *) 0 ) ;
- return( -1 ) ;
- }
-
- index_ptr->base_ref = v4cur_base ;
-
- if (e4parse( index_ptr->expression, &index_ptr->compile ) < 0)
- {
- close( index_ptr->file_hand ) ;
- h4free( (char **) &v4index, index_ref ) ;
- return( -1) ;
- }
-
- if ( v4cur_base >= 0 )
- {
- base_ptr = v4base + v4cur_base ;
- memcpy( base_ptr->old_buf, base_ptr->buffer, (size_t) base_ptr->buffer_len ) ;
- d4go(0L) ;
- result = (char *) e4exec( index_ptr->compile) ;
- memcpy( base_ptr->buffer, base_ptr->old_buf, (size_t) base_ptr->buffer_len ) ;
- }
- else
- result = (char *) e4exec( index_ptr->compile) ;
-
- rc = 0 ;
- if ( result == (char *) 0) rc = -1 ;
-
- /* Check Key Lengths and Types */
- if ( e4type() == 'L' && rc == 0 )
- {
- u4error( E_I_TYPE, index_ptr->name, (char *) 0 ) ;
- rc = -1 ;
- }
-
- #ifdef CLIPPER
- if ( e4type() == 'C' && (int) strlen(result) != index_ptr->key_len )
- {
- u4error( E_I_CHANGED, index_ptr->name, (char *) 0 ) ;
- rc = -1 ;
- }
- #else
- if ( index_ptr->int_or_date != 0 && e4type() == 'C' && rc == 0)
- {
- u4error( E_I_CHANGED, index_ptr->name, (char *) 0 ) ;
- rc = -1 ;
- }
-
- if ( e4type() == 'C' && (int) strlen(result) != index_ptr->key_len )
- {
- u4error( E_I_CHANGED, index_ptr->name, (char *) 0 ) ;
- rc = -1 ;
- }
- #endif
-
- if ( rc != 0 )
- {
- h4free_memory( index_ptr->compile ) ;
- close( index_ptr->file_hand ) ;
- h4free( (char **) &v4index, index_ref ) ;
- return( -1) ;
- }
-
- #ifdef CLIPPER
- if ( index_ptr->sign == 0x106 )
- index_ptr->unique = 1 ;
- #endif
-
- index_ptr->block_last = index_ptr->block_first = index_ptr->block_ref = -1;
- index_ptr->block_max = v4block_max ;
- index_ptr->i_type = e4type() ;
- index_ptr->lock = 0 ;
-
- if ( v4cur_base >= 0 )
- {
- h4add( (char **) &v4index, base_ptr->index_ref, index_ref, 0 ) ;
- base_ptr->index_ref = base_ptr->current_index = index_ref ;
- }
-
- return( index_ref );
- }
-
-
-