123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #include "lib_handler.h"
- #include <ctype.h>
- #include "constants.h"
- #include "util.h"
- soHandle_t lh_load_lib(const char *libName) {
- soHandle_t h = OSQP_NULL;
- if (!libName) {
- #ifdef PRINTING
- c_eprint("no library name given");
- #endif
- return OSQP_NULL;
- }
- #ifdef IS_WINDOWS
- h = LoadLibrary (libName);
- if (!h) {
- #ifdef PRINTING
- c_eprint("Windows error while loading dynamic library %s, error = %d",
- libName, (int)GetLastError());
- #endif
- }
- #else
- h = dlopen (libName, RTLD_LAZY);
- if (!h) {
- #ifdef PRINTING
- c_eprint("Error while loading dynamic library %s: %s", libName, dlerror());
- #endif
- }
- #endif
- return h;
- }
- c_int lh_unload_lib (soHandle_t h) {
- c_int rc = 1;
- #ifdef IS_WINDOWS
- rc = FreeLibrary (h);
- rc = ! rc;
- #else
- rc = dlclose (h);
- #endif
- return rc;
- }
- #ifdef IS_WINDOWS
- typedef FARPROC symtype;
- #else
- typedef void* symtype;
- #endif
- symtype lh_load_sym (soHandle_t h, const char *symName) {
- symtype s;
- const char *from;
- char *to;
- const char *tripSym;
- char* err;
- char lcbuf[257];
- char ucbuf[257];
- char ocbuf[257];
- size_t symLen;
- int trip;
- s = OSQP_NULL;
- err = OSQP_NULL;
-
- symLen = 0;
- for (trip = 1; trip <= 6; trip++) {
- switch (trip) {
- case 1:
- tripSym = symName;
- break;
- case 2:
- for (from = symName, to = lcbuf; *from; from++, to++) {
- *to = tolower(*from);
- }
- symLen = from - symName;
- *to++ = '_';
- *to = '\0';
- tripSym = lcbuf;
- break;
- case 3:
- for (from = symName, to = ucbuf; *from; from++, to++) {
- *to = toupper(*from);
- }
- *to++ = '_';
- *to = '\0';
- tripSym = ucbuf;
- break;
- case 4:
- c_strcpy(ocbuf, symName);
- ocbuf[symLen] = '_';
- ocbuf[symLen+1] = '\0';
- tripSym = ocbuf;
- break;
- case 5:
- lcbuf[symLen] = '\0';
- tripSym = lcbuf;
- break;
- case 6:
- ucbuf[symLen] = '\0';
- tripSym = ucbuf;
- break;
- default:
- tripSym = symName;
- }
- #ifdef IS_WINDOWS
- s = GetProcAddress (h, tripSym);
- if (s) {
- return s;
- } else {
- #ifdef PRINTING
- c_eprint("Cannot find symbol %s in dynamic library, error = %d",
- symName, (int)GetLastError());
- #endif
- }
- #else
- s = dlsym (h, tripSym);
- err = dlerror();
- if (err) {
- #ifdef PRINTING
- c_eprint("Cannot find symbol %s in dynamic library, error = %s",
- symName, err);
- #endif
- } else {
- return s;
- }
- #endif
- }
- return OSQP_NULL;
- }
|