⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringtables.pas

📁 类似文明的游戏源代码。
💻 PAS
字号:
{$INCLUDE switches}

unit StringTables;

interface

const
MaxCount=4000;

type
TCharList=array[0..65535] of char;

TStringTable=class
  constructor Create;
  destructor Destroy; override;
  function LoadFromFile(const FileName: string): boolean;
  function GetHandle(const Item: string): integer;
  function LookupByHandle(Handle: integer; Index: integer =-1): string;
  function Lookup(const Item: string; Index: integer =-1): string;
protected
  Count: integer;
  Data: ^TCharList;
  Lines: array[0..MaxCount-1] of PChar;
  end;


implementation

uses SysUtils;


constructor TStringTable.Create;
begin
Data:=nil;
end;

destructor TStringTable.Destroy;
begin
if Data<>nil then FreeMem(Data);
end;

function TStringTable.LoadFromFile(const FileName: string): boolean;
var
nData, i: integer;
f: file;
begin
if Data<>nil then FreeMem(Data);
AssignFile(f,FileName);
try
  Reset(f,1);
except
  result:=false;
  exit
  end;
result:=true;
nData:=FileSize(f);
GetMem(Data,nData+1);
BlockRead(f,Data^,nData);
CloseFile(f);
i:=0;
Count:=0;
while (i<nData) and (Count<MaxCount) do
  begin
  Lines[Count]:=@Data[i];
  while (i<nData) and (Data[i]<>#13) do inc(i);
  Data[i]:=#0;
  inc(i,2);
  inc(Count);
  end;
end;

function TStringTable.GetHandle(const Item: string): integer;
var
i,l: integer;
begin
l:=Length(Item);
i:=Count-1;
while (i>=0) and ((Lines[i][0]<>'#')
  or (StrLComp(Lines[i]+1,@Item[1],l)<>0)
  or (Lines[i][l+1]<>#0) and (Lines[i][l+1]<>' ')) do
  dec(i);
result:=i
end;

function TStringTable.LookupByHandle(Handle: Integer; Index: integer): string;
var
s: string;
begin
if Index<0 then
  if Handle<0 then result:=''
  else
    begin
    if pos(' ',Lines[Handle])=0 then s:=''
    else s:=copy(Lines[Handle],pos(' ',Lines[Handle])+1,MaxInt);
    while (Handle+1<Count) and (Lines[Handle+1][0]<>'#') do
      begin
      inc(Handle);
      if (Lines[Handle][0]<>#0) and (Lines[Handle][0]<>'''') then
        begin
        if (s<>'') and (s[Length(s)]<>'\') then s:=s+' ';
        s:=s+Lines[Handle];
        end
      end;
    result:=s
    end
else if Handle+Index+1>=Count then result:=''
else result:=Lines[Handle+Index+1];
end;

function TStringTable.Lookup(const Item: string; Index: integer): string;
var
Handle: integer;
begin
Handle:=Gethandle(Item);
if Handle>=0 then result:=LookupByHandle(Handle, Index)
else result:='';
if result='' then
  if Index<0 then result:=Format('[%s]',[Item])
  else result:=Format('[%s %d]',[Item,Index])
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -