Fixed bugs introduced by 'register' and 'return' function changes.
[wine] / loader / libres.c
1 /*
2  * WINElib-Resources
3  *
4  * Copied and modified heavily from loader/resource.c
5  */
6
7 #include <stdlib.h>
8 #include "wine/winestring.h"
9 #include "libres.h"
10 #include "resource.h"
11 #include "debugtools.h"
12 #include "heap.h"
13 #include "xmalloc.h"
14
15 DEFAULT_DEBUG_CHANNEL(resource)
16
17 typedef struct RLE
18 {
19     const wrc_resource32_t * const * Resources;  /* NULL-terminated array of pointers */
20     struct RLE* next;
21 } ResListE;
22
23 static ResListE* ResourceList=NULL;
24
25 void LIBRES_RegisterResources(const wrc_resource32_t * const * Res)
26 {
27   ResListE** Curr;
28   ResListE* n;
29   for(Curr=&ResourceList; *Curr; Curr=&((*Curr)->next)) { }
30   n=xmalloc(sizeof(ResListE));
31   n->Resources=Res;
32   n->next=NULL;
33   *Curr=n;
34 }
35
36 /**********************************************************************
37  *          LIBRES_FindResource
38  */
39 HRSRC LIBRES_FindResource( HINSTANCE hModule, LPCWSTR name, LPCWSTR type )
40 {
41   int nameid=0,typeid;
42   ResListE* ResBlock;
43   const wrc_resource32_t* const * Res;
44
45   if(HIWORD(name))
46   {
47     if(*name=='#')
48     {
49         LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
50         nameid = atoi(nameA+1);
51         HeapFree( GetProcessHeap(), 0, nameA );
52         name=NULL;
53     }
54   }
55   else
56   {
57     nameid=LOWORD(name);
58     name=NULL;
59   }
60   if(HIWORD(type))
61   {
62     if(*type=='#')
63     {
64         LPSTR typeA = HEAP_strdupWtoA( GetProcessHeap(), 0, type );
65         typeid=atoi(typeA+1);
66         HeapFree( GetProcessHeap(), 0, typeA );
67     }
68     else
69     {
70       TRACE("(*,*,type=string): Returning 0\n");
71       return 0;
72     }
73   }
74   else
75     typeid=LOWORD(type);
76   
77   /* FIXME: types can be strings */
78   for(ResBlock=ResourceList; ResBlock; ResBlock=ResBlock->next)
79     for(Res=ResBlock->Resources; *Res; Res++)
80       if(name)
81       {
82         if((*Res)->restype==typeid && !lstrncmpiW((LPCWSTR)((*Res)->resname+1), name, *((*Res)->resname)))
83           return (HRSRC)*Res;
84       }
85       else
86         if((*Res)->restype==typeid && (*Res)->resid==nameid)
87           return (HRSRC)*Res;
88   return 0;
89 }
90
91
92 /**********************************************************************
93  *          LIBRES_LoadResource    
94  */
95 HGLOBAL LIBRES_LoadResource( HINSTANCE hModule, HRSRC hRsrc )
96 {
97   return (HGLOBAL)(((wrc_resource32_t*)hRsrc)->data);
98 }
99
100
101 /**********************************************************************
102  *          LIBRES_SizeofResource    
103  */
104 DWORD LIBRES_SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
105 {
106   return (DWORD)(((wrc_resource32_t*)hRsrc)->datasize);
107 }