-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresutils.pas
95 lines (84 loc) · 3.53 KB
/
resutils.pas
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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit resutils;
interface
type
TResourceCallback = procedure (const Data: Pointer; const Size: Cardinal);
TResourceCallbackNested = procedure (const Data: Pointer; const Size: Cardinal) is nested;
TResourceCallbackMethod = procedure (const Data: Pointer; const Size: Cardinal) of object;
procedure ReadFromResources(const SectionName, ResourceName: AnsiString; const Callback: TResourceCallback);
procedure ReadFromResources(const SectionName, ResourceName: AnsiString; const Callback: TResourceCallbackNested);
procedure ReadFromResources(const SectionName, ResourceName: AnsiString; const Callback: TResourceCallbackMethod);
implementation
procedure ReadFromResources(const SectionName, ResourceName: AnsiString; const Callback: TResourceCallback);
var
FoundResourceHandle: TFPResourceHandle;
LoadedResourceHandle: TFPResourceHGlobal;
Data: Pointer;
Size: Cardinal;
begin
FoundResourceHandle := FindResource(HInstance, ResourceName, SectionName);
Assert(FoundResourceHandle <> 0, 'failed to load resource ' + SectionName + ':' + ResourceName);
LoadedResourceHandle := LoadResource(HInstance, FoundResourceHandle);
Assert(LoadedResourceHandle <> 0);
Data := LockResource(LoadedResourceHandle);
Size := SizeOfResource(HInstance, FoundResourceHandle);
Callback(Data, Size);
UnlockResource(LoadedResourceHandle);
FreeResource(LoadedResourceHandle);
end;
procedure ReadFromResources(const SectionName, ResourceName: AnsiString; const Callback: TResourceCallbackNested);
var
FoundResourceHandle: TFPResourceHandle;
LoadedResourceHandle: TFPResourceHGlobal;
Data: Pointer;
Size: Cardinal;
begin
FoundResourceHandle := FindResource(HInstance, ResourceName, SectionName);
Assert(FoundResourceHandle <> 0, 'failed to load resource ' + SectionName + ':' + ResourceName);
LoadedResourceHandle := LoadResource(HInstance, FoundResourceHandle);
Assert(LoadedResourceHandle <> 0);
Data := LockResource(LoadedResourceHandle);
Size := SizeOfResource(HInstance, FoundResourceHandle);
Callback(Data, Size);
UnlockResource(LoadedResourceHandle);
FreeResource(LoadedResourceHandle);
end;
procedure ReadFromResources(const SectionName, ResourceName: AnsiString; const Callback: TResourceCallbackMethod);
var
FoundResourceHandle: TFPResourceHandle;
LoadedResourceHandle: TFPResourceHGlobal;
Data: Pointer;
Size: Cardinal;
begin
FoundResourceHandle := FindResource(HInstance, ResourceName, SectionName);
Assert(FoundResourceHandle <> 0, 'failed to load resource ' + SectionName + ':' + ResourceName);
LoadedResourceHandle := LoadResource(HInstance, FoundResourceHandle);
Assert(LoadedResourceHandle <> 0);
Data := LockResource(LoadedResourceHandle);
Size := SizeOfResource(HInstance, FoundResourceHandle);
Callback(Data, Size);
UnlockResource(LoadedResourceHandle);
FreeResource(LoadedResourceHandle);
end;
{$IFDEF ENABLETESTS}
{$IFOPT C+} {$ELSE} {$FATAL Can't run tests without assertion support} {$ENDIF}
{$RESOURCE tests/resutils.rc}
procedure TestRes();
procedure Check(const Data: Pointer; const Size: Cardinal);
var
S: AnsiString;
begin
SetLength(S, Size);
Move(Data^, S[1], Size);
Assert(S = 'This is test data.'#$0A);
end;
begin
ReadFromResources('testdata', 'resutils', @Check);
end;
{$ENDIF}
initialization
{$IFDEF ENABLETESTS} TestRes(); {$ENDIF}
end.
// if you need windres: sudo apt-get install mingw32-binutils
// make sure you have a symlink to /usr/bin/*windres* in your path as "windres"