FATS - Fast Access Tree System
Table of Contents
Programming Interfaces
MS-Visual C/C++ for Windows 3.x


The following C/C++ example demonstrates the use of the FATS  indexing commands:

/*
   FATS 02.30
   (c) GCS Software, Udo Gertz 1993-1998

   Test program (Microsoft C/C++, Visual C/C++)
 
   Fast Re-Indexing with the command "XB"
   This program needs the extended version of FATS

   19-03-2009 U.Gertz
*/ 

   #include <stdio.h>
   #include <io.h>
   #include <fcntl.h>
   #include <sys\types.h>
   #include <sys\stat.h>
   #include <stdlib.h>
   #include <string.h>
   #include <memory.h>
   #include "FATS.h"


   struct custdat {
          char DELETEDMARK;
          char ID[6];
          char NAME[26];
          char JOB[26];
          char STREET[26];
          char ZIP[6];
          char CITY[21]; };

   char           szCmnd[256];
   char           szKey[256];
   unsigned int   uErrorcode;
   unsigned long  dwRecno;
   void _far *    lpFATSData;

   struct custdat cust;


main ()
{

   int  hCustomer;
   int  nIOresult;
   unsigned long dwRecords = 0L;



   lpFATSData = FATSLibInit(0, 2);



   hCustomer = open( "CUSTOMER.DAT", O_RDWR );
   setmode( hCustomer, O_BINARY );


   /*
      -------> Create index file
   */ 

   /*
       "C" Create Indexfile
    
      With this command you create an index file, whereby a possibly
      already existing file with the same name is deleted.
      After the file is created it will be opened with the opening
      flags defined with the command  Auto Refresh (Y) and can be
      accessed under the file number you specified.
    
      Max. 200 primary keys per data record can be administered in an
      index file, the max. key length amounts to 240 characters.
    
      Full path names must be specified using forward slashes (/)
      instead of Backslashes (\), because FATS normally uses the
      Backslash character as delimiter.
      You may change the delimiters by placing the desired character
      as the first character of the command string,
      e.g. szCmnd = "&C&C:\ARTICLES.KEY&1&1&A&1". Any character with
      an Ascii code less then 48 will be accepted.
    
      The syntax of the command string:
    
        szCmnd = "C\{Filename}\{KeyLength}\{KeyCount}\{KeyType}\{FileNo}"
    
          FileName   filename, perhaps with an additional path
                     (e.g. C:/DATA/ARTICLES.KEY or ARTICLES.KEY)
    
          KeyLength  Maximum key length (1-250)
                     If you choose to have more than one key for
                     this index file, you may specify the length
                     for each key (separated by a semicolon ";")
                     to conserve diskette space.
                     Otherwise, the maximum length applies to all
                     keys, i.e. every key will occupy the maximum
                     space.
    
          KeyCount   Number of primary keys (1-200)
    
          KeyType    Key type (A = Ascii text, I = Integer)
    
          FileNo     File number (1-40)

   */  

   dwRecno = FATSLibCall( "C\\CUSTOMER.KEY\\5;25\\4\\A\\1", &uErrorcode, szKey, lpFATSData );

   if ( uErrorcode ) {

     printf( "FATS Errorcode: %i\n", uErrorcode );
     return ( uErrorcode );
   }


   /*
      ========================================================================
                Re-Indexing
      ========================================================================
   */ 

   do {

     nIOresult = read( hCustomer, &cust, sizeof( cust ) );

     if ( nIOresult != sizeof( cust ) ) {

       /*
          After the last record was inserted the creating process
          has to be terminated with the command "XB\{FileNo}\0".
          Because this command closes the index file you don't
          have to do a close command.
       */ 

       dwRecno = FATSLibCall( "XB\\1\\0", &uErrorcode, szKey, lpFATSData );
       break;
     }
     dwRecords++;

     if ( !cust.DELETEDMARK ) {

       /*
           "XB" Re-Indexing: Build
        
          This command inserts the primary keys of the specified
          data record ("RecNo") into the index file. The number
          of keys included in the command string must equal the
          number of primary keys you specified in the "C" command.
        
          Use this command to insert the keys of all data records
          into the index after a successful "Create Indexfile" (C)
          command within a programme loop. The command only can be
          used during the re-indexing phase, i.e. immediately
          according to a call of the "C"-command.
        
          The command "XB\{FileNo}\0" terminates re-indexing and
          closes the corresponding index file. The file is closed
          automatically in the case of a premature abnormal
          termination of indexing. A subsequent close-command can
          always remain undone.
        
          The length of the transferred keys may not exceed the
          maximum key length specified with the "Create Indexfile"
          command. Variable length keys will be padded with the Ascii
          char 0 to the maximum key length.
        
          The syntax of the command string:
        
            szCmnd = "XB\{FileNo}\{RecNo}\{KeyStr1}[\{KeyStr2}]"
        
              FileNo     File number
        
              RecNo      <> 0 Record Number
                         == 0 Stop Re-Indexing
        
              KeyStr#    Key value

       */  

       sprintf( szCmnd, "XB\\1\\%lu\\%s\\%s\\%s\\%s%s", dwRecords,
                cust.ID, cust.NAME, cust.JOB, cust.ZIP, cust.CITY );

     } else {

       /*
          Add the data record to the list of deleted records.
       */ 

       sprintf( szCmnd, "DL\\%lu\\S\\1", dwRecords );
     }

       dwRecno = FATSLibCall( szCmnd, &uErrorcode, szKey, lpFATSData );

   } while ( !uErrorcode );


   close( hCustomer );



   lpFATSData = FATSLibExit( lpFATSData );



}

© 2008  GCS Software, Udo Gertz