unit rfile;
interface
uses classes, rvars;
var
gamefile: text;

function OpenCFG: boolean;
procedure CloseCFG;
function ReadSession(filename: string; session: string): boolean;
function ReadValue(name: string): string;
procedure ClearSession;
implementation
uses sysUtils;

var
cfg: text;

function OpenCFG: boolean;
begin
OpenCFG:=false;
if not fileExists('OpenRPG.cfg') then exit;
try
assign(cfg,'OpenRPG.cfg');
reset(cfg);
if ioresult<>0 then exit;
{$I+}
OpenCFG:=true;
Except
exit;
end;
end;

procedure CloseCFG;
begin
try
Close(cfg);
Except
end;
end;

function  ReadSession(filename: string; session: string): boolean;
var l: string;
s: string;
f: text;
begin
ReadSession:=false;
try
assign(f,filename);
reset(f);
while not eof(f) do
begin
readln(f,l);
if l='['+session+']' then
begin
s:=l;
readln(f,l);
value.Add(l);
while (l<>'') or (not eof(f)) do
begin
readln(f,l);
if l='' then break;
value.Add(l);
end;
end;
end;
Except
exit;
end;
if s='['+session+']' then
ReadSession:=true
else
begin
ClearSession;
ReadSession:=false;
end;
close(f);
end;

function ReadValue(name: string): string;
var i: integer;
s: string;
begin
readValue:='';
for i:=0 to value.Count-1 do
begin
if AnsiUpperCase(name)=AnsiUpperCase(copy(value[i],1,pos('=',value[i])-1)) then
begin
s:=value[i];
delete(s,1,pos('=',value[i]));
ReadValue:=s;
end;
end;
end;

procedure ClearSession;
begin
value:=TStringList.Create;
end;

end.
