diff options
Diffstat (limited to '')
| -rw-r--r-- | python/nissy_module.c | 191 |
1 files changed, 179 insertions, 12 deletions
diff --git a/python/nissy_module.c b/python/nissy_module.c index b627300..851a9e6 100644 --- a/python/nissy_module.c +++ b/python/nissy_module.c | |||
| @@ -1,34 +1,201 @@ | |||
| 1 | #define PY_SSIZE_T_CLEAN | 1 | #define PY_SSIZE_T_CLEAN |
| 2 | #include <Python.h> | 2 | #include <Python.h> |
| 3 | #include <stdbool.h> | ||
| 3 | 4 | ||
| 4 | #include "../src/nissy.h" | 5 | #include "../src/nissy.h" |
| 5 | 6 | ||
| 6 | /* TODO from here */ | 7 | #define MAX_CUBE_STR_LEN 1024 /* Update when adding formats */ |
| 7 | 8 | ||
| 8 | static PyObject *compose(PyObject *self, PyObject *args) { | 9 | static bool |
| 10 | check_error(long long err) | ||
| 11 | { | ||
| 12 | char err_string[255]; | ||
| 13 | |||
| 14 | switch (err) { | ||
| 15 | case NISSY_OK: /* Fallthrough */ | ||
| 16 | case NISSY_WARNING_UNSOLVABLE: | ||
| 17 | return true; | ||
| 18 | case NISSY_ERROR_INVALID_CUBE: | ||
| 19 | case NISSY_ERROR_UNSOLVABLE_CUBE: /* Fallthrough */ | ||
| 20 | case NISSY_ERROR_INVALID_MOVES: | ||
| 21 | case NISSY_ERROR_INVALID_TRANS: | ||
| 22 | case NISSY_ERROR_INVALID_FORMAT: | ||
| 23 | case NISSY_ERROR_INVALID_SOLVER: | ||
| 24 | case NISSY_ERROR_NULL_POINTER: | ||
| 25 | case NISSY_ERROR_BUFFER_SIZE: | ||
| 26 | case NISSY_ERROR_DATA: | ||
| 27 | case NISSY_ERROR_OPTIONS: | ||
| 28 | case NISSY_ERROR_UNKNOWN: | ||
| 29 | default: | ||
| 30 | sprintf(err_string, "Error from libnissy (%lld)", err); | ||
| 31 | PyErr_SetString(PyExc_Exception, err_string); | ||
| 32 | return false; | ||
| 33 | } | ||
| 34 | |||
| 35 | } | ||
| 36 | |||
| 37 | static PyObject * | ||
| 38 | string_result(long long err, const char *result) | ||
| 39 | { | ||
| 40 | return check_error(err) ? PyUnicode_FromString(result) : NULL; | ||
| 41 | } | ||
| 42 | |||
| 43 | static PyObject * | ||
| 44 | string_result_free(long long err, char *result) | ||
| 45 | { | ||
| 46 | PyObject *ret; | ||
| 47 | |||
| 48 | ret = PyUnicode_FromString(result); | ||
| 49 | free(result); | ||
| 50 | |||
| 51 | return check_error(err) ? ret : NULL; | ||
| 52 | } | ||
| 53 | |||
| 54 | static PyObject * | ||
| 55 | long_result(long long result) | ||
| 56 | { | ||
| 57 | check_error(result); | ||
| 58 | return PyLong_FromLong(result); | ||
| 59 | } | ||
| 60 | |||
| 61 | static PyObject * | ||
| 62 | compose(PyObject *self, PyObject *args) | ||
| 63 | { | ||
| 64 | long long err; | ||
| 9 | const char *cube, *permutation; | 65 | const char *cube, *permutation; |
| 10 | char result[NISSY_SIZE_B32]; | 66 | char result[NISSY_SIZE_B32]; |
| 11 | 67 | ||
| 12 | if (!PyArg_ParseTuple(args, "ss", &cube, &permutation)) | 68 | if (!PyArg_ParseTuple(args, "ss", &cube, &permutation)) |
| 13 | return NULL; | 69 | return NULL; |
| 14 | 70 | ||
| 15 | nissy_compose(cube, permutation, result); | 71 | err = nissy_compose(cube, permutation, result); |
| 16 | return PyUnicode_FromString(result); | 72 | return string_result(err, result); |
| 73 | } | ||
| 74 | |||
| 75 | static PyObject * | ||
| 76 | inverse(PyObject *self, PyObject *args) | ||
| 77 | { | ||
| 78 | long long err; | ||
| 79 | const char *cube; | ||
| 80 | char result[NISSY_SIZE_B32]; | ||
| 81 | |||
| 82 | if (!PyArg_ParseTuple(args, "s", &cube)) | ||
| 83 | return NULL; | ||
| 84 | |||
| 85 | err = nissy_inverse(cube, result); | ||
| 86 | return string_result(err, result); | ||
| 87 | } | ||
| 88 | |||
| 89 | static PyObject * | ||
| 90 | applymoves(PyObject *self, PyObject *args) | ||
| 91 | { | ||
| 92 | long long err; | ||
| 93 | const char *cube, *moves; | ||
| 94 | char result[NISSY_SIZE_B32]; | ||
| 95 | |||
| 96 | if (!PyArg_ParseTuple(args, "ss", &cube, &moves)) | ||
| 97 | return NULL; | ||
| 98 | |||
| 99 | err = nissy_applymoves(cube, moves, result); | ||
| 100 | return string_result(err, result); | ||
| 101 | } | ||
| 102 | |||
| 103 | static PyObject * | ||
| 104 | applytrans(PyObject *self, PyObject *args) | ||
| 105 | { | ||
| 106 | long long err; | ||
| 107 | const char *cube, *trans; | ||
| 108 | char result[NISSY_SIZE_B32]; | ||
| 109 | |||
| 110 | if (!PyArg_ParseTuple(args, "ss", &cube, &trans)) | ||
| 111 | return NULL; | ||
| 112 | |||
| 113 | err = nissy_applytrans(cube, trans, result); | ||
| 114 | return string_result(err, result); | ||
| 115 | } | ||
| 116 | |||
| 117 | static PyObject * | ||
| 118 | convert(PyObject *self, PyObject *args) | ||
| 119 | { | ||
| 120 | long long err; | ||
| 121 | const char *fin, *fout, *cube; | ||
| 122 | char result[MAX_CUBE_STR_LEN]; | ||
| 123 | |||
| 124 | if (!PyArg_ParseTuple(args, "sss", &fin, &fout, &cube)) | ||
| 125 | return NULL; | ||
| 126 | |||
| 127 | err = nissy_convert(fin, fout, cube, MAX_CUBE_STR_LEN, result); | ||
| 128 | return string_result(err, result); | ||
| 129 | } | ||
| 130 | |||
| 131 | static PyObject * | ||
| 132 | getcube(PyObject *self, PyObject *args) | ||
| 133 | { | ||
| 134 | long long ep, eo, cp, co, err; | ||
| 135 | const char *options; | ||
| 136 | char result[NISSY_SIZE_B32]; | ||
| 137 | |||
| 138 | if (!PyArg_ParseTuple(args, "LLLLs", &ep, &eo, &cp, &co, &options)) | ||
| 139 | return NULL; | ||
| 140 | |||
| 141 | err = nissy_getcube(ep, eo, cp, co, options, result); | ||
| 142 | return string_result(err, result); | ||
| 143 | } | ||
| 144 | |||
| 145 | static PyObject * | ||
| 146 | datasize(PyObject *self, PyObject *args) | ||
| 147 | { | ||
| 148 | long long result; | ||
| 149 | const char *solver; | ||
| 150 | |||
| 151 | if (!PyArg_ParseTuple(args, "s", &solver)) | ||
| 152 | return NULL; | ||
| 153 | |||
| 154 | result = nissy_datasize(solver); | ||
| 155 | return long_result(result); | ||
| 17 | } | 156 | } |
| 18 | 157 | ||
| 19 | static PyMethodDef NissyMethods[] = { | 158 | /* TODO: gendata, checkdata and solve */ |
| 20 | { "compose", compose, METH_VARARGS, "Compose two cubes." }, | 159 | |
| 160 | static PyMethodDef nissy_methods[] = { | ||
| 161 | { "compose", compose, METH_VARARGS, NULL }, | ||
| 162 | { "inverse", inverse, METH_VARARGS, NULL }, | ||
| 163 | { "applymoves", applymoves, METH_VARARGS, NULL }, | ||
| 164 | { "applytrans", applytrans, METH_VARARGS, NULL }, | ||
| 165 | { "convert", convert, METH_VARARGS, NULL }, | ||
| 166 | { "getcube", getcube, METH_VARARGS, NULL }, | ||
| 167 | { "datasize", datasize, METH_VARARGS, NULL }, | ||
| 21 | { NULL, NULL, 0, NULL } | 168 | { NULL, NULL, 0, NULL } |
| 22 | }; | 169 | }; |
| 23 | 170 | ||
| 24 | static struct PyModuleDef nissy_python_module = { | 171 | static struct PyModuleDef nissy_python_module = { |
| 25 | PyModuleDef_HEAD_INIT, | 172 | .m_base = PyModuleDef_HEAD_INIT, |
| 26 | "nissy", | 173 | .m_name = "nissy", |
| 27 | NULL, | 174 | .m_doc = NULL, |
| 28 | -1, | 175 | .m_size = -1, |
| 29 | NissyMethods | 176 | .m_methods = nissy_methods, |
| 177 | .m_slots = NULL, | ||
| 178 | .m_traverse = NULL, | ||
| 179 | .m_clear = NULL, | ||
| 180 | .m_free = NULL | ||
| 30 | }; | 181 | }; |
| 31 | 182 | ||
| 183 | static void | ||
| 184 | log_stdout(const char *str, ...) | ||
| 185 | { | ||
| 186 | va_list args; | ||
| 187 | |||
| 188 | va_start(args, str); | ||
| 189 | vfprintf(stderr, str, args); | ||
| 190 | va_end(args); | ||
| 191 | } | ||
| 192 | |||
| 32 | PyMODINIT_FUNC PyInit_nissy_python_module(void) { | 193 | PyMODINIT_FUNC PyInit_nissy_python_module(void) { |
| 33 | return PyModule_Create(&nissy_python_module); | 194 | PyObject *module; |
| 195 | |||
| 196 | nissy_setlogger(log_stdout); | ||
| 197 | module = PyModule_Create(&nissy_python_module); | ||
| 198 | PyModule_AddStringConstant(module, "solvedcube", NISSY_SOLVED_CUBE); | ||
| 199 | |||
| 200 | return module; | ||
| 34 | } | 201 | } |
