fpc/utils/fpcreslipo/sourcehandler.pp
michael 174de3eab1 Merged revisions 9693-10480 via svnmerge from
svn+ssh://svn.freepascal.org/FPC/svn/fpc/branches/resources

........
  r9694 | michael | 2008-01-09 21:31:18 +0100 (Wed, 09 Jan 2008) | 1 line
  
  * Initial check-in
........
  r9695 | michael | 2008-01-09 21:35:58 +0100 (Wed, 09 Jan 2008) | 1 line
  
  * New version from Giulio Bernardi
........
  r9697 | michael | 2008-01-09 21:41:54 +0100 (Wed, 09 Jan 2008) | 1 line
  
  * Patch from Giulio Bernardi with resource support
........
  r9698 | michael | 2008-01-09 21:46:33 +0100 (Wed, 09 Jan 2008) | 1 line
  
  * Patch from Giulio Bernardi to add more resource testing
........
  r9699 | michael | 2008-01-09 21:57:26 +0100 (Wed, 09 Jan 2008) | 1 line
  
  * New tool from Giulio Bernardi
........
  r9700 | michael | 2008-01-09 21:58:23 +0100 (Wed, 09 Jan 2008) | 1 line
  
  * New tool from Giulio Bernardi
........
  r9701 | michael | 2008-01-09 22:01:54 +0100 (Wed, 09 Jan 2008) | 1 line
  
  * Added fcl-res
........
  r9702 | michael | 2008-01-09 22:01:58 +0100 (Wed, 09 Jan 2008) | 1 line
  
  * Added fcl-res
........
  r9703 | michael | 2008-01-10 08:54:26 +0100 (Thu, 10 Jan 2008) | 1 line
  
  * Fixed double code
........
  r9704 | jonas | 2008-01-10 10:59:20 +0100 (Thu, 10 Jan 2008) | 2 lines
  
    - removed duplicate code
........
  r9705 | jonas | 2008-01-10 11:25:21 +0100 (Thu, 10 Jan 2008) | 2 lines
  
    + added missing fcl-res dependencies
........
  r9706 | jonas | 2008-01-10 11:58:30 +0100 (Thu, 10 Jan 2008) | 2 lines
  
    + dependencies for fpintres and fpextres
........
  r9707 | yury | 2008-01-10 12:47:51 +0100 (Thu, 10 Jan 2008) | 3 lines
  
  * Fixed compilation of resource, which is included in a unit located in different folder than main source.
  * .res files must be copied to units output folder, otherwise .res files will not be found when only compiled units path is available and compiler does not know anything about sources folder.
  * Improved resource related error messages.
........
  r9708 | michael | 2008-01-10 12:52:13 +0100 (Thu, 10 Jan 2008) | 1 line
  
  * Removed double source after end.
........
  r9709 | michael | 2008-01-10 12:52:48 +0100 (Thu, 10 Jan 2008) | 1 line
  
  * No longer needed
........
  r9710 | tom_at_work | 2008-01-10 22:09:08 +0100 (Thu, 10 Jan 2008) | 1 line
  
  * properly align FPC_RESLOCATION so that linking does not fail on some architectures (e.g. ppc64)
........
  r9711 | tom_at_work | 2008-01-10 23:53:12 +0100 (Thu, 10 Jan 2008) | 1 line
  
  * fix splitting of 64 bit load/stores from/to unaligned memory locations into multiple load/stores, which in some cases generated wrong code
........
  r9712 | michael | 2008-01-11 11:00:08 +0100 (Fri, 11 Jan 2008) | 1 line
  
  * Fixed bug in BSS section on 64-bit platforms
........
  r9720 | giulio | 2008-01-12 10:02:04 +0100 (Sat, 12 Jan 2008) | 1 line
  
  Updated fcl-res documentation: occurrences of reslib changed to fcl-res.
........
  r9740 | giulio | 2008-01-13 19:36:44 +0100 (Sun, 13 Jan 2008) | 3 lines
  
   - Don't try to compile resources on systems with a non windows-like resource support.
   - Don't add the .or file to the list of object files if resource compiling failed.
........
  r10201 | giulio | 2008-02-04 11:35:44 +0100 (Mon, 04 Feb 2008) | 5 lines
  
  * resource compiling supported on OS/2 via wrc
  * CompileResourceFiles and CollectResourceFiles don't do target-specific checks anymore
  * refactored a bit
........
  r10389 | giulio | 2008-02-25 21:32:52 +0100 (Mon, 25 Feb 2008) | 2 lines
  
  Deleted test file which was committed by mistake
........
  r10472 | giulio | 2008-03-10 12:22:18 +0100 (Mon, 10 Mar 2008) | 2 lines
  
  changed define FPC_HAS_RESOURCES to FPC_HAS_WINLIKERESOURCES
........

git-svn-id: trunk@10481 -
2008-03-12 21:33:48 +00:00

342 lines
8.1 KiB
ObjectPascal

{
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.