test/testloadso.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 07 Nov 2006 14:31:25 +0000
branchSDL-1.2
changeset 3892 b25dd008167b
child 2068 f16a7d02a176
permissions -rw-r--r--
Added a test program for loadso subsystem.
icculus@3892
     1
icculus@3892
     2
/* Test program to test dynamic loading with the loadso subsystem. 
icculus@3892
     3
*/
icculus@3892
     4
icculus@3892
     5
#include <stdio.h>
icculus@3892
     6
#include <stdlib.h>
icculus@3892
     7
icculus@3892
     8
#include "SDL.h"
icculus@3892
     9
icculus@3892
    10
typedef int (*fntype)(const char *);
icculus@3892
    11
icculus@3892
    12
int main(int argc, char *argv[])
icculus@3892
    13
{
icculus@3892
    14
	int retval = 0;
icculus@3892
    15
	int hello = 0;
icculus@3892
    16
	const char *libname = NULL;
icculus@3892
    17
	const char *symname = NULL;
icculus@3892
    18
	void *lib = NULL;
icculus@3892
    19
	fntype fn = NULL;
icculus@3892
    20
icculus@3892
    21
	if (argc != 3) {
icculus@3892
    22
		fprintf(stderr, "USAGE: %s <library> <functionname>\n");
icculus@3892
    23
		fprintf(stderr, "       %s --hello <library with puts()>\n");
icculus@3892
    24
		return 1;
icculus@3892
    25
	}
icculus@3892
    26
icculus@3892
    27
	/* Initialize SDL */
icculus@3892
    28
	if ( SDL_Init(0) < 0 ) {
icculus@3892
    29
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@3892
    30
		return 2;
icculus@3892
    31
	}
icculus@3892
    32
icculus@3892
    33
	if (strcmp(argv[1], "--hello") == 0) {
icculus@3892
    34
		hello = 1;
icculus@3892
    35
		libname = argv[2];
icculus@3892
    36
		symname = "puts";
icculus@3892
    37
	} else {
icculus@3892
    38
		libname = argv[1];
icculus@3892
    39
		symname = argv[2];
icculus@3892
    40
	}
icculus@3892
    41
icculus@3892
    42
	lib = SDL_LoadObject(libname);
icculus@3892
    43
        if (lib == NULL) {
icculus@3892
    44
		fprintf(stderr, "SDL_LoadObject('%s') failed: %s\n",
icculus@3892
    45
		        libname, SDL_GetError());
icculus@3892
    46
		retval = 3;
icculus@3892
    47
	} else {
icculus@3892
    48
		fn = (fntype) SDL_LoadFunction(lib, symname);
icculus@3892
    49
		if (fn == NULL) {
icculus@3892
    50
			fprintf(stderr, "SDL_LoadFunction('%s') failed: %s\n",
icculus@3892
    51
			        symname, SDL_GetError());
icculus@3892
    52
			retval = 4;
icculus@3892
    53
		} else {
icculus@3892
    54
			printf("Found %s in %s at %p\n", symname, libname);
icculus@3892
    55
			if (hello) {
icculus@3892
    56
				printf("Calling function...\n");
icculus@3892
    57
				fflush(stdout);
icculus@3892
    58
				fn("     HELLO, WORLD!\n");
icculus@3892
    59
				printf("...apparently, we survived.  :)\n");
icculus@3892
    60
				printf("Unloading library...\n");
icculus@3892
    61
				fflush(stdout);
icculus@3892
    62
			}
icculus@3892
    63
		}
icculus@3892
    64
		SDL_UnloadObject(lib);
icculus@3892
    65
	}
icculus@3892
    66
	SDL_Quit();
icculus@3892
    67
	return(0);
icculus@3892
    68
}
icculus@3892
    69
icculus@3892
    70