MythTV  0.26-pre
dl_posix.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of libbluray
00003  * Copyright (C) 2009-2010  Obliter0n
00004  * Copyright (C) 2009-2010  John Stebbins
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library. If not, see
00018  * <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 #if HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #include "dl.h"
00026 #include "util/macro.h"
00027 #include "util/logging.h"
00028 #include "util/strutl.h"
00029 
00030 #if defined(_WIN32)
00031 #   include <windows.h>
00032 #elif defined(HAVE_DLFCN_H)
00033 #   include <dlfcn.h>
00034 #elif defined(HAVE_SYS_DL_H)
00035 #   include <sys/dl.h>
00036 #endif
00037 
00038 #if defined(_WIN32)
00039 static const char *dlerror(char *buf, int buf_size)
00040 {
00041     DWORD error_code = GetLastError();
00042     wchar_t wbuf[256];
00043   
00044     if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
00045                        FORMAT_MESSAGE_MAX_WIDTH_MASK,
00046                        NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00047                        wbuf, sizeof(wbuf)/sizeof(wbuf[0]), NULL)) {
00048         WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, buf, buf_size, NULL, NULL);
00049     } else {
00050         snprintf(buf, buf_size, "error %d", (int)error_code);
00051     }
00052     
00053     return buf;
00054 }
00055 #endif
00056 
00057 void   *dl_dlopen  ( const char* path, const char *version )
00058 {
00059     char *name;
00060     void *result;
00061 
00062 #if defined(__APPLE__)
00063     static const char ext[] = ".dylib";
00064     version = NULL;
00065 #elif defined(_WIN32)
00066     static const char ext[] = ".dll";
00067     version = NULL;
00068 #else
00069     static const char ext[] = ".so";
00070 #endif
00071 
00072     if (version) {
00073         name = str_printf("%s%s.%s", path, ext, version);
00074     } else {
00075         name = str_printf("%s%s", path, ext);
00076     }
00077 
00078     BD_DEBUG(DBG_FILE, "searching for library '%s' ...\n", name);
00079 
00080 #if defined(_WIN32)
00081     wchar_t wname[MAX_PATH];
00082     MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, MAX_PATH);
00083     result = LoadLibraryW(wname);
00084 
00085     if (!result) {
00086         char buf[128];
00087         BD_DEBUG(DBG_FILE, "can't open library '%s': %s\n", name, dlerror(buf, sizeof(buf)));
00088     }
00089 #else
00090     result = dlopen(name, RTLD_LAZY);
00091 
00092     if (!result) {
00093         BD_DEBUG(DBG_FILE, "can't open library '%s': %s\n", name, dlerror());
00094     }
00095 #endif
00096 
00097     X_FREE(name);
00098 
00099     return result;
00100 }
00101 
00102 void   *dl_dlsym   ( void* handle, const char* symbol )
00103 {
00104 #if defined(_WIN32)
00105     void *result = (void *)GetProcAddress(handle, symbol);
00106 
00107     if (!result) {
00108         char buf[128];
00109         BD_DEBUG(DBG_FILE | DBG_CRIT, "GetProcAddress(%p, '%s') failed: %s\n", handle, symbol, dlerror(buf, sizeof(buf)));
00110     }
00111 #else
00112     void *result = dlsym(handle, symbol);
00113 
00114     if (!result) {
00115         BD_DEBUG(DBG_FILE | DBG_CRIT, "dlsym(%p, '%s') failed: %s\n", handle, symbol, dlerror());
00116     }
00117 #endif
00118 
00119     return result;
00120 }
00121 
00122 int     dl_dlclose ( void* handle )
00123 {
00124 #if defined(_WIN32)
00125     FreeLibrary(handle);
00126     return 0;
00127 #else
00128     return dlclose(handle);
00129 #endif
00130 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends