home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CODE4-1.ZIP / SOURCE.ZIP / I4INDEX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-07  |  2.2 KB  |  98 lines

  1.  
  2. /*  i4index.c    (c)Copyright Sequiter Software Inc., 1987, 1988, 1989.  All rights reserved.
  3.  
  4.     Creates an Index File
  5. */
  6.  
  7. #include "d4base.h"
  8. #include "u4error.h"
  9.  
  10. #include <string.h>
  11.  
  12. #ifndef UNIX
  13.    #include <io.h>
  14. #endif
  15.  
  16.  
  17. extern   INDEX  *v4index ;
  18. extern   int     v4cur_base ;
  19. extern   int     v4block_max ;
  20.  
  21. extern unsigned char _osmajor;
  22.  
  23.  
  24.  
  25. int i4index( name, expression, unique, safety)
  26. char *name ;
  27. char *expression ;
  28. int   unique ;      /* 0 - Not Unique Keys;  else, Unique Keys */
  29. int   safety ;      /* 0 - Will Erase any Existing File */
  30. {
  31.    int    index_ref, rc ;
  32.    INDEX *index_ptr ;
  33.    BASE  *base_ptr  ;
  34.    char   full_name[90] ;
  35.  
  36.    #ifdef CLIPPER
  37.       u4name_full( full_name, name, ".NTX" ) ;  
  38.    #else
  39.       u4name_full( full_name, name, ".NDX" ) ;  
  40.    #endif
  41.    rc =  i4ref( full_name ) ;
  42.    if ( rc >= 0 )
  43.    {
  44.       if ( safety ) 
  45.       {
  46.      u4error( E_CREATE, full_name, (char *) 0 ) ;
  47.      return( -1 ) ;
  48.       }
  49.       else
  50.       {
  51.      if ( i4close(rc) < 0 )  return( -1 ) ;
  52.       }
  53.    }
  54.  
  55.    base_ptr =  d4ptr() ;
  56.    if ( base_ptr == (BASE *) 0)
  57.    {
  58.       u4error( E_CREATE, full_name, (char *) 0 ) ;
  59.       return( -1) ;
  60.    }
  61.  
  62.    base_ptr->index_ref= index_ref= h4get((char **)&v4index, base_ptr->index_ref) ;
  63.    if ( index_ref < 0 )  return -1 ;
  64.  
  65.    index_ptr              =  v4index + index_ref ;
  66.    index_ptr->base_ref    =  v4cur_base ;
  67.    index_ptr->block_first =  index_ptr->block_last =  index_ptr->block_ref =  -1 ;
  68.    index_ptr->block_max   =  v4block_max ;
  69.    strncpy( index_ptr->name, full_name, 64) ;
  70.  
  71.    #ifdef CLIPPER
  72.       index_ptr->unique =  unique ? 1 : 0 ;
  73.    #else
  74.       index_ptr->unique =  unique ? 0x100 : 0 ;
  75.    #endif
  76.    strncpy( index_ptr->expression, expression, sizeof(index_ptr->expression) ) ;
  77.  
  78.    if ( safety ) 
  79.       index_ptr->dos_file =  u4open( full_name, 1 ) ;
  80.    else
  81.       index_ptr->dos_file =  u4open( full_name, 2 ) ;
  82.  
  83.    if (index_ptr->dos_file < 0)
  84.    {
  85.       i4close( index_ref ) ;
  86.       return( -1) ;
  87.    }
  88.  
  89.    if (i4reindex( index_ref) < 0)
  90.    {
  91.       i4close( index_ref ) ;
  92.       return( -1) ;
  93.    }
  94.    i4select(  index_ref) ;
  95.  
  96.    return( index_ref ) ;
  97. }
  98.