home *** CD-ROM | disk | FTP | other *** search
-
- /* u4upgrade.c (c)Copyright Sequiter Software Inc., 1990-1991. All rights reserved.
- */
-
- #include "u4upgrad.h"
-
- C4CODE c4code ;
-
- /* Generic reference number allocation and deallocation */
-
- static void u4free_ref( int ref, void ***array_pointer, int *num_ref_used )
- {
- (*array_pointer)[ref] = 0 ;
- (*num_ref_used)-- ;
- }
-
- const int extra_ref = 10 ;
-
- static int u4alloc_ref( void ***array_pointer, int *num_ref_used, int *num_ref )
- {
- int new_ref ;
- void **new_list;
-
-
- if ( *num_ref_used < *num_ref )
- {
- for ( new_ref = 0; new_ref < *num_ref; new_ref++ )
- {
- if ( (*array_pointer)[new_ref] == 0 )
- break ;
- }
- }
- else
- {
- new_ref = *num_ref ;
- new_list = (void **)u4alloc(sizeof(D4DATA)*(*num_ref+extra_ref));
- if (new_list == 0)
- e4severe(e4memory, (char *) 0);
- if ( *array_pointer != 0 )
- {
- memcpy( new_list, *array_pointer, sizeof(void *) * *num_ref ) ;
- u4free( *array_pointer ) ;
- }
- *array_pointer = new_list ;
- *num_ref += extra_ref ;
- }
-
- (*num_ref_used)++ ;
- return new_ref ;
- }
-
- /* Variables for Index file allocation/deallocation */
- static I4INDEX **i4index_list = 0 ;
- static int num_index = 0 ;
- static int num_index_used = 0 ;
-
-
- /* Data allocation and deallocation */
-
- static D4DATA **d4data_list = 0 ;
- static int num_data = 0 ;
- static int num_data_used = 0 ;
- static int v4cur_base = -1 ;
-
- static char buffer[255];
-
- /* Define converted CodeBase 4.2 routines */
-
- /* Conversion Routines ----------------------------------------------------- */
- double c45atod(char *char_string, int string_len)
- {
- return c4atod(char_string,string_len);
- }
-
- int c45atoi(char *ptr, int n)
- {
- return c4atoi(ptr,n);
- }
-
- long c45atol(char *ptr, int n)
- {
- return c4atol(ptr,n);
- }
-
- char *c45dtoa(double doub_value,int len,int dec)
- {
- c4dtoa45(doub_value,buffer,len,dec);
- return buffer;
- }
-
- char *c45dt_format(char *str_date,char *picture)
- {
- a4format(str_date,buffer,picture);
- return buffer;
- }
-
- int c45dt_julian(char *str_date,double *julian_date)
- {
- if ((*julian_date = a4format_mdx(str_date)) == (double)1.0E300) return -2;
- return 0;
- }
-
- void c45dt_str( char *dbf_date, double *index_date)
- {
- c4dt_str(dbf_date,index_date);
- }
-
- char *c45dt_unformat(char *date_data,char *picture)
- {
- a4init(buffer,date_data,picture);
- return buffer;
- }
-
- void c45encode(char *to,char *from,char *t_to,char *t_from)
- {
- c4encode(to,from,t_to,t_from);
- }
-
- char *c45ltoa(long long_value,char *result_ptr,int result_len)
- {
- c4ltoa45(long_value,result_ptr,result_len);
- return result_ptr;
- }
-
- void c45trim_n(char *str,int n_ch)
- {
- c4trim_n(str,n_ch);
- }
-
- /* Database Routines ------------------------------------------------------- */
- static int d4alloc_data_file(D4DATA *ptr)
- {
- int i_data;
- i_data = u4alloc_ref( (void ***) &d4data_list, &num_data_used, &num_data ) ;
- d4data_list[i_data] = ptr;
- return i_data ;
- }
-
- /* Routine to ensure a data file is selected. */
- static void d4select_test(void)
- {
- if ( v4cur_base < 0 )
- e4severe( e4result, "No Data File Selected", (char *) 0 ) ;
- if ( d4data_list[v4cur_base] == 0 )
- e4severe( e4result, "No Data File Selected", (char *) 0 ) ;
- }
-
- D4DATA *d4data_ptr( int ref )
- {
- if ( ref < 0 )
- {
- d4select_test() ;
- return d4data_list[v4cur_base] ;
- }
- else
- return d4data_list[ref] ;
- }
-
- int d45append(void)
- {
- int rc;
- d4select_test() ;
- (*d4data_list[v4cur_base]).record_changed = 0;
- rc = d4append_start(d4data_list[v4cur_base],0) ;
- if (rc < 0) return rc;
- if (rc == r4locked) return -2;
- if (rc == r4unique) return -3;
- rc = d4append(d4data_list[v4cur_base]) ;
- if (rc == r4unique) return -3;
- return -(rc<0);
- }
-
- int d45append_blank()
- {
- int rc;
- d4select_test() ;
- rc = d4append_blank(d4data_list[v4cur_base]) ;
- if (rc == r4unique) return -3;
- if (rc == r4locked) return -2;
- return -(rc<0);
- }
-
- int d45bof()
- {
- d4select_test() ;
- return d4bof(d4data_list[v4cur_base]) ;
- }
-
- int d45bottom()
- {
- int rc;
- d4select_test() ;
- rc = d4bottom(d4data_list[v4cur_base]) ;
- if (rc == r4eof) return 3;
- return -(rc!=0);
- }
-
- long d45buf_init(long start_try,long end_try,long ch_try)
- {
- ch_try = ch_try;
- end_try = end_try;
- c4code.mem_size_sort_pool = (unsigned)(start_try > 0xF000 ? 0xF000 : start_try);
- return 0 ;
- }
-
- int d45buf_total(long n_recs,int max_buffers,int may_lend)
- {
- n_recs = n_recs;
- max_buffers = max_buffers;
- may_lend = may_lend;
- return 0;
- }
-
- int d45buf_unit(long n_recs)
- {
- n_recs = n_recs;
- return 0;
- }
-
- int d45close(void)
- {
- int index_ref, rc=0;
- d4select_test() ;
- /* Close any Index Files, for the data file, opened with 'i4open()' */
- for ( index_ref = 0; index_ref < num_index; index_ref++ )
- if ( i4index_list[index_ref] != 0 )
- if ( i4index_list[index_ref]->data == d4data_list[v4cur_base] )
- if (i45close(index_ref) != 0) rc = -1;
- if (d4close(d4data_list[v4cur_base]) != 0) rc = -1;
- u4free_ref( v4cur_base, (void ***) &d4data_list, &num_data_used ) ;
- v4cur_base = -1 ;
- return rc;
- }
-
- int d45close_all()
- {
- int i, rc=0;
- for ( i = 0; i < num_data; i++ )
- {
- v4cur_base = i ;
- if ( d4data_list[v4cur_base] == 0 ) continue ;
- if (d45close() != 0) rc = -1;
- }
- return rc;
- }
-
- int d45create( char *name, int n_fields, FIELD *fields, int safety )
- {
- int temp, i_field;
- F4FIELD_INFO *field_info;
- D4DATA *result;
-
- temp = c4code.safety;
- c4code.safety = safety ;
- field_info = (F4FIELD_INFO *)u4alloc( sizeof(F4FIELD_INFO) * (n_fields+1));
- if ( field_info == 0 ) {
- e4error( &c4code, e4memory, "Sorting data file", (char *) 0 ) ;
- return(-1);
- }
- for ( i_field = 0; i_field < n_fields; i_field++ )
- {
- field_info[i_field].name = fields[i_field].name ;
- field_info[i_field].type = fields[i_field].type ;
-
- if ( fields[i_field].type == 'M' )
- field_info[i_field].len = 10 ;
- else
- field_info[i_field].len = fields[i_field].width ;
- field_info[i_field].dec = fields[i_field].decimals ;
- }
-
- if ((result = (D4DATA *)d4create(&c4code, name, field_info,0)) == 0) {
- if (safety == 1 && c4code.error_code == r4no_create) {
- c4code.safety = temp;
- return -1;
- }
- e4error( &c4code, e4create, name, (char *) 0);
- return(-1);
- }
- c4code.safety = temp;
- u4free(field_info);
- return(v4cur_base = d4alloc_data_file(result));
- }
-
- int d45delete( long rec )
- {
- d4go(d4data_list[v4cur_base], rec ) ;
- d4delete(d4data_list[v4cur_base]) ;
- return 0 ;
- }
-
- int d45deleted()
- {
- d4select_test() ;
- return (d4deleted(d4data_list[v4cur_base]) > 0) ;
- }
-
- int d45eof()
- {
- d4select_test() ;
- return d4eof(d4data_list[v4cur_base]) ;
- }
-
- int d45flush(int ref)
- {
- d45select( ref ) ;
- return d4flush_all(d4data_list[v4cur_base]) ;
- }
-
- int d45go( long rec )
- {
- int rc;
- if ( rec < 1 )
- {
- d4blank(d4data_list[v4cur_base]) ;
- return 1 ;
- }
- d4select_test() ;
- rc = d4go(d4data_list[v4cur_base], rec ) ;
- if (rc == r4locked) return -2;
- return -(rc!=0);
- }
-
- int d45init()
- {
- d4init(&c4code);
- c4code.auto_open = 0;
- return 0 ;
- }
-
- int d45initialize( int num_base, int num_index, int num_blocks, int eval_space, long buf_bytes )
- {
- d4init(&c4code);
- c4code.auto_open = 0;
- eval_space = eval_space; /* No corresponding setting in C4CODE structure */
- c4code.mem_start_data = num_base;
- c4code.mem_start_index = num_index;
- c4code.mem_start_block = num_blocks;
- c4code.mem_size_sort_pool = (unsigned)(buf_bytes > 0xF000 ? 0xF000 : buf_bytes);
- return 0 ;
- }
-
- int d45init_undo()
- {
- return d4init_undo(&c4code);
- }
-
- int d45lock( long lock_code, int do_wait )
- {
- int temp, rc;
- d4select_test() ;
- temp = c4code.wait;
- c4code.wait = do_wait ;
- if ( lock_code > 0 )
- rc = d4lock(d4data_list[v4cur_base],lock_code) ;
- else if ( lock_code < 0 )
- rc = d4lock_file(d4data_list[v4cur_base]) ;
- else
- rc = d4lock_append(d4data_list[v4cur_base]);
- c4code.wait = temp;
- if (rc == r4locked) return -2;
- return -(rc<0);
- }
-
- int d45locked( long rec_code )
- {
- d4select_test() ;
- if ( rec_code > 0 )
- return d4lock_test(d4data_list[v4cur_base], rec_code ) ;
- if ( rec_code < 0 )
- return d4lock_test_file(d4data_list[v4cur_base]) ;
- return d4lock_test_append(d4data_list[v4cur_base]) ;
- }
-
- int d45lock_all(int do_wait,int free_buffers)
- {
- int temp, rc;
- free_buffers = free_buffers; /* Tag file blocks are always flushed */
- temp = c4code.wait;
- c4code.wait = do_wait;
- rc = d4lock_file(d4data_list[v4cur_base]);
- c4code.wait = temp;
- if (rc == r4locked) return -2;
- if (rc < 0) return -1;
- c4code.wait = do_wait;
- rc = d4lock_index(d4data_list[v4cur_base]);
- c4code.wait = temp;
- if (rc == r4locked) return -2;
- if (rc < 0) return -1;
- return 0;
- }
-
- int d45lock_code( int lock_code )
- {
- int return_value = c4code.read_lock;
- switch( lock_code )
- {
- case 1:
- c4code.read_lock = 1 ;
- break ;
-
- case 2:
- c4code.read_lock = 0 ;
- break ;
-
- case -2:
- break ;
-
- #ifdef DEBUG
- default:
- c4code.error( e4parm, "This operation is not supported under CodeBase 4.5", (char *) 0 ) ;
- #endif
- }
- return return_value ;
- }
-
- int d45lock_wait( int do_wait )
- {
- int return_wait_code = c4code.wait ;
- if ( do_wait >= 0 )
- c4code.wait = do_wait ;
- return return_wait_code ;
- }
-
- int d45lseek( long rec )
- {
- d4select_test() ;
- return (int)d4rec_pos(d4data_list[v4cur_base], rec ) ;
- }
-
- int d45pack()
- {
- int rc;
- d4select_test() ;
- rc = d4pack(d4data_list[v4cur_base]);
- if (rc == r4locked) return -2;
- return -(rc!=0);
- }
-
- int d45recall( long rec )
- {
- d4select_test();
- d45go( rec ) ;
- d4recall(d4data_list[v4cur_base]) ;
- d45write( rec ) ;
- return 0 ;
- }
-
- long d45reccount()
- {
- d4select_test() ;
- return d4reccount(d4data_list[v4cur_base]) ;
- }
-
- long d45recno()
- {
- d4select_test() ;
- return d4recno(d4data_list[v4cur_base]);
- }
-
- int d45ref( char *name )
- {
- int i_data;
- char *temp_ptr, *temp;
- temp_ptr = temp = (char *)u4alloc(strlen(name)+1);
- for ( i_data = 0; i_data< num_data; i_data++ )
- if ( d4data_list[i_data] != 0 )
- {
- memcpy(temp,name,strlen(name));
- *(temp+strlen(name)) = 0;
- while (*temp_ptr != ' ' && *temp_ptr) temp_ptr++;
- *temp_ptr = 0;
- temp_ptr = d4alias(d4data_list[i_data]);
- if (strlen(temp) == strlen(temp_ptr))
- if (!memcmp(temp_ptr,temp,(size_t)strlen(temp))) {
- u4free(temp);
- return i_data ;
- }
- }
- u4free(temp);
- return -1 ;
- }
-
- int d45seek_double( double d )
- {
- int rc;
- d4select_test() ;
- rc = d4seek_double(d4data_list[v4cur_base], d ) ;
- if (rc == r4entry || rc == r4locked) return -1;
- return rc;
- }
-
- int d45seek_str( char *str )
- {
- int rc;
- d4select_test() ;
- rc = d4seek(d4data_list[v4cur_base], str ) ;
- if (rc == r4entry || rc == r4locked) return -1;
- return rc;
- }
-
- int d45select( int base_ref )
- {
- int return_ref;
- if ( base_ref < 0 )
- return v4cur_base ;
- return_ref = v4cur_base ;
- v4cur_base = base_ref ;
- d4select_test() ;
- return return_ref ;
- }
-
- int d45skip( long num_rec )
- {
- int rc;
- d4select_test() ;
- rc = d4skip(d4data_list[v4cur_base], num_rec ) ;
- if (rc == r4bof) return 1;
- if (rc == r4locked) return -2;
- if (rc == r4entry) return -3;
- return rc;
- }
-
- int d45top()
- {
- int rc ;
- d4select_test() ;
- rc = d4top(d4data_list[v4cur_base]) ;
- if ( rc == r4locked ) return -2 ;
- return rc ;
- }
-
- int d45unlock( long lock_code )
- {
- d4select_test() ;
- if ( lock_code > 0 )
- return d4unlock_records(d4data_list[v4cur_base]) ;
- if ( lock_code < 0 )
- return -(d4unlock_all(d4data_list[v4cur_base]) != 0) ;
- return d4unlock_append(d4data_list[v4cur_base]) ;
- }
-
- int d45use( char *name )
- {
- D4DATA *result;
- if ((result = d4open(&c4code, name)) == 0)
- return -1;
- v4cur_base = d4alloc_data_file(result) ;
- return v4cur_base ;
- }
-
- int d45use_excl( char *name )
- {
- int rc ;
- if ( (rc = d45use(name)) < 0 ) return rc ;
- if ( d45lock(-1L,1) < 0 ) return -1 ;
- return rc ;
- }
-
- int d45write( long rec )
- {
- int rc;
- d4select_test() ;
- if ( rec <= 0 ) return d45append() ;
- rc = d4write(d4data_list[v4cur_base], rec ) ;
- if (rc == r4locked) return -2;
- if (rc == r4unique) return -3;
- return -(rc<0);
- }
-
- int d45zap( long start, long end )
- {
- int rc;
- d4select_test() ;
- rc = d4zap(d4data_list[v4cur_base], start, end ) ;
- if (rc == r4locked) return -2;
- return -(rc!=0);
- }
-
- /* Expression Evaluation Routines ------------------------------------------ */
- static int e4last_length;
- static int e4last_type;
-
- void *e45eval( char *expr_ptr )
- {
- E4EXPR *expr;
- char **result_ptr_ptr = 0;
- d4select_test() ;
- expr = e4parse(d4data_list[v4cur_base],expr_ptr) ;
- if (expr == 0) {
- e4free(expr);
- return 0;
- }
- if ((e4last_length = e4vary(expr,result_ptr_ptr)) < 0) {
- e4free(expr);
- return 0;
- }
- e4last_type = e4type(expr);
- e4free(expr);
- return *result_ptr_ptr;
- }
-
- void *e45exec(char *compile_ptr)
- {
- char **result_ptr_ptr = 0;
- if ((e4last_length = e4vary((E4EXPR *)compile_ptr,result_ptr_ptr)) < 0) return 0;
- e4last_type = e4type((E4EXPR *)compile_ptr);
- return *result_ptr_ptr;
- }
-
- int e45length()
- {
- return e4last_length;
- }
-
- int e45parse(char *expr_ptr,char **compile_ptr)
- {
- *compile_ptr = (char *)e4parse(d4data_list[v4cur_base],expr_ptr);
- if (*compile_ptr == 0) return -1;
- return strlen(((E4EXPR *)*compile_ptr)->parsed);
- }
-
- char *e45string(char *compile_ptr)
- {
- char **result_ptr_ptr = 0;
- if ((e4last_length = e4vary((E4EXPR *)compile_ptr,result_ptr_ptr)) < 0) return 0;
- e4last_type = e4type((E4EXPR *)compile_ptr);
- return *result_ptr_ptr;
- }
-
- char e45type()
- {
- if (e4last_type == t4num_str) return 'n';
- if (e4last_type == t4num_doub) return 'N';
- if (e4last_type == t4date_doub) return 'd';
- if (e4last_type == t4date_str) return 'D';
- if (e4last_type == t4str) return 'C';
- return 'L';
- }
-
- /* Field Routines ---------------------------------------------------------- */
- F4FIELD *f4field_ptr( long field_ref )
- {
- int f_num, b_ref ;
- b_ref = (int) (field_ref>>16) ;
- if (b_ref < 0 ) return( (F4FIELD *) 0 ) ;
- f_num = (int) (field_ref & 0xFFFF) ;
- return (d4data_list[b_ref]->fields)+f_num ;
- }
-
- long f45ref( char *field_name )
- {
- int f_num;
- d4select_test() ;
- f_num = d4field_number(d4data_list[v4cur_base], field_name ) - 1;
- if ( f_num < 0 ) return -1L ;
- return( ((long)f_num) | (((long)v4cur_base)<<16) ) ;
- }
-
- char f45char( long f_ref )
- {
- return((char)f4char(f4field_ptr(f_ref))) ;
- }
-
- void f45r_char( long f_ref, char chr )
- {
- f4assign_char(f4field_ptr(f_ref),chr);
- }
-
- int f45decimals( long f_ref)
- {
- return f4decimals(f4field_ptr(f_ref)) ;
- }
-
- double f45double( long f_ref )
- {
- return f4double(f4field_ptr(f_ref)) ;
- }
-
- void f45r_double( long f_ref, double d_value )
- {
- f4assign_double(f4field_ptr(f_ref),d_value) ;
- }
-
- int f45int( long f_ref )
- {
- return f4int( f4field_ptr(f_ref) ) ;
- }
-
- void f45r_int( long f_ref, int i_value )
- {
- f4assign_int(f4field_ptr(f_ref),i_value) ;
- }
-
- long f45long( long f_ref )
- {
- return f4long( f4field_ptr(f_ref) ) ;
- }
-
- void f45r_long( long f_ref, long l_value )
- {
- f4assign_long(f4field_ptr(f_ref), l_value) ;
- }
-
- char *f45name( long f_ref )
- {
- return f4name(f4field_ptr(f_ref)) ;
- }
-
- int f45num_fields()
- {
- d4select_test() ;
- return d4num_fields(d4data_list[v4cur_base]) ;
- }
-
- char * f45ptr( long f_ref)
- {
- d4select_test();
- return f4ptr(f4field_ptr(f_ref));
- }
-
- void * f45record()
- {
- d4select_test() ;
- return d4record(d4data_list[v4cur_base]) ;
- }
-
- int f45record_width()
- {
- d4select_test() ;
- return (int)d4record_width(d4data_list[v4cur_base]) ;
- }
-
- long f45j_ref( int j_ref )
- {
- return( (((long) v4cur_base) << 16) | (long) j_ref-1 ) ;
- }
-
- char * f45str( long f_ref)
- {
- return f4str(f4field_ptr(f_ref));
- }
-
- int f45ncpy( long f_ref, char *ptr, int n )
- {
- return f4ncpy(f4field_ptr(f_ref),ptr,n);
- }
-
- void f45r_str( long f_ref, char *str )
- {
- f4assign(f4field_ptr(f_ref),str) ;
- }
-
- int f45true( long f_ref)
- {
- return f4true(f4field_ptr(f_ref)) ;
- }
-
- char f45type( long f_ref)
- {
- return (char)f4type(f4field_ptr(f_ref)) ;
- }
-
- int f45width( long f_ref)
- {
- return (int)f4len(f4field_ptr(f_ref)) ;
- }
-
- /* Memory Handling Routines ------------------------------------------------ */
- char *h45alloc(unsigned int num_bytes)
- {
- return (char *)u4alloc(num_bytes);
- }
-
- void h45free_memory(void *ptr)
- {
- u4free(ptr);
- }
-
- /* Index Routines ---------------------------------------------------------- */
-
- I4INDEX *i4index_ptr( int ref )
- {
- return i4index_list[ref] ;
- }
-
- static void i4exist_test(int ref)
- {
- if ( ref < 0 )
- e4severe( e4result, "Illegal Index File Reference Number", (char *) 0 ) ;
- if ( i4index_list[ref] == 0 )
- e4severe( e4result, "Illegal Index File Reference Number", (char *) 0 ) ;
- }
-
- int i45add(int index_ref,char *key_ptr,long rec_num)
- {
- int rc;
- rc = i4lock(i4index_list[index_ref]);
- if (rc == r4locked) return -2;
- if (rc < 0) return -1;
- rc = t4add((T4TAG *)i4index_list[index_ref]->tags.selected,key_ptr,rec_num);
- if (rc == r4unique) return 1;
- if (rc == 0) return 0;
- return -1;
- }
-
- int i45bottom(int index_ref)
- {
- int rc;
- rc = t4bottom((T4TAG *)i4index_list[index_ref]->tags.selected);
- if (t4eof((T4TAG *)i4index_list[index_ref]->tags.selected)) return 3;
- if (rc == r4locked) return -2;
- return -(rc<0);
- }
-
- long i45check(int index_ref)
- {
- int rc;
- rc = i4check((I4INDEX *)i4index_list[index_ref]);
- if (rc == r4locked) return -2;
- return -(rc!=0);
- }
-
- int i45close( int ref )
- {
- int rc;
- i4exist_test(ref) ;
- rc = i4close(i4index_list[ref]);
- d4data_list[v4cur_base]->indexes.selected = 0;
- u4free_ref( ref, (void ***) &i4index_list, &num_index_used ) ;
- return rc ;
- }
-
- char *i45eval(int index_ref)
- {
- char **result_ptr_ptr = 0;
- T4TAG *temp;
- temp = (T4TAG *) i4index_list[index_ref]->tags.selected;
- e4vary((E4EXPR *)temp->expr,result_ptr_ptr);
- return *result_ptr_ptr;
- }
-
- int i45free(int index_ref)
- {
- int rc;
- if (i4lock(i4index_list[index_ref]) != 0) return -1;
- rc = t4free_all((T4TAG *)i4index_list[index_ref]->tags.selected);
- if (i4unlock(i4index_list[index_ref]) != 0) return -1;
- return -(rc<0);
- }
-
- int i45go(int index_ref,char *key_ptr,long record_number)
- {
- int rc;
- rc = t4go((T4TAG *)i4index_list[index_ref]->tags.selected,key_ptr,record_number);
- if (rc == r4after) return 2;
- if (rc == r4found) return 1;
- if (rc == r4locked) return -2;
- return -(rc<0);
- }
-
- int i45index( char *name, char *expr, int unique, int safety )
- {
- int ref, temp;
- T4TAG_INFO *tag_info;
- char buf[13];
- d4select_test() ;
- ref = u4alloc_ref( (void ***) &i4index_list, &num_index_used, &num_index ) ;
- tag_info = (T4TAG_INFO *)u4alloc(sizeof(T4TAG_INFO) * 2);
- memset( tag_info, 0, sizeof(T4TAG_INFO)*2 ) ;
- u4name_piece(buf, sizeof(buf), name, 0, 1);
- tag_info[0].name = buf ;
- tag_info[0].expression = expr ;
- tag_info[0].unique = e4unique * unique ;
- temp = c4code.safety;
- c4code.safety = safety;
- i4index_list[ref] = (I4INDEX *)i4create(d4data_list[v4cur_base],name,tag_info);
- c4code.safety = temp;
- u4free(tag_info);
- if (i4index_list[ref] == 0)
- if (c4code.error_code == r4locked) return -2;
- else return -1;
- d4data_list[v4cur_base]->indexes.selected = i4index_list[ref];
- return ref ;
- }
-
- int i45lock( int ref, int wait )
- {
- int rc, temp;
- i4exist_test(ref) ;
- temp = c4code.wait;
- c4code.wait = wait;
- rc = i4lock(i4index_list[ref]) ;
- c4code.wait = temp;
- if ( rc == r4locked )
- return -2 ;
- return rc ;
- }
-
- int i45open( char *name )
- {
- int ref;
- d4select_test() ;
- ref = u4alloc_ref( (void ***) &i4index_list, &num_index_used, &num_index ) ;
- i4index_list[ref] = (I4INDEX *)i4open(d4data_list[v4cur_base],name);
- if ( i4index_list[ref] == 0 ) return -1;
- d4data_list[v4cur_base]->indexes.selected = i4index_list[ref];
- return ref ;
- }
-
- int i45ref( char *name )
- {
- int i_index;
- char *temp_ptr, *temp;
- temp_ptr = temp = (char *)u4alloc(strlen(name)+1);
- for ( i_index = 0; i_index< num_index; i_index++ )
- if ( i4index_list[i_index] != 0 )
- {
- memcpy(temp,name,strlen(name));
- *(temp+strlen(name)) = 0;
- while (*temp_ptr != ' ' && *temp_ptr) temp_ptr++;
- *temp_ptr = 0;
- temp_ptr = i4index_list[i_index]->file.name;
- if (strlen(temp) == strlen(temp_ptr))
-
- if (!memcmp(temp_ptr,temp,(size_t)strlen(temp))) {
- u4free(temp);
- return i_index ;
- }
- }
- u4free(temp);
- return -1 ;
- }
-
- int i45reindex( int ref )
- {
- int rc;
- i4exist_test(ref) ;
- rc = i4reindex(i4index_list[ref]) ;
- if (rc == r4locked) return -2;
- return -(rc!=0);
- }
-
- int i45remove(int index_ref,char *key_ptr,long rec_num)
- {
- int rc;
- rc = i4lock(i4index_list[index_ref]);
- if (rc == r4locked) return -2;
- if (rc < 0) return -1;
- rc = t4remove((T4TAG *)i4index_list[index_ref]->tags.selected,key_ptr,rec_num);
- if (rc == r4entry) return 1;
- return -(rc<0);
- }
-
- int i45seek(int index_ref,void *key_ptr)
- {
- int rc;
- rc = t4seek((T4TAG *)i4index_list[index_ref]->tags.selected,key_ptr,strlen((char *)key_ptr));
- if (t4eof((T4TAG *)i4index_list[index_ref]->tags.selected)) return 3;
- if (rc == r4after) return 2;
- if (rc == r4locked) return -2;
- return -(rc<0);
- }
-
- int i45seek_ref()
- {
- I4INDEX *selected;
- d4select_test() ;
- selected = (I4INDEX *) d4data_list[v4cur_base]->indexes.selected ;
- if ( selected == 0 ) return -1 ;
- return i45ref(selected->file.name);
- }
-
- int i45select( int ref )
- {
- I4INDEX *i_ptr, *was_selected;
- if (ref == -1)
- i_ptr = 0;
- else {
- i4exist_test(ref) ;
- i_ptr = i4index_list[ref] ;
- }
- was_selected = (I4INDEX *)d4data_list[v4cur_base]->indexes.selected;
- d4data_list[v4cur_base]->indexes.selected = i_ptr;
- if ( was_selected == 0 ) return -1 ;
- return i45ref( was_selected->file.name ) ;
- }
-
- long i45skip(int index_ref,long n)
- {
- long rc;
- if (n == 0) return 0;
- rc = t4skip((T4TAG *)i4index_list[index_ref]->tags.selected,n);
- if (c4code.error_code < 0) return -n;
- return rc;
- }
-
- int i45top(int index_ref)
- {
- int rc;
- rc = t4top((T4TAG *)i4index_list[index_ref]->tags.selected);
- if (rc == r4locked) return -2;
- if (t4eof((T4TAG *)i4index_list[index_ref]->tags.selected)) return 3;
- return rc;
- }
-
- char i45type( int ref )
- {
- int i_type;
- i4exist_test(ref) ;
- i_type = ((T4TAG *)(i4index_list[ref]->tags.selected))->expr->type;
- if (i_type == t4num_doub) return 'N';
- if (i_type == t4date_doub) return 'D';
- return 'C';
- }
-
- int i45unlock( int ref )
- {
- i4exist_test(ref) ;
- return i4unlock(i4index_list[ref]) ;
- }
-
- void i45unselect()
- {
- d4select_test() ;
- d4data_list[v4cur_base]->indexes.selected = 0;
- }
-
- /* Memo Routines ----------------------------------------------------------- */
- int m45read(long field_ref,long rec_num,char *str,int str_len)
- {
- int rc;
- if (rec_num > d4reccount(d4data_list[v4cur_base])) {
- *str = '\0';
- d4blank(d4data_list[v4cur_base]);
- return 0;
- }
- rc = d4go(d4data_list[v4cur_base],rec_num);
- if (rc == r4locked) return -2;
- if (rc != 0) return -1;
- rc = m4ncpy(f4field_ptr(field_ref),str,str_len);
- if (rc > 0) return rc;
- if (c4code.error_code == 0) return 0;
- if (c4code.error_code == r4locked) return -2;
- return -1;
- }
-
- int m45write(long field_ref,long rec_num,char *str,int str_len)
- {
- int rc;
- if (rec_num > d4reccount(d4data_list[v4cur_base]))
- rc = d4append_start(d4data_list[v4cur_base],0);
- else
- rc = d4go(d4data_list[v4cur_base],rec_num);
- if (rc == r4locked) return -2;
- if (rc != 0) return -1;
- rc = m4assign_n(f4field_ptr(field_ref),str,str_len);
- if (rc != 0) return -1;
- if (rec_num > d4reccount(d4data_list[v4cur_base])) {
- rc = d4append(d4data_list[v4cur_base]);
- if (rc == r4locked) return -2;
- }
- if (rc < 0) return -1;
- return m4len(f4field_ptr(field_ref));
- }
-