| ![]() | ||||||||||||||||||||
The following Borland Pascal example demonstrates the use of the FATS
 indexing commands:
| "C" Create Indexfile (Creates a index file with the specified characteristics.) | |
| "O" Open Indexfile (Makes a index file available for access.) | |
| "K" Close Indexfile (Releases a index file from availability.) | |
| "I" Insert Record (Insert a new record.) | |
| "F" Search First (Gets the key value and record number of the data record with the first key value.) | |
| "L" Search Last (Gets the key value and record number of the data record with the last key value.) | |
| "N" Search Next (Gets the key value and record number of the data record following the current record.) | |
| "A" Search Next After (Gets the key value and record number of the data record whose key value is greater than the requested key value.) | |
| "E" Search Previous Before (Gets the key value and record number of the data record whose key value is less than the requested key value.) | |
| "Y" Auto Refresh (Specifies file locking and cache modes in single- and multi-user environments.) |
(*
FATS 02.30
(c) GCS Software, Udo Gertz 1993-1998
Test program (Borland Pascal / Protected Mode)
Build the test data file customer.dat.
19-03-2009 U.Gertz
*)
program TST1_BPE;
{$F+}
function FATSDLLInit(uDatasize: word): Pointer;
external 'FATS_BP';
function FATSDLLExit(lpFATSdata: Pointer): Pointer;
external 'FATS_BP';
(*
Calling FATS
============
All commands provided by FATS can be executed with one function:
*)
function FATSDLLCall(var szCmnd: string; var uErrorCode: Word;
var szFATSkey: string; lpFATSdata: Pointer): longint;
external 'FATS_BP';
(*
The meaning of the used parameters:
szCmnd With this command string you specify the actual
FATS command. The available commands are described
in the user manual.
nErrorcode Your application must always pass this variable as
the status parameter on a FATS call. After the
FATS call, the application should always check the
value of this variable. FATS returns a errorcode of
0 after a successful operation. FATS indicates any
errors which occur during processing by returning a
nonzero value in the errorcode variable.
In the manual you can find a list of all FATS
errorcodes and their possible causes.
szReturnKey This variable will contain the key value of a found
key after any normal search command (S,G,F,L,N,P,A,E).
Return Value: Record Number
*)
const
fn_demo = '..\..\..\DEMODATA\CUSTOMER.ASC';
fn_cust = 'CUSTOMER.DAT';
demolen = 105;
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;
hDemodata: file;
custdata: custrec;
demodata: array[1..demolen] of char;
lpFATSdata: Pointer;
szCmnd: string;
szFATSkey: string;
szRecno: string[8];
uFATSError: word;
wCount: word;
dwFATSRecno: longint;
begin
{ -------> init FATS data area }
lpFATSdata := FATSDLLInit( 20000 );
if lpFATSdata = nil then exit;
writeln ('This Test Program was designed for Borland Pascal');
writeln;
writeln ('Choose one of the following dynamic link libraries:');
writeln;
writeln (' FATS_BP.DLL standard version');
writeln;
writeln (' or if you own the extended version of FATS:');
writeln;
writeln (' FATSXBP.DLL extended version');
writeln;
writeln ('Please press the [ENTER] key ...');
writeln;
readln (szCmnd);
(*
-------> Activation of Network Operation
FATS is standardly equiped for the network environment.
Nearly all commands can be executed both in single workstation
and network environment. With the
"Y" Auto Refresh command you
determine the behavior of FATS accessing all index files.
Normally it is sufficient to apply this instruction one time at
the program start whereby the specified access mode will apply
to all coming opened files, i.e. they are accessed either with
network (Y\2) or single workstation access (Y\0).
*)
{ szCmnd:='Y\2'; }
{ dwFATSRecno:=FATSDLLCall(szCmnd, uFATSError, szFATSkey, lpFATSdata); }
(*
-------> Open file with test data
*)
assign (hDemodata, fn_demo);
reset (hDemodata, 1);
(*
-------> create data file
*)
writeln ('Creating Data File ...');
assign (hCustomer, fn_cust);
rewrite (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)
*)
writeln ('Creating Index File ...');
szCmnd:='C\CUSTOMER.KEY\5;25\4\A\1';
dwFATSRecno:=FATSDLLCall(szCmnd, uFATSError, szFATSkey, lpFATSdata);
(*
========================================================================
Insert Records
========================================================================
*)
writeln;
writeln ('290 records will be inserted into the data file.');
writeln ('For each record 4 keys will be stored in the index file.');
writeln;
writeln ('Please press the [ENTER] key ...');
readln (szCmnd);
custdata.ID[0]:=char(5);
custdata.NAME[0]:=char(25);
custdata.JOB[0]:=char(25);
custdata.STREET[0]:=char(25);
custdata.ZIP[0]:=char(5);
custdata.CITY[0]:=char(20);
repeat
blockread(hDemodata, demodata, demolen, wCount);
If wCount = demolen Then Begin
custdata.DELETEDMARK:=' ';
move(demodata[1], custdata.ID[1], 5);
move(demodata[6], custdata.NAME[1], 25);
move(demodata[31], custdata.JOB[1], 25);
move(demodata[56], custdata.STREET[1], 25);
move(demodata[81], custdata.ZIP[1], 5);
move(demodata[86], custdata.CITY[1], 20);
(*
"I" Insert Record
This command is used to insert the primary keys of a new
data record into the index file. After the insert, the
record number of the new record is returned in the "RECNO"
variable. You can use this record number to write the data
record to the data file.
The number of keys included in the command string must equal
the number of primary keys you specified in the
Create Indexfile (C) command.
The length of the transferred keys may not exceed the maximum
key length specified with the Create Indexfile (C) command.
Variable length keys will be padded with the Ascii char 0 to
the maximum key length.
The syntax of the command string:
szCmnd = "I\{FileNo}\{KeyStr1}[\{KeyStr2}[\{KeyStr3}]]"
FileNo File number
KeyStr# Key value
*)
szCmnd:='I\1';
szCmnd:=szCmnd + '\' + custdata.ID;
szCmnd:=szCmnd + '\' + custdata.NAME;
szCmnd:=szCmnd + '\' + custdata.JOB;
szCmnd:=szCmnd + '\' + custdata.ZIP + custdata.CITY;
dwFATSRecno:=FATSDLLCall(szCmnd, uFATSError, szFATSkey, lpFATSdata);
If (uFATSError = 0) Then Begin
seek(hCustomer, dwFATSRecno - 1);
write(hCustomer, custdata);
writeln(custdata.NAME,' --> RecNo ', dwFATSRecno);
End;
End;
until (wCount < demolen) or (uFATSError <> 0);
close(hDemodata);
(*
"K" Close Indexfile
This command closes the index file with the specified
file number.
If you have activated the cache algorithm with the command
Auto Refresh (Y), the possibly still in the cache buffers
presented data are automatically written on the disk.
If the cache is inactive after every FATS command all
changed data are written on the disk. Therefore closing of
a file is necessary only before terminating the application.
If you omit the parameter "FileNo", then all opened index
files are closed. This version is recommended before the
termination of the application program.
The syntax of the command string:
szCmnd = "K\{FileNo}"
FileNo File number
*)
szCmnd:='K\1';
dwFATSRecno:=FATSDLLCall(szCmnd, uFATSError, szFATSkey, lpFATSdata);
(*
========================================================================
Search
========================================================================
*)
(*
"O" Open Indexfile
With this command you open an existing index file with
the opening flags defined with the command Auto Refresh (Y).
After the file was opened it can be accessed under the
file number you specified. An index file already opened
with the same file number is closed before this command
is executed.
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 = "&O&C:\ARTICLES.KEY&1". Any character with an Ascii
code less then 48 will be accepted.
The syntax of the command string:
szCmnd = "O\{FileName}\{FileNo}"
Filename filename, perhaps with an additional path
(e.g. C:/DATEN/ARTICLES.KEY or ARTICLES.KEY)
FileNo File number
*)
szCmnd:='O\CUSTOMER.KEY\1';
dwFATSRecno:=FATSDLLCall(szCmnd, uFATSError, szFATSkey, lpFATSdata);
(*
-------> Print all customers, sorted in ascending order by NAME
*)
writeln;
writeln ('This list shows some fields from the Customer Table,');
writeln ('sorted in ascending alphabetical order on the Customers');
writeln ('surname and forename.');
writeln;
writeln ('Please press the [ENTER] key ...');
readln (szCmnd);
(*
"F" Search First
This command enables your application to retrieve the
record number corresponding to the first key value for
the specified key number.
The syntax of the command string:
szCmnd = "F\{KeyNo}\{FileNo}"
KeyNo Key number
FileNo File number
*)
szCmnd:='F\2\1';
repeat
dwFATSRecno:=FATSDLLCall(szCmnd, uFATSError, szFATSkey, lpFATSdata);
if (uFATSError = 0) then begin
seek(hCustomer, dwFATSRecno - 1);
read(hCustomer, custdata);
writeln(custdata.NAME, ' --> RecNo ', dwFATSRecno);
(*
"A" Search Next After
With this command your application can retrieve the record
number corresponding to the first key value which is greater
than the key value you specify.
If a duplicate key exists, the next higher record number of
the next duplicate will be returned. The key value you specify
with "KeyString" don't have to be a valid key in the index file.
Unlike the "Search Next" command, this command can be used in
a network environment.
The syntax of the command string:
szCmnd = "A\{KeyNo}\{RecNo}\{FileNo}\{KeyString}"
KeyNo Key number
RecNo Record number
FileNo File number
KeyString Key value
*)
str(dwFATSRecno, szRecno);
szCmnd:='A\2\' + szRecno + '\1\' + szFATSkey;
end;
until uFATSError <> 0;
(*
--------> print all customers, sorted in ascending order by JOB
*)
writeln;
writeln ('Two columns - JOB and NAME are displayed, sorted in ascending');
writeln ('order by the JOB field.');
writeln;
writeln ('Please press the [ENTER] key ...');
readln (szCmnd);
szCmnd:='F\3\1';
repeat
dwFATSRecno:=FATSDLLCall(szCmnd, uFATSError, szFATSkey, lpFATSdata);
if uFATSError = 0 then begin
seek(hCustomer, dwFATSRecno - 1);
read(hCustomer, custdata);
writeln(custdata.JOB, ' ', custdata.NAME, ' --> RecNo ', dwFATSRecno);
(*
"N" Search Next
With this command your application can retrieve
the record number corresponding to the first key
value which is greater than the key value recently
returned by one of the search commands, which have
to be executed as the latest command.
If a duplicate key exists, the next higher record
number of the next duplicate will be returned.
Each modification of the index file by inserting or
deletion of keys makes an internal pointer invalid
that is necessary for this instruction. You better
use the command Search Next After (A) because this
can happen in the network environment from any station.
If there is no key in sequence FATS will return an
errorcode of 15.
The syntax of the command string:
szCmnd = "N\{FileNo}"
FileNo File number
*)
szCmnd:='N\1';
end;
until uFATSError <> 0;
(*
--------> print list with records sorted by ZIP and CITY
*)
writeln;
writeln ('Three columns - ZIP, CITY and NAME are displayed, sorted in');
writeln ('descending order by ZIP then CITY.');
writeln;
writeln ('Please press the [ENTER] key ...');
readln (szCmnd);
(*
"L" Search Last
This command enables your application to retrieve the
record number corresponding to the last key value for
the specified key number. If duplicates exist for the
last key value, the record number returned identifies
the last duplicate, that is, the one inserted most
recently.
The syntax of the command string:
szCmnd = "L\{KeyNo}\{FileNo}"
KeyNo Key number
FileNo File number
*)
szCmnd:='L\4\1';
repeat
dwFATSRecno:=FATSDLLCall(szCmnd, uFATSError, szFATSkey, lpFATSdata);
if (uFATSError = 0) then begin
seek(hCustomer, dwFATSRecno - 1);
read(hCustomer, custdata);
writeln(custdata.ZIP,' ', custdata.CITY, ' ', custdata.NAME, ' --> RecNo ', dwFATSRecno);
(*
"E" Search Previous Before
With this command your application can retrieve the record
number corresponding to the first key value which is less
than the key value you specify.
If a duplicate key exists, the next lower record number of
the previous duplicate will be returned. The key value you
specify with "KeyString" don't have to be a valid key in the
index file.
Unlike the "Search Prev" command, this command can be used in
a network environment.
The syntax of the command string:
szCmnd = "E\{KeyNo}\{RecNo}\{FileNo}\{KeyString}"
KeyNo Key number
RecNo Record number
FileNo File number
KeyString Key value
*)
str(dwFATSRecno, szRecno);
szCmnd:='E\4\' + szRecno + '\1\' + szFATSkey;
end;
until uFATSError <> 0;
(*
--------> Close index file
*)
szCmnd:='K\1';
dwFATSRecno:=FATSDLLCall(szCmnd, uFATSError, szFATSkey, lpFATSdata);
close(hCustomer);
{ -------> free FATS data area }
lpFATSdata := FATSDLLExit( lpFATSdata );
end.
© 2008
GCS Software, Udo Gertz