Просмотр исходного кода

+ Changed resource string mechanism to use names.

michael 26 лет назад
Родитель
Сommit
77f10694a1
1 измененных файлов с 58 добавлено и 14 удалено
  1. 58 14
      rtl/objpas/objpas.pp

+ 58 - 14
rtl/objpas/objpas.pp

@@ -57,9 +57,9 @@ unit objpas;
 
 {$ifdef HasResourceStrings}
      { Resourcestring support }
-     Function GetResourceString(Hash : Longint) : AnsiString;
+     Function GetResourceString(Const Name : ShortString) : AnsiString;
      Procedure ResetResourceTables;
-     Function SetResourceString(Hash : longint; Const Value : AnsiString) : Boolean;
+     Function SetResourceString(Const Name : Shortstring; Const Value : AnsiString) : Boolean;
 {$endif}
 
 
@@ -221,10 +221,12 @@ Var OldMM,NEWMM : TmemoryManager;
   ---------------------------------------------------------------------}
 Type
 
+  PResourceStringRecord = ^TResourceStringRecord;
   TResourceStringRecord = Packed Record
      DefaultValue,
      CurrentValue : AnsiString;
      HashValue : longint;
+     Name : ShortString;
    end;
 
    TResourceStringTable = Packed Record
@@ -235,38 +237,77 @@ Type
 Var
   ResourceStringTable : TResourceStringTable; External Name 'RESOURCESTRINGLIST';
 
-Function FindHashIndex (Value : Longint) : Longint;
+function CalcStringHashValue(Const N : ShortString) : longint;
+
+Var hash,g,I : longint;
+
+begin
+   hash:=0;
+   For I:=1 to Length(N) do { 0 terminated }
+     begin
+     hash:=hash shl 4;
+     inc(Hash,Ord(N[i]));
+     g:=hash and ($f shl 28);
+     if g<>0 then
+       begin
+       hash:=hash xor (g shr 24);
+       hash:=hash xor g;
+       end;
+     end;
+   If Hash=0 then
+     CalcStringHashValue:=Not(0)
+   else
+     CalcStringHashValue:=Hash;
+end;
+
+Function FindIndex (Const Value : Shortstring) : Longint;
 
 Var
-  I : longint;
+  I,Hash : longint;
+  
 begin
   // Linear search, later we can implement binary search.
+  Hash:=CalcStringHashValue(Value);
+  Result:=-1;
   With ResourceStringTable do
     For I:=0 to Count-1 do
-      If Value=Resrec[I].HashValue then
+      If Hash=Resrec[I].HashValue then
         begin
         Result:=I;
-        exit;
+        Break;
         end;
-  Result:=-1;
+  If Result<>-1 then
+    begin
+    With ResourceStringTable do
+      While (Result<=Count) do
+        If Value=ResRec[Result].Name then 
+          exit
+        else 
+          Inc(Result);
+    Result:=-1;
+    end;       
 end;
 
 
-Function GetResourceString(Hash : Longint) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
+Function GetResourceString(Const Name : ShortString) : AnsiString;[Public,Alias : 'FPC_GETRESOURCESTRING'];
+
+Var I : longint;
 
 begin
-  Hash:=FindHashIndex(Hash);
-  If Hash<>-1 then
-     Result:=ResourceStringTable.ResRec[Hash].CurrentValue
+  I:=FindIndex(Name);
+  If I<>-1 then
+     Result:=ResourceStringTable.ResRec[I].CurrentValue
   else
      Result:='';
 end;
 
 
-Function SetResourceString(Hash : longint; Const Value : AnsiString) : Boolean;
+Function SetResourceString(Const Name : ShortString; Const Value : AnsiString) : Boolean;
+
+Var Hash : Longint;
 
 begin
-  Hash:=FindHashIndex(Hash);
+  Hash:=FindIndex(Name);
   Result:=Hash<>-1;
   If Result then
     ResourceStringTable.ResRec[Hash].CurrentValue:=Value;
@@ -297,7 +338,10 @@ end.
 
 {
   $Log$
-  Revision 1.30  1999-08-15 18:56:13  michael
+  Revision 1.31  1999-08-15 21:02:56  michael
+  + Changed resource string mechanism to use names.
+
+  Revision 1.30  1999/08/15 18:56:13  michael
   + Delphi-style getmem and freemem
 
   Revision 1.29  1999/07/23 23:13:54  peter