test/testloadso.c
changeset 2067 dcdb175c2829
child 2068 f16a7d02a176
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testloadso.c	Tue Nov 07 14:36:47 2006 +0000
     1.3 @@ -0,0 +1,70 @@
     1.4 +
     1.5 +/* Test program to test dynamic loading with the loadso subsystem. 
     1.6 +*/
     1.7 +
     1.8 +#include <stdio.h>
     1.9 +#include <stdlib.h>
    1.10 +
    1.11 +#include "SDL.h"
    1.12 +
    1.13 +typedef int (*fntype)(const char *);
    1.14 +
    1.15 +int main(int argc, char *argv[])
    1.16 +{
    1.17 +	int retval = 0;
    1.18 +	int hello = 0;
    1.19 +	const char *libname = NULL;
    1.20 +	const char *symname = NULL;
    1.21 +	void *lib = NULL;
    1.22 +	fntype fn = NULL;
    1.23 +
    1.24 +	if (argc != 3) {
    1.25 +		fprintf(stderr, "USAGE: %s <library> <functionname>\n");
    1.26 +		fprintf(stderr, "       %s --hello <library with puts()>\n");
    1.27 +		return 1;
    1.28 +	}
    1.29 +
    1.30 +	/* Initialize SDL */
    1.31 +	if ( SDL_Init(0) < 0 ) {
    1.32 +		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    1.33 +		return 2;
    1.34 +	}
    1.35 +
    1.36 +	if (strcmp(argv[1], "--hello") == 0) {
    1.37 +		hello = 1;
    1.38 +		libname = argv[2];
    1.39 +		symname = "puts";
    1.40 +	} else {
    1.41 +		libname = argv[1];
    1.42 +		symname = argv[2];
    1.43 +	}
    1.44 +
    1.45 +	lib = SDL_LoadObject(libname);
    1.46 +        if (lib == NULL) {
    1.47 +		fprintf(stderr, "SDL_LoadObject('%s') failed: %s\n",
    1.48 +		        libname, SDL_GetError());
    1.49 +		retval = 3;
    1.50 +	} else {
    1.51 +		fn = (fntype) SDL_LoadFunction(lib, symname);
    1.52 +		if (fn == NULL) {
    1.53 +			fprintf(stderr, "SDL_LoadFunction('%s') failed: %s\n",
    1.54 +			        symname, SDL_GetError());
    1.55 +			retval = 4;
    1.56 +		} else {
    1.57 +			printf("Found %s in %s at %p\n", symname, libname);
    1.58 +			if (hello) {
    1.59 +				printf("Calling function...\n");
    1.60 +				fflush(stdout);
    1.61 +				fn("     HELLO, WORLD!\n");
    1.62 +				printf("...apparently, we survived.  :)\n");
    1.63 +				printf("Unloading library...\n");
    1.64 +				fflush(stdout);
    1.65 +			}
    1.66 +		}
    1.67 +		SDL_UnloadObject(lib);
    1.68 +	}
    1.69 +	SDL_Quit();
    1.70 +	return(0);
    1.71 +}
    1.72 +
    1.73 +