ISPP.VarUtils.pas 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. {
  2. Inno Setup Preprocessor
  3. Copyright (C) 2001-2002 Alex Yackimoff
  4. Inno Setup
  5. Copyright (C) 1997-2025 Jordan Russell
  6. Portions by Martijn Laan
  7. For conditions of distribution and use, see LICENSE.TXT.
  8. }
  9. unit ISPP.VarUtils;
  10. interface
  11. uses
  12. ISPP.Intf, ISPP.Base, ISPP.Preprocessor;
  13. function SimplifyLValue(var Src: TIsppVariant): Boolean;
  14. procedure MakeRValue(var Src: TIsppVariant);
  15. function GetRValue(const Src: TIsppVariant): TIsppVariant;
  16. procedure CopyExpVar(Src: TIsppVariant; var Dest: TIsppVariant);
  17. procedure MakeInt(var Op: TIsppVariant; Value: Int64);
  18. procedure MakeStr(var Op: TIsppVariant; const Value: string);
  19. procedure MakeBool(var Op: TIsppVariant; Value: Boolean);
  20. function TryStrToInt(Str: string; var Int: Integer): Boolean;
  21. function ToInt(Op: TIsppVariant): TIsppVariant;
  22. function ToStr(Op: TIsppVariant): TIsppVariant;
  23. const
  24. NULL: TIsppVariant = (Typ: evNull; AsStr: ''; AsInt64: 0);
  25. implementation
  26. uses
  27. SysUtils, ISPP.Consts;
  28. function SimplifyLValue(var Src: TIsppVariant): Boolean;
  29. begin
  30. Result := Src.Typ = evLValue;
  31. if Result then
  32. while Src.AsPtr^.Typ = evLValue do Src := Src.AsPtr^;
  33. end;
  34. procedure MakeRValue(var Src: TIsppVariant);
  35. begin
  36. while Src.Typ = evLValue do Src := Src.AsPtr^;
  37. end;
  38. function GetRValue(const Src: TIsppVariant): TIsppVariant;
  39. begin
  40. Result := Src;
  41. MakeRValue(Result);
  42. end;
  43. procedure CopyExpVar(Src: TIsppVariant; var Dest: TIsppVariant);
  44. begin
  45. MakeRValue(Src);
  46. if Src.Typ = evStr then
  47. begin
  48. Dest.Typ := evStr;
  49. Dest.AsInt64 := 0;
  50. Dest.AsStr := Src.AsStr;
  51. end
  52. else
  53. Move(Src, Dest, SizeOf(TIsppVariant));
  54. end;
  55. procedure MakeInt(var Op: TIsppVariant; Value: Int64);
  56. begin
  57. Op.Typ := evInt;
  58. Op.AsInt64 := Value;
  59. Op.AsStr := '';
  60. end;
  61. procedure MakeStr(var Op: TIsppVariant; const Value: string);
  62. begin
  63. Op.Typ := evStr;
  64. Op.AsInt64 := 0;
  65. Op.AsStr := Value;
  66. end;
  67. procedure MakeBool(var Op: TIsppVariant; Value: Boolean);
  68. begin
  69. MakeInt(Op, Int64(Value));
  70. end;
  71. function TryStrToInt(Str: string; var Int: Integer): Boolean;
  72. var
  73. Err: Integer;
  74. begin
  75. if (Length(Str) > 2) and (Str[1] = '0') and ((Str[2] = 'x') or
  76. (Str[2] = 'X')) then
  77. Str := '$' + Copy(Str, 3, MaxInt);
  78. Val(Str, Int, Err);
  79. Result := Err = 0
  80. end;
  81. function ToInt(Op: TIsppVariant): TIsppVariant;
  82. var
  83. I: Integer;
  84. begin
  85. MakeRValue(Op);
  86. I := 0;
  87. if Op.Typ = evStr then
  88. if (Op.AsStr = '') or TryStrToInt(Op.AsStr, I) then
  89. MakeInt(Result, I)
  90. else
  91. raise EConvertError.CreateFmt('Cannot convert "%s" to integer', [Op.AsStr])
  92. else
  93. if Op.Typ = evNull then
  94. MakeInt(Result, 0)
  95. else
  96. CopyExpVar(Op, Result)
  97. end;
  98. function ToStr(Op: TIsppVariant): TIsppVariant;
  99. begin
  100. MakeRValue(Op);
  101. if Op.Typ = evInt then
  102. MakeStr(Result, IntToStr(Op.AsInt64))
  103. else
  104. if Op.Typ = evNull then
  105. MakeStr(Result, '')
  106. else
  107. MakeStr(Result, Op.AsStr)
  108. end;
  109. end.