New type. (glibtop_backend_module): New private type.

2000-01-12  Martin Baulig  <martin@home-of-linux.org>

	* include/glibtop/backend.h (glibtop_backend_entry): New type.
	(glibtop_backend_module): New private type.

	* lib/backend.c (glibtop_register_backend): This now takes a
	`glibtop_backend_entry' argument, not a `glibtop_backend_info' one.
	(glibtop_backend_by_id): Return a `glibtop_backend_entry' type,
	not a `glibtop_backend_info' one.
	(glibtop_backend_by_name): Likewise.

	* lib/init-backends.c: If we have libxml, read all `.backend' files
	in $(LIBGTOP_BACKEND_DIR), XML parse them and register them via
	`glibtop_backend_register'. This builds a list of all currently
	supported backends without actually loading them into memory.

	* lib/open-backend.c (glibtop_open_backend_l): GModule load the
	backend if it's not already in memory.
This commit is contained in:
Martin Baulig
2000-01-12 20:37:21 +00:00
committed by Martin Baulig
parent 8d532c6298
commit e2e5c3a1cc
6 changed files with 249 additions and 17 deletions

View File

@@ -33,6 +33,18 @@ extern glibtop_backend_info glibtop_backend_server;
extern glibtop_backend_info glibtop_backend_sysdeps;
extern glibtop_backend_info glibtop_backend_common;
#if HAVE_LIBXML
#define LIBGTOP_XML_NAMESPACE "http://www.home-of-linux.org/libgtop/1.1"
#include <gnome-xml/parser.h>
#include <dirent.h>
static void _glibtop_init_gmodule_backends (const char *);
#endif /* HAVE_LIBXML */
void
glibtop_init_backends (void)
{
@@ -42,7 +54,147 @@ glibtop_init_backends (void)
return;
backends_initialized = 1;
glibtop_register_backend (&glibtop_backend_server);
glibtop_register_backend (&glibtop_backend_sysdeps);
glibtop_register_backend (&glibtop_backend_common);
#if HAVE_LIBXML
_glibtop_init_gmodule_backends (LIBGTOP_BACKEND_DIR);
#endif
}
#if HAVE_LIBXML
static gchar *
_get_library_filename (xmlDocPtr doc, xmlNodePtr cur, const char *directory)
{
char *filename = xmlNodeListGetString (doc, cur->childs, 1);
gchar *retval;
if (!filename)
return NULL;
/* already absolute */
if (filename [0] == '/')
retval = g_strdup (filename);
else
retval = g_strdup_printf ("%s/%s", directory, filename);
return retval;
}
static glibtop_backend_entry *
_parseBackend (xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur, const char *dir)
{
glibtop_backend_entry *ret = NULL;
/*
* allocate the struct
*/
ret = g_new0 (glibtop_backend_entry, 1);
/* We don't care what the top level element name is */
cur = cur->childs;
while (cur != NULL) {
if ((!strcmp (cur->name, "Name")) && (cur->ns == ns))
ret->name = xmlNodeListGetString
(doc, cur->childs, 1);
if ((!strcmp (cur->name, "Location")) && (cur->ns == ns)) {
xmlNodePtr sub = cur->childs;
while (sub != NULL) {
if ((!strcmp (sub->name, "LibtoolName")) && (sub->ns == ns))
ret->libtool_name = _get_library_filename (doc, sub, dir);
if ((!strcmp (sub->name, "ShlibName")) && (sub->ns == ns))
ret->shlib_name = _get_library_filename (doc, sub, dir);
sub = sub->next;
}
}
cur = cur->next;
}
return ret;
}
static void
_glibtop_init_gmodule_backends (const char *directory)
{
DIR *dir;
struct dirent *entry;
dir = opendir (directory);
if (!dir) return;
while ((entry = readdir (dir)) != NULL) {
size_t len = strlen (entry->d_name);
gchar *filename;
xmlDocPtr doc;
xmlNsPtr ns;
xmlNodePtr cur;
if (len < 8)
continue;
if (strcmp (entry->d_name+len-8, ".backend"))
continue;
filename = g_strdup_printf ("%s/%s", directory, entry->d_name);
doc = xmlParseFile (filename);
if (!doc) {
g_warning ("Cannot parse %s", filename);
g_free (filename);
continue;
}
/* Make sure the document is of the right kind */
cur = xmlDocGetRootElement (doc);
if (!cur) {
xmlFreeDoc (doc);
g_free (filename);
continue;
}
ns = xmlSearchNsByHref (doc, cur, LIBGTOP_XML_NAMESPACE);
if (!ns) {
g_warning ("File %s of wrong type; LibGTop Namespace not found",
filename);
g_free (filename);
xmlFreeDoc (doc);
continue;
}
if (strcmp (cur->name, "Backends")) {
g_warning ("File %s of the wrong type, root node != 'Backends'",
filename);
g_free (filename);
xmlFreeDoc (doc);
continue;
}
cur = cur->childs;
while (cur != NULL) {
glibtop_backend_entry *backend;
if ((!strcmp(cur->name, "Backend")) && (cur->ns == ns)) {
backend = _parseBackend (doc, ns, cur, directory);
if (!backend) {
g_warning ("File %s of wrong type; cannot parse",
filename);
continue;
}
glibtop_register_backend (backend);
}
cur = cur->next;
}
g_free (filename);
xmlFreeDoc (doc);
}
closedir (dir);
}
#endif /* HAVE_LIBXML */