]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynlib.cpp
HP-UX support added
[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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "dynlib.h"
22 #endif
23
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif //__BORLANDC__
29
30 // TODO should be done by configure
31 #if defined(__UNIX__) && !(defined(HAVE_DLOPEN) || defined(HAVE_SHLLOAD))
32 #if defined(__LINUX__) || defined(__SOLARIS__) || defined(__SUNOS__)
33 #ifndef HAVE_DLOPEN
34 #define HAVE_DLOPEN
35 #endif
36 #elif defined(__HPUX__)
37 #ifndef HAVE_SHLLOAD
38 #define HAVE_SHLLOAD
39 #endif
40 #endif // Unix flavour
41 #endif // !Unix or already have some HAVE_xxx defined
42
43 #include "wx/dynlib.h"
44 #include "wx/filefn.h"
45 #include "wx/intl.h"
46 #include "wx/log.h"
47
48 // ----------------------------------------------------------------------------
49 // conditional compilation
50 // ----------------------------------------------------------------------------
51
52 #if defined(HAVE_DLOPEN)
53 #define wxDllOpen(lib) dlopen(lib, RTLD_LAZY)
54 #define wxDllGetSymbol(handle, name) dlsym(handle, (char *)name)
55 #define wxDllClose dlclose
56 #elif defined(HAVE_SHLLOAD)
57 #define wxDllOpen(lib) shl_open(lib, BIND_DEFERRED, 0)
58 #define wxDllClose shl_unload
59
60 static inline void *wxDllGetSymbol(shl_t *handle, const char *name)
61 {
62 void *sym;
63 if ( shl_findsym(handle, name, TYPE_UNDEFINED, &sym) == 0 )
64 return sym;
65 else
66 return (void *)0;
67 }
68 #elif defined(__WINDOWS__)
69 #define wxDllOpen(lib) ::LoadLibrary(lib)
70 #define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
71 #define wxDllClose ::FreeLibrary
72 #else
73 #error "Don't know how to load shared libraries on this platform."
74 #endif // OS
75
76 // ---------------------------------------------------------------------------
77 // Global variables
78 // ---------------------------------------------------------------------------
79
80 wxLibraries wxTheLibraries;
81
82 // ----------------------------------------------------------------------------
83 // private functions
84 // ----------------------------------------------------------------------------
85
86 // construct the full name from the base shared object name: adds a .dll
87 // suffix under Windows or .so under Unix
88 static wxString ConstructLibraryName(const wxString& basename)
89 {
90 wxString fullname(basename);
91
92 #if defined(__UNIX__)
93 fullname << ".so";
94 #elif defined(__WINDOWS__)
95 fullname << ".dll";
96 #endif
97
98 return fullname;
99 }
100
101 // ============================================================================
102 // implementation
103 // ============================================================================
104
105 // ---------------------------------------------------------------------------
106 // wxLibrary (one instance per dynamic library)
107 // ---------------------------------------------------------------------------
108
109 wxLibrary::wxLibrary(wxDllType handle)
110 {
111 typedef wxClassInfo *(*t_get_first)(void);
112 t_get_first get_first;
113
114 m_handle = handle;
115
116 // Some system may use a local heap for library.
117 get_first = (t_get_first)GetSymbol("wxGetClassFirst");
118 // It is a wxWindows DLL.
119 if (get_first)
120 PrepareClasses(get_first());
121 }
122
123 wxLibrary::~wxLibrary()
124 {
125 if ( m_handle )
126 {
127 wxDllClose(m_handle);
128 }
129 }
130
131 wxObject *wxLibrary::CreateObject(const wxString& name)
132 {
133 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
134
135 if (!info)
136 return NULL;
137
138 return info->CreateObject();
139 }
140
141 void wxLibrary::PrepareClasses(wxClassInfo *first)
142 {
143 // Index all class infos by their class name
144 wxClassInfo *info = first;
145 while (info)
146 {
147 if (info->m_className)
148 classTable.Put(info->m_className, (wxObject *)info);
149 info = info->GetNext();
150 }
151
152 // Set base pointers for each wxClassInfo
153 info = first;
154 while (info)
155 {
156 if (info->GetBaseClassName1())
157 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
158 if (info->GetBaseClassName2())
159 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
160 info = info->m_next;
161 }
162 }
163
164 void *wxLibrary::GetSymbol(const wxString& symbname)
165 {
166 void *symbol = NULL; // return value
167
168 #if defined( __WXMAC__ )
169 Ptr symAddress ;
170 CFragSymbolClass symClass ;
171 Str255 symName ;
172
173 strcpy( (char*) symName , symbname ) ;
174 c2pstr( (char*) symName ) ;
175
176 if ( FindSymbol( m_handle , symName , &symAddress , &symClass ) == noErr )
177 {
178 symbol = (void *)symAddress ;
179 }
180 #else
181 // VZ: hmm... why is WXSTRINGCAST needed? if it's really modified, we
182 // should make a copy of it
183 symbol = wxDllGetSymbol(m_handle, WXSTRINGCAST symbname);
184 #endif
185
186 if ( !symbol )
187 {
188 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
189 symbname.c_str());
190 }
191
192 return symbol;
193 }
194
195 // ---------------------------------------------------------------------------
196 // wxLibraries (only one instance should normally exist)
197 // ---------------------------------------------------------------------------
198
199 wxLibraries::wxLibraries()
200 {
201 }
202
203 wxLibraries::~wxLibraries()
204 {
205 wxNode *node = m_loaded.First();
206
207 while (node) {
208 wxLibrary *lib = (wxLibrary *)node->Data();
209 delete lib;
210
211 node = node->Next();
212 }
213 }
214
215 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
216 {
217 wxNode *node;
218 wxLibrary *lib;
219 wxClassInfo *old_sm_first;
220
221 if ( (node = m_loaded.Find(name.GetData())) )
222 return ((wxLibrary *)node->Data());
223
224 // If DLL shares data, this is necessary.
225 old_sm_first = wxClassInfo::sm_first;
226 wxClassInfo::sm_first = NULL;
227
228 wxString lib_name = ConstructLibraryName(name);
229
230 #if defined(__UNIX__)
231 // TODO use LD_LIBRARY_PATH!
232 lib_name.Prepend("/lib");
233 #endif // __UNIX__
234
235 wxDllType handle ;
236
237 #if defined(__WXMAC__)
238 FSSpec myFSSpec ;
239 Ptr myMainAddr ;
240 Str255 myErrName ;
241
242 wxMacPathToFSSpec( lib_name , &myFSSpec ) ;
243 if (GetDiskFragment( &myFSSpec , 0 , kCFragGoesToEOF , "\p" , kPrivateCFragCopy , &handle , &myMainAddr ,
244 myErrName ) != noErr )
245 {
246 p2cstr( myErrName ) ;
247 wxASSERT_MSG( 1 , (char*)myErrName ) ;
248 return NULL ;
249 }
250 #else // !Mac
251 handle = wxDllOpen(lib_name);
252 #endif // OS
253
254 if ( !handle )
255 {
256 wxLogSysError(_("Failed to load shared library '%s'"),
257 lib_name.c_str());
258
259 return NULL;
260 }
261
262 lib = new wxLibrary(handle);
263
264 wxClassInfo::sm_first = old_sm_first;
265
266 m_loaded.Append(name.GetData(), lib);
267
268 return lib;
269 }
270
271 wxObject *wxLibraries::CreateObject(const wxString& path)
272 {
273 wxNode *node = m_loaded.First();
274 wxObject *obj;
275
276 while (node) {
277 obj = ((wxLibrary *)node->Data())->CreateObject(path);
278 if (obj)
279 return obj;
280
281 node = node->Next();
282 }
283 return NULL;
284 }