-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupgrade.dpr
94 lines (87 loc) · 2.15 KB
/
upgrade.dpr
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
program upgrade;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Apus.MyServis, System.SysUtils;
var
items:array[1..1000,0..1] of String;
iCount:integer;
// Load "config.upgrade" file from the executable folder
// Pass config file name xxx to load "xxx.upgrade" instead of default config
procedure LoadConfig;
var
launchPath,name:string;
config:string;
lines,line:StringArr;
i:integer;
begin
launchPath:=ExtractFilePath(ParamStr(0));
name:='config';
if ParamStr(1)<>'' then name:=ParamStr(1);
name:=launchPath+name+'.upgrade';
config:=LoadFileAsString(name);
iCount:=0;
lines:=split(#13#10,config);
for i:=0 to high(lines) do begin
line:=split('->',lines[i]);
if length(line)<>2 then continue;
inc(iCount);
items[iCount,0]:=Chop(line[0]);
items[iCount,1]:=Chop(line[1]);
end;
Writeln('Config loaded: ',iCount,' items from '+name);
end;
procedure ProcessFile(fName:string);
var
data,bakName:string8;
i,p,start,pSelf:integer;
oldStr,newStr:string8;
begin
data:=LoadFileAsString(fName);
for i:=1 to iCount do begin
start:=1;
oldStr:=items[i,0];
newStr:=items[i,1];
pSelf:=pos(oldStr,newStr);
repeat
p:=PosFrom(oldStr,data,start,true);
if p<=0 then break;
// substring found
start:=p+1;
if data[p-1] in ['A'..'Z','a'..'z'] then continue; // part of another identifier?
if pSelf>0 then
if SameText(Copy(data,p-pSelf+1,length(newStr)),newStr) then continue;
Delete(data,p,length(oldStr));
Insert(newStr,data,p);
inc(start,length(newStr)-1);
until false;
end;
bakName:=ChangeFileExt(fName,'.bak');
RenameFile(fName,bakName);
SaveFile(fName,data);
end;
procedure ProcessFiles;
var
files:StringArr;
fName:string;
cnt:integer;
begin
cnt:=0;
files:=ListFiles(GetCurrentDir,'*.pas',true);
for fName in files do begin
write('Processing file: ',fName);
inc(cnt);
ProcessFile(fName);
writeln(' done!');
end;
writeln(cnt,' files processed');
end;
begin
try
LoadConfig;
ProcessFiles;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.