home *** CD-ROM | disk | FTP | other *** search
-
- /*
- d4write( base_ref, rec_num )
- (c)Copyright Sequiter Software Inc., 1987, 1988, 1989. All rights reserved.
-
- Uses the 'old buffer'.
- Writes the record from 'buffer' to the database.
- All corresponding index files are kept up to date.
- If 'rec_num' does not exist, the record will be appended.
- */
-
- #include "d4base.h"
- #include "u4error.h"
-
- #include <string.h>
- #include <time.h>
- #include <stdlib.h>
- #ifndef UNIX
- #include <io.h>
- #endif
-
- extern long lseek(int, long, int) ;
- extern BASE *v4base ;
- extern INDEX *v4index ;
-
- extern int v4cur_base ;
- extern int v4unique_error ;
-
-
- int d4write( rec_num )
- long rec_num ;
- {
- int index_on, do_append, rc ;
- char *key_ptr, buffer[34], *buf_ptr, key_data[MAX_KEY_SIZE+8] ;
- BASE *base_ptr ;
- INDEX *index_ptr ;
-
- long time_val ; /* For Update Time */
- struct tm *tm_ptr ;
-
- if ( v4cur_base < 0 )
- {
- u4error( E_D_MISSING, (char *) 0 ) ;
- return( -1 ) ;
- }
-
- base_ptr = v4base + v4cur_base ;
- buf_ptr = base_ptr->buffer ;
-
- if ( rec_num <= 0 || rec_num > d4reccount() )
- {
- /* Append Record */
- d4lock( 0L,1 ) ; /* Lock Record Count Bytes */
- rec_num = d4reccount() + 1 ;
- do_append = 1 ;
- }
- else
- {
- #ifndef SMALL
- /* Make sure the Record has Changed to Avoid Extra Index File Work */
- if ( base_ptr->index_ref >= 0 )
- {
- /* Read the Old Record into the Old Buffer */
- base_ptr->buffer = base_ptr->old_buf ;
- rc = d4go( rec_num) ;
- base_ptr->buffer = buf_ptr ;
- if ( rc < 0 ) return -1 ;
-
- /* Return if the Record has not Changed */
- if ( memcmp( base_ptr->old_buf, buf_ptr, base_ptr->buffer_len) == 0)
- return 0 ;
- }
- #endif
- do_append = 0 ;
- }
-
- #ifndef SMALL
- if ( v4unique_error)
- {
- /* Make sure no duplicate keys would be generated for unique key index files */
- index_on = base_ptr->index_ref ;
- while ( index_on >= 0 )
- {
- index_ptr= v4index + index_on ;
- if ( index_ptr->unique )
- {
- if( (key_ptr = i4eval( index_on )) == (char *) 0)
- {
- d4unlock( 0L ) ;
- return( -1) ;
- }
- if ( (rc = i4seek( index_on, key_ptr )) < 0)
- {
- d4unlock( 0L ) ;
- return( -1) ;
- }
- if ( rc == 0 && b4key(index_on)->rec_num != rec_num )
- {
- char buf[70] ;
- d4unlock( 0L ) ;
- c4key( key_ptr, buf, i4type(index_on) ) ;
- u4error( E_UNIQUE, index_ptr->name, "Key:", buf, (char *) 0) ;
- return( -2 ) ;
- }
- }
- index_on = v4index[ index_on ].prev ;
- }
- }
- #endif
-
- if ( ! do_append )
- if ( d4lock( rec_num, 1) < 0)
- {
- d4unlock( 0L ) ;
- return( -1) ;
- }
-
- base_ptr->rec_num = rec_num ;
-
- /* Write the Record */
- lseek( base_ptr->dos_file,
- base_ptr->header_len+ (rec_num-1)* base_ptr->buffer_len, 0 ) ;
- if ( write( base_ptr->dos_file, base_ptr->buffer, base_ptr->buffer_len)
- != base_ptr->buffer_len )
- {
- d4unlock( 0L ) ;
- ltoa( rec_num, buffer, 10) ;
- u4error( E_WRITE, base_ptr->name, "Record Number:", buffer, (char *) 0 ) ;
- return( -1) ;
- }
-
- if ( do_append)
- {
- /* Record is being Appended */
- if ( write( base_ptr->dos_file, "\x1A", 1) != 1)
- {
- d4unlock( 0L ) ;
- ltoa( rec_num, buffer, 10) ;
- u4error( E_WRITE, base_ptr->name, "Record Number:", buffer, (char *) 0 ) ;
- return( -1) ;
- }
-
- /* write the new size */
- lseek( base_ptr->dos_file, (long)4, 0) ;
- if ( write( base_ptr->dos_file, (char *) &rec_num, 4) != 4)
- {
- d4unlock( 0L ) ;
- u4error( E_WRITE, base_ptr->name, "Record Count Bytes.", (char *) 0 ) ;
- return( -1) ;
- }
- d4unlock( 0L ) ;
- }
-
- #ifndef SMALL
- /* Update the Index Files */
- index_on = base_ptr->index_ref ;
- while ( index_on >= 0 )
- {
- key_ptr = i4eval(index_on) ;
- if ( key_ptr == (char *) 0) return( -1 ) ;
-
- index_ptr = v4index + index_on ;
-
- if ( do_append )
- {
- if ( i4add( index_on, key_ptr, rec_num ) < 0)
- return( -1 ) ;
- }
- else
- {
- memcpy( key_data, key_ptr, index_ptr->key_len ) ;
-
- base_ptr->buffer = base_ptr->old_buf ;
- key_ptr = i4eval(index_on) ;
- base_ptr->buffer = buf_ptr ;
- if ( key_ptr == (char *) 0) return( -1 ) ;
-
- if ( memcmp( key_data, key_ptr, index_ptr->key_len ) != 0 )
- {
- /* Remove Old and Add New */
- if ( i4remove( index_on, key_ptr, rec_num ) < 0)
- return( -1 ) ;
- if ( i4add( index_on, key_data, rec_num ) < 0)
- return( -1 ) ;
- }
- }
-
- index_on = index_ptr->prev ;
- }
- #endif
-
- /* Update the last Update Time */
- time ( (time_t *) &time_val) ;
- tm_ptr = localtime( (time_t *) &time_val) ;
-
- base_ptr->yy = (char) tm_ptr->tm_year ;
- base_ptr->mm = (char) tm_ptr->tm_mon+1 ;
- base_ptr->dd = (char) tm_ptr->tm_mday ;
-
- return( 0) ;
- }