rcreader.pp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2008 by Giulio Bernardi
  4. Resource reader/compiler for MS RC script files
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit rcreader;
  12. {$MODE OBJFPC} {$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, resource;
  16. type
  17. { TRCResourceReader }
  18. TRCResourceReader = class(TAbstractResourceReader)
  19. private
  20. fExtensions : string;
  21. fDescription : string;
  22. fRCIncludeDirs: TStringList;
  23. fRCDefines: TStringList;
  24. protected
  25. function GetExtensions : string; override;
  26. function GetDescription : string; override;
  27. procedure Load(aResources : TResources; aStream : TStream); override;
  28. function CheckMagic(aStream : TStream) : boolean; override;
  29. procedure ReadRCFile(aResources : TResources; aLocation: String; aStream : TStream);
  30. public
  31. constructor Create; override;
  32. destructor Destroy; override;
  33. property RCIncludeDirs: TStringList read fRCIncludeDirs;
  34. property RCDefines: TStringList read fRCDefines;
  35. end;
  36. implementation
  37. uses
  38. StreamIO, resdatastream, resfactory, lexlib, rcparser;
  39. { TRCResourceReader }
  40. function TRCResourceReader.GetExtensions: string;
  41. begin
  42. Result:=fExtensions;
  43. end;
  44. function TRCResourceReader.GetDescription: string;
  45. begin
  46. Result:=fDescription;
  47. end;
  48. procedure TRCResourceReader.Load(aResources: TResources; aStream: TStream);
  49. var
  50. fd: String;
  51. begin
  52. if aStream is TFileStream then
  53. fd:= ExtractFilePath(TFileStream(aStream).FileName)
  54. else
  55. fd:= IncludeTrailingPathDelimiter(GetCurrentDir);
  56. try
  57. ReadRCFile(aResources, fd, aStream);
  58. except
  59. on e : EReadError do
  60. raise EResourceReaderUnexpectedEndOfStreamException.Create('');
  61. end;
  62. end;
  63. function TRCResourceReader.CheckMagic(aStream: TStream): boolean;
  64. begin
  65. { TODO : Check for Text-Only file }
  66. Result:= True;
  67. end;
  68. procedure TRCResourceReader.ReadRCFile(aResources: TResources; aLocation: String; aStream: TStream);
  69. var
  70. i: Integer;
  71. begin
  72. AssignStream(lexlib.yyinput, aStream);
  73. Reset(lexlib.yyinput);
  74. try
  75. rcparser.yyfilename:= '#MAIN.RC';
  76. rcparser.SetDefaults;
  77. SetTextCodePage(lexlib.yyinput, rcparser.opt_code_page);
  78. rcparser.yinclude:= tyinclude.Create;
  79. rcparser.yinclude.WorkDir:= aLocation;
  80. rcparser.yinclude.SearchPaths.Assign(fRCIncludeDirs);
  81. rcparser.ypreproc:= typreproc.Create;
  82. rcparser.ypreproc.Defines.Add('RC_INVOKED', '');
  83. for i:= 0 to fRCDefines.Count-1 do
  84. rcparser.ypreproc.Defines.KeyData[fRCDefines.Names[i]]:= fRCDefines.ValueFromIndex[i];
  85. rcparser.aktresources:= aResources;
  86. if rcparser.yyparse <> 0 then
  87. raise EReadError.Create('Parse Error');
  88. finally
  89. rcparser.DisposePools;
  90. FreeAndNil(rcparser.ypreproc);
  91. FreeAndNil(rcparser.yinclude);
  92. end;
  93. end;
  94. constructor TRCResourceReader.Create;
  95. begin
  96. fExtensions:='.rc';
  97. fDescription:='RC script resource reader';
  98. fRCDefines:= TStringList.Create;
  99. fRCIncludeDirs:= TStringList.Create;
  100. end;
  101. destructor TRCResourceReader.Destroy;
  102. begin
  103. fRCIncludeDirs.Free;
  104. fRCDefines.Free;
  105. inherited;
  106. end;
  107. initialization
  108. { don't register automatically, as this is essentially a "catch all" }
  109. //TResources.RegisterReader('.rc',TRCResourceReader);
  110. end.