]>
Commit | Line | Data |
---|---|---|
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 | ||
25 | #ifdef linux | |
26 | #include <dlfcn.h> | |
27 | #endif | |
28 | ||
29 | // --------------------------------------------------------------------------- | |
30 | // Global variables | |
31 | // --------------------------------------------------------------------------- | |
32 | ||
33 | wxLibraries wxTheLibraries; | |
34 | ||
35 | // --------------------------------------------------------------------------- | |
36 | // wxLibrary (one instance per dynamic library | |
37 | // --------------------------------------------------------------------------- | |
38 | ||
39 | wxLibrary::wxLibrary(void *handle) | |
40 | { | |
41 | typedef wxClassLibrary *(*t_get_list)(void); | |
42 | t_get_list get_list; | |
43 | ||
44 | m_handle = handle; | |
45 | ||
46 | get_list = (t_get_list)GetSymbol("GetClassList"); | |
47 | m_liblist = (*get_list)(); | |
48 | } | |
49 | ||
50 | wxLibrary::~wxLibrary() | |
51 | { | |
52 | if (m_handle) { | |
53 | typedef void (*t_free_list)(wxClassLibrary *); | |
54 | t_free_list free_list; | |
55 | ||
56 | free_list = (t_free_list) GetSymbol("FreeClassList"); | |
57 | if (free_list != NULL) | |
58 | free_list(m_liblist); | |
59 | else | |
60 | delete m_liblist; | |
61 | ||
62 | dlclose(m_handle); | |
63 | } | |
64 | } | |
65 | ||
66 | wxObject *wxLibrary::CreateObject(const wxString& name) | |
67 | { | |
68 | return m_liblist->CreateObject(name); | |
69 | } | |
70 | ||
71 | void *wxLibrary::GetSymbol(const wxString& symbname) | |
72 | { | |
73 | #ifdef linux | |
74 | return dlsym(m_handle, symbname.GetData()); | |
75 | #endif | |
76 | } | |
77 | ||
78 | // --------------------------------------------------------------------------- | |
79 | // wxLibraries (only one instance should normally exist) | |
80 | // --------------------------------------------------------------------------- | |
81 | ||
82 | wxLibraries::wxLibraries() | |
83 | { | |
84 | } | |
85 | ||
86 | wxLibraries::~wxLibraries() | |
87 | { | |
88 | wxNode *node = m_loaded.First(); | |
89 | ||
90 | while (node) { | |
91 | wxLibrary *lib = (wxLibrary *)node->Data(); | |
92 | delete lib; | |
93 | ||
94 | node = node->Next(); | |
95 | } | |
96 | } | |
97 | ||
98 | wxLibrary *wxLibraries::LoadLibrary(const wxString& name) | |
99 | { | |
100 | wxString lib_name = name; | |
101 | wxNode *node; | |
102 | wxLibrary *lib; | |
103 | ||
104 | if ( (node = m_loaded.Find(name.GetData())) ) | |
105 | return ((wxLibrary *)node->Data()); | |
106 | ||
107 | #ifdef linux | |
108 | lib_name.Prepend("./lib"); | |
109 | lib_name += ".so"; | |
110 | ||
111 | printf("lib_name = %s\n", WXSTRINGCAST lib_name); | |
112 | ||
113 | void *handle = dlopen(lib_name.GetData(), RTLD_LAZY); | |
114 | ||
115 | printf("handle = %x\n", handle); | |
116 | lib = new wxLibrary(handle); | |
117 | ||
118 | #endif | |
119 | #ifdef __WINDOWS__ | |
120 | lib_name += ".dll"; | |
121 | ||
122 | #endif | |
123 | ||
124 | m_loaded.Append(name.GetData(), lib); | |
125 | return lib; | |
126 | } | |
127 | ||
128 | wxObject *wxLibraries::CreateObject(const wxString& path) | |
129 | { | |
130 | wxNode *node = m_loaded.First(); | |
131 | wxObject *obj; | |
132 | ||
133 | while (node) { | |
134 | obj = ((wxLibrary *)node->Data())->CreateObject(path); | |
135 | if (obj) | |
136 | return obj; | |
137 | ||
138 | node = node->Next(); | |
139 | } | |
140 | return NULL; | |
141 | } | |
142 | ||
143 | // --------------------------------------------------------------------------- | |
144 | // wxClassLibrary (this class is used to access the internal class) | |
145 | // --------------------------------------------------------------------------- | |
146 | ||
147 | wxClassLibrary::wxClassLibrary(void) | |
148 | { | |
149 | } | |
150 | ||
151 | wxClassLibrary::~wxClassLibrary(void) | |
152 | { | |
153 | uint i; | |
154 | ||
155 | for (i=0;i<m_list.Count();i++) | |
156 | delete (m_list[i]); | |
157 | } | |
158 | ||
159 | void wxClassLibrary::RegisterClass(wxClassInfo *class_info, | |
160 | const wxString& path) | |
161 | { | |
162 | wxClassLibInfo *info = new wxClassLibInfo; | |
163 | ||
164 | info->class_info = class_info; | |
165 | info->path = path; | |
166 | m_list.Add(info); | |
167 | } | |
168 | ||
169 | void wxClassLibrary::UnregisterClass(wxClassInfo *class_info) | |
170 | { | |
171 | uint i = 0; | |
172 | ||
173 | while (i < m_list.Count()) { | |
174 | if (m_list[i]->class_info == class_info) { | |
175 | delete (m_list[i]); | |
176 | m_list.Remove(i); | |
177 | return; | |
178 | } | |
179 | i++; | |
180 | } | |
181 | } | |
182 | ||
183 | bool wxClassLibrary::CreateObjects(const wxString& path, | |
184 | wxArrayClassInfo& objs) | |
185 | { | |
186 | wxClassLibInfo *info; | |
187 | uint i = 0; | |
188 | ||
189 | while (i < m_list.Count()) { | |
190 | info = m_list[i]; | |
191 | if (wxMatchWild(path, info->path)) | |
192 | objs.Add(info->class_info); | |
193 | i++; | |
194 | } | |
195 | return (i > 0); | |
196 | } | |
197 | ||
198 | bool wxClassLibrary::FetchInfos(const wxString& path, | |
199 | wxArrayClassLibInfo& infos) | |
200 | { | |
201 | wxClassLibInfo *info; | |
202 | uint i = 0; | |
203 | ||
204 | while (i < m_list.Count()) { | |
205 | info = m_list[i]; | |
206 | if (wxMatchWild(path, info->path)) { | |
207 | wxClassLibInfo *inf = new wxClassLibInfo; | |
208 | *inf = *info; | |
209 | infos.Add(inf); | |
210 | } | |
211 | i++; | |
212 | } | |
213 | return (i > 0); | |
214 | } | |
215 | ||
216 | wxObject *wxClassLibrary::CreateObject(const wxString& path) | |
217 | { | |
218 | wxClassLibInfo *info; | |
219 | uint i = 0; | |
220 | ||
221 | while (i < m_list.Count()) { | |
222 | info = m_list[i]; | |
223 | if (wxMatchWild(path, info->path)) | |
224 | return info->class_info->CreateObject(); | |
225 | i++; | |
226 | } | |
227 | return NULL; | |
228 | } |