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