The files:
{csv.inc, requires potcust.pas}
const c=',';
function process_rec(a:custrectype):string;
function tostr(l:longint):string;
var so:string;
begin
str(l,so);
tostr:=so;
end;
function check(s:string):string;
var
i:byte;
s2:string;
l:byte;
trig:boolean;
begin
l:=length(s);
trig:=false;
s2:='';
for i:=1 to l do
if s[i]='"' then begin
s2:=s2+'"'+s[i];
trig:=true;end else s2:=s2+s[i];
if (trig or (pos(c,s)>0) or (pos(#32,s)>0)) then s2:='"'+s2+'"';
check:=s2;
end;
var s:string;
begin
s:=tostr(a.status)+c;
if a.name='' then s:=s+'""'+c
else s:=s+check(a.name)+c;
if a.adr='' then s:=s+'""'+c
else s:=s+check(a.adr)+c;
if a.zip='' then s:=s+'""'+c
else s:=s+check(a.zip)+c;
if a.city='' then s:=s+'""'+c
else s:=s+check(a.city)+c;
if a.state='' then s:=s+'""'+s
else s:=s+check(a.state)+c;
if a.country='' then s:=s+'""'+c
else s:=s+check(a.country)+c;
if a.number='' then s:=s+'""'+c
else s:=s+check(a.number)+c;
if a.comment='' then s:=s+'""'
else s:=s+check(a.comment);
process_rec:=s;
end;
procedure save_csv;
var csv_:text;
fname:string;
ior:integer;
n,t:longint;
sa:string;
begin
writeln(#13#10);
fname:=namefile+'.csv';
assign(csv_,fname);
{$I-}
rewrite(csv_);
{$I+}
ior:=ioresult;
if ior<>0 then begin
writeln(#13#10,'Error occured creating > ',fname);
writeln('Dos error > ',ior);
halt;
end;
SearchKey(idxFName,recnum,skey);
GetRec(dbfile, RecNum, ACust);
n:=recnum;
t:=0;
writeln(csv_,'Status,Name,Address,ZipCode,City,State,Country,Number,Comment');
repeat
sa:=process_rec(acust);
writeln(csv_,sa);
inc(t);
NextKey(idxFName, RecNum, TempCode);
GetRec(dbfile, RecNum, ACust);
until n=recnum;
writeln(t,' lines written to: ',fname);
writeln('Press any key to Exit');readkey;
close(csv_);
halt;
end;
uses crt,dos,taccess,printer,apdb;
label e1;
const
Esc = #27;
KeyName = 15;
KeyZip = 13;
KeyState= 8;
KeyCtry = 8;
type
NameType = string[50];
TABDEF = array[1..4] of CUSTRECTYPE;
var
chc : Char;
DuplicateFlag,
ExitFlag,
insrt : boolean;
ACUST : CUSTrectype;
skey : NameType;
TempCode : NameType;
KeyIndex : string;
dbfile : datafile;
idxFName : indexfile;
CustRecVar : CustRecType;
temp : String;
recnum : longint;
II,
inp_pos : integer;
ch : char;
NameFile,
NameTest : String;
P: PathStr;
D: DirStr;
N: NameStr;
E: ExtStr;
function Exist(Name:STRING):Boolean;
var F:FILE;
begin
{$I-}
Assign(F,Name);
reset(F);
Close(F);
{$I+}
Exist := ( IOResult = 0 ) and ( Name <>'');
end;
Function UpSir(S:string):string;
var i:integer;
begin
UpSir:='';
For I:=1 to Length(S) do S[i]:=UpCase(S[i]);
UpSir:=S;
end;
procedure mes(x,y:integer; s:string);
begin
gotoxy(x,y); write(s);
end;
Function MakeKey(S:String; Lung:byte):string;
var X:String;
begin
X:=S;
While Length(X) < Lung do X := X + ' ';
MakeKey := X;
end;
procedure CreatIdxName(Name : string);
var
Fidx : IndexFile;
i : LongInt;
begin
Writeln;
Writeln('Create index file '+ Name+'N.IDX ..... ');
writeln('Please Wait ...!');
MakeIndex(Fidx, NameFile + 'N.IDX', 15, Duplicates);
for i := 1 to FileLen(dbfile) - 1 do
begin
GetRec(dbfile, I, ACust);
KeyIndex := Copy(ACust.Name,1,15);
KeyIndex := MakeKey(KeyIndex, 15);
if ACust.Status = 0 then AddKey(Fidx, I, KeyIndex);
end;
Closeindex(Fidx);
end;
Procedure InitCust(Var A:CUSTrectype);
begin
with A do
begin
Status :=0;
name:='';
adr:='';
zip:='';
city:='';
state:='';
country:='';
number := '';
comment:='';
end;
end;
{$I keyboard.h}
{$I getN }
Procedure DISP(A:CUSTrectype;S:string);
begin
clrscr;
gotoxy(1,1); write(S);
GOTOXY(2,3); Write('CUSTOMER NAME :',A.Name);
MES(2,6,'ADDRESS');
GOTOXY(5,8); Write('STREET ',A.adr);
GOTOXY(5,10); Write('CITY ',A.city);
GOTOXY(5,11); Write('STATE ',A.state);
GOTOXY(5,13); Write('ZIPCODE ',A.zip);
GOTOXY(5,14); Write('NATION ',A.country);
GOTOXY(5,17); Write('COMMENT ',A.comment);
end;
procedure READCUST(X:NameType);
label e1,l0,l1,l2,l3,l4,l5,l6;
var s:string[2];
ch:char;
i:integer;
TempCode : NameType;
begin
clrscr;
if not DuplicateFlag
then initCust(ACUST)
else
with ACUST do
begin
Status :=0;
name:='';
adr:='';
number := '';
comment:='';
end;
DISP(ACUST,'POTENTIAL CUSTOMER FILE MAINTENANCE - ADD');
ACUST.Name := X;
gotoxy(45,1); write('Filesize : ',FileLen(dbfile):4);
mes(2,1,'POTENTIAL CUSTOMER FILE MAINTENANCE - ADD');
l0:gotoxy(40,2);clreol; Write('(F5) Duplicate key ',DuplicateFlag);
mes(2,3,'CUSTOMER NAME '+ACUST.Name);
mes(2,6,'ADDRESS');
l1:getS(5,8,'STREET ',ACUST.adr,50);
if chc=Esc then goto e1;
if chc=^E then write(^G);
if Chc=F5 then
begin
DuplicateFlag := not DuplicateFlag;
goto l0
end;
l2:getS(5,10,'CITY ',ACUST.city,25);
if chc=Esc then goto e1;
if chc=^E then goto l1;
if Chc=F5 then
begin
DuplicateFlag := not DuplicateFlag;
gotoxy(40,2);clreol; Write('(F5) Duplicate key ',DuplicateFlag);
goto l2
end;
l3:getS(5,11,'STATE ',ACUST.state,5);
if chc=Esc then goto e1;
if chc=^E then goto l2;
if Chc=F5 then
begin
DuplicateFlag := not DuplicateFlag;
gotoxy(40,2);clreol; Write('(F5) Duplicate key ',DuplicateFlag);
goto l3
end;
l4:getS(5,13,'ZIPCODE ',ACUST.zip,10);
if chc=Esc then goto e1;
if chc=^E then goto l3;
if Chc=F5 then
begin
DuplicateFlag := not DuplicateFlag;
gotoxy(40,2);clreol; Write('(F5) Duplicate key ',DuplicateFlag);
goto l4
end;
l5:getS(5,14,'NATION ',ACUST.country,15);
if chc=Esc then goto e1;
if chc=^E then goto l4;
if Chc=F5 then
begin
DuplicateFlag := not DuplicateFlag;
gotoxy(40,2);clreol; Write('(F5) Duplicate key ',DuplicateFlag);
goto l5
end;
l6:getS(5,17,'COMMENT ',ACUST.comment,50);
if chc=Esc then goto e1;
if chc=^E then goto l5;
if Chc=F5 then
begin
DuplicateFlag := not DuplicateFlag;
gotoxy(40,2);clreol; Write('(F5) Duplicate key ',DuplicateFlag);
goto l6
end;
TempCode := ACUST.Name;
With ACUST do
begin
AddRec(Dbfile, Recnum, ACUST);
FlushFile(DbFile);
KeyIndex:=Copy(Name,1,KeyName);
KeyIndex := MakeKey(KeyIndex,KeyName);
AddKey(idxFName,RecNum,KeyIndex);
end;
e1:
end;
procedure ADD;
label l0;
var
x : NameType;
begin
clrscr;
repeat
clrscr;
if DiskFree(0) < 3 * SizeOf(CustRecType) then
begin
write(^G^G^G^G^G);
mes(5,5,'Disk Full ! Press any key for continue.');
ch:=readKey;
ExitFlag := True;
Exit;
ClrScr;
end;
gotoxy(45,1); write('Filesize : ',FileLen(dbfile):4);
mes(2,1,'POTENTIAL CUSTOMER FILE MAINTENANCE - ADD');
mes(2,3,'CUSTOMER NAME ');
x:='';
l0:gotoxy(40,2);clreol; Write('(F5) Duplicate key ',DuplicateFlag);
getS(20,3,'',x,30);
if Chc = Esc then exit;
if Chc=F5 then
begin
DuplicateFlag := not DuplicateFlag;
goto l0
end;
x:=UpSir(x); gotoxy(20,3);write(x);
skey := MakeKey(X, KeyName);
x:=skey;
FindKey(idxFName,Recnum,skey);
if OKey then
begin
mes(5,5,'Duplicate Customer exist ! Do you want to add (Y/N) ?');
mes(5,6,'Option:');
repeat ch:=UpCase(readkey) until ch in ['Y','N',Esc];
if ch = 'Y'
then READCUST(X)
else Exit;
end else READCUST(X);
until true = false;
end;
procedure CHANGE(var A:CustRecType);
label l0,l1,e0,e1,e2,e3,e4,e5,e6;
var
s : string[2];
begin
e0:clrscr;
DISP(A,'POTENTIAL CUSTOMER FILE MAINTENANCE - CHANGE');
l0:mes(1,23,' ESC - QUIT F2 - PREVIOUS F4 - NEXT ');
gets(2,3,'CUSTOMER NAME :',A.Name,30);
A.Name := UpSir(A.Name);
gotoxy(17,3); write(A.Name);
if Chc = Esc then Exit;
if (Chc = F4) then
begin
NextKey(idxFName, RecNum, TempCode);
GetRec(dbfile, recNum, ACust);
A := ACust;
DISP(A,'POTENTIAL CUSTOMER FILE MAINTENANCE - CHANGE');
goto l0;
end;
if (Chc = F2) then
begin
PrevKey(idxFName, RecNum, TempCode);
GetRec(dbfile, recNum, ACust);
A := ACust;
DISP(A,'POTENTIAL CUSTOMER FILE MAINTENANCE - CHANGE');
goto l0;
end;
{
if Chc in [F1,F2,F3,F4] then goto l0;
}
mes(2,6,'ADDRESS');
insrt:=FALSE;
e2:getS(5,8,'STREET ',A.adr,50);
if chc=Esc then goto e1;
if chc=^E then write(^G);
e3:getS(5,10,'CITY ',A.city,25);
if chc=Esc then goto e1;
if chc=^E then goto e2;
e4:getS(5,11,'STATE ',A.state,5);
if chc=Esc then goto e1;
if chc=^E then goto e3;
e5:getS(5,13,'ZIPCODE ',A.zip,10);
if chc=Esc then goto e1;
if chc=^E then goto e4;
e6:getS(5,14,'NATION ',A.country,15);
if chc=Esc then goto e1;
if chc=^E then goto e5;
getS(5,17,'COMMENT ',A.comment,50);
if chc=Esc then goto e1;
if chc=^E then goto e6;
PutRec(dbfile, Recnum, A);
KeyIndex := Copy(ACust.Name,1,KeyName);
KeyIndex := MakeKey(KeyIndex, KeyName);
DeleteKey(IdxFName, Recnum, KeyIndex);
KeyIndex := Copy(A.Name, 1, KeyName);
KeyIndex := MakeKey(KeyIndex, KeyName);
AddKey(IdxFName, Recnum, KeyIndex);
e1:
end;
procedure CHG;
var
x, tempname:NameType;
AC : CustRecType;
begin
repeat
clrscr;
mes(2,1,'POTENTIAL CUSTOMER FILE MAINTENANCE - CHANGE');
mes(2,4,'CUSTOMER NAME ');
x:='';
getS(20,4,'',x,30);
if Chc = Esc then exit;
x:=UpSir(x); gotoxy(20,4);write(x);
skey := MakeKey(X,15);
x:=skey;
SearchKey(idxFName,recnum,skey);
if not OKey then
begin
mes(5,5,'Customer name was not found ! ');
mes(5,6,'Press any key for Exit:');
ch:=readkey;
end else
begin
GetRec(dbfile, RecNum, ACust);
AC := ACust;
CHANGE(AC);
end;
until true = false;
end;
procedure DSP;
var
x, tempname:NameType;
cnt:longint;
begin
repeat
cnt:=0;
clrscr;
mes(2,1,'POTENTIAL CUSTOMER FILE MAINTENANCE - DISPLAY');
mes(2,4,'CUSTOMER NAME ');
x:='';
getS(20,4,'',x,30);
if Chc = Esc then exit;
x:=UpSir(x); gotoxy(20,4);write(x);
skey := MakeKey(X, KeyName);
x:=skey;
SearchKey(idxFName,recnum,skey);
if not OKey then begin
mes(5,7,'Customer does not exist ! ');
mes(5,8,'Press any key for Exit:');
ch:=readkey;
end else begin
{ writeln('recnum=',recnum);readkey;}
GetRec(dbfile, RecNum, ACust);
repeat
DISP(ACust,'POTENTIAL CUSTOMER FILE MAINTENANCE - DISPLAY');
gotoxy(1,23); write('ENTER : ''N'' NEXT, ''P'' PREVIOUS ,''S'' STOP ');
{ writeln(#13#10,'recNum= ',recnum);
writeln('tempcode= ',tempcode);
writeln('cnt= ',cnt);
inc(cnt);}
repeat ch:=upcase(readkey) until ch in ['N','P','S'];
if ch = 'S' then exit;
if ch = 'N' then NextKey(idxFName, RecNum, TempCode);
if ch = 'P' then PrevKey(idxFName, RecNum, TempCode);
{ writeln('## recnum=',recnum);readkey;}
GetRec(dbfile, recNum, ACust);
until true = false;
end;
until true = false;
end;
procedure DDD;
var
x, tempname:NameType;
RecNr : LongInt;
A : CustRecType;
begin
repeat
clrscr;
mes(2,1,'POTENTIAL CUSTOMER FILE MAINTENANCE - DELETE');
mes(2,4,'CUSTOMER NAME ');
x:='';
getS(20,4,'',x,30);
if Chc = Esc then
begin
exit;
end;
x:=UpSir(x); gotoxy(20,4);write(x);
skey := MakeKey(X, KeyName);
FindKey(idxFName,Recnum,skey);
if not OKey then
begin
mes(5,7,'Customer name :' + X + ': was not found ! ');
mes(5,8,'Press any key for Exit:');
ch:=readkey;
end else
begin
RecNr := RecNum;
GetRec(dbfile, RecNum, A);
clrscr;
gotoxy(1,1); write('POTENTIAL CUSTOMER FILE MAINTENANCE - DELETE');
GOTOXY(2,3); Write('CUSTOMER NAME :',A.Name);
MES(2,6,'ADDRESS');
GOTOXY(5,8); Write('STREET ',A.adr);
GOTOXY(5,10); Write('CITY ',A.city);
GOTOXY(5,11); Write('STATE ',A.state);
GOTOXY(5,13); Write('ZIPCODE ',A.zip);
GOTOXY(5,14); Write('NATION ',A.country);
GOTOXY(5,17); Write('COMMENT ',A.comment);
gotoxy(1,23); write('You want to delete this customer ? [Y/N]');
repeat ch:=UpCase(readkey) until ch in ['Y','N',Esc];
if ch in ['N',Esc] then Exit;
Skey := A.Name;
DeleteKey(idxFName, RecNum, skey);
if not OKey then
begin
clrscr;
writeln('Could not delete this record Error 1',^G^G^G^G);
delay(2000);
end;
DeleteRec(dbfile, RecNr);
flushfile(DbFile);
writeln;
Writeln('Record deleted ...',^G^G);delay(1000);
end;
until true = false;
end;
{$I csv.inc}
procedure proc1;
var ch:char;
begin
repeat
clrscr;
if ExitFlag then Exit;
mes(5,1,'POTENTIAL CUSTOMER FILE MAINTENENCE');
mes(5,2,'===================================');
mes(5,4,'----------------------------------');
mes(5,5,'A - ADD POTENTIAL CUSTOMERS');
mes(5,6,'C - CHANGE POTENTIAL CUSTOMERS');
mes(5,7,'D - DELETE POTENTIAL CUSTOMERS');
mes(5,8,'S - DISPLAY POTENTIAL CUSTOMERS');
mes(5,9,'----------------------------------');
mes(5,10,'Esc - RETURN TO THE MAIN MENU');
mes(5,11,'----------------------------------');
{###}
mes(5,12,'O - Convert to CSV ');{###}
mes(5,15,'OPTION:');
repeat ch := UpCase(readkey) until ch in ['A','C','D','S'{###},'O'{###},Esc];
case ch of
'A':ADD;
'C':CHG;
'D':DDD;
'S':DSP;
{###}
'O':save_csv;
{###}
Esc:begin
Exit;
end;
end;
until true = false;
end;
begin
INITCUST(ACUST);
ExitFlag := False;
DuplicateFlag := False;
NameFile := '';
clrscr;
mes(5,8,'Customer file name (max. 7 char for name ) or Enter to Exit:');
getS(1,9,':',NameFile,30);
if length(Namefile) = 0 then Exit;
FSPLIT(NameFile,D,N,E);
II := Length(N);
NameTest := NameFile;
If II >= 8 then Delete(N, Length(N) - (II - 8), 255);
NameFile := Concat(D,N,E);
if pos('.',NameFile) = 0 then NameFile := NameFile + '.DAT';
if not exist(NameFile) then
begin
gotoxy(5,15);
writeln('The file ---> '+NameFile+' doesn''t exist !!');
gotoxy(5,16);
writeln('You want to create a new file [Y/N] ?');
repeat ch:= UpCase(ReadKey) until ch in ['Y','N',Esc];
if ch in ['N',Esc] then Exit;
if pos('.',NameFile) <> 0
then Delete(NameFile, Pos('.',NameFile), 255);
MakeFile(dbfile, NameFile + '.DAT',MaxDataRecSize);
MakeIndex(idxFName, NameFile + 'N.IDX', 15, Duplicates);
closeFile(dbfile);
closeIndex(idxFName);
end;
if pos('.',NameFile) <> 0 then Delete(NameFile, Pos('.',NameFile), 255);
clrscr;
OpenFile(dbfile,NameFile + '.DAT',MaxDataRecSize);
if not OKey then
begin
gotoxy(1,5); writeln('Could not open the datafile ' +NameFile+'.DAT');
Write(^G^G^G^G^G);
delay(2000);
Exit;
end;
OpenIndex(idxFName,NameFile + 'N.IDX',15,Duplicates);
if not OKey then
begin
CreatIdxName(NameFile);
closeIndex(idxFName);
OpenIndex(idxFName,NameFile + 'N.IDX',15,Duplicates);
end;
e1:
clearkey(idxFName);
Proc1;
closeFile(dbfile);
closeIndex(idxFName);
end.