ObjFW  OFPlugin.m at [9e6dd00ef5]

File src/OFPlugin.m artifact 0c45c3aa04 part of check-in 9e6dd00ef5


/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

#import "OFPlugin.h"
#import "OFExceptions.h"

@implementation OFPlugin
+ pluginFromFile: (const char*)path
{
	char *file;
	size_t pathlen, suffixlen;
	void *handle;
	OFPlugin *(*init_plugin)();
	OFPlugin *plugin;

	pathlen = strlen(path);
	suffixlen = strlen(PLUGIN_SUFFIX);

	if ((file = malloc(pathlen + suffixlen + 1)) == NULL) {
		@throw [OFNoMemException newWithClass: self
					      andSize: pathlen +
					      suffixlen + 1];
	}
	memcpy(file, path, pathlen);
	memcpy(file + pathlen, PLUGIN_SUFFIX, suffixlen);
	file[pathlen + suffixlen] = 0;

	if ((handle = dlopen(file, RTLD_NOW)) == NULL) {
		free(file);
		@throw [OFInitializationFailedException newWithClass: self];
	}
	free(file);

	if ((init_plugin = dlsym(handle, "init_plugin")) == NULL ||
	    (plugin = init_plugin()) == nil) {
		dlclose(handle);
		@throw [OFInitializationFailedException newWithClass: self];
	}

	plugin->handle = handle;
	return plugin;
}

- (void)dealloc
{
	dlclose(handle);

	[super dealloc];
}
@end