test/testloadso.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 05 Jun 2013 22:33:10 -0700
changeset 7286 04087a99e3f4
parent 5535 96594ac5fd1a
child 7437 964e8876ad3b
permissions -rw-r--r--
Added testautomation to the Visual Studio 2008 project.
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
icculus@2067
    12
slouken@3338
    13
/* Test program to test dynamic loading with the loadso subsystem.
icculus@2067
    14
*/
icculus@2067
    15
icculus@2067
    16
#include <stdio.h>
icculus@2067
    17
#include <stdlib.h>
slouken@3338
    18
#include <string.h>
icculus@2067
    19
icculus@2067
    20
#include "SDL.h"
icculus@2067
    21
slouken@2120
    22
typedef int (*fntype) (const char *);
icculus@2067
    23
slouken@2120
    24
int
slouken@2120
    25
main(int argc, char *argv[])
icculus@2067
    26
{
slouken@2120
    27
    int retval = 0;
slouken@2120
    28
    int hello = 0;
slouken@2120
    29
    const char *libname = NULL;
slouken@2120
    30
    const char *symname = NULL;
slouken@2120
    31
    void *lib = NULL;
slouken@2120
    32
    fntype fn = NULL;
icculus@2067
    33
slouken@2120
    34
    if (argc != 3) {
slouken@2120
    35
        const char *app = argv[0];
slouken@2120
    36
        fprintf(stderr, "USAGE: %s <library> <functionname>\n", app);
slouken@2120
    37
        fprintf(stderr, "       %s --hello <lib with puts()>\n", app);
slouken@2120
    38
        return 1;
slouken@2120
    39
    }
icculus@2067
    40
slouken@2120
    41
    /* Initialize SDL */
slouken@2120
    42
    if (SDL_Init(0) < 0) {
slouken@2120
    43
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@2120
    44
        return 2;
slouken@2120
    45
    }
icculus@2067
    46
slouken@2120
    47
    if (strcmp(argv[1], "--hello") == 0) {
slouken@2120
    48
        hello = 1;
slouken@2120
    49
        libname = argv[2];
slouken@2120
    50
        symname = "puts";
slouken@2120
    51
    } else {
slouken@2120
    52
        libname = argv[1];
slouken@2120
    53
        symname = argv[2];
slouken@2120
    54
    }
icculus@2067
    55
slouken@2120
    56
    lib = SDL_LoadObject(libname);
slouken@2120
    57
    if (lib == NULL) {
slouken@2120
    58
        fprintf(stderr, "SDL_LoadObject('%s') failed: %s\n",
slouken@2120
    59
                libname, SDL_GetError());
slouken@2120
    60
        retval = 3;
slouken@2120
    61
    } else {
slouken@2120
    62
        fn = (fntype) SDL_LoadFunction(lib, symname);
slouken@2120
    63
        if (fn == NULL) {
slouken@2120
    64
            fprintf(stderr, "SDL_LoadFunction('%s') failed: %s\n",
slouken@2120
    65
                    symname, SDL_GetError());
slouken@2120
    66
            retval = 4;
slouken@2120
    67
        } else {
slouken@2120
    68
            printf("Found %s in %s at %p\n", symname, libname, fn);
slouken@2120
    69
            if (hello) {
slouken@2120
    70
                printf("Calling function...\n");
slouken@2120
    71
                fflush(stdout);
slouken@2120
    72
                fn("     HELLO, WORLD!\n");
slouken@2120
    73
                printf("...apparently, we survived.  :)\n");
slouken@2120
    74
                printf("Unloading library...\n");
slouken@2120
    75
                fflush(stdout);
slouken@2120
    76
            }
slouken@2120
    77
        }
slouken@2120
    78
        SDL_UnloadObject(lib);
slouken@2120
    79
    }
slouken@2120
    80
    SDL_Quit();
slouken@2120
    81
    return (0);
icculus@2067
    82
}