unit IoFile;
{ unita per gestire i file binari }
interface
uses Nominativo;
type tpersone=indirizzo;
qword=Int64;
type fiolima=object
private
fp:file of indirizzo;
nome:tstr;
modalita:char;
public
function ApriFile(fr:tstr; m:char):boolean;
function NumeroDiCampi:qword;
function FineFile:boolean;
procedure scrivi(rec:tpersone);overload;
procedure scrivi(rec:tpersone; pos:qword);overload;
function leggi:tpersone;overload;
function leggi(pos:qword):tpersone;overload;
function GetNome:string;
procedure ChiudiFile;
end;
function GetCartella:string;
function FileEsistente(nome:string):boolean;
implementation
function fiolima.ApriFile(fr:tstr; m:char):boolean;
var ch:boolean;
begin
nome:=fr;
modalita:=UpCase(m);
AssignFile(fp, fr);
case modalita of
'W': begin
{$I-}
Rewrite(fp);
{$I+}
ch:=IoResult=0;
end;
'R': begin
{$I-}
Reset(fp);
{$I+}
ch:=IoResult=0;
if ch then seek(fp, 0);
end;
'A': begin
{$I-}
reset(fp);
{$I+}
if IoResult=0 then
begin
seek(fp, FileSize(fp));
ch:=true;
end
else
begin
{$I-}
Rewrite(fp);
{$I+}
ch:=IoResult=0;
end;
end;
else ch:=false;
end;
ApriFile:=ch;
end;
function fiolima.NumeroDiCampi:qword;
begin
if (modalita='W') or (modalita='R') or (modalita='A') then NumeroDiCampi:=FileSize(fp)
else NumeroDiCampi:=0;
end;
function fiolima.FineFile:boolean;
var ch:boolean;
begin
if (modalita='W') or (modalita='R') or (modalita='A') then ch:=EOF(fp)
else ch:=false;
FineFile:=ch;
end;
procedure fiolima.scrivi(rec:tpersone); // overload;
begin
if (modalita='W') or (modalita='R') or (modalita='A') then write(fp, rec);
end;
procedure fiolima.scrivi(rec:tpersone; pos:qword); // overload;
begin
if (modalita='W') or (modalita='R') or (modalita='A') then
begin
seek(fp, pos);
write(fp, rec);
end;
end;
function fiolima.leggi:tpersone; // overload;
var rec:tpersone;
begin
if ((modalita='W') or (modalita='R') or (modalita='A')) and (not eof(fp)) then
read(fp, rec);
leggi:=rec;
end;
function fiolima.leggi(pos:qword):tpersone; // overload;
var rec:tpersone;
begin
if (modalita='W') or (modalita='R') or (modalita='A') then
begin
seek(fp, pos);
read(fp, rec);
end;
leggi:=rec;
end;
function fiolima.GetNome:string;
begin
GetNome:=nome;
end;
procedure fiolima.ChiudiFile;
begin
if (modalita='W') or (modalita='R') or (modalita='A') then
begin
// Flush(fp);
seek(fp, 0);
CloseFile(fp);
end;
modalita:='C';
end;
function GetCartella:string;
var cartella:string;
begin
GetDir(0, cartella);
GetCartella:=cartella;
end;
function FileEsistente(nome:string):boolean;
var f:text;
ch:boolean;
begin
assign(f, nome);
{$I-}
reset(f);
{$I+}
ch:=IoResult=0;
if ch then close(f);
FileEsistente:=ch;
end;
begin
end.