home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
beehive
/
utilitys
/
library.arc
/
LIBRARY.PAS
< prev
Wrap
Pascal/Delphi Source File
|
1990-11-30
|
11KB
|
441 lines
{A simple library database}
const
DATA_FILE = 'LIBRARY.DAT';
BIGGEST = 500; {biggest library allowable}
NO_OF_CATS = 99;{biggest no. of categories listable with <T>opic}
type
CAT_STRING = string[20];
line = string[75];
number = 0..5;
rec = record
Title : string[50];
Author : string[30];
Cat : CAT_STRING;
Year : string[4];
Desc : line;
Comm : line;
end;
var
lib : file of rec;
i : byte;
book : rec;
reply, inkey : char;
big : integer;
function GetKey(teststr : CAT_STRING; option : number) : char;
var TestChar : char;
begin
repeat read(kbd,testchar) until pos(upcase(testchar),teststr)>0;
if option=1 then writeln(upcase(testchar));
GetKey:=upcase(testchar);
end;
procedure Topic_List;
var
categ : array[1..NO_OF_CATS] of CAT_STRING;
j, k, listcat, catno, code : integer;
used : boolean;
in1, in2 : char;
listcatstr : string[2];
begin
for i:=1 to NO_OF_CATS do categ[i]:='';
big:=filesize(lib);
seek(lib,0);
catno:=0;
for k:=1 to big do
begin
write('.');
read(lib,book);
used:=FALSE; {used to get an unduplicated list of cat's.}
j:=1;
while j<k do
begin
if book.cat=categ[j] then used:=TRUE;{already in list, so forget it}
j:=j+1;
end;
if not(used) then {not in list so far, so}
begin
catno:=catno+1; {move to next pos. on list}
categ[catno]:=book.cat; {add cat to list,}
end;
end;
writeln;
repeat
writeln;
for i:=1 to catno do
begin
write(i:2,'. ',categ[i]);
for k:=length(categ[i]) to 21 do write(' ');
if (i mod 3)=0 then writeln;
end;
writeln;
repeat
writeln;
write('Enter category to list (1-',catno,') or [Q]uit : ');
in1:=GetKey('123456789Q',0);
if in1='Q' then begin writeln(in1); exit end;
write(in1);
in2:=GetKey(^M+'0123456789',0);
if in2=^M then listcatstr:=in1 else
begin
write(in2);
listcatstr:=in1+in2;
end;
Val(listcatstr,listcat,code);
writeln;
writeln;
until (listcat in [1..catno]) and (code=0);
seek(lib,0);
j:=0;
for i:=1 to big do
begin
read(lib,book);
if book.cat=categ[listcat] then
begin
j:=j+1;
writeln(i:2,'. ',book.title);
if (((j mod 10)=0) and (i<big-2)) then
begin
writeln('Press any key to continue');
repeat until keypressed;
end;
end;
end;
writeln;
write('Press any key to quit, or <T>opic list & search : ');
read(kbd,inkey);
writeln(inkey);
until not(inkey in ['T','t']);
end;
procedure Add_Entry;
var
Temp : array[1..5] of line;
no : byte;
begin
writeln('Add Entry');
writeln;
writeln('Book #',filesize(lib)+1,'.');
with book do
begin
writeln('Enter title : -----------------------------------|');
buflen:=50;
readln(Title);
if (pos('The ',Title)=1) and (length(Title)<50) then
begin
Delete(Title,1,4);
Title:=Title+', The';
end;
writeln('Enter author : --------------|');
buflen:=30;
readln(Author);
writeln('Enter category ----|');
buflen:=20;
readln(Cat);
write('Enter year : ');
buflen:=4;
readln(Year);
buflen:=75;
writeln('Enter description --------------------------------|');
readln(Desc);
buflen:=75;
writeln('Enter comment ---------------------------------------|');
readln(Comm);
no:=0;
end;
writeln;
write('Would you like to <S>ave or <A>bort this entry : ');
inkey:=GetKey('SA',1);
if inkey='S' then
begin
big:=filesize(lib);
seek(lib,big);
write(lib,book);
close(lib);
reset(lib);
end;
end;
procedure Sort_Books;
var
ind : array[1..BIGGEST] of integer;
tit : array[1..BIGGEST] of string[20];
newlib : file of rec;
j, k, tempind : integer;
book1, temp : rec;
temptit : string[50];
begin
big:=filesize(lib);
writeln('Sort books by Title');
writeln;
writeln('N.B. This is a disk-based sort.');
writeln('If there isn''t enough space on the disk, LIBRARY will crash...');
write('Loading, ');
seek(lib,0);
for i:=1 to big do
begin
ind[i]:=i;
read(lib,temp);
tit[i]:=temp.title;
for j:=1 to 20 do tit[i][j]:=upcase(tit[i][j]);
end;
write('Sorting, ');
for k:=big-1 downto 1 do
begin
for j:=1 to k do
begin
if tit[j+1]<tit[j] then
begin
temptit:=tit[j]; tempind:=ind[j];
tit[j]:=tit[j+1]; ind[j]:=ind[j+1];
tit[j+1]:=temptit; ind[j+1]:=tempind;
end
end; {now ind & titles have been sorted in memory.}
end;
assign(newlib,'LIBRARY.$$$');
write('Writing, ');
rewrite(newlib);
for i:=1 to big do
begin
seek(lib,ind[i]-1);
read(lib,book1);
if pos('~~~~~~~',book1.title)=0 then write(newlib,book1);
end;
write('Closing, ');
close(newlib);
close(lib);
erase(lib);
rename(newlib,DATA_FILE);
assign(lib,DATA_FILE);
writeln('Finished.');
reset(lib);
end;
procedure Quit;
begin
writeln('Quit...');
close(lib);
halt;
end;
procedure Display_Entry;
var
code, no : integer;
nostr : string[3];
begin
big:=filesize(lib);
repeat
write('Enter no. of book to display (1-',big,') : ');
buflen:=3;
readln(nostr);
Val(nostr,no,code);
if (code<>0) or (nostr='') then no:=0;
until no in [1..big];
seek(lib,no-1);
read(lib,book);
writeln;
with book do
begin
write('Title : ');
writeln(Title);
write('Author : ');
writeln(Author);
write('Category : ');
writeln(Cat);
write('Year : ');
writeln(Year);
writeln('Description');
writeln(Desc);
writeln('Comments');
writeln(Comm);
end;
end;
procedure Display_Titles;
var
Print_Authors : boolean;
startstr : string[3];
code, start : integer;
begin
write('Display authors too (Y/N) : ');
inkey:=GetKey('YN',1);
Print_Authors:=(inkey='Y');
big:=filesize(lib);
writeln;
repeat
write('Start display at book no [1] : ');
buflen:=3;
readln(startstr);
Val(startstr,start,code);
if (code<>0) or (startstr='') then start:=1;
until start in [1..big];
seek(lib,start-1);
for i:=start to big do
begin
read(lib,book);
write(i:2,': ');
write(book.title);
if Print_Authors then
if (length(book.author)+length(book.title)>71) then
write(#10,#13,' / ',book.author)
else write(' / ',book.author);
writeln;
if ((i+1-start) mod 10)=0 then
begin
writeln('Press any key to continue, or [Q]uit.');
read(kbd,inkey);
if upcase(inkey)='Q' then exit;
end;
end;
if (i mod 10)>2 then
begin
writeln('Press any key to continue');
repeat until keypressed;
end;
end;
procedure Edit_Entry;
var
code, no, ln : integer;
nostr, lnstr : string[3];
temp : string[50];
begin
big:=filesize(lib);
repeat
write('Enter no. of book to edit (1-',big,') : ');
buflen:=3;
readln(nostr);
Val(nostr,no,code);
if (code<>0) or (nostr='') then no:=0;
until no in [1..big];
seek(lib,no-1);
read(lib,book);
writeln;
with book do
begin
writeln('1: ',Title);
writeln('2: ',Author);
writeln('3: ',Cat);
writeln('4: ',Year);
writeln('5: ',Desc);
writeln('6: ',Comm);
writeln;
repeat
write('Enter line to edit (1-6 or any other key to quit) : ');
buflen:=1;
readln(lnstr);
Val(lnstr,ln,code);
if (code<>0) or (lnstr='') then exit;
until ln in [1..6];
writeln('Enter new field : ');
case ln of
1 : buflen:=50;
2 : buflen:=30;
3 : buflen:=10;
4 : buflen:=4;
5,6 : buflen:=75;
end;
readln(temp);
case ln of
1 : title:=temp;
2 : author:=temp;
3 : cat:=temp;
4 : year:=temp;
5 : desc:=temp;
6 : comm:=temp;
end;
write('Would you like to <S>ave or <A>bort this edit : ');
inkey:=GetKey('SA',1);
if inkey='S' then
begin
seek(lib,no-1);
write(lib,book);
close(lib);
reset(lib);
end;
end;
end;
procedure Gobble;
var
in1, in2 : char;
gobno, code : integer;
gobnostr : string[2];
begin
big:=filesize(lib);
repeat
repeat
writeln;
writeln('Get rid of a book.');
writeln;
writeln('This involves a disk-based sort, so if there isn''t enough');
writeln('room on the disk, LIBRARY will crash.');
writeln;
write('Enter book to get rid of (1-',big,') or [Q]uit : ');
in1:=GetKey('123456789Q',0);
if in1='Q' then begin writeln(in1); exit; end;
write(in1);
in2:=GetKey(^M+'1234567890',0);
if in2=^M then gobnostr:=in1 else
begin
write(in2);
gobnostr:=in1+in2;
end;
Val(gobnostr,gobno,code);
writeln;
writeln;
until (gobno in [1..big]) and (code=0);
seek(lib,gobno-1);
read(lib,book);
writeln(gobno,': ',book.title);
writeln;
write('Are you sure that you want to erase the above book (Y/N) : ');
reply:=GetKey('YN',0);
if reply='N' then begin writeln; exit end;
book.title:='~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~';
seek(lib,gobno-1);
write(lib,book);
writeln;
write('Press any key to quit and save changes, or <G>et rid of another book : ');
read(kbd,inkey);
writeln(inkey)
until not(inkey in ['G','g']);
Sort_Books;
end;
procedure Menu;
begin
writeln;
big:=filesize(lib);
write('--- LIBRARY v1.0 ---------------------------------- ');
writeln(big:3,' books in the library ---');
writeln(' <A>dd a book to library');
writeln(' <B>ooks in the library');
writeln(' <D>isplay a book''s details');
writeln(' <E>dit a book''s details');
writeln(' <G>et rid of a book');
writeln(' <S>ort by title');
writeln(' <T>opic list & search');
writeln(' [Q]uit the library.');
writeln;
write('Please enter your choice : ');
inkey:=GetKey('ABDEGSQT',1);
writeln;
case inkey of
'A' : Add_Entry;
'B' : Display_Titles;
'D' : Display_Entry;
'E' : Edit_Entry;
'G' : Gobble;
'Q' : Quit;
'S' : Sort_Books;
'T' : Topic_List;
end;
end;
begin
assign(lib,DATA_FILE);
reset(lib);
repeat Menu until false;
end.