Convert some ansistrings to pchars
authorunc0rr
Sat, 18 Jan 2014 00:10:33 +0400
changeset 10009 88929358d2e1
parent 10008 b8b91c7e0da8
child 10010 2a1483d91977
Convert some ansistrings to pchars
hedgewars/hwengine.pas
hedgewars/uLocale.pas
hedgewars/uPhysFSLayer.pas
hedgewars/uScript.pas
hedgewars/uStore.pas
hedgewars/uVariables.pas
hedgewars/uWorld.pas
--- a/hedgewars/hwengine.pas	Fri Jan 17 18:17:47 2014 +0100
+++ b/hedgewars/hwengine.pas	Sat Jan 18 00:10:33 2014 +0400
@@ -448,6 +448,7 @@
 
     if complete then
     begin
+        uLocale.initModule;
         uPhysFSLayer.initModule;
         uTextures.initModule;
 {$IFDEF ANDROID}GLUnit.initModule;{$ENDIF}
@@ -504,6 +505,7 @@
 {$IFDEF ANDROID}GLUnit.freeModule;{$ENDIF}
         uTextures.freeModule;
         uPhysFSLayer.freeModule;
+        uLocale.freeModule;
     end;
 
     uIO.freeModule;
--- 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.
--- a/hedgewars/uPhysFSLayer.pas	Fri Jan 17 18:17:47 2014 +0100
+++ b/hedgewars/uPhysFSLayer.pas	Sat Jan 18 00:10:33 2014 +0400
@@ -25,7 +25,7 @@
 function pfsClose(f: PFSFile): boolean;
 
 procedure pfsReadLn(f: PFSFile; var s: shortstring);
-procedure pfsReadLnA(f: PFSFile; var s: ansistring);
+procedure pfsReadLnA(f: PFSFile; var s: PChar);
 function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64;
 function pfsEOF(f: PFSFile): boolean;
 
@@ -97,26 +97,34 @@
         end
 end;
 
-procedure pfsReadLnA(f: PFSFile; var s: ansistring);
-var c: char;
-    b: shortstring;
+procedure pfsReadLnA(f: PFSFile; var s: PChar);
+var l, bufsize: Longword;
+    r: Int64;
+    b: PChar;
 begin
-s:= '';
-b[0]:= #0;
+bufsize:= 256;
+s:= StrAlloc(bufsize);
+l:= 0;
 
