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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
{
FPCResLipo - Free Pascal External Resource Thinner
Part of the Free Pascal distribution
Copyright (C) 2008 by Giulio Bernardi
Source files handling
See the file COPYING, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}
unit sourcehandler;
{$MODE OBJFPC} {$H+}
interface
uses
Classes, SysUtils, resource, externalreader, externalwriter;
type
ESourceFilesException = class(Exception);
ECantOpenFileException = class(ESourceFilesException);
EUnknownInputFormatException = class(ESourceFilesException);
ECantCreateFileException = class(ESourceFilesException);
type
{ TSourceFile }
TSourceFile = class
private
fFname : string;
fStream : TStream;
fResources : TResources;
fProcessed : TResources;
fEndianess : byte;
fModified : boolean;
function Delete : boolean;
protected
public
constructor Create(aFileName : string);
destructor Destroy; override;
procedure Update;
property FileName : string read fFname;
property Resources : TResources read fResources;
property Processed : TResources read fProcessed;
property Endianess : byte read fEndianess;
property Modified : boolean read fModified write fModified;
end;
{ TSourceFiles }
TSourceFiles = class
private
similarities, simcount : array of integer;
fList : TFPList;
function GetItem(index : integer) : TSourceFile;
function GetCount : integer;
procedure ResetSimArrays;
function GetMostCommon : integer;
procedure CheckSimilarities(idx : integer; aType,aName : TResourceDesc; aLangID : TLangID);
procedure ExtractCommon(idx : integer; outRes : TResources; aType,aName : TResourceDesc; aLangID : TLangID);
protected
public
constructor Create;
destructor Destroy; override;
procedure NewSourceFile(aFileName : string);
procedure Process(outRes : TResources);
procedure Update;
property Items[index : integer] : TSourceFile read GetItem;
property Count : integer read GetCount;
end;
implementation
uses msghandler;
{ TSourceFile }
function TSourceFile.Delete : boolean;
begin
FreeAndNil(fResources);
FreeAndNil(fStream);
Result:=DeleteFile(fFname);
if not Result then
Messages.DoError(Format('Can''t delete file %s.',[fFname]))
end;
constructor TSourceFile.Create(aFileName: string);
var aReader : TExternalResourceReader;
begin
fModified:=false;
fFName:=aFileName;
Messages.DoVerbose(Format('Trying to open file %s...',[fFName]));
try
fStream:=TFileStream.Create(fFName,fmOpenRead or fmShareDenyWrite);
except
raise ECantOpenFileException.Create(fFName);
end;
aReader:=TExternalResourceReader.Create;
fResources:=TResources.Create;
try
try
try
Messages.DoVerbose('Reading resource information...');
fResources.LoadFromStream(fStream,aReader);
Messages.DoVerbose(Format('%d resources read.',[fResources.Count]));
fEndianess:=aReader.Endianess;
except
on e : EResourceReaderWrongFormatException do
raise EUnknownInputFormatException.Create(fFname);
end;
except
FreeAndNil(fResources);
FreeAndNil(fStream);
end;
finally
aReader.Free;
end;
fProcessed:=TResources.Create;
end;
destructor TSourceFile.Destroy;
begin
if fResources<>nil then fResources.Free;
if fProcessed<>nil then fProcessed.Free;
if fStream<>nil then fStream.Free;
end;
procedure TSourceFile.Update;
var tmp : string;
aWriter : TExternalResourceWriter;
aStream : TFileStream;
begin
if not fModified then
begin
Messages.DoVerbose(Format('File %s is unchanged.',[fFname]));
exit;
end;
if Resources.Count=0 then
begin
if Delete then
Messages.DoVerbose(Format('No more resources in file %s, deleted',[fFname]));
exit;
end;
tmp:=ExtractFileDir(fFname);
if tmp='' then tmp:='.';
tmp:=GetTempFileName(tmp,'tmp');
Messages.DoVerbose(Format('Updating file %s...',[fFname]));
try
aStream:=TFileStream.Create(tmp,fmCreate or fmShareDenyWrite);
except
raise ECantCreateFileException.Create(tmp);
end;
try
aWriter:=TExternalResourceWriter.Create;
aWriter.Endianess:=Endianess;
try
Resources.WriteToStream(aStream,aWriter);
Messages.DoVerbose(Format('%d resources written.',[Resources.Count]));
finally
aWriter.Free;
end;
finally
aStream.Free;
end;
if not Delete then exit;
if not RenameFile(tmp,fFname) then
Messages.DoError(Format('Can''t rename file %s to %s.',[tmp,fFname]))
else
Messages.DoVerbose(Format('File %s updated',[fFname]));
end;
{ TSourceFiles }
function TSourceFiles.GetItem(index : integer) : TSourceFile;
begin
Result:=TSourceFile(fList[index]);
end;
function TSourceFiles.GetCount: integer;
begin
Result:=fList.Count;
end;
procedure TSourceFiles.ResetSimArrays;
var i : integer;
begin
for i:=0 to Count-1 do
begin
similarities[i]:=i;
simcount[i]:=1;
end;
end;
function TSourceFiles.GetMostCommon: integer;
var i : integer;
max, maxidx : integer;
begin
max:=0;
maxidx:=0;
for i:=0 to Count-1 do
if simcount[i]>max then
begin
max:=simcount[i];
maxidx:=i;
end;
Result:=maxidx;
end;
procedure TSourceFiles.CheckSimilarities(idx: integer; aType,
aName: TResourceDesc; aLangID: TLangID);
var i,j : integer;
res1, res2 : TAbstractResource;
begin
for i:=idx to Count-1 do
begin
if similarities[i]<>i then continue;
try
res1:=Items[i].Resources.Find(aType,aName,aLangID);
except
on e : EResourceNotFoundException do continue;
end;
for j:=idx+1 to Count-1 do
begin
try
res2:=Items[j].Resources.Find(aType,aName,aLangID);
except
on e : EResourceNotFoundException do continue;
end;
if res1.CompareContents(res2) then
begin
dec(simcount[similarities[j]]);
inc(simcount[similarities[i]]);
similarities[j]:=similarities[i];
end;
end;
end;
end;
procedure TSourceFiles.ExtractCommon(idx: integer; outRes: TResources; aType,
aName: TResourceDesc; aLangID: TLangID);
var maxidx,i : integer;
res : TAbstractResource;
begin
maxidx:=GetMostCommon;
if simcount[maxidx]<=1 then
begin
for i:=idx to Count-1 do
begin
try
res:=Items[i].Resources.Remove(aType,aName,aLangID);
except
on e : EResourceNotFoundException do continue;
end;
Items[i].Processed.Add(res);
end;
exit;
end;
res:=Items[maxidx].Resources.Remove(aType,aName,aLangID);
Items[maxidx].Modified:=true;
outRes.Add(res);
for i:=idx to Count-1 do
begin
if i=maxidx then continue;
try
res:=Items[i].Resources.Remove(aType,aName,aLangID);
except
on e : EResourceNotFoundException do continue;
end;
if similarities[i]=similarities[maxidx] then
begin
res.Free;
Items[i].Modified:=true;
end
else
Items[i].Processed.Add(res);
end;
end;
constructor TSourceFiles.Create;
begin
fList:=TFPList.Create;
end;
destructor TSourceFiles.Destroy;
var i : integer;
begin
for i:=0 to fList.Count-1 do
TSourceFile(fList[i]).Free;
fList.Free;
end;
procedure TSourceFiles.NewSourceFile(aFileName : string);
var aFile : TSourceFile;
begin
aFile:=TSourceFile.Create(aFileName);
fList.Add(aFile);
end;
procedure TSourceFiles.Process(outRes: TResources);
var i : integer;
res : TAbstractResource;
begin
setlength(similarities,Count);
setlength(simcount,Count);
for i:=0 to Count-1 do
begin
while Items[i].Resources.Count>0 do
begin
ResetSimArrays;
res:=Items[i].Resources[Items[i].Resources.Count-1];
if res.Owner<>nil then
res:=res.Owner;
CheckSimilarities(i,res._Type,res.Name,res.LangID);
ExtractCommon(i,outRes,res._Type,res.Name,res.LangID);
end;
Items[i].Resources.MoveFrom(Items[i].Processed);
end;
end;
procedure TSourceFiles.Update;
var i : integer;
begin
for i:=0 to Count-1 do
Items[i].Update;
end;
end.
|