Atlas - testloadso.c

Home / ext / SDL / test Lines: 2 | Size: 3226 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 3 4 This software is provided 'as-is', without any express or implied 5 warranty. In no event will the authors be held liable for any damages 6 arising from the use of this software. 7 8 Permission is granted to anyone to use this software for any purpose, 9 including commercial applications, and to alter it and redistribute it 10 freely. 11*/ 12 13/* Test program to test dynamic loading with the loadso subsystem. 14 */ 15 16#include <stdio.h> 17 18#include <SDL3/SDL.h> 19#include <SDL3/SDL_main.h> 20#include <SDL3/SDL_test.h> 21 22typedef int (*fntype)(const char *); 23 24static void log_usage(char *progname, SDLTest_CommonState *state) { 25 static const char *options[] = { "library", "functionname|--hello", NULL }; 26 SDLTest_CommonLogUsage(state, progname, options); 27 SDL_Log("USAGE: %s <library> <functionname>", progname); 28 SDL_Log(" %s <lib with puts()> --hello", progname); 29} 30 31int main(int argc, char *argv[]) 32{ 33 int i; 34 int result = 0; 35 int hello = 0; 36 const char *libname = NULL; 37 const char *symname = NULL; 38 SDL_SharedObject *lib = NULL; 39 fntype fn = NULL; 40 SDLTest_CommonState *state; 41 42 /* Initialize test framework */ 43 state = SDLTest_CommonCreateState(argv, 0); 44 if (!state) { 45 return 1; 46 } 47 48 /* Parse commandline */ 49 for (i = 1; i < argc;) { 50 int consumed; 51 52 consumed = SDLTest_CommonArg(state, i); 53 if (!consumed) { 54 if (SDL_strcmp(argv[i], "--hello") == 0) { 55 if (!symname || SDL_strcmp(symname, "puts") == 0) { 56 symname = "puts"; 57 consumed = 1; 58 hello = 1; 59 } 60 } else if (!libname) { 61 libname = argv[i]; 62 consumed = 1; 63 } else if (!symname) { 64 symname = argv[i]; 65 consumed = 1; 66 } 67 } 68 if (consumed <= 0) { 69 log_usage(argv[0], state); 70 return 1; 71 } 72 73 i += consumed; 74 } 75 76 if (!libname || !symname) { 77 log_usage(argv[0], state); 78 return 1; 79 } 80 81 /* Initialize SDL */ 82 if (!SDL_Init(0)) { 83 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError()); 84 return 2; 85 } 86 87 lib = SDL_LoadObject(libname); 88 if (!lib) { 89 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_LoadObject('%s') failed: %s", 90 libname, SDL_GetError()); 91 result = 3; 92 } else { 93 fn = (fntype)SDL_LoadFunction(lib, symname); 94 if (!fn) { 95 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_LoadFunction('%s') failed: %s", 96 symname, SDL_GetError()); 97 result = 4; 98 } else { 99 SDL_Log("Found %s in %s at %p", symname, libname, fn); 100 if (hello) { 101 SDL_Log("Calling function..."); 102 fn(" HELLO, WORLD!\n"); 103 SDL_Log("...apparently, we survived. :)"); 104 SDL_Log("Unloading library..."); 105 } 106 } 107 SDL_UnloadObject(lib); 108 } 109 SDL_Quit(); 110 SDLTest_CommonDestroyState(state); 111 return result; 112} 113
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.