home *** CD-ROM | disk | FTP | other *** search
-
- /* x4sort.c (c)Copyright Sequiter Software Inc., 1987-1990. All rights reserved.
-
- Sorts a database file.
- */
-
- #include "d4all.h"
- #include "u4error.h"
-
- #include <string.h>
-
- extern int v4cur_base, v4lock_wait ;
-
- static char *compile_ptr = (char *) 0 ;
- static int cleanup(void) ;
- static int new_ref, start_ref ;
-
- static int cleanup()
- {
- if ( compile_ptr != (char *) 0 )
- {
- h4free_memory( compile_ptr ) ;
- compile_ptr = (char *) 0 ;
- }
-
- u4sort_free() ;
- d4buf_return() ;
- if ( new_ref >= 0 )
- if ( d4buf_total( 0L, 0, 0) < 0) return -2 ;
-
- d4select( start_ref ) ;
-
- return -1 ;
- }
-
- int x4sort( char *new_base, char *expr, long start_rec, int safety )
- {
- int buf_len, rc, len ;
- long i_rec, count, bytes_left ;
- char *ptr ;
- char H_PTR rec_ptr, H_PTR get_ptr, H_PTR rw_ptr ;
- BASE *base_ptr ;
-
- base_ptr = d4ptr() ;
- start_ref= d4select(-1) ;
- buf_len = f4record_width() ;
- new_ref = -1 ;
-
- if ( (rc = d4lock_all(v4lock_wait, 1)) < 0) return rc ;
- count = d4reccount() ;
-
- if ( e4parse(expr, &compile_ptr) < 0) return ( cleanup()) ;
- d4blank() ;
-
- if ( e4string( (char *) compile_ptr) == (char *) 0 )
- return( cleanup() ) ;
-
- /* Memory Buffer Operations. */
- if ( d4buf_clear_lend(1) == -1 ) return( cleanup() ) ;
- if ( d4flush( v4cur_base) < 0 ) return( cleanup() ) ;
- d4buf_avail_set() ;
-
- rc = u4sort_init( count, (len = e4length()), buf_len) ;
- if ( rc < 0 )
- {
- if ( rc == -2 )
- u4error( E_SORT, new_base, (char *) 0 ) ;
- return( cleanup() ) ;
- }
-
- bytes_left = d4buf_avail() ;
- if ( bytes_left > 0xFFE0 ) bytes_left = 0xFFE0 ;
- if ( u4read_init( base_ptr->file_hand,
- (rw_ptr= (char H_PTR) d4buf_alloc( bytes_left,1)),
- (unsigned) bytes_left,
- (long) base_ptr->header_len+(start_rec-1L)*base_ptr->buffer_len) < 0)
- return( cleanup() ) ;
-
- rec_ptr = (char H_PTR) f4record() ;
-
- for ( i_rec = start_rec; i_rec <= count; i_rec++ )
- {
- if ( u4read( rec_ptr, buf_len ) != 0)
- return( cleanup() ) ;
-
- base_ptr->rec_num = i_rec ;
-
- rc = x4filter_do() ;
- if ( rc == 1 ) continue ;
- if ( rc == 2 ) break ;
- if ( rc < 0 ) return( cleanup() ) ;
-
- if ( x4relate_do() < 0) return( cleanup()) ;
-
- ptr = e4string( compile_ptr ) ;
- if ( u4sort_add( ptr, base_ptr->buffer) < 0) return( cleanup()) ;
- }
-
- new_ref = d4create( new_base, base_ptr->num_fields, base_ptr->fields, safety) ;
- if ( new_ref < 0) return( cleanup()) ;
-
- if ( d4lock( -1L, 1) < 0 ) return( cleanup() ) ;
-
- base_ptr = d4ptr() ;
-
- if ( u4write_init( base_ptr->file_hand, rw_ptr,
- (unsigned) bytes_left, (long) base_ptr->header_len ) < 0 )
- return( cleanup() ) ;
-
- for(;;)
- {
- if ( u4sort_get( &get_ptr) < 0)
- {
- d4close() ;
- return( cleanup()) ;
- }
-
- if ( get_ptr == (char H_PTR) 0 ) break ;
-
- if ( u4write( get_ptr+len, buf_len) < 0 )
- {
- d4close() ;
- return( cleanup() ) ;
- }
- }
-
- if ( u4write( "\032", 1 ) < 0 )
- {
- d4close() ;
- return( cleanup() ) ;
- }
-
- if ( u4write_flush() < 0 ) return( cleanup() ) ;
-
- if ( cleanup() < -1 ) return -1 ;
-
- return( new_ref ) ;
- }
-