sourcehandler.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. {
  2. FPCRes - Free Pascal Resource Converter
  3. Part of the Free Pascal distribution
  4. Copyright (C) 2008 by Giulio Bernardi
  5. Source files handling
  6. See the file COPYING, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. }
  12. unit sourcehandler;
  13. {$MODE OBJFPC} {$H+}
  14. interface
  15. uses
  16. Classes, SysUtils, resource;
  17. type
  18. ESourceFilesException = class(Exception);
  19. ECantOpenFileException = class(ESourceFilesException);
  20. EUnknownInputFormatException = class(ESourceFilesException);
  21. type
  22. { TSourceFiles }
  23. TSourceFiles = class
  24. private
  25. protected
  26. fFileList : TStringList;
  27. fRCIncludeDirs: TStringList;
  28. fRCDefines: TStringList;
  29. fStreamList : TFPList;
  30. fRCMode: Boolean;
  31. public
  32. constructor Create;
  33. destructor Destroy; override;
  34. procedure Load(aResources : TResources);
  35. property FileList : TStringList read fFileList;
  36. property RCIncludeDirs: TStringList read fRCIncludeDirs;
  37. property RCDefines: TStringList read fRCDefines;
  38. property RCMode: Boolean read fRCMode write fRCMode;
  39. end;
  40. implementation
  41. uses msghandler, closablefilestream, rcreader;
  42. { TSourceFiles }
  43. constructor TSourceFiles.Create;
  44. begin
  45. inherited Create;
  46. fFileList:=TStringList.Create;
  47. fStreamList:=TFPList.Create;
  48. fRCDefines:= TStringList.Create;
  49. fRCIncludeDirs:= TStringList.Create;
  50. fRCMode:=False;
  51. end;
  52. destructor TSourceFiles.Destroy;
  53. var i : integer;
  54. begin
  55. fRCIncludeDirs.Free;
  56. fRCDefines.Free;
  57. fFileList.Free;
  58. for i:=0 to fStreamList.Count-1 do
  59. TStream(fStreamList[i]).Free;
  60. fStreamList.Free;
  61. inherited;
  62. end;
  63. procedure TSourceFiles.Load(aResources: TResources);
  64. var aReader : TAbstractResourceReader;
  65. aStream : TClosableFileStream;
  66. i : integer;
  67. tmpres : TResources;
  68. olddir : String;
  69. begin
  70. olddir:=GetCurrentDir;
  71. tmpres:=TResources.Create;
  72. try
  73. for i:=0 to fFileList.Count-1 do
  74. begin
  75. Messages.DoVerbose(Format('Trying to open file %s...',[fFileList[i]]));
  76. try
  77. aStream:=TClosableFileStream.Create(fFileList[i],fmOpenRead or fmShareDenyWrite);
  78. except
  79. raise ECantOpenFileException.Create(fFileList[i]);
  80. end;
  81. fStreamList.Add(aStream);
  82. { the RC reader reads anything, so handle that separately }
  83. if fRCMode then
  84. aReader:=TRCResourceReader.Create
  85. else
  86. try
  87. aReader:=TResources.FindReader(aStream);
  88. except
  89. raise EUnknownInputFormatException.Create(fFileList[i]);
  90. end;
  91. Messages.DoVerbose(Format('Chosen reader: %s',[aReader.Description]));
  92. try
  93. Messages.DoVerbose('Reading resource information...');
  94. if aReader is TRCResourceReader then begin
  95. TRCResourceReader(aReader).RCIncludeDirs.Assign(fRCIncludeDirs);
  96. TRCResourceReader(aReader).RCDefines.Assign(fRCDefines);
  97. SetCurrentDir(ExtractFilePath(ExpandFileName(fFileList[i])));
  98. end;
  99. try
  100. tmpres.LoadFromStream(aStream,aReader);
  101. except
  102. on e :EParserError do
  103. begin
  104. e.message:=fFileList[i]+': '+e.message;
  105. raise;
  106. end;
  107. end;
  108. aResources.MoveFrom(tmpres);
  109. Messages.DoVerbose('Resource information read');
  110. finally
  111. if aReader is TRCResourceReader then begin
  112. SetCurrentDir(olddir);
  113. end;
  114. aReader.Free;
  115. end;
  116. end;
  117. Messages.DoVerbose(Format('%d resources read.',[aResources.Count]));
  118. finally
  119. tmpres.Free;
  120. end;
  121. end;
  122. end.