1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include "sjme/config.h"
12 #if !defined(SJME_CONFIG_HAS_NO_DYLIB_SUPPORT)
13 #if defined(SJME_CONFIG_HAS_LINUX) || \
14 defined(SJME_CONFIG_HAS_BSD) || \
15 defined(SJME_CONFIG_HAS_MACOS)
17 #define SJME_CONFIG_DYLIB_HAS_DLFCN
23 #if defined(SJME_CONFIG_DYLIB_HAS_DLFCN)
25 #elif defined(SJME_CONFIG_HAS_WINDOWS)
26 #define WIN32_LEAN_AND_MEAN 1
30 #undef WIN32_LEAN_AND_MEAN
33 #include "sjme/dylib.h"
34 #include "sjme/debug.h"
36 sjme_errorCode
sjme_dylib_close(
37 sjme_attrInNotNull sjme_dylib inLib
)
40 return SJME_ERROR_NULL_ARGUMENTS
;
42 #if defined(SJME_CONFIG_HAS_NO_DYLIB_SUPPORT)
43 return SJME_ERROR_UNSUPPORTED_OPERATION
;
44 #elif defined(SJME_CONFIG_DYLIB_HAS_DLFCN)
45 if (dlclose(inLib
) == 0)
46 return SJME_ERROR_COULD_NOT_UNLOAD_LIBRARY
;
49 return SJME_ERROR_NONE
;
50 #elif defined(SJME_CONFIG_HAS_WINDOWS)
51 if (FreeLibrary(inLib
) == 0)
52 return SJME_ERROR_COULD_NOT_UNLOAD_LIBRARY
;
55 return SJME_ERROR_NONE
;
58 return SJME_ERROR_NOT_IMPLEMENTED
;
62 sjme_errorCode
sjme_dylib_lookup(
63 sjme_attrInNotNull sjme_dylib inLib
,
64 sjme_attrInNotNull sjme_lpcstr inSymbol
,
67 #if defined(SJME_CONFIG_DYLIB_HAS_DLFCN)
69 #elif defined(SJME_CONFIG_HAS_WINDOWS)
73 if (inLib
== NULL
|| inSymbol
== NULL
|| outPtr
== NULL
)
74 return SJME_ERROR_NULL_ARGUMENTS
;
76 #if defined(SJME_CONFIG_HAS_NO_DYLIB_SUPPORT)
77 return SJME_ERROR_UNSUPPORTED_OPERATION
;
78 #elif defined(SJME_CONFIG_DYLIB_HAS_DLFCN)
79 /* Attempt symbol lookup. */
80 handle
= dlsym(inLib
, inSymbol
);
84 sjme_message("sjme_dylib_lookup(%p, %s): %s",
85 inLib
, inSymbol
, dlerror());
87 return SJME_ERROR_INVALID_LIBRARY_SYMBOL
;
92 return SJME_ERROR_NONE
;
93 #elif defined(SJME_CONFIG_HAS_WINDOWS)
94 handle
= GetProcAddress(inLib
, inSymbol
);
96 return SJME_ERROR_INVALID_LIBRARY_SYMBOL
;
100 return SJME_ERROR_NONE
;
103 return SJME_ERROR_NOT_IMPLEMENTED
;
107 sjme_errorCode
sjme_dylib_name(
108 sjme_attrInNotNull sjme_lpcstr inLibName
,
109 sjme_attrOutNotNullBuf(outLen
) sjme_lpstr outName
,
110 sjme_attrInPositive sjme_jint outLen
)
112 sjme_intPointer outNameBase
;
114 if (inLibName
== NULL
|| outName
== NULL
)
115 return SJME_ERROR_NULL_ARGUMENTS
;
117 outNameBase
= (sjme_intPointer
)outName
;
118 if (outLen
< 0 || (outNameBase
+ outLen
) < outNameBase
)
119 return SJME_ERROR_INDEX_OUT_OF_BOUNDS
;
121 /* Pointless operation? */
123 return SJME_ERROR_NONE
;
126 #if defined(SJME_CONFIG_HAS_LINUX) || \
127 defined(SJME_CONFIG_HAS_BSD) || \
128 defined(SJME_CONFIG_HAS_BEOS)
129 snprintf(outName
, outLen
- 1, "lib%s.so", inLibName
);
130 #elif defined(SJME_CONFIG_HAS_CYGWIN)
131 snprintf(outName
, outLen
- 1, "lib%s.dll", inLibName
);
132 #elif defined(SJME_CONFIG_HAS_WINDOWS)
133 snprintf(outName
, outLen
- 1, "%s.dll", inLibName
);
134 #elif defined(SJME_CONFIG_HAS_MACOS)
135 snprintf(outName
, outLen
- 1, "lib%s.dylib", inLibName
);
137 return SJME_ERROR_NOT_IMPLEMENTED
;
140 /* Always add NULL. */
141 outName
[outLen
- 1] = 0;
144 return SJME_ERROR_NONE
;
147 sjme_errorCode
sjme_dylib_open(
148 sjme_attrInNotNull sjme_lpcstr libPath
,
149 sjme_attrInOutNotNull sjme_dylib
* outLib
)
151 #if defined(SJME_CONFIG_DYLIB_HAS_DLFCN)
153 #elif defined(SJME_CONFIG_HAS_WINDOWS)
157 if (libPath
== NULL
|| outLib
== NULL
)
158 return SJME_ERROR_NULL_ARGUMENTS
;
160 #if defined(SJME_CONFIG_HAS_NO_DYLIB_SUPPORT)
161 return SJME_ERROR_UNSUPPORTED_OPERATION
;
162 #elif defined(SJME_CONFIG_DYLIB_HAS_DLFCN)
163 /* Attempt loading the library. */
164 handle
= dlopen(libPath
, RTLD_NOW
| RTLD_LOCAL
);
168 sjme_message("sjme_dylib_open(%s): %s",
171 return SJME_ERROR_COULD_NOT_LOAD_LIBRARY
;
176 return SJME_ERROR_NONE
;
177 #elif defined(SJME_CONFIG_HAS_WINDOWS)
178 handle
= LoadLibraryExA(libPath
, NULL
, 0);
180 return SJME_ERROR_COULD_NOT_LOAD_LIBRARY
;
184 return SJME_ERROR_NONE
;
187 return SJME_ERROR_NOT_IMPLEMENTED
;
191 sjme_errorCode
sjme_dylib_self(
192 sjme_attrInOutNotNull sjme_dylib
* outLib
)
194 #if defined(SJME_CONFIG_DYLIB_HAS_DLFCN)
196 #elif defined(SJME_CONFIG_HAS_WINDOWS)
201 return SJME_ERROR_NULL_ARGUMENTS
;
203 #if defined(SJME_CONFIG_HAS_NO_DYLIB_SUPPORT)
204 return SJME_ERROR_UNSUPPORTED_OPERATION
;
205 #elif defined(SJME_CONFIG_DYLIB_HAS_DLFCN)
206 /* Attempt loading the library. */
207 handle
= dlopen(NULL
, RTLD_NOW
| RTLD_LOCAL
);
209 return SJME_ERROR_COULD_NOT_LOAD_LIBRARY
;
213 return SJME_ERROR_NONE
;
214 #elif defined(SJME_CONFIG_HAS_WINDOWS)
215 handle
= GetModuleHandle(NULL
);
217 return SJME_ERROR_COULD_NOT_LOAD_LIBRARY
;
221 return SJME_ERROR_NONE
;
224 return SJME_ERROR_NOT_IMPLEMENTED
;