-while (PHYSFS_readBytes(f, @c, 1) = 1) and (c <> #10) do
-    if (c <> #13) then
+repeat
+    r:= PHYSFS_readBytes(f, @s[l], 1);
+
+    if (r = 1) and (s[l] <> #13) then
         begin
-        inc(b[0]);
-        b[byte(b[0])]:= c;
-        if b[0] = #255 then
+        inc(l);
+        if l = bufsize then
             begin
-            s:= s + b;
-            b[0]:= #0
+            b:= s;
+            inc(bufsize, 256);
+            s:= StrAlloc(bufsize);
+            StrCopy(s, b);
+            StrDispose(b)
             end
         end;
-        
-s:= s + b
+
+until (r = 0) or (s[l - 1] = #10);
+
+if (r = 0) then s[l]:= #0 else s[l - 1]:= #0
 end;
 
 function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64;
--- a/hedgewars/uScript.pas	Fri Jan 17 18:17:47 2014 +0100
+++ b/hedgewars/uScript.pas	Sat Jan 18 00:10:33 2014 +0400
@@ -224,7 +224,7 @@
 begin
     if lua_gettop(L) = 5 then
         begin
-        ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
+        ShowMission(lua_tolstring(L, 1, nil), lua_tolstring(L, 2, nil), lua_tolstring(L, 3, nil), lua_tointeger(L, 4), lua_tointeger(L, 5));
         end
     else
         LuaParameterCountError('ShowMission', 'caption, subcaption, text, icon, time', lua_gettop(L));
@@ -268,10 +268,10 @@
 function lc_addcaption(L : Plua_State) : LongInt; Cdecl;
 begin
     if lua_gettop(L) = 1 then
-        AddCaption(lua_tostring(L, 1), cWhiteColor, capgrpMessage)
+        AddCaption(lua_tolstring(L, 1, nil), cWhiteColor, capgrpMessage)
     else if lua_gettop(L) = 3 then
         begin
-        AddCaption(lua_tostring(L, 1), lua_tointeger(L, 2) shr 8, TCapGroup(lua_tointeger(L, 3)));
+        AddCaption(lua_tolstring(L, 1, nil), lua_tointeger(L, 2) shr 8, TCapGroup(lua_tointeger(L, 3)));
         end
     else
         LuaParameterCountError('AddCaption', 'text[, color, captiongroup]', lua_gettop(L));
--- a/hedgewars/uStore.pas	Fri Jan 17 18:17:47 2014 +0100
+++ b/hedgewars/uStore.pas	Sat Jan 18 00:10:33 2014 +0400
@@ -44,7 +44,7 @@
 procedure LoadHedgehogHat(var HH: THedgehog; newHat: shortstring);
 procedure SetupOpenGL;
 procedure SetScale(f: GLfloat);
-function  RenderHelpWindow(caption, subcaption, description, extra: ansistring; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture;
+function  RenderHelpWindow(caption, subcaption, description, extra: PChar; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture;
 procedure RenderWeaponTooltip(atype: TAmmoType);
 procedure ShowWeaponTooltip(x, y: LongInt);
 procedure FreeWeaponTooltip;
@@ -905,7 +905,7 @@
     Step:= 0
 end;
 
-function RenderHelpWindow(caption, subcaption, description, extra: ansistring; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture;
+function RenderHelpWindow(caption, subcaption, description, extra: PChar; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture;
 var tmpsurf: PSDL_SURFACE;
     w, h, i, j: LongInt;
     font: THWFont;
@@ -1025,7 +1025,7 @@
 procedure RenderWeaponTooltip(atype: TAmmoType);
 var r: TSDL_Rect;
     i: LongInt;
-    extra: ansistring;
+    extra: PChar;
     extracolor: LongInt;
 begin
 // don't do anything if the window shouldn't be shown
--- a/hedgewars/uVariables.pas	Fri Jan 17 18:17:47 2014 +0100
+++ b/hedgewars/uVariables.pas	Sat Jan 18 00:10:33 2014 +0400
@@ -2367,18 +2367,18 @@
 
     cTestLua : Boolean;
 
-var trammo:  array[TAmmoStrId] of ansistring;   // name of the weapon
-    trammoc: array[TAmmoStrId] of ansistring;   // caption of the weapon
-    trammod: array[TAmmoStrId] of ansistring;   // description of the weapon
-    trmsg:   array[TMsgStrId]  of ansistring;   // message of the event
-    trgoal:  array[TGoalStrId] of ansistring;   // message of the goal
+var trammo:  array[TAmmoStrId] of PChar;   // name of the weapon
+    trammoc: array[TAmmoStrId] of PChar;   // caption of the weapon
+    trammod: array[TAmmoStrId] of PChar;   // description of the weapon
+    trmsg:   array[TMsgStrId]  of PChar;   // message of the event
+    trgoal:  array[TGoalStrId] of PChar;   // message of the goal
 
 procedure preInitModule;
 procedure initModule;
 procedure freeModule;
 
 implementation
-uses strutils;
+uses strutils, sysutils;
 
 procedure preInitModule;
 begin
@@ -2423,7 +2423,21 @@
 end;
 
 procedure initModule;
+var asid: TAmmoStrId;
+    msid: TMsgStrId;
+    gsid: TGoalStrId;
 begin
+    for asid:= Low(TAmmoStrId) to High(TAmmoStrId) do
+        begin
+        trammo[asid]:= nil;
+        trammoc[asid]:= nil;
+        trammod[asid]:= nil;
+        end;
+    for msid:= Low(TMsgStrId) to High(TMsgStrId) do
+        trmsg[msid]:= nil;
+    for gsid:= Low(TGoalStrId) to High(TGoalStrId) do
+        trgoal[gsid]:= nil;
+        
     // TODO: we could just have one cLocale variables and drop strutils
     cLocale:= ExtractDelimited(1, cLocaleFName, StdWordDelims);
 
@@ -2606,7 +2620,20 @@
 end;
 
 procedure freeModule;
+var asid: TAmmoStrId;
+    msid: TMsgStrId;
+    gsid: TGoalStrId;
 begin
+    for asid:= Low(TAmmoStrId) to High(TAmmoStrId) do
+        begin
+        if trammo[asid] <> nil then StrDispose(trammo[asid]);
+        if trammoc[asid] <> nil then StrDispose(trammoc[asid]);
+        if trammod[asid] <> nil then StrDispose(trammod[asid]);
+        end;
+    for msid:= Low(TMsgStrId) to High(TMsgStrId) do
+        if trmsg[msid] <> nil then StrDispose(trmsg[msid]);
+    for gsid:= Low(TGoalStrId) to High(TGoalStrId) do
+        if trgoal[gsid] <> nil then StrDispose(trgoal[gsid]);
 end;
 
 end.
--- a/hedgewars/uWorld.pas	Fri Jan 17 18:17:47 2014 +0100
+++ b/hedgewars/uWorld.pas	Sat Jan 18 00:10:33 2014 +0400
@@ -31,7 +31,7 @@
 
 procedure DrawWorld(Lag: LongInt);
 procedure DrawWorldStereo(Lag: LongInt; RM: TRenderMode);
-procedure ShowMission(caption, subcaption, text: ansistring; icon, time : LongInt);
+procedure ShowMission(caption, subcaption, mtext: PChar; icon, time : LongInt);
 procedure HideMission;
 procedure ShakeCamera(amount: LongInt);
 procedure InitCameraBorders;
@@ -213,7 +213,7 @@
 
 // if the string has been set, show it for (default timeframe) seconds
 if g <> '' then
-    ShowMission(trgoal[gidCaption], trgoal[gidSubCaption], g, 1, 0);
+    ShowMission(trgoal[gidCaption], trgoal[gidSubCaption], PChar(g), 1, 0);
 
 cWaveWidth:= SpritesData[sprWater].Width;
 //cWaveHeight:= SpritesData[sprWater].Height;
@@ -1897,7 +1897,7 @@
     WorldDx:= 1024;
 end;
 
-procedure ShowMission(caption, subcaption, text: ansistring; icon, time : LongInt);
+procedure ShowMission(caption, subcaption, mtext: PChar; icon, time : LongInt);
 var r: TSDL_Rect;
 begin
 r.w:= 32;
@@ -1912,13 +1912,13 @@
     begin
     r.x:= 0;
     r.y:= icon * 32;
-    missionTex:= RenderHelpWindow(caption, subcaption, text, '', 0, MissionIcons, @r)
+    missionTex:= RenderHelpWindow(caption, subcaption, mtext, '', 0, MissionIcons, @r)
     end
 else
     begin
     r.x:= ((-icon - 1) shr 4) * 32;
     r.y:= ((-icon - 1) mod 16) * 32;
-    missionTex:= RenderHelpWindow(caption, subcaption, text, '', 0, SpritesData[sprAMAmmos].Surface, @r)
+    missionTex:= RenderHelpWindow(caption, subcaption, mtext, '', 0, SpritesData[sprAMAmmos].Surface, @r)
     end;
 end;