| 1 | /*************************************************************************** |
|---|
| 2 | * toolsLibrary.cc |
|---|
| 3 | * |
|---|
| 4 | * Thu Mar 13 2008 |
|---|
| 5 | * Copyright 2008 Dmytro Milinevskyy |
|---|
| 6 | * milinevskyy@gmail.com |
|---|
| 7 | ****************************************************************************/ |
|---|
| 8 | |
|---|
| 9 | /* |
|---|
| 10 | * This program is free software; you can redistribute it and/or modifyto |
|---|
| 11 | * it under the terms of the GNU Lesser General Public License version 2.1 as published by |
|---|
| 12 | * the Free Software Foundation; |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU Library General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU Lesser General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * vim indentation settings |
|---|
| 26 | * set tabstop=4 |
|---|
| 27 | * set shiftwidth=4 |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #include <libdodo/directives.h> |
|---|
| 31 | |
|---|
| 32 | #ifdef DL_EXT |
|---|
| 33 | #include <dlfcn.h> |
|---|
| 34 | #include <stdlib.h> |
|---|
| 35 | |
|---|
| 36 | #include <libdodo/toolsLibrary.h> |
|---|
| 37 | #include <libdodo/toolsLibraryEx.h> |
|---|
| 38 | #include <libdodo/types.h> |
|---|
| 39 | #include <libdodo/toolsMisc.h> |
|---|
| 40 | |
|---|
| 41 | using namespace dodo::tools; |
|---|
| 42 | |
|---|
| 43 | library::library() : handle(NULL) |
|---|
| 44 | { |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | //------------------------------------------------------------------- |
|---|
| 48 | |
|---|
| 49 | library::library(const dodo::string &path) |
|---|
| 50 | { |
|---|
| 51 | #ifdef DL_FAST |
|---|
| 52 | handle = dlopen(path.data(), RTLD_LAZY | RTLD_NODELETE); |
|---|
| 53 | #else |
|---|
| 54 | handle = dlopen(path.data(), RTLD_LAZY); |
|---|
| 55 | #endif |
|---|
| 56 | |
|---|
| 57 | if (handle == NULL) |
|---|
| 58 | dodo_throw exception::basic(exception::MODULE_TOOLSLIBRARY, LIBRARYEX_LIBRARY, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | //------------------------------------------------------------------- |
|---|
| 62 | |
|---|
| 63 | library::~library() |
|---|
| 64 | { |
|---|
| 65 | #ifndef DL_FAST |
|---|
| 66 | if (handle != NULL) |
|---|
| 67 | dlclose(handle); |
|---|
| 68 | |
|---|
| 69 | #endif |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | //------------------------------------------------------------------- |
|---|
| 73 | |
|---|
| 74 | void |
|---|
| 75 | library::open(const dodo::string &path) |
|---|
| 76 | { |
|---|
| 77 | #ifndef DL_FAST |
|---|
| 78 | if (handle != NULL) |
|---|
| 79 | if (dlclose(handle) != 0) |
|---|
| 80 | dodo_throw exception::basic(exception::MODULE_TOOLSLIBRARY, LIBRARYEX_OPEN, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); |
|---|
| 81 | |
|---|
| 82 | #endif |
|---|
| 83 | |
|---|
| 84 | #ifdef DL_FAST |
|---|
| 85 | handle = dlopen(path.data(), RTLD_LAZY | RTLD_NODELETE); |
|---|
| 86 | #else |
|---|
| 87 | handle = dlopen(path.data(), RTLD_LAZY); |
|---|
| 88 | #endif |
|---|
| 89 | |
|---|
| 90 | if (handle == NULL) |
|---|
| 91 | dodo_throw exception::basic(exception::MODULE_TOOLSLIBRARY, LIBRARYEX_OPEN, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | //------------------------------------------------------------------- |
|---|
| 95 | |
|---|
| 96 | void |
|---|
| 97 | library::close() |
|---|
| 98 | { |
|---|
| 99 | #ifndef DL_FAST |
|---|
| 100 | if (handle != NULL) |
|---|
| 101 | if (dlclose(handle) != 0) |
|---|
| 102 | dodo_throw exception::basic(exception::MODULE_TOOLSLIBRARY, LIBRARYEX_CLOSE, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); |
|---|
| 103 | #endif |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | //------------------------------------------------------------------- |
|---|
| 107 | |
|---|
| 108 | void * |
|---|
| 109 | library::function(const dodo::string &name) |
|---|
| 110 | { |
|---|
| 111 | if (handle == NULL) |
|---|
| 112 | dodo_throw exception::basic(exception::MODULE_TOOLSLIBRARY, LIBRARYEX_FUNCTION, exception::ERRNO_LIBDODO, LIBRARYEX_LIBRARYNOTOPENED, TOOLSLIBRARYEX_NOTOPENED_STR, __LINE__, __FILE__); |
|---|
| 113 | |
|---|
| 114 | void *func = dlsym(handle, name.data()); |
|---|
| 115 | if (func == NULL) |
|---|
| 116 | dodo_throw exception::basic(exception::MODULE_TOOLSLIBRARY, LIBRARYEX_FUNCTION, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); |
|---|
| 117 | |
|---|
| 118 | return func; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | //------------------------------------------------------------------- |
|---|
| 122 | |
|---|
| 123 | void * |
|---|
| 124 | library::operator[](const dodo::string &name) |
|---|
| 125 | { |
|---|
| 126 | if (handle == NULL) |
|---|
| 127 | dodo_throw exception::basic(exception::MODULE_TOOLSLIBRARY, LIBRARYEX_BROPERATORSTRING, exception::ERRNO_LIBDODO, LIBRARYEX_LIBRARYNOTOPENED, TOOLSLIBRARYEX_NOTOPENED_STR, __LINE__, __FILE__); |
|---|
| 128 | |
|---|
| 129 | void *func = dlsym(handle, name.data()); |
|---|
| 130 | if (func == NULL) |
|---|
| 131 | dodo_throw exception::basic(exception::MODULE_TOOLSLIBRARY, LIBRARYEX_BROPERATORSTRING, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); |
|---|
| 132 | |
|---|
| 133 | return func; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | #endif |
|---|
| 137 | |
|---|
| 138 | //------------------------------------------------------------------- |
|---|