Lua: Fix drawing lines in certain angles
[lsnes.git] / src / core / loadlib.cpp
blobae6bc4fa81ce6ab8adf406960e5906205272e97f
1 #include "core/loadlib.hpp"
2 #include "core/command.hpp"
3 #include <stdexcept>
4 #include <sstream>
6 namespace {
7 function_ptr_command<arg_filename> load_lib("load-library", "Load a library",
8 "Syntax: load-library <file>\nLoad library <file>\n",
9 [](arg_filename args) throw(std::bad_alloc, std::runtime_error) {
10 try {
11 load_library(args);
12 } catch(std::exception& e) {
13 std::ostringstream x;
14 x << "Can't load '" << std::string(args) << "': " << e.what();
15 throw std::runtime_error(x.str());
17 });
20 #if !defined(NO_DLFCN) && !defined(_WIN32) && !defined(_WIN64)
21 #include <dlfcn.h>
22 #include <unistd.h>
23 void load_library(const std::string& filename)
25 char buffer[16384];
26 getcwd(buffer, 16383);
27 std::string _filename = filename;
28 if(filename.find_first_of("/") >= filename.length())
29 _filename = buffer + std::string("/") + filename;
30 void* h = dlopen(_filename.c_str(), RTLD_LOCAL | RTLD_NOW);
31 if(!h)
32 throw std::runtime_error(dlerror());
34 extern const bool load_library_supported = true;
35 #ifdef __APPLE__
36 const char* library_is_called = "Dynamic Library";
37 const char* library_extension = "bundle";
38 #else
39 const char* library_is_called = "Shared Object";
40 const char* library_extension = "so";
41 #endif
42 #elif defined(_WIN32) || defined(_WIN64)
43 #include <windows.h>
44 void load_library(const std::string& filename)
46 char buffer[16384];
47 GetCurrentDirectory(16383, buffer);
48 std::string _filename = filename;
49 if(filename.find_first_of("/\\") >= filename.length())
50 _filename = buffer + std::string("/") + filename;
51 HMODULE h = LoadLibraryA(_filename.c_str());
52 if(!h) {
53 int errcode = GetLastError();
54 char errorbuffer[1024];
55 if(FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, NULL, errcode, 0,
56 errorbuffer, sizeof(errorbuffer), NULL))
57 throw std::runtime_error(errorbuffer);
58 else {
59 std::ostringstream str;
60 str << "Unknown system error (code " << errcode << ")";
61 throw std::runtime_error(str.str());
65 extern const bool load_library_supported = true;
66 const char* library_is_called = "Dynamic Link Library";
67 const char* library_extension = "dll";
68 #else
69 void load_library(const std::string& filename)
71 throw std::runtime_error("Library loader not supported on this platform");
73 extern const bool load_library_supported = false;
74 const char* library_is_called = NULL;
75 const char* library_extension = NULL;
76 #endif