📄 holders.c
字号:
/* Copyright (C) 2001-2002 Mikael Ylikoski * See the accompanying file "README" for the full copyright notice *//** * @file * Loads and keeps track of available classifiers and stemmers. * * @author Mikael Ylikoski * @date 2001-2002 */#include <dirent.h>#include <dlfcn.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include "doc_classifier.h"#include "multi.h"#include "stemmer.h"#include "utility.h"/** * Holdee. */typedef struct { char *name; /**< Name */ char *file; /**< File name */ void *funcs; /**< Functions structure */ void *handle; /**< Dynamic library handle */} holdee;static parray *classifier_array; /**< Array of classifiers */static parray *doc_classifier_array; /**< Array of document classifiers */static parray *stemmer_array; /**< Array of stemmers *//** * Create a new holdee. * * @param name name of holdee * @param file file name of holdee * @return The holdee. */static holdee *holders_new_holdee (char *name, char *file) { holdee *ht; ht = my_malloc (sizeof(holdee)); ht->name = name; ht->file = file; ht->funcs = NULL; ht->handle = NULL; return ht;}/** * Load classifiers and stemmers into holders. * * @param dir plugin directory * @return Zero if ok, or non-zero if there was an error. */intholders_load (const char *dir) { int dirlen; char file[512]; char *name; void *handle; struct dirent *dp; DIR *dirp; holdee *ht; dirlen = strlen (dir); if (dirlen > 500) return -1; strcpy (file, dir); if (file[dirlen - 1] != '/') file[dirlen++] = '/'; classifier_array = parray_new (0); if (!classifier_array) return -1; doc_classifier_array = parray_new (0); if (!doc_classifier_array) return -1; stemmer_array = parray_new (0); if (!stemmer_array) return -1; dirp = opendir (dir); if (!dirp) return -1; while ((dp = readdir (dirp))) { if (!strstr (dp->d_name, ".so") && !strstr (dp->d_name, ".dll")) continue; if (strlen (dp->d_name) > 511 - dirlen) continue; strcpy (&file[dirlen], dp->d_name); handle = dlopen (file, RTLD_NOW); if (!handle) continue; if ((name = (char *)dlsym (handle, "my_classifier_name"))) { name = *((char **)name); ht = holders_new_holdee (my_strdup (name), my_strdup (file)); parray_append (classifier_array, ht); } else if ((name = (char *)dlsym (handle, "my_doc_classifier_name"))) { name = *((char **)name); ht = holders_new_holdee (my_strdup (name), my_strdup (file)); parray_append (doc_classifier_array, ht); } else if ((name = (char *)dlsym (handle, "my_stemmer_language"))) { name = *((char **)name); ht = holders_new_holdee (my_strdup (name), my_strdup (file)); parray_append (stemmer_array, ht); } dlclose (handle); } closedir (dirp); return 0;}/** * Free holders. */voidholders_free (void) { int i; holdee *hol; for (i = 0; i < array_get_size (classifier_array); i++) { hol = (holdee *)array_get_value (classifier_array, i); if (hol->handle) dlclose (hol->handle); free (hol->name); free (hol->file); free (hol); } for (i = 0; i < array_get_size (doc_classifier_array); i++) { hol = (holdee *)array_get_value (doc_classifier_array, i); if (hol->handle) dlclose (hol->handle); free (hol->name); free (hol->file); free (hol); } for (i = 0; i < array_get_size (stemmer_array); i++) { hol = (holdee *)array_get_value (stemmer_array, i); if (hol->handle) dlclose (hol->handle); free (hol->name); free (hol->file); free (hol); }}/** * Find holdees in a holder. * * @param name name to look for * @param pa array to look in * @return The holdee functions, if found, otherwise NULL. */static void *holders_find (const char *name, parray *pa) { int i; holdee *ht; if (!pa) return NULL; for (i = 0; i < array_get_size (pa); i++) { ht = (holdee *)array_get_value (pa, i); if (!ht) continue; if (!strcmp (name, ht->name)) { if (!ht->funcs) { ht->handle = dlopen (ht->file, RTLD_NOW); if (!ht->handle) continue; ht->funcs = dlsym (ht->handle, "my_functions"); if (!ht->funcs) { dlclose (ht->handle); ht->handle = NULL; continue; } } return ht->funcs; } } return NULL;}/** * Find a classifier. * * @param name name of classifier to find * @return The classifiers functions. */multi_functions *holders_find_classifier (const char *name) { return (multi_functions *)holders_find (name, classifier_array);}/** * Find a document classifier. * * @param name name of classifier to find * @return The classifiers functions. */doc_classifier_functions *holders_find_doc_classifier (const char *name) { return (doc_classifier_functions *) holders_find (name, doc_classifier_array);}/** * Find a stemmer. * * @param name language to find stemmer for * @return The stemmers functions. */stemmer_functions *holders_find_stemmer (const char *name) { return (stemmer_functions *)holders_find (name, stemmer_array);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -