summaryrefslogtreecommitdiff
path: root/tests/library/test_loader.c
blob: f060a1bb74e607f16e3479f5679e3f78b519bf77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
    Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "library/loader.h"

#include <stdlib.h>		/* for EXIT_SUCCESS, EXIT_FAILURE */
#include <stdio.h>		/* for fprintf */
#include <assert.h>		/* for assert */

#ifdef _WIN32
#define LIBRARY_NAME "testlib.dll"
#else
#define LIBRARY_NAME "./testlib.so.0.0.0"
#endif
#define LIBRARY_FUNC "multiply_by_two"
#define LIBRARY_VAR "seven"
#define LIBRARY_STRUCT "mod_descr"

typedef int (*multiply_by_two_func)( int );

typedef struct {
	int major;
	int minor;
	multiply_by_two_func func;
} module_descriptor_t;

int main( void ) {
	wolf_library_p library;
	wolf_error_t error;
	char errbuf[512];
	WOLF_LIBRARY_FUNCPTR symbol;
	multiply_by_two_func func;
	int res = 0;
	int *var;
	module_descriptor_t *mod_descr;

	/* open the libray */
	library = wolf_library_load( LIBRARY_NAME, &error );
	if( error != WOLF_OK ) {
		fprintf( stderr, "Error %d (%s) loading library '%s':\n",
			error, wolf_error_msg( error, errbuf, 512 ), LIBRARY_NAME );
		fprintf( stderr, "Internal loader error: %s\n",
			wolf_library_error_msg( library, errbuf, 512 ) );
		(void)wolf_library_unload( library );
		return EXIT_FAILURE;
	}

	/* fetch a known function symbol (multiply_by_two) */
	symbol = wolf_library_get( library, LIBRARY_FUNC, &error );
	if( error != WOLF_OK ) {
		fprintf( stderr, "Error %d (%s) fetching function '%s' from the library\n",
			error, wolf_error_msg( error, errbuf, 512 ), LIBRARY_FUNC );
		fprintf( stderr, "Internal loader error: %s\n",
			wolf_library_error_msg( library, errbuf, 512 ) );
		(void)wolf_library_unload( library );
		return EXIT_FAILURE;
	}
	WOLF_LIBRARY_FUNC_CAST( symbol, multiply_by_two_func, func );
	
	/* call it */
	res = func( 7 );
	printf( "func(7) = %d\n", res );
	assert( res == 14 );

	/* fetch address as address to a variable (seven) */
	var = (int *)wolf_library_get( library, LIBRARY_VAR, &error );
	if( error != WOLF_OK ) {
		fprintf( stderr, "Error %d (%s) fetching variable '%s' from the library\n",
			error, wolf_error_msg( error, errbuf, 512 ), LIBRARY_VAR );
		fprintf( stderr, "Internal loader error: %s\n",
			wolf_library_error_msg( library, errbuf, 512 ) );
		(void)wolf_library_unload( library );
		return EXIT_FAILURE;
	}
	printf( "var = %d\n", *var );
	assert( *var == 7 );
	
	/* fetch something complex (in this case something like a module descriptor) */ 
	mod_descr = (module_descriptor_t *)wolf_library_get( library, LIBRARY_STRUCT, &error );
	if( error != WOLF_OK ) {
		fprintf( stderr, "Error %d (%s) fetching structure '%s' from the library\n",
			error, wolf_error_msg( error, errbuf, 512 ), LIBRARY_STRUCT );
		fprintf( stderr, "Internal loader error: %s\n",
			wolf_library_error_msg( library, errbuf, 512 ) );
		(void)wolf_library_unload( library );
		return EXIT_FAILURE;
	}
	printf( "module version: %d.%d\n", mod_descr->major, mod_descr->minor );
	assert( mod_descr->major == 1 && mod_descr->minor == 0 );
	
	/* close library */
	error = wolf_library_unload( library );
	if( error != WOLF_OK ) {
		fprintf( stderr, "Error %d (%s) unloading the library\n",
			error, wolf_error_msg( error, errbuf, 512 ) );
		fprintf( stderr, "Internal loader error: %s\n",
			wolf_library_error_msg( library, errbuf, 512 ) );
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}