hedgewars/uLocale.pas
changeset 10009 88929358d2e1
parent 9998 736015b847e3
child 10015 4feced261c68
--- a/hedgewars/uLocale.pas	Fri Jan 17 18:17:47 2014 +0100
+++ b/hedgewars/uLocale.pas	Sat Jan 18 00:10:33 2014 +0400
@@ -26,21 +26,23 @@
 
 procedure LoadLocale(FileName: shortstring);
 function  Format(fmt: shortstring; var arg: shortstring): shortstring;
-function  FormatA(fmt: ansistring; var arg: ansistring): ansistring;
-function  GetEventString(e: TEventId): ansistring;
+function  FormatA(fmt: PChar; arg: ansistring): ansistring;
+function  GetEventString(e: TEventId): PChar;
+procedure initModule;
+procedure freeModule;
 
 {$IFDEF HWLIBRARY}
 procedure LoadLocaleWrapper(str: pchar); cdecl; export;
 {$ENDIF}
 
 implementation
-uses uRandom, uUtils, uVariables, uDebug, uPhysFSLayer;
+uses uRandom, uUtils, uVariables, uDebug, uPhysFSLayer, sysutils;
 
-var trevt: array[TEventId] of array [0..Pred(MAX_EVENT_STRINGS)] of ansistring;
+var trevt: array[TEventId] of array [0..Pred(MAX_EVENT_STRINGS)] of PChar;
     trevt_n: array[TEventId] of integer;
 
 procedure LoadLocale(FileName: shortstring);
-var s: ansistring = '';
+var s, sc: PChar;
     f: pfsFile;
     a, b, c: LongInt;
     first: array[TEventId] of boolean;
@@ -57,47 +59,48 @@
     while not pfsEof(f) do
         begin
         pfsReadLnA(f, s);
-        if Length(s) = 0 then
-            continue;
-        if (s[1] < '0') or (s[1] > '9') then
-            continue;
-        TryDo(Length(s) > 6, 'Load locale: empty string', true);
-        val(s[1]+s[2], a, c);
-        TryDo(c = 0, 'Load locale: numbers should be two-digit: ' + s, true);
-        TryDo(s[3] = ':', 'Load locale: ":" expected', true);
-        val(s[4]+s[5], b, c);
-        TryDo(c = 0, 'Load locale: numbers should be two-digit' + s, true);
-        TryDo(s[6] = '=', 'Load locale: "=" expected', true);
-        Delete(s, 1, 6);
-        case a of
-            0: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
-                trammo[TAmmoStrId(b)]:= s;
-            1: if (b >=0) and (b <= ord(High(TMsgStrId))) then
-                trmsg[TMsgStrId(b)]:= s;
-            2: if (b >=0) and (b <= ord(High(TEventId))) then
-                begin
-                TryDo(trevt_n[TEventId(b)] < MAX_EVENT_STRINGS, 'Too many event strings in ' + IntToStr(a) + ':' + IntToStr(b), false);
-                if first[TEventId(b)] then
+        if (Length(s) > 0) and (s[0] >= '0') and (s[0] <= '9') then
+            begin
+            TryDo(Length(s) > 6, 'Load locale: empty string', true);
+            val(s[0]+s[1], a, c);
+            TryDo(c = 0, 'Load locale: numbers should be two-digit: ' + s, true);
+            TryDo(s[2] = ':', 'Load locale: ":" expected', true);
+            val(s[3]+s[4], b, c);
+            TryDo(c = 0, 'Load locale: numbers should be two-digit' + s, true);
+            TryDo(s[5] = '=', 'Load locale: "=" expected', true);
+            sc:= StrAlloc(length(s) - 5);
+            StrCopy(sc, @s[6]);
+            case a of
+                0: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
+                    trammo[TAmmoStrId(b)]:= sc;
+                1: if (b >=0) and (b <= ord(High(TMsgStrId))) then
+                    trmsg[TMsgStrId(b)]:= sc;
+                2: if (b >=0) and (b <= ord(High(TEventId))) then
                     begin
-                    trevt_n[TEventId(b)]:= 0;
-                    first[TEventId(b)]:= false;
+                    TryDo(trevt_n[TEventId(b)] < MAX_EVENT_STRINGS, 'Too many event strings in ' + IntToStr(a) + ':' + IntToStr(b), false);
+                    if first[TEventId(b)] then
+                        begin
+                        trevt_n[TEventId(b)]:= 0;
+                        first[TEventId(b)]:= false;
+                        end;
+                    trevt[TEventId(b)][trevt_n[TEventId(b)]]:= sc;
+                    inc(trevt_n[TEventId(b)]);
                     end;
-                trevt[TEventId(b)][trevt_n[TEventId(b)]]:= s;
-                inc(trevt_n[TEventId(b)]);
-                end;
-            3: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
-                trammoc[TAmmoStrId(b)]:= s;
-            4: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
-                trammod[TAmmoStrId(b)]:= s;
-            5: if (b >=0) and (b <= ord(High(TGoalStrId))) then
-                trgoal[TGoalStrId(b)]:= s;
-           end;
-       end;
+                3: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
+                    trammoc[TAmmoStrId(b)]:= sc;
+                4: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
+                    trammod[TAmmoStrId(b)]:= sc;
+                5: if (b >=0) and (b <= ord(High(TGoalStrId))) then
+                    trgoal[TGoalStrId(b)]:= sc;
+            end;
+            end;
+        StrDispose(s);
+        end;
    pfsClose(f);
    end;
 end;
 
-function GetEventString(e: TEventId): ansistring;
+function GetEventString(e: TEventId): PChar;
 begin
     if trevt_n[e] = 0 then // no messages for this event type?
         GetEventString:= '*missing translation*'
@@ -115,14 +118,17 @@
     Format:= copy(fmt, 1, i - 1) + arg + Format(copy(fmt, i + 2, Length(fmt) - i - 1), arg)
 end;
 
-function FormatA(fmt: ansistring; var arg: ansistring): ansistring;
+function FormatA(fmt: PChar; arg: ansistring): ansistring;
 var i: LongInt;
+    s: ansistring;
 begin
-i:= Pos('%1', fmt);
+s:= fmt;
+
+i:= Pos('%1', s);
 if i = 0 then
-    FormatA:= fmt
+    FormatA:= s
 else
-    FormatA:= copy(fmt, 1, i - 1) + arg + FormatA(copy(fmt, i + 2, Length(fmt) - i - 1), arg)
+    FormatA:= copy(s, 1, i - 1) + arg + FormatA(PChar(copy(s, i + 2, Length(s) - i - 1)), arg)
 end;
 
 {$IFDEF HWLIBRARY}
@@ -132,4 +138,22 @@
 end;
 {$ENDIF}
 
+procedure initModule;
+var e: TEventId;
+    i: LongInt;
+begin
+    for e:= Low(TEventId) to High(TEventId) do
+        for i:= 0 to Pred(MAX_EVENT_STRINGS) do
+            trevt[e][i]:= nil;
+end;
+
+procedure freeModule;
+var e: TEventId;
+    i: LongInt;
+begin
+    for e:= Low(TEventId) to High(TEventId) do
+        for i:= 0 to Pred(trevt_n[e]) do
+            StrDispose(trevt[e][i]);
+end;
+
 end.