Compile fixes.
[SquirrelJME.git] / nanocoat / lib / base / dylib.c
blob3b5840e693a061e77054dc96e43fb12b9eebeaa7
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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)
16 /** Uses dlfcn.h. */
17 #define SJME_CONFIG_DYLIB_HAS_DLFCN
18 #endif
19 #endif
21 #include <stdio.h>
23 #if defined(SJME_CONFIG_DYLIB_HAS_DLFCN)
24 #include <dlfcn.h>
25 #elif defined(SJME_CONFIG_HAS_WINDOWS)
26 #define WIN32_LEAN_AND_MEAN 1
28 #include <windows.h>
30 #undef WIN32_LEAN_AND_MEAN
31 #endif
33 #include "sjme/dylib.h"
34 #include "sjme/debug.h"
36 sjme_errorCode sjme_dylib_close(
37 sjme_attrInNotNull sjme_dylib inLib)
39 if (inLib == NULL)
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;
48 /* Success! */
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;
54 /* Success! */
55 return SJME_ERROR_NONE;
56 #else
57 sjme_todo("Impl?");
58 return SJME_ERROR_NOT_IMPLEMENTED;
59 #endif
62 sjme_errorCode sjme_dylib_lookup(
63 sjme_attrInNotNull sjme_dylib inLib,
64 sjme_attrInNotNull sjme_lpcstr inSymbol,
65 sjme_pointer* outPtr)
67 #if defined(SJME_CONFIG_DYLIB_HAS_DLFCN)
68 sjme_pointer handle;
69 #elif defined(SJME_CONFIG_HAS_WINDOWS)
70 FARPROC handle;
71 #endif
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);
81 if (handle == NULL)
83 /* Debug. */
84 sjme_message("sjme_dylib_lookup(%p, %s): %s",
85 inLib, inSymbol, dlerror());
87 return SJME_ERROR_INVALID_LIBRARY_SYMBOL;
90 /* Success! */
91 *outPtr = handle;
92 return SJME_ERROR_NONE;
93 #elif defined(SJME_CONFIG_HAS_WINDOWS)
94 handle = GetProcAddress(inLib, inSymbol);
95 if (handle == NULL)
96 return SJME_ERROR_INVALID_LIBRARY_SYMBOL;
98 /* Success! */
99 *outPtr = handle;
100 return SJME_ERROR_NONE;
101 #else
102 sjme_todo("Impl?");
103 return SJME_ERROR_NOT_IMPLEMENTED;
104 #endif
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? */
122 if (outLen <= 1)
123 return SJME_ERROR_NONE;
125 /* Print what? */
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);
136 #else
137 return SJME_ERROR_NOT_IMPLEMENTED;
138 #endif
140 /* Always add NULL. */
141 outName[outLen - 1] = 0;
143 /* Success! */
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)
152 sjme_pointer handle;
153 #elif defined(SJME_CONFIG_HAS_WINDOWS)
154 HMODULE handle;
155 #endif
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);
165 if (handle == NULL)
167 /* Debug. */
168 sjme_message("sjme_dylib_open(%s): %s",
169 libPath, dlerror());
171 return SJME_ERROR_COULD_NOT_LOAD_LIBRARY;
174 /* Success! */
175 *outLib = handle;
176 return SJME_ERROR_NONE;
177 #elif defined(SJME_CONFIG_HAS_WINDOWS)
178 handle = LoadLibraryExA(libPath, NULL, 0);
179 if (handle == NULL)
180 return SJME_ERROR_COULD_NOT_LOAD_LIBRARY;
182 /* Success! */
183 *outLib = handle;
184 return SJME_ERROR_NONE;
185 #else
186 sjme_todo("Impl?");
187 return SJME_ERROR_NOT_IMPLEMENTED;
188 #endif
191 sjme_errorCode sjme_dylib_self(
192 sjme_attrInOutNotNull sjme_dylib* outLib)
194 #if defined(SJME_CONFIG_DYLIB_HAS_DLFCN)
195 sjme_pointer handle;
196 #elif defined(SJME_CONFIG_HAS_WINDOWS)
197 HMODULE handle;
198 #endif
200 if (outLib == NULL)
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);
208 if (handle == NULL)
209 return SJME_ERROR_COULD_NOT_LOAD_LIBRARY;
211 /* Success! */
212 *outLib = handle;
213 return SJME_ERROR_NONE;
214 #elif defined(SJME_CONFIG_HAS_WINDOWS)
215 handle = GetModuleHandle(NULL);
216 if (handle == NULL)
217 return SJME_ERROR_COULD_NOT_LOAD_LIBRARY;
219 /* Success! */
220 *outLib = handle;
221 return SJME_ERROR_NONE;
222 #else
223 sjme_todo("Impl?");
224 return SJME_ERROR_NOT_IMPLEMENTED;
225 #endif