home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / SOURCE / X4SORT.C < prev   
Encoding:
C/C++ Source or Header  |  1990-12-03  |  3.3 KB  |  139 lines

  1.  
  2. /*  x4sort.c   (c)Copyright Sequiter Software Inc., 1987-1990.  All rights reserved.
  3.  
  4.     Sorts a database file.
  5. */
  6.  
  7. #include "d4all.h"
  8. #include "u4error.h"
  9.  
  10. #include <string.h>
  11.  
  12. extern int  v4cur_base, v4lock_wait ;
  13.  
  14. static  char *compile_ptr = (char *) 0 ;
  15. static  int  cleanup(void) ;
  16. static  int  new_ref, start_ref ;
  17.  
  18. static int  cleanup()
  19. {
  20.    if ( compile_ptr != (char *) 0 )
  21.    {
  22.       h4free_memory( compile_ptr ) ;
  23.       compile_ptr =  (char *) 0 ;
  24.    }
  25.  
  26.    u4sort_free() ;
  27.    d4buf_return() ;
  28.    if ( new_ref >= 0 )
  29.       if ( d4buf_total( 0L, 0, 0) < 0) return -2 ;
  30.  
  31.    d4select( start_ref ) ;
  32.  
  33.    return -1 ;
  34. }
  35.  
  36. int  x4sort( char *new_base, char *expr, long start_rec, int safety )
  37. {
  38.    int   buf_len, rc, len ;
  39.    long  i_rec, count, bytes_left ;
  40.    char *ptr ;
  41.    char H_PTR rec_ptr, H_PTR get_ptr, H_PTR rw_ptr ;
  42.    BASE *base_ptr ;
  43.  
  44.    base_ptr =  d4ptr() ;
  45.    start_ref=  d4select(-1) ;
  46.    buf_len  =  f4record_width() ;
  47.    new_ref  =  -1 ;
  48.  
  49.    if ( (rc = d4lock_all(v4lock_wait, 1)) < 0)  return rc ;
  50.    count =  d4reccount() ;
  51.  
  52.    if ( e4parse(expr, &compile_ptr) < 0)  return ( cleanup()) ;
  53.    d4blank() ;
  54.  
  55.    if ( e4string( (char *) compile_ptr) == (char *) 0 )
  56.       return( cleanup() ) ;
  57.  
  58.    /* Memory Buffer Operations. */
  59.    if ( d4buf_clear_lend(1) == -1 )  return( cleanup() ) ;
  60.    if ( d4flush( v4cur_base) < 0 )  return( cleanup() ) ;
  61.    d4buf_avail_set() ;
  62.  
  63.    rc =  u4sort_init( count, (len = e4length()), buf_len) ;
  64.    if ( rc < 0 )
  65.    {
  66.       if ( rc == -2 ) 
  67.          u4error( E_SORT, new_base, (char *) 0 ) ;
  68.       return( cleanup() ) ;
  69.    }
  70.  
  71.    bytes_left =  d4buf_avail() ;
  72.    if ( bytes_left > 0xFFE0 )  bytes_left = 0xFFE0 ;
  73.    if ( u4read_init( base_ptr->file_hand, 
  74.            (rw_ptr= (char H_PTR) d4buf_alloc( bytes_left,1)),
  75.            (unsigned) bytes_left, 
  76.            (long) base_ptr->header_len+(start_rec-1L)*base_ptr->buffer_len) < 0)
  77.       return( cleanup() ) ;
  78.  
  79.    rec_ptr =  (char H_PTR) f4record() ;
  80.  
  81.    for ( i_rec = start_rec; i_rec <= count; i_rec++ )
  82.    {
  83.       if ( u4read( rec_ptr, buf_len ) != 0)
  84.          return( cleanup() ) ;
  85.  
  86.       base_ptr->rec_num =  i_rec ;
  87.  
  88.       rc = x4filter_do() ;
  89.       if ( rc == 1 )  continue ;
  90.       if ( rc == 2 )  break ;
  91.       if ( rc < 0 )  return( cleanup() ) ;
  92.  
  93.       if ( x4relate_do() < 0)  return( cleanup()) ;
  94.  
  95.       ptr =  e4string( compile_ptr ) ;
  96.       if ( u4sort_add( ptr, base_ptr->buffer) < 0)  return( cleanup()) ;
  97.    }
  98.  
  99.    new_ref =  d4create( new_base, base_ptr->num_fields, base_ptr->fields, safety) ;
  100.    if ( new_ref < 0)  return( cleanup()) ;
  101.  
  102.    if ( d4lock( -1L, 1) < 0 )  return( cleanup() ) ;
  103.  
  104.    base_ptr =  d4ptr() ;
  105.  
  106.    if ( u4write_init( base_ptr->file_hand, rw_ptr,
  107.                  (unsigned) bytes_left, (long) base_ptr->header_len ) < 0 )
  108.       return( cleanup() ) ;
  109.  
  110.    for(;;)
  111.    {
  112.       if ( u4sort_get( &get_ptr) < 0)
  113.       {
  114.      d4close() ;
  115.          return( cleanup()) ;
  116.       }
  117.  
  118.       if ( get_ptr == (char H_PTR) 0 )   break ;
  119.  
  120.       if ( u4write( get_ptr+len, buf_len) < 0 ) 
  121.       {
  122.      d4close() ;
  123.          return( cleanup() ) ;
  124.       }
  125.    }
  126.  
  127.    if ( u4write( "\032", 1 ) < 0 ) 
  128.    {
  129.       d4close() ;
  130.       return( cleanup() ) ;
  131.    }
  132.  
  133.    if ( u4write_flush() < 0 )  return( cleanup() ) ;
  134.  
  135.    if ( cleanup() < -1 )  return -1 ;
  136.  
  137.    return( new_ref ) ;
  138. }
  139.