]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
* Fixed two memory leaks.
[wxWidgets.git] / src / common / dynlib.cpp
CommitLineData
7a4b9130
GL
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
77ff2d26 25#ifdef __UNIX__
7a4b9130
GL
26#include <dlfcn.h>
27#endif
28
123a7fdd
GL
29#ifdef __WINDOWS__
30#include <windows.h>
31#endif
32
7a4b9130
GL
33// ---------------------------------------------------------------------------
34// Global variables
35// ---------------------------------------------------------------------------
36
37wxLibraries wxTheLibraries;
38
39// ---------------------------------------------------------------------------
40// wxLibrary (one instance per dynamic library
41// ---------------------------------------------------------------------------
42
43wxLibrary::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
54wxLibrary::~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
77ff2d26 66#ifdef __UNIX__
7a4b9130 67 dlclose(m_handle);
123a7fdd
GL
68#endif
69#ifdef __WINDOWS__
70 FreeLibrary((HMODULE)m_handle);
71#endif
7a4b9130
GL
72 }
73}
74
75wxObject *wxLibrary::CreateObject(const wxString& name)
76{
77 return m_liblist->CreateObject(name);
78}
79
80void *wxLibrary::GetSymbol(const wxString& symbname)
81{
77ff2d26 82#ifdef __UNIX__
123a7fdd 83 return dlsym(m_handle, WXSTRINGCAST symbname);
7a4b9130 84#endif
123a7fdd
GL
85#ifdef __WINDOWS__
86 return GetProcAddress(m_handle, WXSTRINGCAST symbname);
87#endif
88 return NULL;
7a4b9130
GL
89}
90
91// ---------------------------------------------------------------------------
92// wxLibraries (only one instance should normally exist)
93// ---------------------------------------------------------------------------
94
95wxLibraries::wxLibraries()
96{
97}
98
99wxLibraries::~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
111wxLibrary *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
77ff2d26 120#ifdef __UNIX__
1d44aaf8 121 lib_name.Prepend("./lib");
7a4b9130
GL
122 lib_name += ".so";
123
124 printf("lib_name = %s\n", WXSTRINGCAST lib_name);
125
123a7fdd 126 void *handle = dlopen(WXSTRINGCAST lib_name, RTLD_LAZY);
7a4b9130 127
123a7fdd
GL
128 if (!handle)
129 return NULL;
77ff2d26
RR
130#endif
131#ifdef __WINDOWS__
7a4b9130
GL
132 lib_name += ".dll";
133
123a7fdd
GL
134 HMODULE handle = LoadLibrary(lib_name);
135 if (!handle)
136 return NULL;
7a4b9130 137#endif
123a7fdd 138 lib = new wxLibrary((void *)handle);
7a4b9130
GL
139
140 m_loaded.Append(name.GetData(), lib);
141 return lib;
142}
143
144wxObject *wxLibraries::CreateObject(const wxString& path)
145{
146 wxNode *node = m_loaded.First();
147 wxObject *obj;
148
149 while (node) {
150 obj = ((wxLibrary *)node->Data())->CreateObject(path);
151 if (obj)
152 return obj;
153
154 node = node->Next();
155 }
156 return NULL;
157}
158
159// ---------------------------------------------------------------------------
160// wxClassLibrary (this class is used to access the internal class)
161// ---------------------------------------------------------------------------
162
163wxClassLibrary::wxClassLibrary(void)
164{
165}
166
167wxClassLibrary::~wxClassLibrary(void)
168{
c86f1403 169 size_t i;
7a4b9130
GL
170
171 for (i=0;i<m_list.Count();i++)
172 delete (m_list[i]);
173}
174
175void wxClassLibrary::RegisterClass(wxClassInfo *class_info,
176 const wxString& path)
177{
178 wxClassLibInfo *info = new wxClassLibInfo;
179
180 info->class_info = class_info;
181 info->path = path;
182 m_list.Add(info);
183}
184
185void wxClassLibrary::UnregisterClass(wxClassInfo *class_info)
186{
c86f1403 187 size_t i = 0;
7a4b9130
GL
188
189 while (i < m_list.Count()) {
190 if (m_list[i]->class_info == class_info) {
191 delete (m_list[i]);
192 m_list.Remove(i);
193 return;
194 }
195 i++;
196 }
197}
198
199bool wxClassLibrary::CreateObjects(const wxString& path,
200 wxArrayClassInfo& objs)
201{
202 wxClassLibInfo *info;
c86f1403 203 size_t i = 0;
7a4b9130
GL
204
205 while (i < m_list.Count()) {
206 info = m_list[i];
207 if (wxMatchWild(path, info->path))
208 objs.Add(info->class_info);
209 i++;
210 }
211 return (i > 0);
212}
213
214bool wxClassLibrary::FetchInfos(const wxString& path,
215 wxArrayClassLibInfo& infos)
216{
217 wxClassLibInfo *info;
c86f1403 218 size_t i = 0;
7a4b9130
GL
219
220 while (i < m_list.Count()) {
221 info = m_list[i];
222 if (wxMatchWild(path, info->path)) {
223 wxClassLibInfo *inf = new wxClassLibInfo;
224 *inf = *info;
225 infos.Add(inf);
226 }
227 i++;
228 }
229 return (i > 0);
230}
231
232wxObject *wxClassLibrary::CreateObject(const wxString& path)
233{
234 wxClassLibInfo *info;
c86f1403 235 size_t i = 0;
7a4b9130
GL
236
237 while (i < m_list.Count()) {
238 info = m_list[i];
239 if (wxMatchWild(path, info->path))
240 return info->class_info->CreateObject();
241 i++;
242 }
243 return NULL;
244}