-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrepeat.c
63 lines (51 loc) · 1.35 KB
/
repeat.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
#include <Python.h>
static PyObject *RepeatError;
static PyObject *
Rep(PyObject *self, PyObject *args) {
int i, s, e, r;
char *buf, *ret;
PyObject *objret;
if (!PyArg_ParseTuple(args, "siii", &buf, &s, &e, &r)) {
fprintf(stderr, "DEBUG: PyArg_ParseTuple filed\n");
return NULL;
}
// check indexes
if(s>=e || r<1) {
PyErr_SetString(RepeatError, "System command failed");
return NULL;
}
else {
s = s>0?s:0; e=strlen(buf)<e?strlen(buf):e;
ret = malloc((e-s)*r+1);
for(i=0; i<r; i++)
strncpy(ret+i*(e-s), buf+s, e-s);
}
objret = PyString_FromString(ret);
free(ret);
return objret;
}
static PyMethodDef RepeatMethods[] = {
{"repeat", Rep, METH_VARARGS, "repeat(buf, s, e, r)\n\nReturn buf[s:e]*r"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
initrepeat(void)
{
PyObject *module;
module = Py_InitModule3("repeat", RepeatMethods,"Test module for repeating substring");
if(module == NULL) return;
RepeatError = PyErr_NewException("repeat.error", NULL, NULL);
Py_INCREF(RepeatError);
PyModule_AddObject(module, "error", RepeatError);
}
int
main(int argc, char *argv[])
{
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(argv[0]);
/* Initialize the Python interpreter. Required. */
Py_Initialize();
/* Add a static module */
initrepeat();
return 0;
}