File: test_c.c

package info (click to toggle)
fungw 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,268 kB
  • sloc: ansic: 24,257; makefile: 500; sh: 32; awk: 9; perl: 8; tcl: 7; javascript: 7; ruby: 7; python: 6
file content (65 lines) | stat: -rw-r--r-- 1,624 bytes parent folder | download | duplicates (3)
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "fgw_string.h"
#include <libfungwbind/c/fungw_c.h>

int wrap_atoi(fgw_ctx_t *ctx, const char *s)
{
	fgw_arg_t res;
	fgw_vcall(ctx, &res, "atoi", FGW_STR, s, 0);
	fgw_arg_conv(ctx, &res, FGW_INT);
	return res.val.nat_int;
}

int main()
{
	fgw_ctx_t ctx;
	fgw_arg_t res, arg[8];
	fgw_error_t err;
	fgw_func_t *func_atoi;
	char *sres;

	/* initialize the "string" engine */
	string_init();

	/* initialize a context */
	fgw_init(&ctx, "host");

	/* Create an object called "str", using the "string" engine */
	fgw_obj_new(&ctx, "str", "string", NULL, NULL);

	/* get a function handle */
	func_atoi = fgw_func_lookup(&ctx, "atoi");
	if (func_atoi == NULL) {
		fprintf(stderr, "Error: failed to find global function atoi\n");
		return 1;
	}

	/* direct C call to the C function - no fungw overhead, but type conversion overhead still applies; assumes the function is implemented in C */
	arg[0].type = FGW_FUNC;
	arg[0].val.func = func_atoi;
	arg[1].type = FGW_STR;
	arg[1].val.str = "42";
	err = fgwc_atoi(&res, 2, arg);
	printf("err=%d rt=%x rv=%d\n", err, res.type, res.val.nat_int);

	/* string based call */
	sres = fgw_scall(&ctx, "atoi", "42", NULL);
	printf("scall sres='%s'\n", sres);
	free(sres);

	/* type/data based call */
	err = fgw_vcall(&ctx, &res, "atoi", FGW_STR, "42", NULL);
	printf("vcall err=%d rt=%x rv=%d\n", err, res.type, res.val.nat_int);

	/* local wrapper */
	printf("wrap: %d\n", wrap_atoi(&ctx, "123"));

	/* clean up and exit */
	fgw_dump_ctx(&ctx, stdout, "");
	fgw_uninit(&ctx);
	fgw_atexit();
	return 0;
}