home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.barnyard.co.uk
/
2015.02.ftp.barnyard.co.uk.tar
/
ftp.barnyard.co.uk
/
cpm
/
walnut-creek-CDROM
/
CPM
/
TURBOPAS
/
TP-UTIL.ARK
/
KSEARCH.SRC
< prev
next >
Wrap
Text File
|
1986-01-06
|
2KB
|
47 lines
{>>>>KEY_SEARCH<<<<}
FUNCTION KEY_SEARCH(VAR KEYS : KEY_FILE;
VAR KEY_REF : INTEGER;
MATCH_IT : STRING80) : BOOLEAN;
VAR HI,LO,MID : INTEGER;
REC_COUNT : INTEGER;
SEARCH_REC : KEY_REC;
FOUND : BOOLEAN;
COLLIDED : BOOLEAN;
BEGIN
KEY_REF := 0;
REC_COUNT := FILESIZE(KEYS);
HI := REC_COUNT;
LO := 0;
KEY_SEARCH := FALSE; FOUND := FALSE; COLLIDED := FALSE;
MID := (LO + HI) DIV 2; { Calc first midpoint }
IF REC_COUNT > 0 THEN { Don't search if file is empty }
REPEAT
SEEK(KEYS,MID);
READ(KEYS,SEARCH_REC);
{ Was there a collision between MID & LO or MID & HI? }
IF (LO = MID) OR (HI = MID) THEN COLLIDED := TRUE;
IF MATCH_IT = SEARCH_REC.KEY THEN { Found it! }
BEGIN
FOUND := TRUE; { Set found flag... }
KEY_SEARCH := TRUE; { ...and function value... }
KEY_REF := SEARCH_REC.REF { ...and key into data file }
END
ELSE { No luck...divide and try again... }
BEGIN
IF MATCH_IT > SEARCH_REC.KEY THEN LO := MID
ELSE HI := MID; { Halve the field }
MID := (LO + HI) DIV 2; { Recalc the midpoint }
KEY_REF := MID { Save MID in KEY_REF }
END
UNTIL COLLIDED OR FOUND
END;
}
{--------------------------------------------------------------}
PROCEDURE GetDat