src/loadso/macos/SDL_loadso.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 17 Nov 2005 03:15:05 +0000
changeset 1173 e9cf8c1b4590
child 1312 c9b51268668f
permissions -rw-r--r--
Split up src/SDL_loadso.c into platform directories.
icculus@1173
     1
/*
icculus@1173
     2
    SDL - Simple DirectMedia Layer
icculus@1173
     3
    Copyright (C) 1997-2004 Sam Lantinga
icculus@1173
     4
icculus@1173
     5
    This library is free software; you can redistribute it and/or
icculus@1173
     6
    modify it under the terms of the GNU Library General Public
icculus@1173
     7
    License as published by the Free Software Foundation; either
icculus@1173
     8
    version 2 of the License, or (at your option) any later version.
icculus@1173
     9
icculus@1173
    10
    This library is distributed in the hope that it will be useful,
icculus@1173
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1173
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
icculus@1173
    13
    Library General Public License for more details.
icculus@1173
    14
icculus@1173
    15
    You should have received a copy of the GNU Library General Public
icculus@1173
    16
    License along with this library; if not, write to the Free
icculus@1173
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
icculus@1173
    18
icculus@1173
    19
    Sam Lantinga
icculus@1173
    20
    slouken@libsdl.org
icculus@1173
    21
*/
icculus@1173
    22
icculus@1173
    23
#ifdef SAVE_RCSID
icculus@1173
    24
static char rcsid =
icculus@1173
    25
 "@(#) $Id$";
icculus@1173
    26
#endif
icculus@1173
    27
icculus@1173
    28
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
icculus@1173
    29
/* System dependent library loading routines                           */
icculus@1173
    30
icculus@1173
    31
#if !SDL_INTERNAL_BUILDING_LOADSO
icculus@1173
    32
#error Do not compile directly...compile src/SDL_loadso.c instead!
icculus@1173
    33
#endif
icculus@1173
    34
icculus@1173
    35
#if !defined(macintosh)
icculus@1173
    36
#error Compiling for the wrong platform?
icculus@1173
    37
#endif
icculus@1173
    38
icculus@1173
    39
#include <stdio.h>
icculus@1173
    40
#include <string.h>
icculus@1173
    41
#define OLDP2C 1
icculus@1173
    42
#include <Strings.h>
icculus@1173
    43
#include <CodeFragments.h>
icculus@1173
    44
#include <Errors.h>
icculus@1173
    45
icculus@1173
    46
#include "SDL_types.h"
icculus@1173
    47
#include "SDL_error.h"
icculus@1173
    48
#include "SDL_loadso.h"
icculus@1173
    49
icculus@1173
    50
void *SDL_LoadObject(const char *sofile)
icculus@1173
    51
{
icculus@1173
    52
	void *handle = NULL;
icculus@1173
    53
	const char *loaderror = NULL;
icculus@1173
    54
	CFragConnectionID library_id;
icculus@1173
    55
	Ptr mainAddr;
icculus@1173
    56
	Str255 errName;
icculus@1173
    57
	OSErr error;
icculus@1173
    58
	char psofile[512];
icculus@1173
    59
icculus@1173
    60
	strncpy(psofile, sofile, SDL_TABLESIZE(psofile));
icculus@1173
    61
	psofile[SDL_TABLESIZE(psofile)-1] = '\0';
icculus@1173
    62
	error = GetSharedLibrary(C2PStr(psofile), kCompiledCFragArch,
icculus@1173
    63
			kLoadCFrag, &library_id, &mainAddr, errName);
icculus@1173
    64
	switch (error) {
icculus@1173
    65
		case noErr:
icculus@1173
    66
			loaderror = NULL;
icculus@1173
    67
			break;
icculus@1173
    68
		case cfragNoLibraryErr:
icculus@1173
    69
			loaderror = "Library not found";
icculus@1173
    70
			break;
icculus@1173
    71
		case cfragUnresolvedErr:
icculus@1173
    72
			loaderror = "Unabled to resolve symbols";
icculus@1173
    73
			break;
icculus@1173
    74
		case cfragNoPrivateMemErr:
icculus@1173
    75
		case cfragNoClientMemErr:
icculus@1173
    76
			loaderror = "Out of memory";
icculus@1173
    77
			break;
icculus@1173
    78
		default:
icculus@1173
    79
			loaderror = "Unknown Code Fragment Manager error";
icculus@1173
    80
			break;
icculus@1173
    81
	}
icculus@1173
    82
	if ( loaderror == NULL ) {
icculus@1173
    83
		handle = (void *)(library_id);
icculus@1173
    84
	} else {
icculus@1173
    85
		SDL_SetError("Failed loading %s: %s", sofile, loaderror);
icculus@1173
    86
	}
icculus@1173
    87
	return(handle);
icculus@1173
    88
}
icculus@1173
    89
icculus@1173
    90
void *SDL_LoadFunction(void *handle, const char *name)
icculus@1173
    91
{
icculus@1173
    92
	void *symbol = NULL;
icculus@1173
    93
	const char *loaderror = NULL;
icculus@1173
    94
	CFragSymbolClass class;
icculus@1173
    95
	CFragConnectionID library_id = (CFragConnectionID)handle;
icculus@1173
    96
	char pname[512];
icculus@1173
    97
icculus@1173
    98
	strncpy(pname, name, SDL_TABLESIZE(pname));
icculus@1173
    99
	pname[SDL_TABLESIZE(pname)-1] = '\0';
icculus@1173
   100
	if ( FindSymbol(library_id, C2PStr(pname),
icculus@1173
   101
	                (char **)&symbol, &class) != noErr ) {
icculus@1173
   102
		loaderror = "Symbol not found";
icculus@1173
   103
	}
icculus@1173
   104
icculus@1173
   105
	if ( symbol == NULL ) {
icculus@1173
   106
		SDL_SetError("Failed loading %s: %s", name, loaderror);
icculus@1173
   107
	}
icculus@1173
   108
	return(symbol);
icculus@1173
   109
}
icculus@1173
   110
icculus@1173
   111
void SDL_UnloadObject(void *handle)
icculus@1173
   112
{
icculus@1173
   113
	CFragConnectionID library_id;
icculus@1173
   114
	if ( handle != NULL ) {
icculus@1173
   115
		library_id = (CFragConnectionID)handle;
icculus@1173
   116
		CloseConnection(&library_id);
icculus@1173
   117
	}
icculus@1173
   118
}
icculus@1173
   119