Git init
[pkgs/e/elektra.git] / src / libloader / kdbLibLoader.c
1 /***************************************************************************
2             kdbloader.c  -  Dynamically loading backends
3                              -------------------
4     begin                : Sun Mar 19 2006
5     copyright            : (C) 2004 by Avi Alkalay
6     email                : avi@unix.sh
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the BSD License (revised).                      *
13  *                                                                         *
14  ***************************************************************************/
15
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #ifdef HAVE_STRING_H
22 #include <string.h>
23 #endif
24
25 #include "kdbloader.h"
26
27 #ifdef ELEKTRA_STATIC
28 /* Static case */
29
30 int kdbLibInit(void)
31 {
32         return 0;
33 }
34
35 kdbLibHandle kdbLibLoad(const char *module)
36 {
37         kdblib_symbol   *current;
38         current = kdb_exported_syms;
39         while ( current->name != NULL ) {
40                 /* Skip symbols, we're searching for
41                  * the module name */
42                 if ( current->function == NULL && strcmp(current->name, module) == 0 ) {
43                         /* Go to the first symbol for this file */
44                         current++;
45                         return current;
46                 }
47
48                 current++;
49         }
50
51         return NULL;
52 }
53
54 kdbLibFunc kdbLibSym(kdbLibHandle handle, const char *symbol)
55 {
56         kdblib_symbol   *current;
57
58         current = handle;
59         /* For each symbol about this module */
60         while ( current->function != NULL ) {
61                 if ( strcmp(current->name, symbol) == 0 )
62                         return current->function;
63
64                 current++;
65         }
66         
67         return NULL;
68 }
69
70 int kdbLibClose(kdbLibHandle handle)
71 {
72         return 0;
73 }
74
75 #else
76 #ifdef WIN32
77 /* Windows dynamic case */
78
79 int kdbLibInit(void)
80 {
81   return 0;
82 }
83
84 kdbLibHandle kdbLibLoad(const char *module)
85 {
86   char *modulename = malloc((sizeof(char)*strlen(module))+sizeof(".dll"));
87   kdbLibHandle handle;
88   strcpy(modulename, module);
89   strcat(modulename, ".dll");
90   handle = LoadLibrary(modulename);
91   free(modulename);
92   return handle;
93 }
94
95 kdbLibFunc kdbLibSym(kdbLibHandle handle, const char *symbol)
96 {
97   return GetProcAddress(handle, symbol);
98 }
99
100 int kdbLibClose(kdbLibHandle handle)
101 {
102   return FreeLibrary(handle);
103 }
104
105 #else
106 /* Generic case using libltdl */
107 int kdbLibInit(void)
108 {
109   int init_errors = 0;
110   init_errors = lt_dlinit();
111   if (init_errors)
112     return init_errors;
113   return lt_dladdsearchdir(BACKEND_LIBDIR);
114 }
115
116 kdbLibHandle kdbLibLoad(const char *module)
117 {
118         kdbLibHandle    handle;
119         handle = lt_dlopenext(module);
120         return handle;
121 }
122
123 kdbLibFunc kdbLibSym(kdbLibHandle handle, const char *symbol)
124 {
125   return (kdbLibFunc) lt_dlsym(handle, symbol);
126 }
127
128 int kdbLibClose(kdbLibHandle handle)
129 {
130   return lt_dlclose(handle);
131 }
132
133 #endif
134 #endif