Skip to content

Commit ccd0ca7

Browse files
committed
Initial C code
1 parent 20aabf3 commit ccd0ca7

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

c/helloworld.c

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
============================================================================
3+
Name : helloworld.c
4+
Author :
5+
Version :
6+
Copyright : Your copyright notice
7+
Description : Hello World in C, Ansi-style
8+
============================================================================
9+
*/
10+
11+
#define ZF_DLL /* Required only for dynamically linked libraries. */
12+
13+
#include <cdzf.h>
14+
#include <Python.h>
15+
16+
#undef ERROR
17+
/*
18+
*
19+
#include <stdio.h>
20+
#include <stdlib.h>
21+
*/
22+
23+
int Initialize() {
24+
Py_Initialize();
25+
return ZF_SUCCESS;
26+
}
27+
28+
int Finalize() {
29+
Py_Finalize();
30+
return ZF_SUCCESS;
31+
}
32+
33+
int GetRandom(double* random) {
34+
35+
Py_Initialize();
36+
// https://stackoverflow.com/questions/3286448/calling-a-python-method-from-c-c-and-extracting-its-return-value
37+
// First, import your module :
38+
PyObject* myModuleString = PyUnicode_FromString((char*) "random");
39+
PyObject* myModule = PyImport_Import(myModuleString);
40+
41+
// Then getting a reference to your function :
42+
PyObject* myFunction = PyObject_GetAttrString(myModule, (char*) "random");
43+
//PyObject* args = PyTuple_Pack(1,PyFloat_FromDouble(2.0));
44+
PyObject* args = PyTuple_Pack(0);
45+
46+
//Then getting your result :
47+
PyObject* myResult = PyObject_CallObject(myFunction, args);
48+
49+
//And getting back to a double :
50+
double result = PyFloat_AsDouble(myResult);
51+
52+
//Py_Main(argc, argv);
53+
Py_Finalize();
54+
*random = result;
55+
// set value to be returned by the $ZF function call
56+
return ZF_SUCCESS; // set the exit status code
57+
}
58+
59+
int GetRandomSimple(double* random) {
60+
61+
Py_Initialize();
62+
PyRun_SimpleString("import random;");
63+
PyRun_SimpleString("x=random.random();");
64+
65+
PyObject *mainModule = PyImport_AddModule("__main__");
66+
PyObject *var = PyObject_GetAttrString(mainModule, "x");
67+
68+
*random = PyFloat_AsDouble(var);
69+
Py_Finalize();
70+
71+
// set value to be returned by the $ZF function call
72+
return ZF_SUCCESS; // set the exit status code
73+
}
74+
75+
int SimpleString(char *command, double* result) {
76+
77+
Py_Initialize();
78+
PyRun_SimpleString(command);
79+
80+
PyObject *mainModule = PyImport_AddModule("__main__");
81+
PyObject *var = PyObject_GetAttrString(mainModule, "x");
82+
83+
*result = PyFloat_AsDouble(var);
84+
Py_Finalize();
85+
86+
// set value to be returned by the $ZF function call
87+
return ZF_SUCCESS; // set the exit status code
88+
}
89+
90+
int SimpleStringN(char *command, char *resultVar, double* result) {
91+
PyRun_SimpleString(command);
92+
93+
PyObject *mainModule = PyImport_AddModule("__main__");
94+
95+
//char varName = *resultVar;
96+
97+
PyObject *var = PyObject_GetAttrString(mainModule, resultVar);
98+
*result = PyFloat_AsDouble(var);
99+
100+
// TODO
101+
// https://stackoverflow.com/questions/5356773/python-get-string-representation-of-pyobject
102+
103+
104+
// set value to be returned by the $ZF function call
105+
return ZF_SUCCESS; // set the exit status code
106+
}
107+
108+
int main(int argc, char **argv) {
109+
printf("Random: ");
110+
//exec_interactive_interpreter(argc, argv);
111+
double random = 0;
112+
//GetRandom(&random);
113+
GetRandomSimple(&random);
114+
printf("%lf", random);
115+
return 0;
116+
}
117+
118+
ZFBEGIN
119+
ZFENTRY("Initialize","",Initialize)
120+
ZFENTRY("Finalize","",Finalize)
121+
ZFENTRY("GetRandom","D",GetRandom)
122+
ZFENTRY("SimpleString","cD",SimpleString)
123+
ZFENTRY("SimpleStringN","ccD",SimpleStringN)
124+
ZFEND

0 commit comments

Comments
 (0)