src/SDL_loadso.c
changeset 294 d2d48e10f370
child 296 fab1ddc4d7bf
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/SDL_loadso.c	Wed Mar 06 05:20:11 2002 +0000
     1.3 @@ -0,0 +1,193 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    1.32 +/* System dependent library loading routines                           */
    1.33 +
    1.34 +#include <stdio.h>
    1.35 +#if defined(USE_DLOPEN)
    1.36 +# include <dlfcn.h>
    1.37 +#elif defined(WIN32)
    1.38 +# include <windows.h>
    1.39 +#elif defined(__BEOS__)
    1.40 +# include <be/kernel/image.h>
    1.41 +#elif defined(macintosh)
    1.42 +# include <string.h>
    1.43 +# include <Strings.h>
    1.44 +# include <CodeFragments.h>
    1.45 +# include <Errors.h>
    1.46 +#else
    1.47 +/*#error Unsupported dynamic link environment*/
    1.48 +#endif /* system type */
    1.49 +
    1.50 +#include "SDL_error.h"
    1.51 +#include "SDL_loadso.h"
    1.52 +
    1.53 +void *SDL_LoadObject(const char *sofile)
    1.54 +{
    1.55 +	void *handle = NULL;
    1.56 +	const char *loaderror = "SDL_LoadObject() not implemented";
    1.57 +#if defined(USE_DLOPEN)
    1.58 +/* * */
    1.59 +	handle = dlopen(sofile, RTLD_NOW);
    1.60 +	loaderror = (char *)dlerror();
    1.61 +#elif defined(WIN32)
    1.62 +/* * */
    1.63 +	char errbuf[512];
    1.64 +
    1.65 +	handle = (void *)LoadLibrary(sofile);
    1.66 +
    1.67 +	/* Generate an error message if all loads failed */
    1.68 +	if ( handle == NULL ) {
    1.69 +		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
    1.70 +					FORMAT_MESSAGE_FROM_SYSTEM),
    1.71 +				NULL, GetLastError(), 
    1.72 +				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    1.73 +				errbuf, SDL_TABLESIZE(errbuf), NULL);
    1.74 +		loaderror = errbuf;
    1.75 +	}
    1.76 +#elif defined(__BEOS__)
    1.77 +/* * */
    1.78 +	image_id library_id;
    1.79 +
    1.80 +	library_id = load_add_on(sofile);
    1.81 +	if ( library_id == B_ERROR ) {
    1.82 +		loaderror = "BeOS error";
    1.83 +	} else {
    1.84 +		handle = (void *)(library_id);
    1.85 +	}
    1.86 +#elif defined(macintosh)
    1.87 +/* * */
    1.88 +	CFragConnectionID library_id;
    1.89 +	Ptr mainAddr;
    1.90 +	Str255 errName;
    1.91 +	OSErr error;
    1.92 +	char psofile[512];
    1.93 +
    1.94 +	strncpy(psofile, sofile, SDL_TABLESIZE(psofile));
    1.95 +	psofile[SDL_TABLESIZE(psofile)-1] = '\0';
    1.96 +	error = GetSharedLibrary(C2PStr(psofile), kCompiledCFragArch,
    1.97 +			kLoadCFrag, &library_id, &mainAddr, errName);
    1.98 +	switch (error) {
    1.99 +		case noErr:
   1.100 +			break;
   1.101 +		case cfragNoLibraryErr:
   1.102 +			loaderror = "Library not found";
   1.103 +			break;
   1.104 +		case cfragUnresolvedErr:
   1.105 +			loaderror = "Unabled to resolve symbols";
   1.106 +			break;
   1.107 +		case cfragNoPrivateMemErr:
   1.108 +		case cfragNoClientMemErr:
   1.109 +			loaderror = "Out of memory";
   1.110 +			break;
   1.111 +		default:
   1.112 +			loaderror = "Unknown Code Fragment Manager error";
   1.113 +			break;
   1.114 +	}
   1.115 +	if ( loaderror == NULL ) {
   1.116 +		handle = (void *)(library_id);
   1.117 +	}
   1.118 +#endif /* system type */
   1.119 +
   1.120 +	if ( handle == NULL ) {
   1.121 +		SDL_SetError("Failed loading %s: %s", sofile, loaderror);
   1.122 +	}
   1.123 +	return(handle);
   1.124 +}
   1.125 +
   1.126 +void *SDL_LoadFunction(void *handle, const char *name)
   1.127 +{
   1.128 +	void *symbol = NULL;
   1.129 +	const char *loaderror = "SDL_LoadFunction not implemented";
   1.130 +#if defined(USE_DLOPEN)
   1.131 +/* * */
   1.132 +	symbol = dlsym(handle, name);
   1.133 +	if ( symbol == NULL ) {
   1.134 +		loaderror = (char *)dlerror();
   1.135 +	}
   1.136 +#elif defined(WIN32)
   1.137 +/* * */
   1.138 +	char errbuf[512];
   1.139 +
   1.140 +	symbol = (void *)GetProcAddress((HMODULE)handle, name);
   1.141 +	if ( symbol == NULL ) {
   1.142 +		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
   1.143 +					FORMAT_MESSAGE_FROM_SYSTEM),
   1.144 +				NULL, GetLastError(), 
   1.145 +				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   1.146 +				errbuf, SDL_TABLESIZE(errbuf), NULL);
   1.147 +		loaderror = errbuf;
   1.148 +	}
   1.149 +#elif defined(__BEOS__)
   1.150 +/* * */
   1.151 +	image_id library_id = (image_id)handle;
   1.152 +	if ( get_image_symbol(library_id,
   1.153 +		name, B_SYMBOL_TYPE_TEXT, &symbol) != B_NO_ERROR ) {
   1.154 +		loaderror = "Symbol not found";
   1.155 +	}
   1.156 +#elif defined(macintosh)
   1.157 +/* * */
   1.158 +	CFragSymbolClass class;
   1.159 +	CFragConnectionID library_id = (CFragConnectionID)handle;
   1.160 +	char pname[512];
   1.161 +
   1.162 +	strncpy(pname, name, SDL_TABLESIZE(pname));
   1.163 +	pname[SDL_TABLESIZE(pname)-1] = '\0';
   1.164 +	if ( FindSymbol(library_id, C2PStr(pname),
   1.165 +	                (char **)&symbol, &class) != noErr ) {
   1.166 +		loaderror = "Symbol not found";
   1.167 +	}
   1.168 +#endif /* system type */
   1.169 +
   1.170 +	if ( symbol == NULL ) {
   1.171 +		SDL_SetError("Failed loading %s: %s", name, loaderror);
   1.172 +	}
   1.173 +	return(symbol);
   1.174 +}
   1.175 +
   1.176 +void SDL_UnloadObject(void *handle)
   1.177 +{
   1.178 +	if ( handle == NULL ) {
   1.179 +		return;
   1.180 +	}
   1.181 +#if defined(USE_DLOPEN)
   1.182 +/* * */
   1.183 +	dlclose(handle);
   1.184 +#elif defined(WIN32)
   1.185 +/* * */
   1.186 +	FreeLibrary((HMODULE)handle);
   1.187 +#elif defined(__BEOS__)
   1.188 +/* * */
   1.189 +	image_id library_id = (image_id)handle;
   1.190 +	unload_add_on(library_id);
   1.191 +#elif defined(macintosh)
   1.192 +/* * */
   1.193 +	CFragConnectionID library_id = (CFragConnectionID)handle;
   1.194 +	CloseConnection(library_id);
   1.195 +#endif /* system type */
   1.196 +}