Allow to escape | and : character in engine translation files
authorWuzzy <Wuzzy2@mail.ru>
Fri, 26 Oct 2018 14:59:08 +0200
changeset 14001 3183c4dc6e53
parent 14000 6938cab93016
child 14002 8e479dabdd88
Allow to escape | and : character in engine translation files
hedgewars/uStore.pas
hedgewars/uUtils.pas
--- a/hedgewars/uStore.pas	Fri Oct 26 06:15:57 2018 +0200
+++ b/hedgewars/uStore.pas	Fri Oct 26 14:59:08 2018 +0200
@@ -976,7 +976,9 @@
 while length(tmpdesc) > 0 do
     begin
     tmpline:= tmpdesc;
+    EscapeCharA(tmpline, '|');
     SplitByCharA(tmpline, tmpdesc, '|');
+    UnEscapeCharA(tmpline, '|');
     if length(tmpline) > 0 then
         begin
         TTF_SizeUTF8(Fontz[font].Handle, PChar(tmpline), @i, @j);
@@ -1019,7 +1021,9 @@
 while length(tmpdesc) > 0 do
     begin
     tmpline:= tmpdesc;
+    EscapeCharA(tmpline, '|');
     SplitByCharA(tmpline, tmpdesc, '|');
+    UnEscapeCharA(tmpline, '|');
     r2:= r;
     if length(tmpline) > 0 then
         begin
@@ -1027,6 +1031,7 @@
         // Render highlighted caption if there is a ':',
         // from the beginning of the line to (and including) the ':'.
         // With '::', the colons will be suppressed in the final text.
+        EscapeCharA(tmpline, ':');
         tmpline2:= _S'';
         SplitByCharA(tmpline, tmpline2, ':');
         if length(tmpline2) > 0 then
@@ -1038,11 +1043,15 @@
                 end
             else
                 tmpline3:= tmpline + Copy(tmpline2, 2, Length(tmpline2)-1);
+            UnEscapeCharA(tmpline3, ':');
             r:= WriteInRect(tmpsurf, cFontBorder + 2, r.y + r.h, $ff707070, font, PChar(tmpline3));
             WriteInRect(tmpsurf, cFontBorder + 2, r2.y + r2.h, $ffc7c7c7, font, PChar(tmpline));
             end
         else
+            begin
+            UnEscapeCharA(tmpline, ':');
             r:= WriteInRect(tmpsurf, cFontBorder + 2, r.y + r.h, $ff707070, font, PChar(tmpline));
+            end
         end;
     end;
 
--- a/hedgewars/uUtils.pas	Fri Oct 26 06:15:57 2018 +0200
+++ b/hedgewars/uUtils.pas	Fri Oct 26 14:59:08 2018 +0200
@@ -30,6 +30,9 @@
 procedure SplitByChar(var a, b: shortstring; c: char);
 procedure SplitByCharA(var a, b: ansistring; c: char);
 
+procedure EscapeCharA(var a: ansistring; e: char);
+procedure UnEscapeCharA(var a: ansistring; e: char);
+
 function ExtractFileDir(s: shortstring) : shortstring;
 function ExtractFileName(s: shortstring) : shortstring;
 
@@ -251,6 +254,40 @@
     end else b:= '';
 end; { SplitByCharA }
 
+// In the ansistring a, escapes all instances of
+// '\e' with an ASCII ESC character, where e is
+// a char chosen by you.
+procedure EscapeCharA(var a: ansistring; e: char);
+var i: LongInt;
+begin
+repeat
+    i:= Pos(e, a);
+    if (i > 1) and (a[i - 1] = '\') then
+        begin
+        a[i]:= ^[; // ASCII ESC
+        Delete(a, i - 1, 1);
+        end
+    else
+        break;
+until (i <= 0);
+end; { EscapeCharA }
+
+// Unescapes a previously escaped string and inserts
+// e back into the string. e is a char chosen by you.
+procedure UnEscapeCharA(var a: ansistring; e: char);
+var i: LongInt;
+begin
+repeat
+    i:= Pos(^[, a); // ASCII ESC
+    if (i > 0) then
+        begin
+        a[i]:= e;
+        end
+    else
+        break;
+until (i <= 0);
+end; { UnEscapeCharA }
+
 function EnumToStr(const en : TGearType) : shortstring; overload;
 begin
 EnumToStr:= GetEnumName(TypeInfo(TGearType), ord(en))