Browse Source

Updated to use Jupiter::ReadableString

release/0.19
JustinAJ 11 years ago
parent
commit
ce126af6cb
  1. 45
      Jupiter/Plugin.cpp
  2. 17
      Jupiter/Plugin.h

45
Jupiter/Plugin.cpp

@ -23,8 +23,9 @@
#include "Functions.h" #include "Functions.h"
#include "ArrayList.h" #include "ArrayList.h"
#include "CString.h" #include "CString.h"
#include "String.h"
Jupiter::CStringS pluginDir = DEFAULT_PLUGINS_DIRECTORY; Jupiter::StringS pluginDir = DEFAULT_PLUGINS_DIRECTORY;
Jupiter::ArrayList<Jupiter::Plugin> _plugins; Jupiter::ArrayList<Jupiter::Plugin> _plugins;
Jupiter::ArrayList<Jupiter::Plugin> *Jupiter::plugins = &_plugins; Jupiter::ArrayList<Jupiter::Plugin> *Jupiter::plugins = &_plugins;
@ -60,31 +61,25 @@ dlib::~dlib()
} }
} }
void Jupiter::setPluginDirectory(const char *dir) void Jupiter::setPluginDirectory(const Jupiter::ReadableString &dir)
{ {
if (dir == nullptr) pluginDir = DEFAULT_PLUGINS_DIRECTORY; if (pluginDir.set(dir) != 0 && pluginDir.get(pluginDir.size() - 1) != DIR_CHR) pluginDir += DIR_CHR;
else
{
pluginDir = dir;
if (pluginDir.size() != 0 && dir[pluginDir.size() - 1] != DIR_CHR) pluginDir += DIR_CHR;
}
} }
const char *Jupiter::getPluginDirectory() const Jupiter::ReadableString &Jupiter::getPluginDirectory()
{ {
return pluginDir.c_str(); return pluginDir;
} }
Jupiter::Plugin *Jupiter::loadPlugin(const char *pluginName)
{
#if defined _WIN32 #if defined _WIN32
char *fileName = new char[strlen(pluginName) + pluginDir.size() + 5]; #define MODULE_FILE_EXTENSION ".dll"
sprintf(fileName, "%s%s.dll", pluginDir.c_str(), pluginName);
#else // _WIN32 #else // _WIN32
char *fileName = new char[strlen(pluginName) + pluginDirLen + 4]; #define MODULE_FILE_EXTENSION ".so"
sprintf(fileName, "%s%s.so", pluginDir.c_str(), pluginName);
#endif // _WIN32 #endif // _WIN32
return Jupiter::loadPluginFile(fileName);
Jupiter::Plugin *Jupiter::loadPlugin(const Jupiter::ReadableString &pluginName)
{
return Jupiter::loadPluginFile(Jupiter::CStringS::Format("%.*s%.*s" MODULE_FILE_EXTENSION, pluginDir.size(), pluginDir.ptr(), pluginName.size(), pluginName.ptr()).c_str());
} }
Jupiter::Plugin *Jupiter::loadPluginFile(const char *file) Jupiter::Plugin *Jupiter::loadPluginFile(const char *file)
@ -121,7 +116,7 @@ fail:
return nullptr; return nullptr;
} }
bool Jupiter::freePlugin(unsigned int index) bool Jupiter::freePlugin(size_t index)
{ {
if (index < _plugins.size()) if (index < _plugins.size())
{ {
@ -146,30 +141,30 @@ bool Jupiter::freePlugin(unsigned int index)
bool Jupiter::freePlugin(Jupiter::Plugin *plugin) bool Jupiter::freePlugin(Jupiter::Plugin *plugin)
{ {
if (plugin == nullptr) return false; if (plugin == nullptr) return false;
for (int i = _plugins.size() - 1; i >= 0; i--) if (_plugins.get(i) == plugin) return Jupiter::freePlugin(i); for (size_t i = 0; i != _plugins.size(); i++) if (_plugins.get(i) == plugin) return Jupiter::freePlugin(i);
return false; return false;
} }
bool Jupiter::freePlugin(const char *pluginName) bool Jupiter::freePlugin(const Jupiter::ReadableString &pluginName)
{ {
if (pluginName == nullptr) return false; if (pluginName == nullptr) return false;
for (int i = _plugins.size() - 1; i >= 0; i--) if (strmatchi(pluginName, _plugins.get(i)->getName())) return Jupiter::freePlugin(i); for (size_t i = 0; i != _plugins.size(); i++) if (pluginName.matchi(_plugins.get(i)->getName())) return Jupiter::freePlugin(i);
return false; return false;
} }
Jupiter::Plugin *Jupiter::getPlugin(unsigned int index) Jupiter::Plugin *Jupiter::getPlugin(size_t index)
{ {
if (index < _plugins.size()) return _plugins.get(index); if (index < _plugins.size()) return _plugins.get(index);
return nullptr; return nullptr;
} }
Jupiter::Plugin *Jupiter::getPlugin(const char *pluginName) Jupiter::Plugin *Jupiter::getPlugin(const Jupiter::ReadableString &pluginName)
{ {
Jupiter::Plugin *p; Jupiter::Plugin *p;
for (int i = _plugins.size() - 1; i >= 0; i--) for (size_t i = 0; i != _plugins.size(); i++)
{ {
p = _plugins.get(i); p = _plugins.get(i);
if (strmatchi(pluginName, p->getName())) return p; if (pluginName.matchi(p->getName())) return p;
} }
return nullptr; return nullptr;
} }

17
Jupiter/Plugin.h

@ -25,7 +25,7 @@
#include "ArrayList.h" #include "ArrayList.h"
#include "Thinker.h" #include "Thinker.h"
#include "CString.h" #include "String_Type.h"
#include "Rehash.h" #include "Rehash.h"
namespace Jupiter namespace Jupiter
@ -232,18 +232,17 @@ namespace Jupiter
/** /**
* @brief Sets the directory to look in for plugins. * @brief Sets the directory to look in for plugins.
* Note: This DOES NOT copy the input string.
* *
* @param dir Directory to look for plugins in. * @param dir Directory to look for plugins in.
*/ */
JUPITER_API void setPluginDirectory(const char *dir); JUPITER_API void setPluginDirectory(const Jupiter::ReadableString &dir);
/** /**
* @brief Returns the current plugin directory. * @brief Returns the current plugin directory.
* *
* @return String containing the directory which is prepended to module load attempts. * @return String containing the directory which is prepended to module load attempts.
*/ */
JUPITER_API const char *getPluginDirectory(); JUPITER_API const Jupiter::ReadableString &getPluginDirectory();
/** /**
* @brief Loads a module, appending .so or .dll as appropriate for the operating system. * @brief Loads a module, appending .so or .dll as appropriate for the operating system.
@ -251,7 +250,7 @@ namespace Jupiter
* @param pluginName The name of the plugin to load. * @param pluginName The name of the plugin to load.
* @return A pointer to the plugin that was loaded on success, nullptr otherwise. * @return A pointer to the plugin that was loaded on success, nullptr otherwise.
*/ */
JUPITER_API Jupiter::Plugin *loadPlugin(const char *pluginName); JUPITER_API Jupiter::Plugin *loadPlugin(const Jupiter::ReadableString &pluginName);
/** /**
* @brief Loads a module based on a true file name. * @brief Loads a module based on a true file name.
@ -267,7 +266,7 @@ namespace Jupiter
* @param index Index of the module. * @param index Index of the module.
* @return True if a module was unloaded, false otherwise. * @return True if a module was unloaded, false otherwise.
*/ */
JUPITER_API bool freePlugin(unsigned int index); JUPITER_API bool freePlugin(size_t index);
/** /**
* @brief Unloads a module and removes it from the module list, based on its data. * @brief Unloads a module and removes it from the module list, based on its data.
@ -284,7 +283,7 @@ namespace Jupiter
* @param pluginName Name of the module to unload. * @param pluginName Name of the module to unload.
* @return True if a module was unloaded, false otherwise. * @return True if a module was unloaded, false otherwise.
*/ */
JUPITER_API bool freePlugin(const char *pluginName); JUPITER_API bool freePlugin(const Jupiter::ReadableString &pluginName);
/** /**
* @brief Fetches a plugin from the list and returns it, based on its index. * @brief Fetches a plugin from the list and returns it, based on its index.
@ -292,7 +291,7 @@ namespace Jupiter
* @param index Index of the module to return. * @param index Index of the module to return.
* @return A module on success, nullptr otherwise. * @return A module on success, nullptr otherwise.
*/ */
JUPITER_API Jupiter::Plugin *getPlugin(unsigned int index); JUPITER_API Jupiter::Plugin *getPlugin(size_t index);
/** /**
* @brief Fetches a plugin from the list and returns it, based on its name. * @brief Fetches a plugin from the list and returns it, based on its name.
@ -300,7 +299,7 @@ namespace Jupiter
* @param pluginName String containing the name of the plugin. * @param pluginName String containing the name of the plugin.
* @return A module on success, nullptr otherwise. * @return A module on success, nullptr otherwise.
*/ */
JUPITER_API Jupiter::Plugin *getPlugin(const char *pluginName); JUPITER_API Jupiter::Plugin *getPlugin(const Jupiter::ReadableString &pluginName);
} }
#endif // _PLUGIN_H_HEADER #endif // _PLUGIN_H_HEADER
Loading…
Cancel
Save