fpc/rtl/inc/intres.inc
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

336 lines
9.5 KiB
PHP

{
This file is part of the Free Pascal run time library.
Copyright (c) 2008 by Giulio Bernardi
Resource support for non-PECOFF targets (ELF, Mach-O)
See the file COPYING.FPC, 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.
**********************************************************************}
type
PResInfoNode = ^TResInfoNode;
TResInfoNode = packed record
nameid : PChar; //name / integer ID / languageID
ncounthandle : longword; //named sub-entries count / resource handle
idcountsize : longword; //id sub-entries count / resource size
subptr : PResInfoNode; //first sub-entry pointer
end;
TResHdr = packed record
rootptr : PResInfoNode; //pointer to root node
count : longword; //number of resources in the file
usedhandles : longword; //last resource handle used
handles : PPtrUint; //pointer to handles
end;
PResHdr = ^TResHdr;
var
{$ifdef FPC_HAS_WINLIKERESOURCES}
ResHeader : PResHdr; external name 'FPC_RESLOCATION';
{$else}
ResHeader : PResHdr= nil;
{$endif}
(*****************************************************************************
Private Helper Functions
*****************************************************************************)
//resource functions are case insensitive... copied from genstr.inc
function ResStrIComp(Str1, Str2 : PChar): SizeInt;
var
counter: SizeInt;
c1, c2: char;
begin
counter := 0;
c1 := upcase(str1[counter]);
c2 := upcase(str2[counter]);
while c1 = c2 do
begin
if (c1 = #0) or (c2 = #0) then break;
inc(counter);
c1 := upcase(str1[counter]);
c2 := upcase(str2[counter]);
end;
ResStrIComp := ord(c1) - ord(c2);
end;
{!fixme!}
//function InternalIsIntResource(aStr : pchar; out aInt : PtrUint) : boolean;
function InternalIsIntResource(aStr : pchar; var aInt : PtrUint) : boolean;
var i : integer;
s : shortstring;
code : word;
begin
InternalIsIntResource:=((PtrUInt(aStr) shr 16)=0);
if InternalIsIntResource then aInt:=PtrUInt(aStr)
else
begin
//a string like #number specifies an integer id
if aStr[0]='#' then
begin
i:=1;
while aStr[i]<>#0 do
inc(i);
if i>256 then i:=256;
s[0]:=chr(i-1);
Move(aStr[1],s[1],i-1);
Val(s,aInt,code);
InternalIsIntResource:=code=0;
end;
end;
end;
function BinSearchStr(arr : PResInfoNode; query : pchar; left, right : integer)
: PResInfoNode;
var pivot, res : integer;
resstr : pchar;
begin
BinSearchStr:=nil;
while left<=right do
begin
pivot:=(left+right) div 2;
resstr:=arr[pivot].nameid;
res:=ResStrIComp(resstr,query);
if res<0 then left:=pivot+1
else if res>0 then right:=pivot-1
else
begin
BinSearchStr:=@arr[pivot];
exit;
end;
end;
end;
function BinSearchInt(arr : PResInfoNode; query : pchar; left, right : integer)
: PResInfoNode;
var pivot : integer;
begin
BinSearchInt:=nil;
while left<=right do
begin
pivot:=(left+right) div 2;
if PtrUint(arr[pivot].nameid)<PtrUInt(query) then left:=pivot+1
else if PtrUint(arr[pivot].nameid)>PtrUInt(query) then right:=pivot-1
else
begin
BinSearchInt:=@arr[pivot];
exit;
end;
end;
end;
function BinSearchRes(root : PResInfoNode; aDesc : PChar) : PResInfoNode;
var aID : PtrUint;
begin
if InternalIsIntResource(aDesc,aID) then
BinSearchRes:=BinSearchInt(root^.subptr,PChar(aID),root^.ncounthandle,
root^.ncounthandle+root^.idcountsize-1)
else
BinSearchRes:=BinSearchStr(root^.subptr,aDesc,0,root^.ncounthandle-1);
end;
//Returns a pointer to a name node.
function InternalFindResource(ResourceName, ResourceType: PChar):
PResInfoNode;
begin
InternalFindResource:=nil;
if ResHeader=nil then exit;
InternalFindResource:=ResHeader^.rootptr;
InternalFindResource:=BinSearchRes(InternalFindResource,ResourceType);
if InternalFindResource<>nil then
InternalFindResource:=BinSearchRes(InternalFindResource,ResourceName);
end;
function FindSubLanguage(aPtr : PResInfoNode; aLangID : word; aMask: word) : PResInfoNode;
var arr : PResInfoNode;
i : longword;
begin
FindSubLanguage:=nil;
arr:=aPtr^.subptr;
i:=0;
while i<aPtr^.idcountsize do
begin
if (PtrUInt(arr[i].nameid) and aMask)=(aLangID and aMask) then
begin
FindSubLanguage:=@arr[i];
exit;
end;
inc(i);
end;
end;
(*****************************************************************************
Public Resource Functions
*****************************************************************************)
Function IntHINSTANCE : TFPResourceHMODULE;
begin
IntHINSTANCE:=0;
end;
Function IntEnumResourceTypes(ModuleHandle : TFPResourceHMODULE; EnumFunc : EnumResTypeProc; lParam : PtrInt) : LongBool;
var ptr : PResInfoNode;
tot, i : integer;
begin
IntEnumResourceTypes:=False;
if ResHeader=nil then exit;
tot:=ResHeader^.rootptr^.ncounthandle+ResHeader^.rootptr^.idcountsize;
ptr:=ResHeader^.rootptr^.subptr;
IntEnumResourceTypes:=true;
i:=0;
while i<tot do
begin
if not EnumFunc(ModuleHandle,ptr[i].nameid,lParam) then exit;
inc(i);
end;
end;
Function IntEnumResourceNames(ModuleHandle : TFPResourceHMODULE; ResourceType : PChar; EnumFunc : EnumResNameProc; lParam : PtrInt) : LongBool;
var ptr : PResInfoNode;
tot, i : integer;
begin
IntEnumResourceNames:=False;
if ResHeader=nil then exit;
ptr:=ResHeader^.rootptr;
ptr:=BinSearchRes(ptr,ResourceType);
if ptr=nil then exit;
tot:=ptr^.ncounthandle+ptr^.idcountsize;
ptr:=ptr^.subptr;
IntEnumResourceNames:=true;
i:=0;
while i<tot do
begin
if not EnumFunc(ModuleHandle,ResourceType,ptr[i].nameid,lParam) then exit;
inc(i);
end;
end;
Function IntEnumResourceLanguages(ModuleHandle : TFPResourceHMODULE; ResourceType, ResourceName : PChar; EnumFunc : EnumResLangProc; lParam : PtrInt) : LongBool;
var ptr : PResInfoNode;
tot, i : integer;
begin
IntEnumResourceLanguages:=False;
ptr:=InternalFindResource(ResourceName,ResourceType);
if ptr=nil then exit;
tot:=ptr^.idcountsize;
ptr:=ptr^.subptr;
IntEnumResourceLanguages:=true;
i:=0;
while i<tot do
begin
if not EnumFunc(ModuleHandle,ResourceType,ResourceName,PtrUInt(ptr[i].nameid),lParam) then exit;
inc(i);
end;
end;
Function IntFindResource(ModuleHandle: TFPResourceHMODULE; ResourceName,
ResourceType: PChar): TFPResourceHandle;
var ptr : PResInfoNode;
begin
IntFindResource:=0;
ptr:=InternalFindResource(ResourceName,ResourceType);
if ptr=nil then exit;
//first language id
ptr:=ptr^.subptr;
if ptr^.ncounthandle=0 then
begin
ResHeader^.handles[ResHeader^.usedhandles]:=PtrUint(ptr);
inc(ResHeader^.usedhandles);
ptr^.ncounthandle:=ResHeader^.usedhandles;
end;
IntFindResource:=ptr^.ncounthandle;
end;
Function IntFindResourceEx(ModuleHandle: TFPResourceHMODULE; ResourceType,
ResourceName: PChar; Language : word): TFPResourceHandle;
const LANG_NEUTRAL = 0;
LANG_ENGLISH = 9;
var nameptr,ptr : PResInfoNode;
begin
IntFindResourceEx:=0;
nameptr:=InternalFindResource(ResourceName,ResourceType);
if nameptr=nil then exit;
//try exact match
ptr:=FindSubLanguage(nameptr,Language,$FFFF);
//try primary language
if ptr=nil then
ptr:=FindSubLanguage(nameptr,Language,$3FF);
//try language neutral
if ptr=nil then
ptr:=FindSubLanguage(nameptr,LANG_NEUTRAL,$3FF);
//try english
if ptr=nil then
ptr:=FindSubLanguage(nameptr,LANG_ENGLISH,$3FF);
//nothing found, return the first one
if ptr=nil then
ptr:=nameptr^.subptr;
if ptr^.ncounthandle=0 then
begin
ResHeader^.handles[ResHeader^.usedhandles]:=PtrUint(ptr);
inc(ResHeader^.usedhandles);
ptr^.ncounthandle:=ResHeader^.usedhandles;
end;
IntFindResourceEx:=ptr^.ncounthandle;
end;
Function IntLoadResource(ModuleHandle: TFPResourceHMODULE; ResHandle: TFPResourceHandle): TFPResourceHGLOBAL;
begin
IntLoadResource:=0;
if ResHeader=nil then exit;
if (ResHandle<=0) or (ResHandle>ResHeader^.usedhandles) then exit;
IntLoadResource:=TFPResourceHGLOBAL(PResInfoNode(ResHeader^.handles[ResHandle-1])^.subptr);
end;
Function IntSizeofResource(ModuleHandle: TFPResourceHMODULE; ResHandle: TFPResourceHandle): LongWord;
begin
IntSizeofResource:=0;
if ResHeader=nil then exit;
if (ResHandle<=0) or (ResHandle>ResHeader^.usedhandles) then exit;
IntSizeofResource:=PResInfoNode(ResHeader^.handles[ResHandle-1])^.idcountsize;
end;
Function IntLockResource(ResData: TFPResourceHGLOBAL): Pointer;
begin
IntLockResource:=Nil;
if ResHeader=nil then exit;
IntLockResource:=Pointer(ResData);
end;
Function IntUnlockResource(ResData: TFPResourceHGLOBAL): LongBool;
begin
IntUnlockResource:=(ResHeader<>nil);
end;
Function IntFreeResource(ResData: TFPResourceHGLOBAL): LongBool;
begin
IntFreeResource:=(ResHeader<>nil);
end;
const
InternalResourceManager : TResourceManager =
(
HINSTANCEFunc : @IntHINSTANCE;
EnumResourceTypesFunc : @IntEnumResourceTypes;
EnumResourceNamesFunc : @IntEnumResourceNames;
EnumResourceLanguagesFunc : @IntEnumResourceLanguages;
FindResourceFunc : @IntFindResource;
FindResourceExFunc : @IntFindResourceEx;
LoadResourceFunc : @IntLoadResource;
SizeofResourceFunc : @IntSizeofResource;
LockResourceFunc : @IntLockResource;
UnlockResourceFunc : @IntUnlockResource;
FreeResourceFunc : @IntFreeResource;
);