| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING |
|---|
| 17 | |
|---|
| 18 | #include "dynamic_loader.h" |
|---|
| 19 | #include "resource_manager.h" |
|---|
| 20 | |
|---|
| 21 | using namespace std; |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * standard constructor |
|---|
| 25 | * @todo this constructor is not jet implemented - do it |
|---|
| 26 | */ |
|---|
| 27 | DynamicLoader::DynamicLoader (const std::string& libName) |
|---|
| 28 | { |
|---|
| 29 | this->setClassID(CL_DYNAMIC_LOADER, "DynamicLoader"); |
|---|
| 30 | |
|---|
| 31 | this->handle = NULL; |
|---|
| 32 | |
|---|
| 33 | if (loadDynamicLib(libName)); |
|---|
| 34 | this->setName(&libName[0]); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * @brief initializes the Dynamic Library loader |
|---|
| 39 | * @returns true on succes, false otherwise |
|---|
| 40 | */ |
|---|
| 41 | bool DynamicLoader::initialize() |
|---|
| 42 | { |
|---|
| 43 | if (lt_dlinit () != 0) |
|---|
| 44 | { |
|---|
| 45 | PRINTF(1)("Initializing LT_DL_LIB: %s\n", lt_dlerror()); |
|---|
| 46 | return false; |
|---|
| 47 | } |
|---|
| 48 | else |
|---|
| 49 | return true; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * standard deconstructor |
|---|
| 54 | */ |
|---|
| 55 | DynamicLoader::~DynamicLoader () |
|---|
| 56 | { |
|---|
| 57 | // delete what has to be deleted here |
|---|
| 58 | if (this->handle != NULL) |
|---|
| 59 | lt_dlclose(this->handle); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | bool DynamicLoader::loadDynamicLib(const std::string& libName) |
|---|
| 64 | { |
|---|
| 65 | DynamicLoader::initialize(); |
|---|
| 66 | |
|---|
| 67 | this->handle = lt_dlopen(&libName[0]); |
|---|
| 68 | if(this->handle == NULL) |
|---|
| 69 | { |
|---|
| 70 | return false; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | bool DynamicLoader::loadDyLib(const std::string& libName) |
|---|
| 75 | { |
|---|
| 76 | DynamicLoader::initialize(); |
|---|
| 77 | |
|---|
| 78 | void* handle; |
|---|
| 79 | handle = lt_dlopen(libName.c_str()); |
|---|
| 80 | if(handle == NULL) |
|---|
| 81 | { |
|---|
| 82 | PRINTF(1)("unable to load %s: %s\n", libName.c_str(), lt_dlerror()); |
|---|
| 83 | |
|---|
| 84 | return false; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | void DynamicLoader::addSearchDir(const std::string& searchDir) |
|---|
| 90 | { |
|---|
| 91 | DynamicLoader::initialize(); |
|---|
| 92 | |
|---|
| 93 | lt_dladdsearchdir(searchDir.c_str()); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * @param relSearchDir: the Relative directory to add to searchPath of lt_dl |
|---|
| 98 | * @returns true if the Path was Valid, false otherwise |
|---|
| 99 | */ |
|---|
| 100 | bool DynamicLoader::addSearchDirRelative(const std::string& relSearchDir) |
|---|
| 101 | { |
|---|
| 102 | std::string absSearchDir = ResourceManager::getAbsDir(relSearchDir); |
|---|
| 103 | if (ResourceManager::isDir(absSearchDir)) |
|---|
| 104 | { |
|---|
| 105 | //printf("ADDED %s\n", absSearchDir.c_str()); |
|---|
| 106 | DynamicLoader::addSearchDir(absSearchDir); |
|---|
| 107 | return true; |
|---|
| 108 | } |
|---|
| 109 | else |
|---|
| 110 | { |
|---|
| 111 | //printf("not a directory %s\n", absSearchDir.c_str()); |
|---|
| 112 | return false; |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | bool DynamicLoader::addSearchDirInLibDir(const std::string& relSearchDir) |
|---|
| 117 | { |
|---|
| 118 | |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | const char* DynamicLoader::getSearchDir() |
|---|
| 123 | { |
|---|
| 124 | return lt_dlgetsearchpath(); |
|---|
| 125 | } |
|---|