Minor syntactical changes for readability.
[xuni.git] / src / loadso.c
bloba25d95f2e08e97b37384906a80a72752db72b1a3
1 /*! \file loadso.c
3 */
5 #include "loadso.h"
6 #include "error.h"
7 #include "memory.h"
9 #ifdef LOADSO_SDL_VERSION
10 #include "SDL_loadso.h"
11 #elif defined(LOADSO_DLOPEN_VERSION)
12 #include <dlfcn.h>
13 #endif
15 #ifdef LOADSO_STATIC_VERSION
16 #include "utility.h"
18 #include "test/game.h"
19 #include "test/menu.h"
20 #include "test/options.h"
22 typedef void (*init_menu_type)(void *vdata, struct xuni_t *xuni,
23 struct widget_t *panel, struct resource_t *settings);
25 loadso_t xuni_loadso_load_object(struct loadso_array_t *loadso,
26 const char *filename) {
28 /* could validate filename here */
30 return 0;
33 void xuni_loadso_free_object(loadso_t object) {
34 /* do nothing */
36 #else
38 static void loadso_add_object(struct loadso_array_t *loadso,
39 const char *name, loadso_t object);
41 loadso_t xuni_loadso_load_object(struct loadso_array_t *loadso,
42 const char *filename) {
44 loadso_t object;
46 if(!filename || !*filename) return 0;
48 #ifdef LOADSO_SDL_VERSION
49 object = SDL_LoadObject(filename);
50 #elif defined(LOADSO_DLOPEN_VERSION)
51 object = dlopen(filename, RTLD_LAZY);
52 #endif
54 if(!object) {
55 #ifdef LOADSO_SDL_VERSION
56 log_message(ERROR_TYPE_WARNING, ERROR_FLAG_SDL, __FILE__, __LINE__,
57 "Can't load object \"%s\"", filename);
58 #elif defined(LOADSO_DLOPEN_VERSION)
59 log_message(ERROR_TYPE_WARNING, 0, __FILE__, __LINE__,
60 "Can't load object \"%s\": %s", filename, dlerror());
61 #endif
63 else loadso_add_object(loadso, filename, object);
65 return object;
68 func_point_t xuni_loadso_load_function(loadso_t object, const char *func) {
69 func_point_t sofunc;
71 /* Trying to load a function from a NULL object will fail.
72 No error message is displayed because xuni_loadso_load_object() will
73 have already displayed one.
75 if(!object) {
76 return 0;
79 if(!func || !*func) {
80 log_message(ERROR_TYPE_WARNING, 0, __FILE__, __LINE__,
81 "Can't load function with NULL or empty name");
82 return 0;
85 /* This generates an unavoidable warning, because function pointers
86 cannot be stored in void pointers and vise versa, and dlsym() etc
87 chose to use void pointers for some reason.
89 sofunc = (func_point_t)
90 #ifdef LOADSO_SDL_VERSION
91 SDL_LoadFunction(object, func);
92 #elif defined(LOADSO_DLOPEN_VERSION)
93 dlsym(object, func);
94 #endif
96 if(!sofunc) {
97 #ifdef LOADSO_SDL_VERSION
98 log_message(ERROR_TYPE_WARNING, ERROR_FLAG_SDL, __FILE__, __LINE__,
99 "Can't load function \"%s\"", func);
100 #elif defined(LOADSO_DLOPEN_VERSION)
101 log_message(ERROR_TYPE_WARNING, 0, __FILE__, __LINE__,
102 "Can't load function \"%s\": %s", func, dlerror());
103 #endif
106 return sofunc;
109 void xuni_loadso_free_object(loadso_t object) {
110 #ifdef LOADSO_SDL_VERSION
111 SDL_UnloadObject(object);
112 #elif defined(LOADSO_DLOPEN_VERSION)
113 dlclose(object);
114 #endif
117 static void loadso_add_object(struct loadso_array_t *loadso,
118 const char *name, loadso_t object) {
120 loadso->object = xuni_memory_resize(loadso->object, (loadso->objects + 1)
121 * sizeof(*loadso->object));
122 loadso->object[loadso->objects].object = object;
123 loadso->object[loadso->objects].name = xuni_memory_duplicate_string(name);
125 loadso->objects ++;
127 #endif
129 void init_loadso(struct loadso_array_t *loadso) {
130 loadso->object = 0;
131 loadso->objects = 0;
134 void free_loadso(struct loadso_array_t *loadso) {
135 size_t x;
137 for(x = 0; x < loadso->objects; x ++) {
138 xuni_loadso_free_object(loadso->object[x].object);
139 xuni_memory_free((void *)loadso->object[x].name);
142 xuni_memory_free(loadso->object);
143 xuni_memory_free(loadso);