forked from jrsoftware/issrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSStackHelper.pas
161 lines (140 loc) · 4.99 KB
/
PSStackHelper.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
unit PSStackHelper;
{
Inno Setup
Copyright (C) 1997-2024 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
ROPS TPSStack helper class
}
interface
uses
Classes,
uPSRuntime;
type
TPSStackHelper = class helper for TPSStack
private
function GetArray(const ItemNo, FieldNo: Longint; out N: Integer): TPSVariantIFC;
function SetArray(const ItemNo, FieldNo: Longint; const N: Integer): TPSVariantIFC; overload;
public
type
TArrayOfInteger = array of Integer;
TArrayOfString = array of String;
TArrayBuilder = record
Arr: TPSVariantIFC;
I: Integer;
procedure Add(const Data: String);
end;
TArrayEnumerator = record
Arr: TPSVariantIFC;
N, I: Integer;
function HasNext: Boolean;
function Next: String;
end;
function GetChar(const ItemNo: Longint): Char;
function GetIntArray(const ItemNo: Longint; const FieldNo: Longint = -1): TArrayOfInteger;
function GetProc(const ItemNo: Longint; const Exec: TPSExec): TMethod;
function GetStringArray(const ItemNo: Longint; const FieldNo: Longint = -1): TArrayOfString;
function InitArrayBuilder(const ItemNo: LongInt; const FieldNo: Longint = -1): TArrayBuilder;
function InitArrayEnumerator(const ItemNo: LongInt; const FieldNo: Longint = -1): TArrayEnumerator;
procedure SetArray(const ItemNo: Longint; const Data: TArray<String>; const FieldNo: Longint = -1); overload;
procedure SetArray(const ItemNo: Longint; const Data: TStrings; const FieldNo: Longint = -1); overload;
procedure SetInt(const ItemNo: Longint; const Data: Integer; const FieldNo: Longint = -1);
end;
implementation
function TPSStackHelper.GetArray(const ItemNo, FieldNo: Longint;
out N: Integer): TPSVariantIFC;
begin
if FieldNo >= 0 then
Result := NewTPSVariantRecordIFC(Items[ItemNo], FieldNo)
else
Result := NewTPSVariantIFC(Items[ItemNo], True);
N := PSDynArrayGetLength(Pointer(Result.Dta^), Result.aType);
end;
function TPSStackHelper.SetArray(const ItemNo, FieldNo: Longint;
const N: Integer): TPSVariantIFC;
begin
if FieldNo >= 0 then
Result := NewTPSVariantRecordIFC(Items[ItemNo], FieldNo)
else
Result := NewTPSVariantIFC(Items[ItemNo], True);
PSDynArraySetLength(Pointer(Result.Dta^), Result.aType, N);
end;
function TPSStackHelper.GetChar(const ItemNo: Longint): Char;
begin
var S := GetString(ItemNo);
if S <> '' then
Result := S[1]
else
Result := #0;
end;
function TPSStackHelper.GetIntArray(const ItemNo, FieldNo: Longint): TArrayOfInteger;
begin
var N: Integer;
var Arr := GetArray(ItemNo, FieldNo, N);
SetLength(Result, N);
for var I := 0 to N-1 do
Result[I] := VNGetInt(PSGetArrayField(Arr, I));
end;
function TPSStackHelper.GetProc(const ItemNo: Longint; const Exec: TPSExec): TMethod;
begin
var P := PPSVariantProcPtr(Items[ItemNo]);
{ ProcNo 0 means nil was passed by the script and GetProcAsMethod will then return a (nil, nil) TMethod }
Result := Exec.GetProcAsMethod(P.ProcNo);
end;
function TPSStackHelper.GetStringArray(const ItemNo, FieldNo: Longint): TArrayOfString;
begin
var N: Integer;
var Arr := GetArray(ItemNo, FieldNo, N);
SetLength(Result, N);
for var I := 0 to N-1 do
Result[I] := VNGetString(PSGetArrayField(Arr, I));
end;
function TPSStackHelper.InitArrayBuilder(const ItemNo, FieldNo: Longint): TArrayBuilder;
begin
Result.Arr := SetArray(ItemNo, FieldNo, 0);
Result.I := 0;
end;
procedure TPSStackHelper.TArrayBuilder.Add(const Data: String);
begin
PSDynArraySetLength(Pointer(Arr.Dta^), Arr.aType, I+1);
VNSetString(PSGetArrayField(Arr, I), Data);
Inc(I);
end;
function TPSStackHelper.InitArrayEnumerator(const ItemNo, FieldNo: Longint): TArrayEnumerator;
begin
Result.Arr := GetArray(ItemNo, FieldNo, Result.N);
Result.I := 0;
end;
function TPSStackHelper.TArrayEnumerator.HasNext: Boolean;
begin
Result := I < N;
end;
function TPSStackHelper.TArrayEnumerator.Next: String;
begin
Result := VNGetString(PSGetArrayField(Arr, I));
Inc(I);
end;
procedure TPSStackHelper.SetArray(const ItemNo: Longint; const Data: TArray<String>; const FieldNo: Longint);
begin
var N := System.Length(Data);
var Arr := SetArray(ItemNo, FieldNo, N);
for var I := 0 to N-1 do
VNSetString(PSGetArrayField(Arr, I), Data[I]);
end;
procedure TPSStackHelper.SetArray(const ItemNo: Longint; const Data: TStrings; const FieldNo: Longint);
begin
var N := Data.Count;
var Arr := SetArray(ItemNo, FieldNo, N);
for var I := 0 to N-1 do
VNSetString(PSGetArrayField(Arr, I), Data[I]);
end;
procedure TPSStackHelper.SetInt(const ItemNo: Longint; const Data: Integer;
const FieldNo: Longint);
begin
if FieldNo >= 0 then begin
var PSVariantIFC := NewTPSVariantRecordIFC(Items[ItemNo], FieldNo);
VNSetInt(PSVariantIFC, Data);
end else
inherited SetInt(ItemNo, Data)
end;
end.