Fix C code
[linpy.git] / pypol / _islhelper.c
1 #include <Python.h>
2
3 #include <isl/constraint.h>
4 #include <isl/set.h>
5
6
7 struct pointer_list {
8 int cursor;
9 PyObject *pointers;
10 };
11
12 typedef struct pointer_list pointer_list;
13
14
15 static int pointer_list_append_constraint(isl_constraint *c, void *user) {
16 pointer_list *list;
17 PyObject *value;
18
19 list = (pointer_list *) user;
20 value = PyLong_FromVoidPtr(c);
21 if (value == NULL) {
22 return -1;
23 }
24 PyList_SET_ITEM(list->pointers, list->cursor++, value);
25 return 0;
26 }
27
28 static PyObject * isl_basic_set_constraints(PyObject *self, PyObject* args) {
29 long ptr;
30 isl_basic_set *bset;
31 int n;
32 PyObject *pointers;
33 pointer_list *list;
34
35 if (!PyArg_ParseTuple(args, "l", &ptr)) {
36 return NULL;
37 }
38 bset = (isl_basic_set *) ptr;
39 bset = isl_basic_set_finalize(bset);
40 n = isl_basic_set_n_constraint(bset);
41 if (n == -1) {
42 PyErr_SetString(PyExc_RuntimeError,
43 "an error occurred in isl_basic_set_n_constraint");
44 return NULL;
45 }
46 pointers = PyList_New(n);
47 if (pointers == NULL) {
48 return NULL;
49 }
50 list = malloc(sizeof(pointer_list));
51 if (list == NULL) {
52 Py_DECREF(pointers);
53 return PyErr_NoMemory();
54 }
55 list->cursor = 0;
56 list->pointers = pointers;
57 n = isl_basic_set_foreach_constraint(bset, pointer_list_append_constraint,
58 list);
59 free(list);
60 if (n == -1) {
61 PyErr_SetString(PyExc_RuntimeError,
62 "an error occurred in isl_basic_set_foreach_constraint");
63 Py_DECREF(pointers);
64 return NULL;
65 }
66 return pointers;
67 }
68
69
70 static int pointer_list_append_basic_set(isl_basic_set *bset, void *user) {
71 pointer_list *list;
72 PyObject *value;
73
74 list = (pointer_list *) user;
75 value = PyLong_FromVoidPtr(bset);
76 if (value == NULL) {
77 return -1;
78 }
79 PyList_SET_ITEM(list->pointers, list->cursor++, value);
80 return 0;
81 }
82
83 static PyObject * isl_set_basic_sets(PyObject *self, PyObject *args) {
84 long ptr;
85 isl_set *set;
86 int n;
87 PyObject *pointers;
88 pointer_list *list;
89
90 if (!PyArg_ParseTuple(args, "l", &ptr)) {
91 return NULL;
92 }
93 set = (isl_set *) ptr;
94 n = isl_set_n_basic_set(set);
95 if (n == -1) {
96 PyErr_SetString(PyExc_RuntimeError,
97 "an error occurred in isl_set_n_basic_set");
98 return NULL;
99 }
100 pointers = PyList_New(n);
101 if (pointers == NULL) {
102 return NULL;
103 }
104 list = malloc(sizeof(pointer_list));
105 if (list == NULL) {
106 Py_DECREF(pointers);
107 return PyErr_NoMemory();
108 }
109 list->cursor = 0;
110 list->pointers = pointers;
111 n = isl_set_foreach_basic_set(set, pointer_list_append_basic_set, list);
112 free(list);
113 if (n == -1) {
114 PyErr_SetString(PyExc_RuntimeError,
115 "an error occurred in isl_set_foreach_basic_set");
116 Py_DECREF(pointers);
117 return NULL;
118 }
119 return pointers;
120 }
121
122
123 static PyMethodDef _islhelper_methods[] = {
124 {"isl_basic_set_constraints", isl_basic_set_constraints, METH_VARARGS, NULL},
125 {"isl_set_basic_sets", isl_set_basic_sets, METH_VARARGS, NULL},
126 {NULL, NULL, 0, NULL}
127 };
128
129 static struct PyModuleDef _islhelpermodule = {
130 PyModuleDef_HEAD_INIT,
131 "_islhelper",
132 NULL,
133 0,
134 _islhelper_methods
135 };
136
137 PyMODINIT_FUNC PyInit__islhelper(void) {
138 PyObject *m;
139 m = PyModule_Create(&_islhelpermodule);
140 if (m == NULL) {
141 return NULL;
142 }
143
144 if (PyModule_AddObject(m, "dim_set", PyLong_FromLong(isl_dim_set)) == -1) {
145 return NULL;
146 }
147
148 return m;
149 }