-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
104 lines (86 loc) · 2.54 KB
/
main.c
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
/*
* MetaCall Embedding Matplotlib Example by Parra Studios
* An example of embedding matplotlib from Python into C/C++.
*
* Copyright (C) 2016 - 2020 Vicente Eduardo Ferrer Garcia <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <metacall/metacall.h>
#include <stdio.h>
#define metacall_value_create_str(str) \
metacall_value_create_string(str, sizeof(str) - 1)
static int cleanup(int code)
{
if (metacall_destroy() != 0)
{
return code != 0 ? -code : -255;
}
return code;
}
int main(int argc, char *argv[])
{
struct metacall_log_stdio_type log_stdio = { stdout };
printf(metacall_print_info());
// Define log stream
if (metacall_log(METACALL_LOG_STDIO, (void *)&log_stdio) != 0)
{
return cleanup(1);
}
// Initialize MetaCall
if (metacall_initialize() != 0)
{
return cleanup(2);
}
// Python
{
// Array of scripts to be loaded by MetaCall
const char * py_scripts[] =
{
"plot.py"
};
const void * array_x[] = { metacall_value_create_int(1), metacall_value_create_int(2) };
const void * array_y[] = { metacall_value_create_int(3), metacall_value_create_int(4) };
// Parameters to be passed to the plot function
void * args[] =
{
metacall_value_create_str("Hello World"), // title
metacall_value_create_array(array_x, sizeof(array_x) / sizeof(array_x[0])), // x
metacall_value_create_array(array_y, sizeof(array_y) / sizeof(array_y[0])), // y
metacall_value_create_str("X"), // xlabel
metacall_value_create_str("Y"), // ylabel
metacall_value_create_str("output/plot.png") // output
};
void * ret = NULL;
// Load scripts
if (metacall_load_from_file("py", py_scripts, sizeof(py_scripts) / sizeof(py_scripts[0]), NULL) != 0)
{
return cleanup(3);
}
// Call to plot function
ret = metacallv("plot", args);
// Clean up arguments
for (size_t it = 0; it < sizeof(args) / sizeof(args[0]); ++it)
{
metacall_value_destroy(args[it]);
}
if (ret == NULL)
{
return cleanup(4);
}
// Clean up return value
metacall_value_destroy(ret);
}
return cleanup(0);
}