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
|
{
FPCRes - Free Pascal Resource Converter
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;
type
ESourceFilesException = class(Exception);
ECantOpenFileException = class(ESourceFilesException);
EUnknownInputFormatException = class(ESourceFilesException);
type
{ TSourceFiles }
TSourceFiles = class
private
protected
fFileList : TStringList;
fRCIncludeDirs: TStringList;
fRCDefines: TStringList;
fStreamList : TFPList;
fRCMode: Boolean;
public
constructor Create;
destructor Destroy; override;
procedure Load(aResources : TResources);
property FileList : TStringList read fFileList;
property RCIncludeDirs: TStringList read fRCIncludeDirs;
property RCDefines: TStringList read fRCDefines;
property RCMode: Boolean read fRCMode write fRCMode;
end;
implementation
uses msghandler, closablefilestream, rcreader;
{ TSourceFiles }
constructor TSourceFiles.Create;
begin
inherited Create;
fFileList:=TStringList.Create;
fStreamList:=TFPList.Create;
fRCDefines:= TStringList.Create;
fRCIncludeDirs:= TStringList.Create;
fRCMode:=False;
end;
destructor TSourceFiles.Destroy;
var i : integer;
begin
fRCIncludeDirs.Free;
fRCDefines.Free;
fFileList.Free;
for i:=0 to fStreamList.Count-1 do
TStream(fStreamList[i]).Free;
fStreamList.Free;
inherited;
end;
procedure TSourceFiles.Load(aResources: TResources);
var aReader : TAbstractResourceReader;
aStream : TClosableFileStream;
i : integer;
tmpres : TResources;
olddir : String;
begin
olddir:=GetCurrentDir;
tmpres:=TResources.Create;
try
for i:=0 to fFileList.Count-1 do
begin
Messages.DoVerbose(Format('Trying to open file %s...',[fFileList[i]]));
try
aStream:=TClosableFileStream.Create(fFileList[i],fmOpenRead or fmShareDenyWrite);
except
raise ECantOpenFileException.Create(fFileList[i]);
end;
fStreamList.Add(aStream);
{ the RC reader reads anything, so handle that separately }
if fRCMode then
aReader:=TRCResourceReader.Create
else
try
aReader:=TResources.FindReader(aStream);
except
raise EUnknownInputFormatException.Create(fFileList[i]);
end;
Messages.DoVerbose(Format('Chosen reader: %s',[aReader.Description]));
try
Messages.DoVerbose('Reading resource information...');
if aReader is TRCResourceReader then begin
TRCResourceReader(aReader).RCIncludeDirs.Assign(fRCIncludeDirs);
TRCResourceReader(aReader).RCDefines.Assign(fRCDefines);
SetCurrentDir(ExtractFilePath(ExpandFileName(fFileList[i])));
end;
tmpres.LoadFromStream(aStream,aReader);
aResources.MoveFrom(tmpres);
Messages.DoVerbose('Resource information read');
finally
if aReader is TRCResourceReader then begin
SetCurrentDir(olddir);
end;
aReader.Free;
end;
end;
Messages.DoVerbose(Format('%d resources read.',[aResources.Count]));
finally
tmpres.Free;
end;
end;
end.
|