home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
w3_prog
/
db3tpwdl.arj
/
DB3DEMDL.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1991-09-13
|
6KB
|
248 lines
{ Simple program to test the DB3 DLL DBase file open functions }
{ Requires db3dll.dll in windows path }
{ Written by Nigel Salt 1991 - apologies for PASCAL it is not }
{ my first language! }
PROGRAM db3demdl;
{$F+}
USES WinTypes, WinProcs,WObjects, Strings, db3dlun;
{db3dlun imports the routines from the DLL}
TYPE
TDB3App=object(TApplication)
procedure InitMainWindow; virtual;
end;
PDB3Win=^TDB3Win;
TDB3Win=object(TWindow)
procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
end;
{*************************************************************}
VAR
{*************************************************************}
testdb: dbfRecord;
testdbflds: ARRAY[1..3] OF _FieldRecord;
testdbfldptr: _dFields;
testdbdata: String;
Iresult: Integer;
DB3App: TDB3App;
DB3Screen: array [0..80,0..80] of char;
DB3SLastlin: 0..80;
DB3SCurlin: 0..80;
StrBuff: array [0..255] of char;
{************************}
procedure StrPadR(S: PChar; L: Integer);
{************************}
var
i: integer;
begin
for i:=strlen(S) to L do
StrCat(S,' ');
end;
{************************}
procedure DB3OutLn(S: PChar);
{************************}
begin
StrCat(DB3Screen[DB3SCurlin],S);
Inc(DB3SCurlin);
Inc(DB3SLastlin);
end;
{************************}
procedure DB3Out(S: PChar);
{************************}
begin
StrCat(DB3Screen[DB3SCurlin],S);
end;
{************************}
PROCEDURE WriteDBError(Errno: Integer);
{************************}
BEGIN
CASE Errno OF
NOT_DB_FILE: DB3OutLn('Not a recognised DBase file');
INVALID_FIELD: DB3OutLn('Invalid field');
REC_TOO_HIGH: DB3OutLn('Tried to read beyond EOF');
PARTIAL_READ: DB3OutLn('Only part of record could be read');
ELSE DB3OutLn('DBF IO Error');
END;
END;
{************************************}
PROCEDURE WriteDBFormat(D: dbfRecord);
{************************************}
VAR
CurField: Integer;
BEGIN
DB3Out('Filename : ');
StrPCopy(StrBuff,D.FileName);
DB3OutLn(StrBuff);
DB3Out('Last updated : ');
StrPCopy(StrBuff,D.DateOfUpdate);
DB3OutLn(StrBuff);
DB3Out('Records : ');
Str(D.NumRecs,StrBuff);
DB3OutLn(StrBuff);
DB3Out('Record length : ');
Str(D.RecLen,StrBuff);
DB3OutLn(StrBuff);
DB3Out('Number fields : ');
Str(D.NumFields,StrBuff);
DB3OutLn(StrBuff);
DB3OutLn(' ');
DB3OutLn('FIELDS:');
DB3OutLn('NAME TYPE LENGTH DEC OFF');
FOR CurField:=1 TO D.NumFields DO
BEGIN
StrPCopy(StrBuff,D.Fields^[CurField].Name);
StrPadR(StrBuff,12);
StrCat(StrBuff,' ');
DB3Out(StrBuff);
StrPCopy(StrBuff,D.Fields^[CurField].Typ);
StrCat(StrBuff,' ');
DB3Out(StrBuff);
Str(D.Fields^[CurField].Len:7,StrBuff);
DB3Out(StrBuff);
DB3Out(' ');
Str(D.Fields^[CurField].Dec:4,StrBuff);
DB3Out(StrBuff);
DB3Out(' ');
Str(D.Fields^[CurField].Off:4,StrBuff);
DB3OutLn(StrBuff);
END;
END;
{************************************}
PROCEDURE WriteDBRec(D: dbfRecord; RecNum: Longint;
VAR dbfError: Integer);
{************************************}
VAR
CurField: Integer;
CurByte: Integer;
FieldOff, FieldEnd: Integer;
BEGIN
DB3OutLn(' ');
DB3Out('RECORD: ');
Str(RecNum,StrBuff);
DB3OutLn(StrBuff);
GetDbfRecord(D,Recnum, dbfError);
IF dbfError<>0 THEN
WriteDBError(dbfError)
ELSE
BEGIN
CurByte:=1;
FOR CurField:=1 TO D.NumFields DO
BEGIN
FieldOff:=D.Fields^[CurField].Off;
StrPCopy(StrBuff,D.Fields^[CurField].Name);
StrPadR(StrBuff,12);
StrCat(StrBuff,' : ');
DB3Out(StrBuff);
StrLCopy(StrBuff,@D.CurRecord^[CurByte],D.Fields^[Curfield].Len);
DB3OutLn(StrBuff);
CurByte:=CurByte+D.Fields^[Curfield].Len;
END;
END;
END;
{************************************}
procedure DB3DoDem;
{************************************}
BEGIN
{Set up fields}
{ Use upper case for DBase compatibility }
testdbfldptr:=@testdbflds;
testdbflds[1].Name:= 'CUSTOMER';
testdbflds[1].Typ := 'C';
testdbflds[1].Len := 20;
testdbflds[1].Dec := 0;
testdbflds[1].Off := 1;
testdbflds[2].Name:= 'DATE';
testdbflds[2].Typ := 'D';
testdbflds[2].Len := 8;
testdbflds[2].Dec := 0;
testdbflds[2].Off := 21;
testdbflds[3].Name:= 'AMOUNT';
testdbflds[3].Typ := 'N';
testdbflds[3].Len := 16;
testdbflds[3].Dec := 0;
testdbflds[3].Off := 29;
{Create a new database}
CreateDbf(testdb, 'dbintst.dbf', 3, @testdbflds[1],Iresult);
{Append 3 records}
{01234567890123456789012345678901234567890123}
testdbdata:='ALPHA 19910801-100.11 ';
Move(testdbdata,testdb.CurRecord^,44);
AppendDbf(testdb,Iresult);
testdbdata:='BETA 199108022000.22 ';
Move(testdbdata,testdb.CurRecord^,44);
AppendDbf(testdb,Iresult);
testdbdata:='GAMMA 19910803330 ';
Move(testdbdata,testdb.CurRecord^,44);
AppendDbf(testdb,Iresult);
CloseDbf(testdb,Iresult);
{Now open and read the three records that were created}
testdb.FileName:='dbintst.dbf';
OpenDbf(testdb,Iresult);
IF Iresult<>0 THEN WriteDBError(Iresult)
ELSE
BEGIN
WriteDBFormat(testdb);
WriteDBRec(testdb,1,Iresult);
WriteDBRec(testdb,2,Iresult);
WriteDBRec(testdb,3,Iresult);
END;
CloseDbf(testdb,Iresult);
END;
{************************}
procedure TDB3App.InitMainWindow;
{************************}
begin
MainWindow:=New(PDB3Win,Init(nil,'DBase DLL Demo output'));
DB3SLastlin:=0;
DB3SCurlin:=0;
DB3DoDem;
end;
{************************}
procedure TDB3Win.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
{************************}
var
i: integer;
TabArr: Integer;
begin
InvalidateRect(PaintDC,nil,False);
for i:=0 to DB3SLastLin do
TabbedTextOut(PaintDC,10,i*20+10,DB3Screen[i],StrLen(DB3Screen[i]),0,TabArr,0);
end;
{************************}
{ MAIN BODY }
{************************}
BEGIN
DB3App.Init('DB3DemDl');
DB3App.Run;
DB3App.Done;
END.