Factorized some code in the shared library loaders
authormolivier <molivier@d7cf8633-e32d-0410-b094-e92efae38249>
Wed, 24 Mar 2004 13:40:43 +0000 (13:40 +0000)
committermolivier <molivier@d7cf8633-e32d-0410-b094-e92efae38249>
Wed, 24 Mar 2004 13:40:43 +0000 (13:40 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@4056 d7cf8633-e32d-0410-b094-e92efae38249

fs.c
jpeg.c
snd_ogg.c
sys.h
sys_shared.c

diff --git a/fs.c b/fs.c
index 093d0ac..90ba070 100644 (file)
--- a/fs.c
+++ b/fs.c
@@ -317,11 +317,7 @@ Unload the Zlib DLL
 */
 void PK3_CloseLibrary (void)
 {
-       if (!zlib_dll)
-               return;
-
-       Sys_UnloadLibrary (zlib_dll);
-       zlib_dll = NULL;
+       Sys_UnloadLibrary (&zlib_dll);
 }
 
 
@@ -335,7 +331,6 @@ Try to load the Zlib DLL
 qboolean PK3_OpenLibrary (void)
 {
        const char* dllname;
-       const dllfunction_t *func;
 
        // Already loaded?
        if (zlib_dll)
@@ -347,27 +342,14 @@ qboolean PK3_OpenLibrary (void)
        dllname = "libz.so";
 #endif
 
-       // Initializations
-       for (func = zlibfuncs; func && func->name != NULL; func++)
-               *func->funcvariable = NULL;
-
        // Load the DLL
-       if (! (zlib_dll = Sys_LoadLibrary (dllname)))
+       if (! Sys_LoadLibrary (dllname, &zlib_dll, zlibfuncs))
        {
-               Con_Printf("Can't find %s. Compressed files support disabled\n", dllname);
+               Con_Printf ("Compressed files support disabled\n");
                return false;
        }
 
-       // Get the function adresses
-       for (func = zlibfuncs; func && func->name != NULL; func++)
-               if (!(*func->funcvariable = (void *) Sys_GetProcAddress (zlib_dll, func->name)))
-               {
-                       Con_Printf("missing function \"%s\" - broken Zlib library!\n", func->name);
-                       PK3_CloseLibrary ();
-                       return false;
-               }
-
-       Con_Printf("%s loaded. Compressed files support enabled\n", dllname);
+       Con_Printf ("Compressed files support enabled\n");
        return true;
 }
 
diff --git a/jpeg.c b/jpeg.c
index c979fb1..2637a03 100644 (file)
--- a/jpeg.c
+++ b/jpeg.c
@@ -395,7 +395,6 @@ Try to load the JPEG DLL
 qboolean JPEG_OpenLibrary (void)
 {
        const char* dllname;
-       const dllfunction_t *func;
 
        // Already loaded?
        if (jpeg_dll)
@@ -407,27 +406,14 @@ qboolean JPEG_OpenLibrary (void)
        dllname = "libjpeg.so.62";
 #endif
 
-       // Initializations
-       for (func = jpegfuncs; func && func->name != NULL; func++)
-               *func->funcvariable = NULL;
-
        // Load the DLL
-       if (! (jpeg_dll = Sys_LoadLibrary (dllname)))
+       if (! Sys_LoadLibrary (dllname, &jpeg_dll, jpegfuncs))
        {
-               Con_DPrintf("Can't find %s. JPEG support disabled\n", dllname);
+               Con_Printf ("JPEG support disabled\n");
                return false;
        }
 
-       // Get the function adresses
-       for (func = jpegfuncs; func && func->name != NULL; func++)
-               if (!(*func->funcvariable = (void *) Sys_GetProcAddress (jpeg_dll, func->name)))
-               {
-                       Con_Printf("missing function \"%s\" - broken JPEG library!\n", func->name);
-                       JPEG_CloseLibrary ();
-                       return false;
-               }
-
-       Con_DPrintf("%s loaded. JPEG support enabled\n", dllname);
+       Con_Printf ("JPEG support enabled\n");
        return true;
 }
 
@@ -441,11 +427,7 @@ Unload the JPEG DLL
 */
 void JPEG_CloseLibrary (void)
 {
-       if (!jpeg_dll)
-               return;
-
-       Sys_UnloadLibrary (jpeg_dll);
-       jpeg_dll = NULL;
+       Sys_UnloadLibrary (&jpeg_dll);
 }
 
 
index d1a44f2..c8eae94 100644 (file)
--- a/snd_ogg.c
+++ b/snd_ogg.c
@@ -292,7 +292,6 @@ Try to load the VorbisFile DLL
 qboolean OGG_OpenLibrary (void)
 {
        const char* dllname;
-       const dllfunction_t *func;
 
        // Already loaded?
        if (vf_dll)
@@ -304,27 +303,14 @@ qboolean OGG_OpenLibrary (void)
        dllname = "libvorbisfile.so";
 #endif
 
-       // Initializations
-       for (func = oggvorbisfuncs; func && func->name != NULL; func++)
-               *func->funcvariable = NULL;
-
        // Load the DLL
-       if (! (vf_dll = Sys_LoadLibrary (dllname)))
+       if (! Sys_LoadLibrary (dllname, &vf_dll, oggvorbisfuncs))
        {
-               Con_DPrintf("Can't find %s. Ogg Vorbis support disabled\n", dllname);
+               Con_Printf ("Ogg Vorbis support disabled\n");
                return false;
        }
 
-       // Get the function adresses
-       for (func = oggvorbisfuncs; func && func->name != NULL; func++)
-               if (!(*func->funcvariable = (void *) Sys_GetProcAddress (vf_dll, func->name)))
-               {
-                       Con_Printf("missing function \"%s\" - broken Ogg Vorbis library!\n", func->name);
-                       OGG_CloseLibrary ();
-                       return false;
-               }
-
-       Con_DPrintf("%s loaded. Ogg Vorbis support enabled\n", dllname);
+       Con_Printf ("Ogg Vorbis support enabled\n");
        return true;
 }
 
@@ -338,11 +324,7 @@ Unload the VorbisFile DLL
 */
 void OGG_CloseLibrary (void)
 {
-       if (!vf_dll)
-               return;
-
-       Sys_UnloadLibrary (vf_dll);
-       vf_dll = NULL;
+       Sys_UnloadLibrary (&vf_dll);
 }
 
 
diff --git a/sys.h b/sys.h
index 689f2b9..f0e2ad5 100644 (file)
--- a/sys.h
+++ b/sys.h
@@ -44,8 +44,8 @@ typedef struct
 }
 dllfunction_t;
 
