-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunVariables.pas
232 lines (192 loc) · 5.12 KB
/
unVariables.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
unit unVariables;
interface
uses System.Classes,Winapi.messages;
const WM_REFVARS_MSG = WM_USER + 252;
Type
TArduVar=Class
VarName:String;
VarID:Integer;
VarValue:String;
function getCvarname:string;
End;
TArduVariables=Class(TStringlist)
private
function GetNewID: Integer;
Public
Constructor Create;
Destructor Destroy;Override;
Function Newvar(VName:String):TArduVar;
Function GetVarByID(id:integer):TArduVar;
Function GetVarByName(nm:String):TArduVar;
function GetRealVarName(nm: string): string;
Procedure DeleteVar(VName:String);
Procedure SaveVars(fnm:String);
Procedure LoadVars(fnm:String);
Function GetArduVar(idx:integer):TArduVar;
procedure EmptyVars;
end;
var
ArduVars:TArduVariables;
implementation
uses math,sysutils,inifiles,vcl.forms;
{ TArduVariables }
function greeklish(name:string):string;
Const greek:array[0..65] of string=('á','Ü','¢','Á','â','Â','ã', 'Ã', 'ä','Ä','å','Ý','Å','¸','æ','Æ','ç','Þ','Ç','è','È','é','ß','ú','À','É','º', 'ê','Ê','ë','Ë','ì','Ì','í','Í','î','Î','ï','ü','Ï','¼','ð','Ð','ñ','Ñ','ó','ò', 'Ó','ô','Ô','õ','ý','Õ','¾','ö','Ö','÷','×','ø','Ø','ù','þ','Ù','¿',' ',',');
english:array[0..65] of string=('a', 'a','A','A','b','B','g','G','d','D','e','e','E','E','z','Z','i','i','I','th','Th', 'i','i','i','i','I','I','k','K','l','L','m','M','n','N','x','X','o','o','O','O','p','P' ,'r','R','s','s','S','t','T','u','u','Y','Y','f','F','ch','Ch','ps','Ps','o','o','O','O','_','_');
var i:Integer;
function Changeletter(l:char):string;
var i:integer;
Begin
result:='';
for I := 0 to High(greek) do
if greek[i]=l then
Begin
result:=english[i];
break;
End;
if result='' then result:=l;
End;
Begin
result:='';
for I := 1 to Length(name) do
result:=result+Changeletter(name[i]);
// result:=StringReplace(name,name)
// $string = str_replace($greek, $english, $Name);
End;
constructor TArduVariables.Create;
begin
end;
procedure TArduVariables.DeleteVar(VName: String);
Var av:TArduVar;
i:Integer;
begin
av:=GetVarByName(VName);
if assigned(av) then
Begin
i:=IndexOf(VName);
Delete(i);
av.Free;
End;
Application.MainForm.Perform(WM_REFVARS_MSG,0,WM_REFVARS_MSG);
end;
procedure TArduVariables.EmptyVars;
Var i:integer;
Begin
for i:=count-1 downto 0 do
Begin
TArduVar(objects[i]).Free;
Delete(i);
End;
Application.MainForm.Perform(WM_REFVARS_MSG,0,WM_REFVARS_MSG);
End;
destructor TArduVariables.Destroy;
begin
EmptyVars;
inherited;
end;
function TArduVariables.GetVarByID(id: integer): TArduVar;
Var i:integer;
begin
Result:=nil;
for i:= 0 to count-1 do
Begin
Result:=TArduVar(objects[i]);
if Result.VarID=id then
Break
Else
Result:=nil;
End;
end;
function TArduVariables.GetVarByName(nm: String): TArduVar;
var i:integer;
begin
i:=IndexOf(nm);
if i>-1 then
Result:= TArduVar(Objects[i])
else Result:=nil;
end;
function TArduVariables.GetRealVarName(nm:string): string;
Var t:TArduVar;
Begin
t:=GetVarByName(nm);
if t<>nil then
result:=greeklish(nm)
else result:='NoVAR';
End;
procedure TArduVariables.LoadVars(fnm: String);
Var Newnm:String;
sl:TIniFile;
i,cnt:integer;
sectnm,vrnm:String;
av:TArduVar;
begin
EmptyVars;
Newnm:=ChangeFileExt(fnm,'.var');
sl:=TInifile.create(Newnm);
cnt:=sl.ReadInteger('Var Count','Count',0);
for I := 0 to cnt-1 do
Begin
sectnm:='Var_'+inttostr(i);
vrnm:=sl.ReadString(sectnm,'VarName','');
av:=Newvar(vrnm);
av.VarID:=sl.ReadInteger(sectnm,'VarID',av.VarID);
av.VarValue:=sl.ReadString(sectnm,'VarValue',av.VarValue);
End;
sl.Free;
end;
function TArduVariables.GetArduVar(idx: integer): TArduVar;
begin
if (idx>=0) and (idx<count) then
Result:=TArduVar(Objects[idx])
else result:=nil;
end;
function TArduVariables.GetNewID:Integer;
Var i:integer;
Begin
Result:=0;
if Count>0 then
for i:=0 to Count-1 do
Result:=Max(Result,TArduVar(Objects[i]).VarID);
Result:=Result+1;
End;
function TArduVariables.Newvar(VName: String): TArduVar;
Var Msg: TMessage;
begin
Result:=TArduVar.Create;
Result.VarName:=vname;
Result.VarID:=GetNewID;
Result.VarValue:='';
AddObject(vname,Result);
Msg.Msg := WM_REFVARS_MSG;
Msg.WParam := 0;
Msg.LParam := Result.VarID;
Msg.Result := 0;
// Application.MainForm.Broadcast(msg);
Application.MainForm.Perform(WM_REFVARS_MSG,0,WM_REFVARS_MSG);
end;
procedure TArduVariables.SaveVars(fnm: String);
Var Newnm:String;
i:integer;
sl:Tinifile;
sectnm:String;
begin
Newnm:=ChangeFileExt(fnm,'.var');
sl:=TInifile.create(Newnm);
sl.WriteInteger('Var Count','Count',count);
for I := 0 to count-1 do
Begin
sectnm:='Var_'+inttostr(i);
sl.WriteString(sectnm,'VarName',TArduVar(Objects[i]).VarName);
sl.WriteInteger(sectnm,'VarID',TArduVar(Objects[i]).VarID);
sl.WriteString(sectnm,'VarValue',TArduVar(Objects[i]).VarValue);
End;
sl.Free;
end;
{ TArduVar }
function TArduVar.getCvarname: string;
begin
result:=greeklish(Varname);
end;
initialization
ArduVars:=TArduVariables.Create;
end.