Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | |
| 3 | static char xreadlines_doc [] = |
| 4 | "xreadlines(f)\n\ |
| 5 | \n\ |
| 6 | Return an xreadlines object for the file f."; |
| 7 | |
| 8 | typedef struct { |
| 9 | PyObject_HEAD |
| 10 | PyObject *file; |
| 11 | PyObject *lines; |
| 12 | int lineslen; |
| 13 | int lineno; |
| 14 | int abslineno; |
| 15 | } PyXReadlinesObject; |
| 16 | |
| 17 | staticforward PyTypeObject XReadlinesObject_Type; |
| 18 | |
| 19 | static void |
Thomas Wouters | 9fdcf4a | 2001-01-11 15:40:39 +0000 | [diff] [blame] | 20 | xreadlines_dealloc(PyXReadlinesObject *op) |
| 21 | { |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 22 | Py_XDECREF(op->file); |
| 23 | Py_XDECREF(op->lines); |
| 24 | PyObject_DEL(op); |
| 25 | } |
| 26 | |
| 27 | /* A larger chunk size doesn't seem to make a difference */ |
| 28 | #define CHUNKSIZE 8192 |
| 29 | |
| 30 | static PyXReadlinesObject * |
Thomas Wouters | 9fdcf4a | 2001-01-11 15:40:39 +0000 | [diff] [blame] | 31 | newreadlinesobject(PyObject *file) |
| 32 | { |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 33 | PyXReadlinesObject *op; |
| 34 | op = PyObject_NEW(PyXReadlinesObject, &XReadlinesObject_Type); |
| 35 | if (op == NULL) |
| 36 | return NULL; |
| 37 | Py_XINCREF(file); |
| 38 | op->file = file; |
| 39 | op->lines = NULL; |
| 40 | op->abslineno = op->lineno = op->lineslen = 0; |
| 41 | return op; |
| 42 | } |
| 43 | |
| 44 | static PyObject * |
Thomas Wouters | 9fdcf4a | 2001-01-11 15:40:39 +0000 | [diff] [blame] | 45 | xreadlines(PyObject *self, PyObject *args) |
| 46 | { |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 47 | PyObject *file; |
| 48 | PyXReadlinesObject *ret; |
| 49 | |
| 50 | if (!PyArg_ParseTuple(args, "O:xreadlines", &file)) |
| 51 | return NULL; |
| 52 | ret = newreadlinesobject(file); |
| 53 | Py_XINCREF(ret); |
| 54 | return (PyObject*)ret; |
| 55 | } |
| 56 | |
Thomas Wouters | 9fdcf4a | 2001-01-11 15:40:39 +0000 | [diff] [blame] | 57 | static PyObject * |
| 58 | xreadlines_item(PyXReadlinesObject *a, int i) |
| 59 | { |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 60 | if (i != a->abslineno) { |
| 61 | PyErr_SetString(PyExc_RuntimeError, |
| 62 | "xreadlines object accessed out of order"); |
| 63 | return NULL; |
| 64 | } |
| 65 | if (a->lineno >= a->lineslen) { |
| 66 | Py_XDECREF(a->lines); |
| 67 | a->lines = PyObject_CallMethod(a->file, "readlines", "(i)", |
| 68 | CHUNKSIZE); |
| 69 | if (a->lines == NULL) |
| 70 | return NULL; |
| 71 | a->lineno = 0; |
| 72 | if ((a->lineslen = PySequence_Size(a->lines)) < 0) |
| 73 | return NULL; |
| 74 | } |
| 75 | a->abslineno++; |
| 76 | return PySequence_GetItem(a->lines, a->lineno++); |
| 77 | } |
| 78 | |
| 79 | static PySequenceMethods xreadlines_as_sequence = { |
| 80 | 0, /*sq_length*/ |
| 81 | 0, /*sq_concat*/ |
| 82 | 0, /*sq_repeat*/ |
| 83 | (intargfunc)xreadlines_item, /*sq_item*/ |
| 84 | }; |
| 85 | |
| 86 | static PyTypeObject XReadlinesObject_Type = { |
Guido van Rossum | 7150a77 | 2001-01-19 00:29:06 +0000 | [diff] [blame] | 87 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 88 | 0, |
| 89 | "xreadlines", |
| 90 | sizeof(PyXReadlinesObject) + PyGC_HEAD_SIZE, |
| 91 | 0, |
| 92 | (destructor)xreadlines_dealloc, /*tp_dealloc*/ |
| 93 | 0, /*tp_print*/ |
| 94 | 0, /*tp_getattr*/ |
| 95 | 0, /*tp_setattr*/ |
| 96 | 0, /*tp_compare*/ |
| 97 | 0, /*tp_repr*/ |
| 98 | 0, /*tp_as_number*/ |
| 99 | &xreadlines_as_sequence, /*tp_as_sequence*/ |
| 100 | 0, /*tp_as_mapping*/ |
| 101 | 0, /*tp_hash*/ |
| 102 | 0, /*tp_call*/ |
| 103 | 0, /*tp_str*/ |
| 104 | 0, /*tp_getattro*/ |
| 105 | 0, /*tp_setattro*/ |
| 106 | 0, /*tp_as_buffer*/ |
| 107 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 108 | 0, /* tp_doc */ |
| 109 | }; |
| 110 | |
| 111 | static PyMethodDef xreadlines_methods[] = { |
| 112 | {"xreadlines", xreadlines, METH_VARARGS, xreadlines_doc}, |
| 113 | {NULL, NULL} |
| 114 | }; |
| 115 | |
Tim Peters | 58c82f0 | 2001-01-09 23:26:39 +0000 | [diff] [blame] | 116 | DL_EXPORT(void) |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 117 | initxreadlines(void) |
| 118 | { |
| 119 | PyObject *m; |
| 120 | |
Guido van Rossum | 7150a77 | 2001-01-19 00:29:06 +0000 | [diff] [blame] | 121 | XReadlinesObject_Type.ob_type = &PyType_Type; |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 122 | m = Py_InitModule("xreadlines", xreadlines_methods); |
| 123 | } |