test/testloadso.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 25 Jun 2014 17:24:06 -0400
changeset 8956 a99e30f2bf0a
parent 8149 681eb46b8ac4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Patched to compile on Windows.
slouken@5535
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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];
aschiffler@7639
    36
        SDL_Log("USAGE: %s <library> <functionname>\n", app);
aschiffler@7639
    37
        SDL_Log("       %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) {
aschiffler@7639
    43
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "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) {
aschiffler@7639
    58
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "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) {
aschiffler@7639
    64
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_LoadFunction('%s') failed: %s\n",
slouken@2120
    65
                    symname, SDL_GetError());
slouken@2120
    66
            retval = 4;
slouken@2120
    67
        } else {
aschiffler@7639
    68
            SDL_Log("Found %s in %s at %p\n", symname, libname, fn);
slouken@2120
    69
            if (hello) {
aschiffler@7639
    70
                SDL_Log("Calling function...\n");
slouken@2120
    71
                fflush(stdout);
slouken@2120
    72
                fn("     HELLO, WORLD!\n");
aschiffler@7639
    73
                SDL_Log("...apparently, we survived.  :)\n");
aschiffler@7639
    74
                SDL_Log("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();
philipp@7437
    81
    return retval;
icculus@2067
    82
}