Rewrite StrToInt code in uStore; get rid of "try" and "except" keywords
authorWuzzy <Wuzzy2@mail.ru>
Tue, 02 Oct 2018 21:51:27 +0200
changeset 13826 94d0d1ab7a0e
parent 13825 cf7626f46bb2
child 13827 b56639b52e59
Rewrite StrToInt code in uStore; get rid of "try" and "except" keywords
hedgewars/uStore.pas
hedgewars/uUtils.pas
--- a/hedgewars/uStore.pas	Tue Oct 02 21:26:25 2018 +0200
+++ b/hedgewars/uStore.pas	Tue Oct 02 21:51:27 2018 +0200
@@ -772,8 +772,9 @@
 var i: LongInt;
     f: PFSFile;
     key, value, l, temp: shortstring;
-    color: Longword;
-    c: byte;
+    color, tempColor: Longword;
+    clanID, tempClanID: byte;
+    conversionSuccess: boolean;
 begin
     if cOnlyStats then exit;
 
@@ -786,6 +787,7 @@
         while (not pfsEOF(f)) and (l <> '[colors]') do
             pfsReadLn(f, l);
 
+        conversionSuccess:= false;
         while (not pfsEOF(f)) and (l <> '') do
             begin
             pfsReadLn(f, l);
@@ -801,11 +803,11 @@
             if temp = 'color' then
                 begin
                 temp:= copy(key, 6, length(key) - 5);
-                try
-                    c:= StrToInt(temp);
-                except
-                    on E : EConvertError do continue;
-                end;
+                tempClanID:= StrToInt(temp, conversionSuccess);
+                if conversionSuccess then
+                    clanID:= tempClanID
+                else
+                    continue;
                 end
             else
                 continue;
@@ -820,15 +822,15 @@
                 if value[1] <> '#' then
                     continue;
                 temp:= copy(value, 2, length(value) - 1);
-                try
-                    color:= StrToInt('0x'+temp);
-                except
-                    on E : EConvertError do continue;
-                end;
+                tempColor:= StrToInt('0x'+temp, conversionSuccess);
+                if conversionSuccess then
+                    color:= tempColor
+                else
+                    continue;
                 end;
 
-            if c <= cClanColors then
-                ClanColorArray[c]:= color;
+            if clanID <= cClanColors then
+                ClanColorArray[clanID]:= color;
 
             end;
 
--- a/hedgewars/uUtils.pas	Tue Oct 02 21:26:25 2018 +0200
+++ b/hedgewars/uUtils.pas	Tue Oct 02 21:51:27 2018 +0200
@@ -50,6 +50,7 @@
 
 function  IntToStr(n: LongInt): shortstring;
 function  StrToInt(s: shortstring): LongInt;
+function  StrToInt(s: shortstring; var success: boolean): LongInt;
 function  FloatToStr(n: hwFloat): shortstring;
 
 function  DxDy2Angle(const _dY, _dX: hwFloat): real; inline;
@@ -331,9 +332,22 @@
 str(n, IntToStr)
 end;
 
-function StrToInt(s: shortstring): LongInt;
+// Convert string to longint, with error checking.
+// Success will be set to false when conversion failed.
+// See documentation on Val procedure for syntax of s
+function StrToInt(s: shortstring; var success: boolean): LongInt;
+var Code: Word;
 begin
-val(s, StrToInt);
+val(s, StrToInt, Code);
+success:= Code = 0;
+end;
+
+// Convert string to longint, without error checking
+function StrToInt(s: shortstring): LongInt;
+var success: boolean; // ignored
+begin
+success:= true; // avoid compiler hint
+StrToInt:= StrToInt(s, success);
 end;
 
 function FloatToStr(n: hwFloat): shortstring;