]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynlib.cpp
177e21a773d0efa0b6b8c3cb72edcae0ffbbae38
[wxWidgets.git] / src / common / dynlib.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dynlib.cpp
3 // Purpose: Dynamic library management
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 20/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "dynlib.h"
14 #endif
15
16 #include <wx/dynlib.h>
17 #include <wx/filefn.h>
18 #include <wx/list.h>
19 #include <wx/string.h>
20
21 // ---------------------------------------------------------------------------
22 // System dependent include
23 // ---------------------------------------------------------------------------
24
25 #if defined(__LINUX__) || defined(__SGI__)
26 #include <dlfcn.h>
27 #endif
28
29 #ifdef __WINDOWS__
30 #include <windows.h>
31 #endif
32
33 // ---------------------------------------------------------------------------
34 // Global variables
35 // ---------------------------------------------------------------------------
36
37 wxLibraries wxTheLibraries;
38
39 // ---------------------------------------------------------------------------
40 // wxLibrary (one instance per dynamic library
41 // ---------------------------------------------------------------------------
42
43 wxLibrary::wxLibrary(void *handle)
44 {
45 typedef wxClassLibrary *(*t_get_list)(void);
46 t_get_list get_list;
47
48 m_handle = handle;
49
50 get_list = (t_get_list)GetSymbol("GetClassList");
51 m_liblist = (*get_list)();
52 }
53
54 wxLibrary::~wxLibrary()
55 {
56 if (m_handle) {
57 typedef void (*t_free_list)(wxClassLibrary *);
58 t_free_list free_list;
59
60 free_list = (t_free_list) GetSymbol("FreeClassList");
61 if (free_list != NULL)
62 free_list(m_liblist);
63 else
64 delete m_liblist;
65
66 #if defined(__LINUX__) || defined(__SGI__)
67 dlclose(m_handle);
68 #endif
69 #ifdef __WINDOWS__
70 FreeLibrary((HMODULE)m_handle);
71 #endif
72 }
73 }
74
75 wxObject *wxLibrary::CreateObject(const wxString& name)
76 {
77 return m_liblist->CreateObject(name);
78 }
79
80 void *wxLibrary::GetSymbol(const wxString& symbname)
81 {
82 #if defined(__LINUX__) || defined(__SGI__)
83 return dlsym(m_handle, WXSTRINGCAST symbname);
84 #endif
85 #ifdef __WINDOWS__
86 return GetProcAddress(m_handle, WXSTRINGCAST symbname);
87 #endif
88 return NULL;
89 }
90
91 // ---------------------------------------------------------------------------
92 // wxLibraries (only one instance should normally exist)
93 // ---------------------------------------------------------------------------
94
95 wxLibraries::wxLibraries()
96 {
97 }
98
99 wxLibraries::~wxLibraries()
100 {
101 wxNode *node = m_loaded.First();
102
103 while (node) {
104 wxLibrary *lib = (wxLibrary *)node->Data();
105 delete lib;
106
107 node = node->Next();
108 }
109 }
110
111 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
112 {
113 wxString lib_name = name;
114 wxNode *node;
115 wxLibrary *lib;
116
117 if ( (node = m_loaded.Find(name.GetData())) )
118 return ((wxLibrary *)node->Data());
119
120 #if defined(__LINUX__) || defined(__SGI__)
121 lib_name.Prepend("lib");
122 lib_name += ".so";
123
124 printf("lib_name = %s\n", WXSTRINGCAST lib_name);
125
126 void *handle = dlopen(WXSTRINGCAST lib_name, RTLD_LAZY);
127
128 if (!handle)
129 return NULL;
130 #elif defined( __WINDOWS__ )
131 lib_name += ".dll";
132
133 HMODULE handle = LoadLibrary(lib_name);
134 if (!handle)
135 return NULL;
136 #else
137 return NULL;
138 #endif
139 #if defined(__LINUX__) || defined(__SGI__) || defined (__WINDOWS__)
140 lib = new wxLibrary((void *)handle);
141
142 m_loaded.Append(name.GetData(), lib);
143 return lib;
144 #endif
145 }
146
147 wxObject *wxLibraries::CreateObject(const wxString& path)
148 {
149 wxNode *node = m_loaded.First();
150 wxObject *obj;
151
152 while (node) {
153 obj = ((wxLibrary *)node->Data())->CreateObject(path);
154 if (obj)
155 return obj;
156
157 node = node->Next();
158 }
159 return NULL;
160 }
161
162 // ---------------------------------------------------------------------------
163 // wxClassLibrary (this class is used to access the internal class)
164 // ---------------------------------------------------------------------------
165
166 wxClassLibrary::wxClassLibrary(void)
167 {
168 }
169
170 wxClassLibrary::~wxClassLibrary(void)
171 {
172 uint i;
173
174 for (i=0;i<m_list.Count();i++)
175 delete (m_list[i]);
176 }
177
178 void wxClassLibrary::RegisterClass(wxClassInfo *class_info,
179 const wxString& path)
180 {
181 wxClassLibInfo *info = new wxClassLibInfo;
182
183 info->class_info = class_info;
184 info->path = path;
185 m_list.Add(info);
186 }
187
188 void wxClassLibrary::UnregisterClass(wxClassInfo *class_info)
189 {
190 uint i = 0;
191
192 while (i < m_list.Count()) {
193 if (m_list[i]->class_info == class_info) {
194 delete (m_list[i]);
195 m_list.Remove(i);
196 return;
197 }
198 i++;
199 }
200 }
201
202 bool wxClassLibrary::CreateObjects(const wxString& path,
203 wxArrayClassInfo& objs)
204 {
205 wxClassLibInfo *info;
206 uint i = 0;
207
208 while (i < m_list.Count()) {
209 info = m_list[i];
210 if (wxMatchWild(path, info->path))
211 objs.Add(info->class_info);
212 i++;
213 }
214 return (i > 0);
215 }
216
217 bool wxClassLibrary::FetchInfos(const wxString& path,
218 wxArrayClassLibInfo& infos)
219 {
220 wxClassLibInfo *info;
221 uint i = 0;
222
223 while (i < m_list.Count()) {
224 info = m_list[i];
225 if (wxMatchWild(path, info->path)) {
226 wxClassLibInfo *inf = new wxClassLibInfo;
227 *inf = *info;
228 infos.Add(inf);
229 }
230 i++;
231 }
232 return (i > 0);
233 }
234
235 wxObject *wxClassLibrary::CreateObject(const wxString& path)
236 {
237 wxClassLibInfo *info;
238 uint i = 0;
239
240 while (i < m_list.Count()) {
241 info = m_list[i];
242 if (wxMatchWild(path, info->path))
243 return info->class_info->CreateObject();
244 i++;
245 }
246 return NULL;
247 }