All pastes #180914 Raw Edit

Something

public text v1 · immutable
#180914 ·published 2006-09-24 09:12 UTC
rendered paste body
#include "bot.h"

IRCModuleWrapper::IRCModuleWrapper(IRCBot *bot_pointer) {
	bot_ptr = bot_pointer;
}

IRCModuleWrapper::~IRCModuleWrapper() {
	//
}

IRCModuleLoadResult IRCModuleWrapper::LoadModule(char *filename) {
	void *handle = dlopen(filename, RTLD_NOW);

	if(handle == NULL) {
		char *error = dlerror();

		if(strstr(error, "cannot open shared object file: No such file or directory") != NULL) {
			return LOAD_FILE_NOT_FOUND;
		}

		return LOAD_UNKNOWN_ERROR;
	}
	
	void *maker = dlsym(handle, "GiveModulePtrToProg");
	
	if(maker == NULL) {
		std::cerr << "Module loading error: " << dlerror() << std::endl;
		dlclose(handle);
		return LOAD_UNKNOWN_ERROR;
	}
	
	IRCModule *module = ((IRCModule *(*)())(maker))();
	
	if(!module_list) {
		module_list = new IRCModuleList;
		module_list->loaded = false;
		module_list->module = module;
		module_list->info = module->getModuleInfo();
		module_list->next = 0;
		
		RegisterModule(module_list, module->getModuleInfo(), module);
	} else {
		RegisterModule(module_list, module->getModuleInfo(), module);
	}

	dlclose(handle);

	return LOAD_SUCCESS;
}

void IRCModuleWrapper::UnloadModule(char *module_id) {
	IRCModuleList *mod_list = module_list;

	while(mod_list) {
		if(strcmp(mod_list->info->id, module_id) == 0) {
			UnregisterModule(mod_list, false);
			return;
		}

		if(mod_list->next) {
			mod_list = mod_list->next;
		} else {
			return;
		}
	}
}

void IRCModuleWrapper::UnloadAllModules() {
	UnregisterModule(module_list, true);
}

void IRCModuleWrapper::RegisterModule(IRCModuleList *mod_list, IRCModuleInfo *info, IRCModule *module) {
	if(!mod_list->module) {
		if(!mod_list->next) {
			mod_list->next = new IRCModuleList;
			mod_list->next->info = 0;
			mod_list->next->loaded = false;
			mod_list->next->module = 0;
			mod_list->next->next = 0;
		}

		RegisterModule(mod_list->next, info, module);
	} else {
		mod_list->loaded = false;
		mod_list->info = info;
		mod_list->module = module;
	}
}

void IRCModuleWrapper::UnregisterModule(IRCModuleList *mod_list, bool recursive) {
	if(recursive) {
		if(mod_list->next) {
			UnregisterModule(mod_list->next, true);
		}

		delete mod_list->info;
		delete mod_list->module;
		delete mod_list;
	} else {
		if(mod_list->next) {
			mod_list->info = mod_list->next->info;
			mod_list->loaded = mod_list->next->loaded;
			mod_list->module = mod_list->next->module;
			mod_list->next = mod_list->next->next;
		} else {
			delete mod_list->info;
			delete mod_list->module;
			delete mod_list;
		}
	}
}