|
The following Delphi example demonstrates the use of the FATS  indexing commands:
(* FATS 02.30 (c) GCS Software, Udo Gertz 1993-1998 Test program (Borland Delphi 32bit) Fast Re-Indexing with the command "XB" This program needs the extended version of FATS 19-03-2009 U.Gertz *) program REINDEX; uses windows, sysutils; type {$IFOPT H+} FATScmndstr = string; {$ELSE} FATScmndstr = string[255]; {$ENDIF} FATSkeystr = string[255]; function FATSLibInit(nSize: integer; nSign: integer): Pointer; stdcall; external 'FATS_W32.DLL'; function FATSLibExit(lpFATSdata: Pointer): Pointer; stdcall; external 'FATS_W32.DLL'; function FATSLibCall(var szCmnd: FATScmndstr; var uErrorCode: integer; var szFATSkey: FATSkeystr; lpFATSdata: Pointer): longint; stdcall; external 'FATS_W32.DLL' name 'FATSLibCallA'; const fn_cust = 'CUSTOMER.DAT'; type custrec = record DELETEDMARK: char; ID: string[5]; NAME: string[25]; JOB: string[25]; STREET: string[25]; ZIP: string[5]; CITY: string[20]; end; var hCustomer: file of custrec; custdata: custrec; lpFATSdata: Pointer; szCmnd: FATScmndstr; szFATSkey: FATSkeystr; szRecno: string[8]; uFATSError: word; dwFATSRecno: longint; dwRecords: longint; label exitbuild; begin {$IFOPT H+} lpFATSdata := FATSLibInit( 0, 4 ); {$ELSE} lpFATSdata := FATSLibInit( 0, 1 ); {$ENDIF} if lpFATSdata = nil then exit; assign (hCustomer, fn_cust); reset (hCustomer); (* -------> 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) *) szCmnd:='C\CUSTOMER.KEY\5;25\4\A\1'; dwFATSRecno:=FATSLibCall(szCmnd, uFATSError, szFATSkey, lpFATSdata); If uFATSError <> 0 Then Begin writeln; write ('FATS Errorcode: '); write (uFATSError); writeln (' (Command: C)'); writeln; close( hCustomer ); exit; End; (* ======================================================================== Re-Indexing ======================================================================== *) dwRecords:=0; repeat If EOF( hCustomer ) Then Begin (* 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. *) szCmnd:='XB\1\0'; dwFATSRecno:=FATSLibCall(szCmnd, uFATSError, szFATSkey, lpFATSdata); goto exitbuild; End; seek(hCustomer, dwRecords); read(hCustomer, custdata); dwRecords:=dwRecords + 1; str(dwRecords, szRecno); If custdata.DELETEDMARK = ' ' Then Begin (* "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 *) szCmnd:='XB\1\' + szRecno; szCmnd:=szCmnd + '\' + custdata.ID; szCmnd:=szCmnd + '\' + custdata.NAME; szCmnd:=szCmnd + '\' + custdata.JOB; szCmnd:=szCmnd + '\' + custdata.ZIP + custdata.CITY; End Else Begin (* Add the data record to the list of deleted records. *) szCmnd:='DL\' + szRecno + '\S\1'; End; dwFATSRecno:=FATSLibCall(szCmnd, uFATSError, szFATSkey, lpFATSdata); until uFATSError <> 0; exitbuild: close(hCustomer); lpFATSdata := FATSLibExit( lpFATSdata ); end.
© 2008 GCS Software, Udo Gertz