plugin.c

Go to the documentation of this file.
00001 /* File: plugin.c
00002  *
00003  * Time-stamp: <Tue 02 Aug 2005-- 21:44:10 Paolo Frasconi>
00004  * $Id: plugin.c,v 1.1 2006/05/31 09:21:19 andrea Exp $
00005  *
00006  * Author: Alessio Ceroni and Paolo Frasconi
00007  *
00008  * Description: svm-Dlight - wrappers around customizable functions
00009  * 
00010  */
00011 # include <stdio.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014 #include <dlfcn.h>
00015 # include "plugin.h"
00016 #include "sparsevector.h" /* Default data type is sparse vector */
00017 
00018 static void *dl_library = NULL;
00019 
00020 
00021 static void*(*plugin_null_fcn)(void) = NULL;
00022 static void*(*plugin_copy_constructor_fcn)(void*) = NULL;
00023 static void*(*plugin_parse_document_fcn)
00024   (char*, double*, long*, long*, double*, long*, long, char**) = NULL;
00025 static double(*plugin_kernel_fcn)(void*,void*) = NULL;
00026 static void(*plugin_write_fcn)(FILE*,void*) = NULL;
00027 static void(*plugin_destructor_fcn)(void*) = NULL;
00028 static void(*plugin_clip_fcn)(void*, int) = NULL;
00029 static void(*plugin_kernel_setparm_fcn)(char*) = NULL;
00030 
00031 void empty_function(char* a)
00032 {
00033 }
00034 
00035 void plugin_init(char *plugin_file) {
00036 
00037   const char *error;
00038   plugin_exit();  /* Alessio: a che serve? */
00039 
00040   /* open library */
00041   if( plugin_file != NULL && strlen(plugin_file)>0 ) {
00042     dl_library = dlopen(plugin_file,RTLD_LAZY);
00043     if( dl_library==NULL ) {
00044       fprintf (stderr, "Cannot open plugin file <%s>, using default: %s\n",
00045                plugin_file, dlerror()); 
00046     }
00047   }
00048     
00049   if( dl_library!=NULL ) {
00050     /* clear errors */
00051     dlerror();
00052     
00053     /* load symbols */
00054     plugin_null_fcn = dlsym(dl_library, "plugin_null");
00055     if ((error = dlerror()) != NULL)  {
00056       fprintf (stderr, "Fatal plugin error: %s\n", error);
00057       exit(1);
00058     }
00059 
00060     plugin_copy_constructor_fcn = dlsym(dl_library, "plugin_copy_constructor");
00061     if ((error = dlerror()) != NULL)  {
00062       fprintf (stderr, "Fatal plugin error: %s\n", error);
00063       exit(1);
00064     }
00065 
00066     plugin_parse_document_fcn = dlsym(dl_library, "plugin_parse_document");
00067     if ((error = dlerror()) != NULL)  {
00068       fprintf (stderr, "Fatal plugin error: %s\n", error);
00069       exit(1);
00070     }
00071 
00072     plugin_kernel_fcn = dlsym(dl_library, "plugin_kernel");
00073     if ((error = dlerror()) != NULL)  {
00074       fprintf (stderr, "Fatal plugin error: %s\n", error);
00075       exit(1);
00076     }
00077 
00078     plugin_write_fcn = dlsym(dl_library, "plugin_write");
00079     if ((error = dlerror()) != NULL)  {
00080       fprintf (stderr, "Fatal plugin error: %s\n", error);
00081       exit(1);
00082     }
00083 
00084     plugin_destructor_fcn = dlsym(dl_library, "plugin_destructor");
00085     if ((error = dlerror()) != NULL)  {
00086       fprintf (stderr, "Fatal plugin error: %s\n", error);
00087       exit(1);
00088     }
00089 
00090     plugin_clip_fcn = dlsym(dl_library, "plugin_clip");
00091     if ((error = dlerror()) != NULL)  {
00092       fprintf (stderr, "Fatal plugin error: %s\n", error);
00093       exit(1);
00094     }
00095 
00096     plugin_kernel_setparm_fcn = dlsym(dl_library, "plugin_kernel_setparm");
00097     if ((error = dlerror()) != NULL)  {
00098       fprintf (stderr, "Fatal plugin error: %s\n", error);
00099       exit(1);
00100     }
00101 
00102 
00103   }
00104   else {
00105     /* default */
00106     plugin_null_fcn = sparsevector_null;
00107     plugin_copy_constructor_fcn = sparsevector_copy_constructor;
00108     plugin_parse_document_fcn = sparsevector_parse_document;
00109     plugin_kernel_fcn = sparsevector_kernel;
00110     plugin_write_fcn = sparsevector_write;
00111     plugin_destructor_fcn = sparsevector_destructor;
00112     plugin_clip_fcn = sparsevector_clip;
00113     plugin_kernel_setparm_fcn = empty_function;
00114   }
00115 }
00116 
00117 void plugin_exit() {
00118   plugin_null_fcn = NULL;
00119   plugin_copy_constructor_fcn = NULL;
00120   plugin_parse_document_fcn = NULL;
00121   plugin_kernel_fcn = NULL; 
00122   plugin_write_fcn = NULL;
00123   plugin_destructor_fcn = NULL;
00124   plugin_clip_fcn = NULL;
00125   
00126   if( dl_library!=NULL )
00127     dlclose(dl_library);
00128   dl_library = NULL;  
00129 }
00130 
00131 
00132 
00133 void* plugin_null(void) {
00134   return plugin_null_fcn();
00135 }
00136 
00137 void* plugin_copy_constructor(void *words) {
00138   return plugin_copy_constructor_fcn(words);
00139 }
00140 
00141 void* 
00142 plugin_parse_document(char* line, 
00143                       double* alphay,
00144                       long int* queryid,
00145                       long int* slackid, 
00146                       double* costfactor,
00147                       /* long* wpos, */
00148                       long int* highest_feature, 
00149                       long int max_words, 
00150                       char** comment) 
00151 {
00152   return plugin_parse_document_fcn(line, alphay, queryid, slackid, costfactor,
00153                                    highest_feature, max_words, comment);
00154 }
00155 
00156 
00157 double plugin_kernel(void *a, void *b) {
00158   return plugin_kernel_fcn(a,b);
00159 }
00160 
00161 void plugin_write(FILE *fl, void *words) {
00162   plugin_write_fcn(fl,words);
00163 }
00164 
00165 void plugin_destructor(void *words) {
00166   plugin_destructor_fcn(words);
00167 }
00168 
00169 void plugin_clip(void *words, int maxwords) {
00170   plugin_clip_fcn(words, maxwords);
00171 }
00172 
00173 
00174 void plugin_kernel_setparm(char* kernel_parm) {
00175   plugin_kernel_setparm_fcn(kernel_parm);
00176 }
00177 
00178 

Generated on Wed Sep 5 17:26:48 2007 for SVM-Dlight by  doxygen 1.5.1