2
0

ISPP.VarUtils.pas 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 ToInt(Op: TIsppVariant): TIsppVariant;
  21. function ToStr(Op: TIsppVariant): TIsppVariant;
  22. const
  23. NULL: TIsppVariant = (Typ: evNull; AsStr: ''; AsInt64: 0);
  24. implementation
  25. uses
  26. SysUtils, ISPP.Consts;
  27. function SimplifyLValue(var Src: TIsppVariant): Boolean;
  28. begin
  29. Result := Src.Typ = evLValue;
  30. if Result then
  31. while Src.AsPtr^.Typ = evLValue do Src := Src.AsPtr^;
  32. end;
  33. procedure MakeRValue(var Src: TIsppVariant);
  34. begin
  35. while Src.Typ = evLValue do Src := Src.AsPtr^;
  36. end;
  37. function GetRValue(const Src: TIsppVariant): TIsppVariant;
  38. begin
  39. Result := Src;
  40. MakeRValue(Result);
  41. end;
  42. procedure CopyExpVar(Src: TIsppVariant; var Dest: TIsppVariant);
  43. begin
  44. MakeRValue(Src);
  45. if Src.Typ = evStr then
  46. begin
  47. Dest.Typ := evStr;
  48. Dest.AsInt64 := 0;
  49. Dest.AsStr := Src.AsStr;
  50. end
  51. else
  52. Move(Src, Dest, SizeOf(TIsppVariant));
  53. end;
  54. procedure MakeInt(var Op: TIsppVariant; Value: Int64);
  55. begin
  56. Op.Typ := evInt;
  57. Op.AsInt64 := Value;
  58. Op.AsStr := '';
  59. end;
  60. procedure MakeStr(var Op: TIsppVariant; const Value: string);
  61. begin
  62. Op.Typ := evStr;
  63. Op.AsInt64 := 0;
  64. Op.AsStr := Value;
  65. end;
  66. procedure MakeBool(var Op: TIsppVariant; Value: Boolean);
  67. begin
  68. MakeInt(Op, Int64(Value));
  69. end;
  70. function ToInt(Op: TIsppVariant): TIsppVariant;
  71. begin
  72. MakeRValue(Op);
  73. var I: Int64 := 0;
  74. if Op.Typ = evStr then
  75. if (Op.AsStr = '') or TryStrToInt64(Op.AsStr, I) then
  76. MakeInt(Result, I)
  77. else
  78. raise EConvertError.CreateFmt('Cannot convert "%s" to integer', [Op.AsStr])
  79. else
  80. if Op.Typ = evNull then
  81. MakeInt(Result, 0)
  82. else
  83. CopyExpVar(Op, Result)
  84. end;
  85. function ToStr(Op: TIsppVariant): TIsppVariant;
  86. begin
  87. MakeRValue(Op);
  88. if Op.Typ = evInt then
  89. MakeStr(Result, IntToStr(Op.AsInt64))
  90. else
  91. if Op.Typ = evNull then
  92. MakeStr(Result, '')
  93. else
  94. MakeStr(Result, Op.AsStr)
  95. end;
  96. end.