-dllhandle_t Sys_LoadLibrary (const char* name);
-void Sys_UnloadLibrary (dllhandle_t handle);
+qboolean Sys_LoadLibrary (const char* dllname, dllhandle_t* handle, const dllfunction_t *fcts);
+void Sys_UnloadLibrary (dllhandle_t* handle);
 void* Sys_GetProcAddress (dllhandle_t handle, const char* name);
 
 
index a8016fa..258e6cf 100644 (file)
@@ -1,9 +1,10 @@
 
 #include "quakedef.h"
-#include <time.h>
+# include <time.h>
 #ifndef WIN32
-#include <unistd.h>
-#include <fcntl.h>
+# include <unistd.h>
+# include <fcntl.h>
+# include <dlfcn.h>
 #endif
 
 extern cvar_t  timestamps;
@@ -135,26 +136,56 @@ DLL MANAGEMENT
 ===============================================================================
 */
 
-#ifndef WIN32
-#include <dlfcn.h>
-#endif
-
-dllhandle_t Sys_LoadLibrary (const char* name)
+qboolean Sys_LoadLibrary (const char* dllname, dllhandle_t* handle, const dllfunction_t *fcts)
 {
+       const dllfunction_t *func;
+       dllhandle_t dllhandle;
+
+       if (handle == NULL)
+               return false;
+
+       // Initializations
+       for (func = fcts; func && func->name != NULL; func++)
+               *func->funcvariable = NULL;
+
+       // Load the DLL
 #ifdef WIN32
-       return LoadLibrary (name);
+       dllhandle = LoadLibrary (dllname);
 #else
-       return dlopen (name, RTLD_LAZY);
+       dllhandle = dlopen (dllname, RTLD_LAZY);
 #endif
+       if (! dllhandle)
+       {
+               Con_Printf ("Can't load \"%s\".\n", dllname);
+               return false;
+       }
+
+       // Get the function adresses
+       for (func = fcts; func && func->name != NULL; func++)
+               if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
+               {
+                       Con_Printf ("Missing function \"%s\" - broken library!\n", func->name);
+                       Sys_UnloadLibrary (&dllhandle);
+                       return false;
+               }
+
+       *handle = dllhandle;
+       Con_DPrintf("\"%s\" loaded.\n", dllname);
+       return true;
 }
 
-void Sys_UnloadLibrary (dllhandle_t handle)
+void Sys_UnloadLibrary (dllhandle_t* handle)
 {
+       if (handle == NULL || *handle == NULL)
+               return;
+
 #ifdef WIN32
-       FreeLibrary (handle);
+       FreeLibrary (*handle);
 #else
-       dlclose (handle);
+       dlclose (*handle);
 #endif
+
+       *handle = NULL;
 }
 
 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)