Changeset 63 for trunk


Ignore:
Timestamp:
Jan 13, 2017, 10:50:31 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Loading of StringTables with localization texts redone to simple load lines from text files encoded in UTF-8.
  • Fixed: Correctly load localized files.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/ScreenTools.pas

    r62 r63  
    15581558  if FileExists(DataDir + 'Localization' + DirectorySeparator + 'Language.txt') then
    15591559  begin
    1560     Phrases.loadfromfile(DataDir + 'Localization' + DirectorySeparator + ' + Language.txt');
     1560    Phrases.loadfromfile(DataDir + 'Localization' + DirectorySeparator + 'Language.txt');
    15611561    if FileExists(DataDir + 'Localization' + DirectorySeparator + 'Language2.txt') then
    15621562      Phrases2.loadfromfile(DataDir + 'Localization' + DirectorySeparator + 'Language2.txt')
  • trunk/StringTables.pas

    r38 r63  
    44interface
    55
    6 const
    7   MaxCount = 4000;
     6uses
     7  Classes;
    88
    99type
    10   TCharList = array [0 .. 9999999] of AnsiChar;
     10  { TStringTable }
    1111
    1212  TStringTable = class
     13  private
     14    Lines: TStringList;
     15    function IsLabel(Text, Name: string): Boolean;
     16  public
    1317    constructor Create;
    1418    destructor Destroy; override;
     
    1822    function Lookup(const Item: string; Index: integer = -1): string;
    1923    function Search(const Content: string; var Handle, Index: integer): boolean;
    20   protected
    21     Count: integer;
    22     Data: ^TCharList;
    23     Lines: array [0 .. MaxCount - 1] of PAnsiChar;
    2424  end;
    2525
     
    2727
    2828uses
    29   Classes, SysUtils;
     29  SysUtils;
     30
     31function TStringTable.IsLabel(Text, Name: string): Boolean;
     32begin
     33  Result := ((Copy(Text, 1, 1) = '#') and (Copy(Text, 2, Length(Name)) = Name))
     34    and ((Length(Text) = (Length(Name) + 1)) or (Copy(Text, Length(Name) + 2, 1) = ' '));
     35end;
    3036
    3137constructor TStringTable.Create;
    3238begin
    33   Data := nil;
     39  Lines := TStringList.Create;
    3440end;
    3541
    3642destructor TStringTable.Destroy;
    3743begin
    38   if Data <> nil then
    39     FreeMem(Data);
     44  FreeAndNil(Lines);
    4045end;
    4146
    42 function TStringTable.LoadFromFile(const FileName: string): boolean;
    43 var
    44   nData, i: integer;
    45   f: TFileStream;
     47function TStringTable.LoadFromFile(const FileName: String): boolean;
    4648begin
    47   if Data <> nil then
    48     FreeMem(Data);
    49   try
    50     f := TFileStream.Create(FileName, fmOpenRead or fmShareExclusive);
    51   except
    52     result := false;
    53     exit;
    54   end;
    55   result := true;
    56   nData := f.Size;
    57   GetMem(Data, nData + 1);
    58   f.read(Data^, nData);
    59   f.Free;
    60   i := 0;
    61   Count := 0;
    62   while (i < nData) and (Count < MaxCount) do
    63   begin
    64     Lines[Count] := @Data[i];
    65     while (i < nData) and (Data[i] <> #13) do
    66       inc(i);
    67     Data[i] := #0;
    68     inc(i, 2);
    69     inc(Count);
    70   end;
     49  Lines.LoadFromFile(FileName);
    7150end;
    7251
    7352function TStringTable.GetHandle(const Item: AnsiString): integer;
    7453var
    75   i, l: integer;
     54  I: Integer;
    7655begin
    77   l := Length(Item);
    78   i := Count - 1;
    79   while (i >= 0) and ((Lines[i][0] <> '#') or (StrLComp(Lines[i] + 1, @Item[1],
    80     l) <> 0) or (Lines[i][l + 1] <> #0) and (Lines[i][l + 1] <> ' ')) do
    81     dec(i);
    82   result := i
     56  I := 0;
     57  while (I < Lines.Count) and not IsLabel(Lines[I], Item) do
     58    Inc(I);
     59  if I < Lines.Count then Result := I
     60    else Result := -1;
    8361end;
    8462
     
    8866begin
    8967  if Index < 0 then
    90     if Handle < 0 then
    91     begin
    92       result := '';
    93       exit
    94     end
    95     else
    96     begin
    97       if pos(' ', Lines[Handle]) = 0 then
    98         s := ''
    99       else
    100         s := copy(Lines[Handle], pos(' ', Lines[Handle]) + 1, MaxInt);
    101       while (Handle + 1 < Count) and (Lines[Handle + 1][0] <> '#') do
    102       begin
    103         inc(Handle);
    104         if (Lines[Handle][0] <> #0) and (Lines[Handle][0] <> '''') then
    105         begin
     68    if Handle < 0 then begin
     69      Result := '';
     70      Exit;
     71    end else begin
     72      if Pos(' ', Lines[Handle]) = 0 then S := ''
     73        else s := Copy(Lines[Handle], Pos(' ', Lines[Handle]) + 1, MaxInt);
     74      while ((Handle + 1) < Lines.Count) and (Copy(Lines[Handle + 1], 1, 1) <> '#') do begin
     75        Inc(Handle);
     76        if (Length(Lines[Handle]) > 0) and (Lines[Handle][1] <> '''') then begin
    10677          if (s <> '') and (s[Length(s)] <> '\') then
    10778            s := s + ' ';
    10879          s := s + Lines[Handle];
    109         end
     80        end;
    11081      end;
    111       result := s
    112     end
    113   else if Handle + Index + 1 >= Count then
    114   begin
    115     result := '';
    116     exit
    117   end
    118   else
    119     result := Lines[Handle + Index + 1];
    120   while (result <> '') and ((result[1] = ' ') or (result[1] = #9)) do
    121     Delete(result, 1, 1);
    122   while (result <> '') and ((result[Length(result)] = ' ') or
    123     (result[Length(result)] = #9)) do
    124     Delete(result, Length(result), 1);
    125   if result = '' then
    126     result := '*';
     82      Result := S;
     83    end else
     84    if (Handle + Index + 1) >= Lines.Count then begin
     85      Result := '';
     86      Exit;
     87    end else Result := Lines[Handle + Index + 1];
     88  while (Result <> '') and ((Result[1] = ' ') or (Result[1] = #9)) do
     89    Delete(Result, 1, 1);
     90  while (Result <> '') and ((Result[Length(Result)] = ' ') or
     91    (Result[Length(Result)] = #9)) do
     92    Delete(Result, Length(Result), 1);
     93  if Result = '' then Result := '*';
    12794end;
    12895
     
    178145    i := Index + 1;
    179146  repeat
    180     if h + i + 1 >= Count then
     147    if h + i + 1 >= Lines.Count then
    181148    begin
    182149      result := false;
    183150      exit
    184151    end;
    185     if Lines[h + i + 1][0] = '#' then
     152    if Copy(Lines[h + i + 1], 1, 1) = '#' then
    186153    begin
    187154      h := h + i + 1;
    188155      i := -1
    189156    end;
    190     if (h >= 0) and not(Lines[h + i + 1][0] in ['#', ':', ';']) and
    191       (pos(UContent, UpperCase(Lines[h + i + 1])) > 0) then
     157    if (h >= 0) and not ((Length(Lines[h + i + 1]) > 0) and (Lines[h + i + 1][1] in ['#', ':', ';'])) and
     158      (Pos(UContent, UpperCase(Lines[h + i + 1])) > 0) then
    192159    begin
    193160      Index := i;
    194161      Handle := h;
    195       result := true;
    196       exit
     162      Result := True;
     163      Exit;
    197164    end;
    198     inc(i);
    199   until false;
     165    Inc(I);
     166  until False;
    200167end;
    201168
Note: See TracChangeset for help on using the changeset viewer.